Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:55

0001 //===- Utils.h - Misc utilities for the front-end ---------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 //  This header contains miscellaneous utilities for various front-end actions.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_CLANG_FRONTEND_UTILS_H
0014 #define LLVM_CLANG_FRONTEND_UTILS_H
0015 
0016 #include "clang/Basic/Diagnostic.h"
0017 #include "clang/Basic/LLVM.h"
0018 #include "clang/Driver/OptionUtils.h"
0019 #include "clang/Frontend/DependencyOutputOptions.h"
0020 #include "llvm/ADT/ArrayRef.h"
0021 #include "llvm/ADT/IntrusiveRefCntPtr.h"
0022 #include "llvm/ADT/StringMap.h"
0023 #include "llvm/ADT/StringRef.h"
0024 #include "llvm/ADT/StringSet.h"
0025 #include "llvm/Support/FileCollector.h"
0026 #include "llvm/Support/VirtualFileSystem.h"
0027 #include <cstdint>
0028 #include <memory>
0029 #include <string>
0030 #include <system_error>
0031 #include <utility>
0032 #include <vector>
0033 
0034 namespace clang {
0035 
0036 class ASTReader;
0037 class CompilerInstance;
0038 class CompilerInvocation;
0039 class DiagnosticsEngine;
0040 class ExternalSemaSource;
0041 class FrontendOptions;
0042 class PCHContainerReader;
0043 class Preprocessor;
0044 class PreprocessorOptions;
0045 class PreprocessorOutputOptions;
0046 class CodeGenOptions;
0047 
0048 /// InitializePreprocessor - Initialize the preprocessor getting it and the
0049 /// environment ready to process a single file.
0050 void InitializePreprocessor(Preprocessor &PP, const PreprocessorOptions &PPOpts,
0051                             const PCHContainerReader &PCHContainerRdr,
0052                             const FrontendOptions &FEOpts,
0053                             const CodeGenOptions &CodeGenOpts);
0054 
0055 /// DoPrintPreprocessedInput - Implement -E mode.
0056 void DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS,
0057                               const PreprocessorOutputOptions &Opts);
0058 
0059 /// An interface for collecting the dependencies of a compilation. Users should
0060 /// use \c attachToPreprocessor and \c attachToASTReader to get all of the
0061 /// dependencies.
0062 /// FIXME: Migrate DependencyGraphGen to use this interface.
0063 class DependencyCollector {
0064 public:
0065   virtual ~DependencyCollector();
0066 
0067   virtual void attachToPreprocessor(Preprocessor &PP);
0068   virtual void attachToASTReader(ASTReader &R);
0069   ArrayRef<std::string> getDependencies() const { return Dependencies; }
0070 
0071   /// Called when a new file is seen. Return true if \p Filename should be added
0072   /// to the list of dependencies.
0073   ///
0074   /// The default implementation ignores <built-in> and system files.
0075   virtual bool sawDependency(StringRef Filename, bool FromModule,
0076                              bool IsSystem, bool IsModuleFile, bool IsMissing);
0077 
0078   /// Called when the end of the main file is reached.
0079   virtual void finishedMainFile(DiagnosticsEngine &Diags) {}
0080 
0081   /// Return true if system files should be passed to sawDependency().
0082   virtual bool needSystemDependencies() { return false; }
0083 
0084   /// Add a dependency \p Filename if it has not been seen before and
0085   /// sawDependency() returns true.
0086   virtual void maybeAddDependency(StringRef Filename, bool FromModule,
0087                                   bool IsSystem, bool IsModuleFile,
0088                                   bool IsMissing);
0089 
0090 protected:
0091   /// Return true if the filename was added to the list of dependencies, false
0092   /// otherwise.
0093   bool addDependency(StringRef Filename);
0094 
0095 private:
0096   llvm::StringSet<> Seen;
0097   std::vector<std::string> Dependencies;
0098 };
0099 
0100 /// Builds a dependency file when attached to a Preprocessor (for includes) and
0101 /// ASTReader (for module imports), and writes it out at the end of processing
0102 /// a source file.  Users should attach to the ast reader whenever a module is
0103 /// loaded.
0104 class DependencyFileGenerator : public DependencyCollector {
0105 public:
0106   DependencyFileGenerator(const DependencyOutputOptions &Opts);
0107 
0108   void attachToPreprocessor(Preprocessor &PP) override;
0109 
0110   void finishedMainFile(DiagnosticsEngine &Diags) override;
0111 
0112   bool needSystemDependencies() final { return IncludeSystemHeaders; }
0113 
0114   bool sawDependency(StringRef Filename, bool FromModule, bool IsSystem,
0115                      bool IsModuleFile, bool IsMissing) final;
0116 
0117 protected:
0118   void outputDependencyFile(llvm::raw_ostream &OS);
0119 
0120 private:
0121   void outputDependencyFile(DiagnosticsEngine &Diags);
0122 
0123   std::string OutputFile;
0124   std::vector<std::string> Targets;
0125   bool IncludeSystemHeaders;
0126   bool PhonyTarget;
0127   bool AddMissingHeaderDeps;
0128   bool SeenMissingHeader;
0129   bool IncludeModuleFiles;
0130   DependencyOutputFormat OutputFormat;
0131   unsigned InputFileIndex;
0132 };
0133 
0134 /// Collects the dependencies for imported modules into a directory.  Users
0135 /// should attach to the AST reader whenever a module is loaded.
0136 class ModuleDependencyCollector : public DependencyCollector {
0137   std::string DestDir;
0138   bool HasErrors = false;
0139   llvm::StringSet<> Seen;
0140   llvm::vfs::YAMLVFSWriter VFSWriter;
0141   llvm::FileCollector::PathCanonicalizer Canonicalizer;
0142 
0143   std::error_code copyToRoot(StringRef Src, StringRef Dst = {});
0144 
0145 public:
0146   ModuleDependencyCollector(std::string DestDir)
0147       : DestDir(std::move(DestDir)) {}
0148   ~ModuleDependencyCollector() override { writeFileMap(); }
0149 
0150   StringRef getDest() { return DestDir; }
0151   virtual bool insertSeen(StringRef Filename) { return Seen.insert(Filename).second; }
0152   virtual void addFile(StringRef Filename, StringRef FileDst = {});
0153 
0154   virtual void addFileMapping(StringRef VPath, StringRef RPath) {
0155     VFSWriter.addFileMapping(VPath, RPath);
0156   }
0157 
0158   void attachToPreprocessor(Preprocessor &PP) override;
0159   void attachToASTReader(ASTReader &R) override;
0160 
0161   virtual void writeFileMap();
0162   virtual bool hasErrors() { return HasErrors; }
0163 };
0164 
0165 /// AttachDependencyGraphGen - Create a dependency graph generator, and attach
0166 /// it to the given preprocessor.
0167 void AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile,
0168                               StringRef SysRoot);
0169 
0170 /// AttachHeaderIncludeGen - Create a header include list generator, and attach
0171 /// it to the given preprocessor.
0172 ///
0173 /// \param DepOpts - Options controlling the output.
0174 /// \param ShowAllHeaders - If true, show all header information instead of just
0175 /// headers following the predefines buffer. This is useful for making sure
0176 /// includes mentioned on the command line are also reported, but differs from
0177 /// the default behavior used by -H.
0178 /// \param OutputPath - If non-empty, a path to write the header include
0179 /// information to, instead of writing to stderr.
0180 /// \param ShowDepth - Whether to indent to show the nesting of the includes.
0181 /// \param MSStyle - Whether to print in cl.exe /showIncludes style.
0182 void AttachHeaderIncludeGen(Preprocessor &PP,
0183                             const DependencyOutputOptions &DepOpts,
0184                             bool ShowAllHeaders = false,
0185                             StringRef OutputPath = {},
0186                             bool ShowDepth = true, bool MSStyle = false);
0187 
0188 /// The ChainedIncludesSource class converts headers to chained PCHs in
0189 /// memory, mainly for testing.
0190 IntrusiveRefCntPtr<ExternalSemaSource>
0191 createChainedIncludesSource(CompilerInstance &CI,
0192                             IntrusiveRefCntPtr<ExternalSemaSource> &Reader);
0193 
0194 /// Optional inputs to createInvocation.
0195 struct CreateInvocationOptions {
0196   /// Receives diagnostics encountered while parsing command-line flags.
0197   /// If not provided, these are printed to stderr.
0198   IntrusiveRefCntPtr<DiagnosticsEngine> Diags = nullptr;
0199   /// Used e.g. to probe for system headers locations.
0200   /// If not provided, the real filesystem is used.
0201   /// FIXME: the driver does perform some non-virtualized IO.
0202   IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr;
0203   /// Whether to attempt to produce a non-null (possibly incorrect) invocation
0204   /// if any errors were encountered.
0205   /// By default, always return null on errors.
0206   bool RecoverOnError = false;
0207   /// Allow the driver to probe the filesystem for PCH files.
0208   /// This is used to replace -include with -include-pch in the cc1 args.
0209   /// FIXME: ProbePrecompiled=true is a poor, historical default.
0210   /// It misbehaves if the PCH file is from GCC, has the wrong version, etc.
0211   bool ProbePrecompiled = false;
0212   /// If set, the target is populated with the cc1 args produced by the driver.
0213   /// This may be populated even if createInvocation returns nullptr.
0214   std::vector<std::string> *CC1Args = nullptr;
0215 };
0216 
0217 /// Interpret clang arguments in preparation to parse a file.
0218 ///
0219 /// This simulates a number of steps Clang takes when its driver is invoked:
0220 /// - choosing actions (e.g compile + link) to run
0221 /// - probing the system for settings like standard library locations
0222 /// - spawning a cc1 subprocess to compile code, with more explicit arguments
0223 /// - in the cc1 process, assembling those arguments into a CompilerInvocation
0224 ///   which is used to configure the parser
0225 ///
0226 /// This simulation is lossy, e.g. in some situations one driver run would
0227 /// result in multiple parses. (Multi-arch, CUDA, ...).
0228 /// This function tries to select a reasonable invocation that tools should use.
0229 ///
0230 /// Args[0] should be the driver name, such as "clang" or "/usr/bin/g++".
0231 /// Absolute path is preferred - this affects searching for system headers.
0232 ///
0233 /// May return nullptr if an invocation could not be determined.
0234 /// See CreateInvocationOptions::ShouldRecoverOnErrors to try harder!
0235 std::unique_ptr<CompilerInvocation>
0236 createInvocation(ArrayRef<const char *> Args,
0237                  CreateInvocationOptions Opts = {});
0238 
0239 } // namespace clang
0240 
0241 #endif // LLVM_CLANG_FRONTEND_UTILS_H