clang  12.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/Option/OptSpecifier.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 llvm {
35 
36 class Triple;
37 
38 } // namespace llvm
39 
40 namespace clang {
41 
42 class ASTReader;
43 class CompilerInstance;
44 class CompilerInvocation;
45 class DiagnosticsEngine;
46 class ExternalSemaSource;
47 class FrontendOptions;
48 class HeaderSearch;
49 class HeaderSearchOptions;
50 class LangOptions;
51 class PCHContainerReader;
52 class Preprocessor;
53 class PreprocessorOptions;
54 class PreprocessorOutputOptions;
55 
56 /// Apply the header search options to get given HeaderSearch object.
57 void ApplyHeaderSearchOptions(HeaderSearch &HS,
58  const HeaderSearchOptions &HSOpts,
59  const LangOptions &Lang,
60  const llvm::Triple &triple);
61 
62 /// InitializePreprocessor - Initialize the preprocessor getting it and the
63 /// environment ready to process a single file.
64 void InitializePreprocessor(Preprocessor &PP, const PreprocessorOptions &PPOpts,
65  const PCHContainerReader &PCHContainerRdr,
66  const FrontendOptions &FEOpts);
67 
68 /// DoPrintPreprocessedInput - Implement -E mode.
69 void DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS,
70  const PreprocessorOutputOptions &Opts);
71 
72 /// An interface for collecting the dependencies of a compilation. Users should
73 /// use \c attachToPreprocessor and \c attachToASTReader to get all of the
74 /// dependencies.
75 /// FIXME: Migrate DependencyGraphGen to use this interface.
77 public:
78  virtual ~DependencyCollector();
79 
80  virtual void attachToPreprocessor(Preprocessor &PP);
81  virtual void attachToASTReader(ASTReader &R);
82  ArrayRef<std::string> getDependencies() const { return Dependencies; }
83 
84  /// Called when a new file is seen. Return true if \p Filename should be added
85  /// to the list of dependencies.
86  ///
87  /// The default implementation ignores <built-in> and system files.
88  virtual bool sawDependency(StringRef Filename, bool FromModule,
89  bool IsSystem, bool IsModuleFile, bool IsMissing);
90 
91  /// Called when the end of the main file is reached.
92  virtual void finishedMainFile(DiagnosticsEngine &Diags) {}
93 
94  /// Return true if system files should be passed to sawDependency().
95  virtual bool needSystemDependencies() { return false; }
96 
97  /// Add a dependency \p Filename if it has not been seen before and
98  /// sawDependency() returns true.
99  virtual void maybeAddDependency(StringRef Filename, bool FromModule,
100  bool IsSystem, bool IsModuleFile,
101  bool IsMissing);
102 
103 protected:
104  /// Return true if the filename was added to the list of dependencies, false
105  /// otherwise.
106  bool addDependency(StringRef Filename);
107 
108 private:
109  llvm::StringSet<> Seen;
110  std::vector<std::string> Dependencies;
111 };
112 
113 /// Builds a dependency file when attached to a Preprocessor (for includes) and
114 /// ASTReader (for module imports), and writes it out at the end of processing
115 /// a source file. Users should attach to the ast reader whenever a module is
116 /// loaded.
118 public:
120 
121  void attachToPreprocessor(Preprocessor &PP) override;
122 
123  void finishedMainFile(DiagnosticsEngine &Diags) override;
124 
125  bool needSystemDependencies() final override { return IncludeSystemHeaders; }
126 
127  bool sawDependency(StringRef Filename, bool FromModule, bool IsSystem,
128  bool IsModuleFile, bool IsMissing) final override;
129 
130 protected:
131  void outputDependencyFile(llvm::raw_ostream &OS);
132 
133 private:
134  void outputDependencyFile(DiagnosticsEngine &Diags);
135 
136  std::string OutputFile;
137  std::vector<std::string> Targets;
138  bool IncludeSystemHeaders;
139  bool PhonyTarget;
140  bool AddMissingHeaderDeps;
141  bool SeenMissingHeader;
142  bool IncludeModuleFiles;
143  DependencyOutputFormat OutputFormat;
144  unsigned InputFileIndex;
145 };
146 
147 /// Collects the dependencies for imported modules into a directory. Users
148 /// should attach to the AST reader whenever a module is loaded.
150  std::string DestDir;
151  bool HasErrors = false;
152  llvm::StringSet<> Seen;
153  llvm::vfs::YAMLVFSWriter VFSWriter;
154  llvm::StringMap<std::string> SymLinkMap;
155 
156  bool getRealPath(StringRef SrcPath, SmallVectorImpl<char> &Result);
157  std::error_code copyToRoot(StringRef Src, StringRef Dst = {});
158 
159 public:
160  ModuleDependencyCollector(std::string DestDir)
161  : DestDir(std::move(DestDir)) {}
162  ~ModuleDependencyCollector() override { writeFileMap(); }
163 
164  StringRef getDest() { return DestDir; }
165  virtual bool insertSeen(StringRef Filename) { return Seen.insert(Filename).second; }
166  virtual void addFile(StringRef Filename, StringRef FileDst = {});
167 
168  virtual void addFileMapping(StringRef VPath, StringRef RPath) {
169  VFSWriter.addFileMapping(VPath, RPath);
170  }
171 
172  void attachToPreprocessor(Preprocessor &PP) override;
173  void attachToASTReader(ASTReader &R) override;
174 
175  virtual void writeFileMap();
176  virtual bool hasErrors() { return HasErrors; }
177 };
178 
179 /// AttachDependencyGraphGen - Create a dependency graph generator, and attach
180 /// it to the given preprocessor.
181 void AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile,
182  StringRef SysRoot);
183 
184 /// AttachHeaderIncludeGen - Create a header include list generator, and attach
185 /// it to the given preprocessor.
186 ///
187 /// \param DepOpts - Options controlling the output.
188 /// \param ShowAllHeaders - If true, show all header information instead of just
189 /// headers following the predefines buffer. This is useful for making sure
190 /// includes mentioned on the command line are also reported, but differs from
191 /// the default behavior used by -H.
192 /// \param OutputPath - If non-empty, a path to write the header include
193 /// information to, instead of writing to stderr.
194 /// \param ShowDepth - Whether to indent to show the nesting of the includes.
195 /// \param MSStyle - Whether to print in cl.exe /showIncludes style.
197  const DependencyOutputOptions &DepOpts,
198  bool ShowAllHeaders = false,
199  StringRef OutputPath = {},
200  bool ShowDepth = true, bool MSStyle = false);
201 
202 /// The ChainedIncludesSource class converts headers to chained PCHs in
203 /// memory, mainly for testing.
207 
208 /// createInvocationFromCommandLine - Construct a compiler invocation object for
209 /// a command line argument vector.
210 ///
211 /// \param ShouldRecoverOnErrors - whether we should attempt to return a
212 /// non-null (and possibly incorrect) CompilerInvocation if any errors were
213 /// encountered. When this flag is false, always return null on errors.
214 ///
215 /// \param CC1Args - if non-null, will be populated with the args to cc1
216 /// expanded from \p Args. May be set even if nullptr is returned.
217 ///
218 /// \return A CompilerInvocation, or nullptr if none was built for the given
219 /// argument vector.
220 std::unique_ptr<CompilerInvocation> createInvocationFromCommandLine(
225  bool ShouldRecoverOnErrors = false,
226  std::vector<std::string> *CC1Args = nullptr);
227 
228 // Frontend timing utils
229 
230 /// If the user specifies the -ftime-report argument on an Clang command line
231 /// then the value of this boolean will be true, otherwise false.
232 extern bool FrontendTimesIsEnabled;
233 
234 } // namespace clang
235 
236 #endif // LLVM_CLANG_FRONTEND_UTILS_H
IntrusiveRefCntPtr< ExternalSemaSource > createChainedIncludesSource(CompilerInstance &CI, IntrusiveRefCntPtr< ExternalSemaSource > &Reader)
The ChainedIncludesSource class converts headers to chained PCHs in memory, mainly for testing...
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
Definition: Dominators.h:30
An interface for collecting the dependencies of a compilation.
Definition: Utils.h:76
virtual bool insertSeen(StringRef Filename)
Definition: Utils.h:165
Definition: Format.h:2679
virtual bool needSystemDependencies()
Return true if system files should be passed to sawDependency().
Definition: Utils.h:95
virtual void addFileMapping(StringRef VPath, StringRef RPath)
Definition: Utils.h:168
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:153
Defines the Diagnostic-related interfaces.
ModuleDependencyCollector(std::string DestDir)
Definition: Utils.h:160
Builds a dependency file when attached to a Preprocessor (for includes) and ASTReader (for module imp...
Definition: Utils.h:117
StringRef Filename
Definition: Format.cpp:2018
void DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS, const PreprocessorOutputOptions &Opts)
DoPrintPreprocessedInput - Implement -E mode.
ArrayRef< std::string > getDependencies() const
Definition: Utils.h:82
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...
Collects the dependencies for imported modules into a directory.
Definition: Utils.h:149
void ApplyHeaderSearchOptions(HeaderSearch &HS, const HeaderSearchOptions &HSOpts, const LangOptions &Lang, const llvm::Triple &triple)
Apply the header search options to get given HeaderSearch object.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
void AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile, StringRef SysRoot)
AttachDependencyGraphGen - Create a dependency graph generator, and attach it to the given preprocess...
DependencyOutputOptions - Options for controlling the compiler dependency file generation.
virtual bool hasErrors()
Definition: Utils.h:176
Dataflow Directional Tag Classes.
virtual void finishedMainFile(DiagnosticsEngine &Diags)
Called when the end of the main file is reached.
Definition: Utils.h:92
bool needSystemDependencies() final override
Return true if system files should be passed to sawDependency().
Definition: Utils.h:125
bool FrontendTimesIsEnabled
If the user specifies the -ftime-report argument on an Clang command line then the value of this bool...
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:340
~ModuleDependencyCollector() override
Definition: Utils.h:162
std::unique_ptr< CompilerInvocation > createInvocationFromCommandLine(ArrayRef< const char *> Args, IntrusiveRefCntPtr< DiagnosticsEngine > Diags=IntrusiveRefCntPtr< DiagnosticsEngine >(), IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS=nullptr, bool ShouldRecoverOnErrors=false, std::vector< std::string > *CC1Args=nullptr)
createInvocationFromCommandLine - Construct a compiler invocation object for a command line argument ...
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 ...
DependencyOutputFormat
DependencyOutputFormat - Format for the compiler dependency file.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:128