clang  15.0.0git
Utils.h
Go to the documentation of this file.
1 //===- Utils.h - Misc utilities for the front-end ---------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This header contains miscellaneous utilities for various front-end actions.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_FRONTEND_UTILS_H
14 #define LLVM_CLANG_FRONTEND_UTILS_H
15 
16 #include "clang/Basic/Diagnostic.h"
17 #include "clang/Basic/LLVM.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/IntrusiveRefCntPtr.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/StringSet.h"
25 #include "llvm/Support/FileCollector.h"
26 #include "llvm/Support/VirtualFileSystem.h"
27 #include <cstdint>
28 #include <memory>
29 #include <string>
30 #include <system_error>
31 #include <utility>
32 #include <vector>
33 
34 namespace clang {
35 
36 class ASTReader;
37 class CompilerInstance;
38 class CompilerInvocation;
39 class DiagnosticsEngine;
40 class ExternalSemaSource;
41 class FrontendOptions;
42 class PCHContainerReader;
43 class Preprocessor;
44 class PreprocessorOptions;
45 class PreprocessorOutputOptions;
46 
47 /// InitializePreprocessor - Initialize the preprocessor getting it and the
48 /// environment ready to process a single file.
49 void InitializePreprocessor(Preprocessor &PP, const PreprocessorOptions &PPOpts,
50  const PCHContainerReader &PCHContainerRdr,
51  const FrontendOptions &FEOpts);
52 
53 /// DoPrintPreprocessedInput - Implement -E mode.
54 void DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS,
55  const PreprocessorOutputOptions &Opts);
56 
57 /// An interface for collecting the dependencies of a compilation. Users should
58 /// use \c attachToPreprocessor and \c attachToASTReader to get all of the
59 /// dependencies.
60 /// FIXME: Migrate DependencyGraphGen to use this interface.
62 public:
63  virtual ~DependencyCollector();
64 
65  virtual void attachToPreprocessor(Preprocessor &PP);
66  virtual void attachToASTReader(ASTReader &R);
67  ArrayRef<std::string> getDependencies() const { return Dependencies; }
68 
69  /// Called when a new file is seen. Return true if \p Filename should be added
70  /// to the list of dependencies.
71  ///
72  /// The default implementation ignores <built-in> and system files.
73  virtual bool sawDependency(StringRef Filename, bool FromModule,
74  bool IsSystem, bool IsModuleFile, bool IsMissing);
75 
76  /// Called when the end of the main file is reached.
77  virtual void finishedMainFile(DiagnosticsEngine &Diags) {}
78 
79  /// Return true if system files should be passed to sawDependency().
80  virtual bool needSystemDependencies() { return false; }
81 
82  /// Add a dependency \p Filename if it has not been seen before and
83  /// sawDependency() returns true.
84  virtual void maybeAddDependency(StringRef Filename, bool FromModule,
85  bool IsSystem, bool IsModuleFile,
86  bool IsMissing);
87 
88 protected:
89  /// Return true if the filename was added to the list of dependencies, false
90  /// otherwise.
91  bool addDependency(StringRef Filename);
92 
93 private:
94  llvm::StringSet<> Seen;
95  std::vector<std::string> Dependencies;
96 };
97 
98 /// Builds a dependency file when attached to a Preprocessor (for includes) and
99 /// ASTReader (for module imports), and writes it out at the end of processing
100 /// a source file. Users should attach to the ast reader whenever a module is
101 /// loaded.
103 public:
105 
106  void attachToPreprocessor(Preprocessor &PP) override;
107 
108  void finishedMainFile(DiagnosticsEngine &Diags) override;
109 
110  bool needSystemDependencies() final override { return IncludeSystemHeaders; }
111 
112  bool sawDependency(StringRef Filename, bool FromModule, bool IsSystem,
113  bool IsModuleFile, bool IsMissing) final override;
114 
115 protected:
116  void outputDependencyFile(llvm::raw_ostream &OS);
117 
118 private:
120 
121  std::string OutputFile;
122  std::vector<std::string> Targets;
123  bool IncludeSystemHeaders;
124  bool PhonyTarget;
125  bool AddMissingHeaderDeps;
126  bool SeenMissingHeader;
127  bool IncludeModuleFiles;
128  DependencyOutputFormat OutputFormat;
129  unsigned InputFileIndex;
130 };
131 
132 /// Collects the dependencies for imported modules into a directory. Users
133 /// should attach to the AST reader whenever a module is loaded.
135  std::string DestDir;
136  bool HasErrors = false;
137  llvm::StringSet<> Seen;
138  llvm::vfs::YAMLVFSWriter VFSWriter;
139  llvm::FileCollector::PathCanonicalizer Canonicalizer;
140 
141  std::error_code copyToRoot(StringRef Src, StringRef Dst = {});
142 
143 public:
145  : DestDir(std::move(DestDir)) {}
147 
148  StringRef getDest() { return DestDir; }
149  virtual bool insertSeen(StringRef Filename) { return Seen.insert(Filename).second; }
150  virtual void addFile(StringRef Filename, StringRef FileDst = {});
151 
152  virtual void addFileMapping(StringRef VPath, StringRef RPath) {
153  VFSWriter.addFileMapping(VPath, RPath);
154  }
155 
156  void attachToPreprocessor(Preprocessor &PP) override;
157  void attachToASTReader(ASTReader &R) override;
158 
159  virtual void writeFileMap();
160  virtual bool hasErrors() { return HasErrors; }
161 };
162 
163 /// AttachDependencyGraphGen - Create a dependency graph generator, and attach
164 /// it to the given preprocessor.
165 void AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile,
166  StringRef SysRoot);
167 
168 /// AttachHeaderIncludeGen - Create a header include list generator, and attach
169 /// it to the given preprocessor.
170 ///
171 /// \param DepOpts - Options controlling the output.
172 /// \param ShowAllHeaders - If true, show all header information instead of just
173 /// headers following the predefines buffer. This is useful for making sure
174 /// includes mentioned on the command line are also reported, but differs from
175 /// the default behavior used by -H.
176 /// \param OutputPath - If non-empty, a path to write the header include
177 /// information to, instead of writing to stderr.
178 /// \param ShowDepth - Whether to indent to show the nesting of the includes.
179 /// \param MSStyle - Whether to print in cl.exe /showIncludes style.
180 void AttachHeaderIncludeGen(Preprocessor &PP,
181  const DependencyOutputOptions &DepOpts,
182  bool ShowAllHeaders = false,
183  StringRef OutputPath = {},
184  bool ShowDepth = true, bool MSStyle = false);
185 
186 /// The ChainedIncludesSource class converts headers to chained PCHs in
187 /// memory, mainly for testing.
188 IntrusiveRefCntPtr<ExternalSemaSource>
189 createChainedIncludesSource(CompilerInstance &CI,
190  IntrusiveRefCntPtr<ExternalSemaSource> &Reader);
191 
192 /// Optional inputs to createInvocation.
194  /// Receives diagnostics encountered while parsing command-line flags.
195  /// If not provided, these are printed to stderr.
197  /// Used e.g. to probe for system headers locations.
198  /// If not provided, the real filesystem is used.
199  /// FIXME: the driver does perform some non-virtualized IO.
201  /// Whether to attempt to produce a non-null (possibly incorrect) invocation
202  /// if any errors were encountered.
203  /// By default, always return null on errors.
204  bool RecoverOnError = false;
205  /// Allow the driver to probe the filesystem for PCH files.
206  /// This is used to replace -include with -include-pch in the cc1 args.
207  /// FIXME: ProbePrecompiled=true is a poor, historical default.
208  /// It misbehaves if the PCH file is from GCC, has the wrong version, etc.
209  bool ProbePrecompiled = false;
210  /// If set, the target is populated with the cc1 args produced by the driver.
211  /// This may be populated even if createInvocation returns nullptr.
212  std::vector<std::string> *CC1Args = nullptr;
213 };
214 
215 /// Interpret clang arguments in preparation to parse a file.
216 ///
217 /// This simulates a number of steps Clang takes when its driver is invoked:
218 /// - choosing actions (e.g compile + link) to run
219 /// - probing the system for settings like standard library locations
220 /// - spawning a cc1 subprocess to compile code, with more explicit arguments
221 /// - in the cc1 process, assembling those arguments into a CompilerInvocation
222 /// which is used to configure the parser
223 ///
224 /// This simulation is lossy, e.g. in some situations one driver run would
225 /// result in multiple parses. (Multi-arch, CUDA, ...).
226 /// This function tries to select a reasonable invocation that tools should use.
227 ///
228 /// Args[0] should be the driver name, such as "clang" or "/usr/bin/g++".
229 /// Absolute path is preferred - this affects searching for system headers.
230 ///
231 /// May return nullptr if an invocation could not be determined.
232 /// See CreateInvocationOptions::ShouldRecoverOnErrors to try harder!
233 std::unique_ptr<CompilerInvocation>
235  CreateInvocationOptions Opts = {});
236 
237 } // namespace clang
238 
239 #endif // LLVM_CLANG_FRONTEND_UTILS_H
clang::DependencyOutputOptions
DependencyOutputOptions - Options for controlling the compiler dependency file generation.
Definition: DependencyOutputOptions.h:33
clang::DependencyFileGenerator::sawDependency
bool sawDependency(StringRef Filename, bool FromModule, bool IsSystem, bool IsModuleFile, bool IsMissing) final override
Called when a new file is seen.
Definition: DependencyFile.cpp:209
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
OptionUtils.h
clang::DependencyFileGenerator::finishedMainFile
void finishedMainFile(DiagnosticsEngine &Diags) override
Called when the end of the main file is reached.
Definition: DependencyFile.cpp:231
Filename
StringRef Filename
Definition: Format.cpp:2568
clang::DiagnosticsEngine
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:192
clang::createInvocation
std::unique_ptr< CompilerInvocation > createInvocation(ArrayRef< const char * > Args, CreateInvocationOptions Opts={})
Interpret clang arguments in preparation to parse a file.
Definition: CreateInvocationFromCommandLine.cpp:30
clang::ModuleDependencyCollector::writeFileMap
virtual void writeFileMap()
Definition: ModuleDependencyCollector.cpp:130
clang::DependencyCollector::attachToPreprocessor
virtual void attachToPreprocessor(Preprocessor &PP)
Definition: DependencyFile.cpp:177
clang::DependencyCollector::finishedMainFile
virtual void finishedMainFile(DiagnosticsEngine &Diags)
Called when the end of the main file is reached.
Definition: Utils.h:77
clang::CreateInvocationOptions::ProbePrecompiled
bool ProbePrecompiled
Allow the driver to probe the filesystem for PCH files.
Definition: Utils.h:209
clang::DependencyOutputFormat
DependencyOutputFormat
DependencyOutputFormat - Format for the compiler dependency file.
Definition: DependencyOutputOptions.h:21
clang::ModuleDependencyCollector::hasErrors
virtual bool hasErrors()
Definition: Utils.h:160
clang::DependencyCollector::~DependencyCollector
virtual ~DependencyCollector()
Definition: DependencyFile.cpp:176
clang::DependencyFileGenerator::attachToPreprocessor
void attachToPreprocessor(Preprocessor &PP) override
Definition: DependencyFile.cpp:201
clang::CreateInvocationOptions::VFS
IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS
Used e.g.
Definition: Utils.h:200
clang::ModuleDependencyCollector::~ModuleDependencyCollector
~ModuleDependencyCollector() override
Definition: Utils.h:146
Diagnostic.h
clang::InitializePreprocessor
void InitializePreprocessor(Preprocessor &PP, const PreprocessorOptions &PPOpts, const PCHContainerReader &PCHContainerRdr, const FrontendOptions &FEOpts)
InitializePreprocessor - Initialize the preprocessor getting it and the environment ready to process ...
Definition: InitPreprocessor.cpp:1302
DependencyOutputOptions.h
clang::ModuleDependencyCollector::attachToASTReader
void attachToASTReader(ASTReader &R) override
Definition: ModuleDependencyCollector.cpp:101
clang::DependencyCollector::sawDependency
virtual bool sawDependency(StringRef Filename, bool FromModule, bool IsSystem, bool IsModuleFile, bool IsMissing)
Called when a new file is seen.
Definition: DependencyFile.cpp:169
clang::DependencyCollector
An interface for collecting the dependencies of a compilation.
Definition: Utils.h:61
clang::DependencyFileGenerator
Builds a dependency file when attached to a Preprocessor (for includes) and ASTReader (for module imp...
Definition: Utils.h:102
clang::CreateInvocationOptions::CC1Args
std::vector< std::string > * CC1Args
If set, the target is populated with the cc1 args produced by the driver.
Definition: Utils.h:212
clang::DependencyFileGenerator::needSystemDependencies
bool needSystemDependencies() final override
Return true if system files should be passed to sawDependency().
Definition: Utils.h:110
llvm::ArrayRef< std::string >
clang::CreateInvocationOptions
Optional inputs to createInvocation.
Definition: Utils.h:193
LLVM.h
clang::ModuleDependencyCollector
Collects the dependencies for imported modules into a directory.
Definition: Utils.h:134
clang::DependencyCollector::needSystemDependencies
virtual bool needSystemDependencies()
Return true if system files should be passed to sawDependency().
Definition: Utils.h:80
clang::ModuleDependencyCollector::insertSeen
virtual bool insertSeen(StringRef Filename)
Definition: Utils.h:149
clang::DoPrintPreprocessedInput
void DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS, const PreprocessorOutputOptions &Opts)
DoPrintPreprocessedInput - Implement -E mode.
Definition: PrintPreprocessedOutput.cpp:959
clang::ASTReader
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:341
std
Definition: Format.h:4296
clang::DependencyFileGenerator::DependencyFileGenerator
DependencyFileGenerator(const DependencyOutputOptions &Opts)
Definition: DependencyFile.cpp:187
clang::DependencyCollector::attachToASTReader
virtual void attachToASTReader(ASTReader &R)
Definition: DependencyFile.cpp:183
clang::createChainedIncludesSource
IntrusiveRefCntPtr< ExternalSemaSource > createChainedIncludesSource(CompilerInstance &CI, IntrusiveRefCntPtr< ExternalSemaSource > &Reader)
The ChainedIncludesSource class converts headers to chained PCHs in memory, mainly for testing.
Definition: ChainedIncludesSource.cpp:113
clang
Definition: CalledOnceCheck.h:17
clang::ModuleDependencyCollector::attachToPreprocessor
void attachToPreprocessor(Preprocessor &PP) override
Definition: ModuleDependencyCollector.cpp:105
clang::DependencyFileGenerator::outputDependencyFile
void outputDependencyFile(llvm::raw_ostream &OS)
Definition: DependencyFile.cpp:330
clang::DependencyCollector::maybeAddDependency
virtual void maybeAddDependency(StringRef Filename, bool FromModule, bool IsSystem, bool IsModuleFile, bool IsMissing)
Add a dependency Filename if it has not been seen before and sawDependency() returns true.
Definition: DependencyFile.cpp:135
clang::ModuleDependencyCollector::ModuleDependencyCollector
ModuleDependencyCollector(std::string DestDir)
Definition: Utils.h:144
clang::DependencyCollector::getDependencies
ArrayRef< std::string > getDependencies() const
Definition: Utils.h:67
clang::CreateInvocationOptions::RecoverOnError
bool RecoverOnError
Whether to attempt to produce a non-null (possibly incorrect) invocation if any errors were encounter...
Definition: Utils.h:204
clang::Preprocessor
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:131
clang::AttachDependencyGraphGen
void AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile, StringRef SysRoot)
AttachDependencyGraphGen - Create a dependency graph generator, and attach it to the given preprocess...
Definition: DependencyGraph.cpp:62
clang::DependencyCollector::addDependency
bool addDependency(StringRef Filename)
Return true if the filename was added to the list of dependencies, false otherwise.
Definition: DependencyFile.cpp:143
clang::ModuleDependencyCollector::addFileMapping
virtual void addFileMapping(StringRef VPath, StringRef RPath)
Definition: Utils.h:152
llvm::IntrusiveRefCntPtr
Definition: LLVM.h:47
clang::ModuleDependencyCollector::addFile
virtual void addFile(StringRef Filename, StringRef FileDst={})
Definition: ModuleDependencyCollector.cpp:196
clang::ModuleDependencyCollector::getDest
StringRef getDest()
Definition: Utils.h:148
clang::AttachHeaderIncludeGen
void AttachHeaderIncludeGen(Preprocessor &PP, const DependencyOutputOptions &DepOpts, bool ShowAllHeaders=false, StringRef OutputPath={}, bool ShowDepth=true, bool MSStyle=false)
AttachHeaderIncludeGen - Create a header include list generator, and attach it to the given preproces...
Definition: HeaderIncludeGen.cpp:81
clang::CreateInvocationOptions::Diags
IntrusiveRefCntPtr< DiagnosticsEngine > Diags
Receives diagnostics encountered while parsing command-line flags.
Definition: Utils.h:196