1 //===--- Utils.h - Misc utilities for the front-end -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header contains miscellaneous utilities for various front-end actions.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_FRONTEND_UTILS_H
15 #define LLVM_CLANG_FRONTEND_UTILS_H
17 #include "clang/Basic/Diagnostic.h"
18 #include "clang/Basic/VirtualFileSystem.h"
19 #include "llvm/ADT/IntrusiveRefCntPtr.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/StringSet.h"
22 #include "llvm/Option/OptSpecifier.h"
36 class CompilerInstance;
37 class CompilerInvocation;
39 class DependencyOutputOptions;
40 class DiagnosticsEngine;
41 class DiagnosticOptions;
42 class ExternalSemaSource;
45 class HeaderSearchOptions;
46 class IdentifierTable;
48 class PCHContainerOperations;
50 class PreprocessorOptions;
51 class PreprocessorOutputOptions;
55 class FrontendOptions;
57 /// Apply the header search options to get given HeaderSearch object.
58 void ApplyHeaderSearchOptions(HeaderSearch &HS,
59 const HeaderSearchOptions &HSOpts,
60 const LangOptions &Lang,
61 const llvm::Triple &triple);
63 /// InitializePreprocessor - Initialize the preprocessor getting it and the
64 /// environment ready to process a single file.
65 void InitializePreprocessor(Preprocessor &PP, const PreprocessorOptions &PPOpts,
66 const PCHContainerOperations &PCHContainerOps,
67 const FrontendOptions &FEOpts);
69 /// DoPrintPreprocessedInput - Implement -E mode.
70 void DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream* OS,
71 const PreprocessorOutputOptions &Opts);
73 /// An interface for collecting the dependencies of a compilation. Users should
74 /// use \c attachToPreprocessor and \c attachToASTReader to get all of the
76 // FIXME: Migrate DependencyFileGen, DependencyGraphGen, ModuleDepCollectory to
77 // use this interface.
78 class DependencyCollector {
80 void attachToPreprocessor(Preprocessor &PP);
81 void attachToASTReader(ASTReader &R);
82 llvm::ArrayRef<std::string> getDependencies() const { return Dependencies; }
84 /// Called when a new file is seen. Return true if \p Filename should be added
85 /// to the list of dependencies.
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 /// Called when the end of the main file is reached.
91 virtual void finishedMainFile() { }
92 /// Return true if system files should be passed to sawDependency().
93 virtual bool needSystemDependencies() { return false; }
94 virtual ~DependencyCollector();
96 public: // implementation detail
97 /// Add a dependency \p Filename if it has not been seen before and
98 /// sawDependency() returns true.
99 void maybeAddDependency(StringRef Filename, bool FromModule, bool IsSystem,
100 bool IsModuleFile, bool IsMissing);
102 llvm::StringSet<> Seen;
103 std::vector<std::string> Dependencies;
106 /// Builds a depdenency file when attached to a Preprocessor (for includes) and
107 /// ASTReader (for module imports), and writes it out at the end of processing
108 /// a source file. Users should attach to the ast reader whenever a module is
110 class DependencyFileGenerator {
111 void *Impl; // Opaque implementation
112 DependencyFileGenerator(void *Impl);
114 static DependencyFileGenerator *CreateAndAttachToPreprocessor(
115 Preprocessor &PP, const DependencyOutputOptions &Opts);
116 void AttachToASTReader(ASTReader &R);
119 /// Collects the dependencies for imported modules into a directory. Users
120 /// should attach to the AST reader whenever a module is loaded.
121 class ModuleDependencyCollector {
124 llvm::StringSet<> Seen;
125 vfs::YAMLVFSWriter VFSWriter;
128 StringRef getDest() { return DestDir; }
129 bool insertSeen(StringRef Filename) { return Seen.insert(Filename).second; }
130 void setHasErrors() { HasErrors = true; }
131 void addFileMapping(StringRef VPath, StringRef RPath) {
132 VFSWriter.addFileMapping(VPath, RPath);
135 void attachToASTReader(ASTReader &R);
137 bool hasErrors() { return HasErrors; }
138 ModuleDependencyCollector(std::string DestDir)
139 : DestDir(DestDir), HasErrors(false) {}
140 ~ModuleDependencyCollector() { writeFileMap(); }
143 /// AttachDependencyGraphGen - Create a dependency graph generator, and attach
144 /// it to the given preprocessor.
145 void AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile,
148 /// AttachHeaderIncludeGen - Create a header include list generator, and attach
149 /// it to the given preprocessor.
151 /// \param ShowAllHeaders - If true, show all header information instead of just
152 /// headers following the predefines buffer. This is useful for making sure
153 /// includes mentioned on the command line are also reported, but differs from
154 /// the default behavior used by -H.
155 /// \param OutputPath - If non-empty, a path to write the header include
156 /// information to, instead of writing to stderr.
157 /// \param ShowDepth - Whether to indent to show the nesting of the includes.
158 /// \param MSStyle - Whether to print in cl.exe /showIncludes style.
159 void AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders = false,
160 StringRef OutputPath = "",
161 bool ShowDepth = true, bool MSStyle = false);
163 /// Cache tokens for use with PCH. Note that this requires a seekable stream.
164 void CacheTokens(Preprocessor &PP, raw_pwrite_stream *OS);
166 /// The ChainedIncludesSource class converts headers to chained PCHs in
167 /// memory, mainly for testing.
168 IntrusiveRefCntPtr<ExternalSemaSource>
169 createChainedIncludesSource(CompilerInstance &CI,
170 IntrusiveRefCntPtr<ExternalSemaSource> &Reader);
172 /// createInvocationFromCommandLine - Construct a compiler invocation object for
173 /// a command line argument vector.
175 /// \return A CompilerInvocation, or 0 if none was built for the given
178 createInvocationFromCommandLine(ArrayRef<const char *> Args,
179 IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
180 IntrusiveRefCntPtr<DiagnosticsEngine>());
182 /// Return the value of the last argument as an integer, or a default. If Diags
183 /// is non-null, emits an error if the argument is given, but non-integral.
184 int getLastArgIntValue(const llvm::opt::ArgList &Args,
185 llvm::opt::OptSpecifier Id, int Default,
186 DiagnosticsEngine *Diags = nullptr);
188 inline int getLastArgIntValue(const llvm::opt::ArgList &Args,
189 llvm::opt::OptSpecifier Id, int Default,
190 DiagnosticsEngine &Diags) {
191 return getLastArgIntValue(Args, Id, Default, &Diags);
194 uint64_t getLastArgUInt64Value(const llvm::opt::ArgList &Args,
195 llvm::opt::OptSpecifier Id, uint64_t Default,
196 DiagnosticsEngine *Diags = nullptr);
198 inline uint64_t getLastArgUInt64Value(const llvm::opt::ArgList &Args,
199 llvm::opt::OptSpecifier Id,
201 DiagnosticsEngine &Diags) {
202 return getLastArgUInt64Value(Args, Id, Default, &Diags);
205 // When Clang->getFrontendOpts().DisableFree is set we don't delete some of the
206 // global objects, but we don't want LeakDetectors to complain, so we bury them
207 // in a globally visible array.
208 void BuryPointer(const void *Ptr);
209 template <typename T> void BuryPointer(std::unique_ptr<T> Ptr) {
210 BuryPointer(Ptr.release());
213 } // end namespace clang