Yi Kong | 7bf2a68 | 2019-04-18 17:30:49 -0700 | [diff] [blame] | 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" |
| 18 | #include "llvm/ADT/ArrayRef.h" |
| 19 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
| 20 | #include "llvm/ADT/StringMap.h" |
| 21 | #include "llvm/ADT/StringRef.h" |
| 22 | #include "llvm/ADT/StringSet.h" |
| 23 | #include "llvm/Option/OptSpecifier.h" |
| 24 | #include "llvm/Support/VirtualFileSystem.h" |
| 25 | #include <cstdint> |
| 26 | #include <memory> |
| 27 | #include <string> |
| 28 | #include <system_error> |
| 29 | #include <utility> |
| 30 | #include <vector> |
| 31 | |
| 32 | namespace llvm { |
| 33 | |
| 34 | class Triple; |
| 35 | |
| 36 | namespace opt { |
| 37 | |
| 38 | class ArgList; |
| 39 | |
| 40 | } // namespace opt |
| 41 | |
| 42 | } // namespace llvm |
| 43 | |
| 44 | namespace clang { |
| 45 | |
| 46 | class ASTReader; |
| 47 | class CompilerInstance; |
| 48 | class CompilerInvocation; |
| 49 | class DependencyOutputOptions; |
| 50 | class DiagnosticsEngine; |
| 51 | class ExternalSemaSource; |
| 52 | class FrontendOptions; |
| 53 | class HeaderSearch; |
| 54 | class HeaderSearchOptions; |
| 55 | class LangOptions; |
| 56 | class PCHContainerReader; |
| 57 | class Preprocessor; |
| 58 | class PreprocessorOptions; |
| 59 | class PreprocessorOutputOptions; |
| 60 | |
| 61 | /// Apply the header search options to get given HeaderSearch object. |
| 62 | void ApplyHeaderSearchOptions(HeaderSearch &HS, |
| 63 | const HeaderSearchOptions &HSOpts, |
| 64 | const LangOptions &Lang, |
| 65 | const llvm::Triple &triple); |
| 66 | |
| 67 | /// InitializePreprocessor - Initialize the preprocessor getting it and the |
| 68 | /// environment ready to process a single file. |
| 69 | void InitializePreprocessor(Preprocessor &PP, const PreprocessorOptions &PPOpts, |
| 70 | const PCHContainerReader &PCHContainerRdr, |
| 71 | const FrontendOptions &FEOpts); |
| 72 | |
| 73 | /// DoPrintPreprocessedInput - Implement -E mode. |
| 74 | void DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS, |
| 75 | const PreprocessorOutputOptions &Opts); |
| 76 | |
| 77 | /// An interface for collecting the dependencies of a compilation. Users should |
| 78 | /// use \c attachToPreprocessor and \c attachToASTReader to get all of the |
| 79 | /// dependencies. |
| 80 | /// FIXME: Migrate DependencyFileGen and DependencyGraphGen to use this |
| 81 | /// interface. |
| 82 | class DependencyCollector { |
| 83 | public: |
| 84 | virtual ~DependencyCollector(); |
| 85 | |
| 86 | virtual void attachToPreprocessor(Preprocessor &PP); |
| 87 | virtual void attachToASTReader(ASTReader &R); |
| 88 | ArrayRef<std::string> getDependencies() const { return Dependencies; } |
| 89 | |
| 90 | /// Called when a new file is seen. Return true if \p Filename should be added |
| 91 | /// to the list of dependencies. |
| 92 | /// |
| 93 | /// The default implementation ignores <built-in> and system files. |
| 94 | virtual bool sawDependency(StringRef Filename, bool FromModule, |
| 95 | bool IsSystem, bool IsModuleFile, bool IsMissing); |
| 96 | |
| 97 | /// Called when the end of the main file is reached. |
| 98 | virtual void finishedMainFile() {} |
| 99 | |
| 100 | /// Return true if system files should be passed to sawDependency(). |
| 101 | virtual bool needSystemDependencies() { return false; } |
| 102 | |
| 103 | // implementation detail |
| 104 | /// Add a dependency \p Filename if it has not been seen before and |
| 105 | /// sawDependency() returns true. |
| 106 | void maybeAddDependency(StringRef Filename, bool FromModule, bool IsSystem, |
| 107 | bool IsModuleFile, bool IsMissing); |
| 108 | |
| 109 | private: |
| 110 | llvm::StringSet<> Seen; |
| 111 | std::vector<std::string> Dependencies; |
| 112 | }; |
| 113 | |
| 114 | /// Builds a depdenency file when attached to a Preprocessor (for includes) and |
| 115 | /// ASTReader (for module imports), and writes it out at the end of processing |
| 116 | /// a source file. Users should attach to the ast reader whenever a module is |
| 117 | /// loaded. |
| 118 | class DependencyFileGenerator { |
| 119 | void *Impl; // Opaque implementation |
| 120 | |
| 121 | DependencyFileGenerator(void *Impl); |
| 122 | |
| 123 | public: |
| 124 | static DependencyFileGenerator *CreateAndAttachToPreprocessor( |
| 125 | Preprocessor &PP, const DependencyOutputOptions &Opts); |
| 126 | |
| 127 | void AttachToASTReader(ASTReader &R); |
| 128 | }; |
| 129 | |
| 130 | /// Collects the dependencies for imported modules into a directory. Users |
| 131 | /// should attach to the AST reader whenever a module is loaded. |
| 132 | class ModuleDependencyCollector : public DependencyCollector { |
| 133 | std::string DestDir; |
| 134 | bool HasErrors = false; |
| 135 | llvm::StringSet<> Seen; |
| 136 | llvm::vfs::YAMLVFSWriter VFSWriter; |
| 137 | llvm::StringMap<std::string> SymLinkMap; |
| 138 | |
| 139 | bool getRealPath(StringRef SrcPath, SmallVectorImpl<char> &Result); |
| 140 | std::error_code copyToRoot(StringRef Src, StringRef Dst = {}); |
| 141 | |
| 142 | public: |
| 143 | ModuleDependencyCollector(std::string DestDir) |
| 144 | : DestDir(std::move(DestDir)) {} |
| 145 | ~ModuleDependencyCollector() override { writeFileMap(); } |
| 146 | |
| 147 | StringRef getDest() { return DestDir; } |
| 148 | virtual bool insertSeen(StringRef Filename) { return Seen.insert(Filename).second; } |
| 149 | virtual void addFile(StringRef Filename, StringRef FileDst = {}); |
| 150 | |
| 151 | virtual void addFileMapping(StringRef VPath, StringRef RPath) { |
| 152 | VFSWriter.addFileMapping(VPath, RPath); |
| 153 | } |
| 154 | |
| 155 | void attachToPreprocessor(Preprocessor &PP) override; |
| 156 | void attachToASTReader(ASTReader &R) override; |
| 157 | |
| 158 | virtual void writeFileMap(); |
| 159 | virtual bool hasErrors() { return HasErrors; } |
| 160 | }; |
| 161 | |
| 162 | /// AttachDependencyGraphGen - Create a dependency graph generator, and attach |
| 163 | /// it to the given preprocessor. |
| 164 | void AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile, |
| 165 | StringRef SysRoot); |
| 166 | |
| 167 | /// AttachHeaderIncludeGen - Create a header include list generator, and attach |
| 168 | /// it to the given preprocessor. |
| 169 | /// |
| 170 | /// \param DepOpts - Options controlling the output. |
| 171 | /// \param ShowAllHeaders - If true, show all header information instead of just |
| 172 | /// headers following the predefines buffer. This is useful for making sure |
| 173 | /// includes mentioned on the command line are also reported, but differs from |
| 174 | /// the default behavior used by -H. |
| 175 | /// \param OutputPath - If non-empty, a path to write the header include |
| 176 | /// information to, instead of writing to stderr. |
| 177 | /// \param ShowDepth - Whether to indent to show the nesting of the includes. |
| 178 | /// \param MSStyle - Whether to print in cl.exe /showIncludes style. |
| 179 | void AttachHeaderIncludeGen(Preprocessor &PP, |
| 180 | const DependencyOutputOptions &DepOpts, |
| 181 | bool ShowAllHeaders = false, |
| 182 | StringRef OutputPath = {}, |
| 183 | bool ShowDepth = true, bool MSStyle = false); |
| 184 | |
| 185 | /// The ChainedIncludesSource class converts headers to chained PCHs in |
| 186 | /// memory, mainly for testing. |
| 187 | IntrusiveRefCntPtr<ExternalSemaSource> |
| 188 | createChainedIncludesSource(CompilerInstance &CI, |
| 189 | IntrusiveRefCntPtr<ExternalSemaSource> &Reader); |
| 190 | |
| 191 | /// createInvocationFromCommandLine - Construct a compiler invocation object for |
| 192 | /// a command line argument vector. |
| 193 | /// |
| 194 | /// \return A CompilerInvocation, or 0 if none was built for the given |
| 195 | /// argument vector. |
| 196 | std::unique_ptr<CompilerInvocation> createInvocationFromCommandLine( |
| 197 | ArrayRef<const char *> Args, |
| 198 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags = |
| 199 | IntrusiveRefCntPtr<DiagnosticsEngine>(), |
| 200 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr); |
| 201 | |
| 202 | /// Return the value of the last argument as an integer, or a default. If Diags |
| 203 | /// is non-null, emits an error if the argument is given, but non-integral. |
| 204 | int getLastArgIntValue(const llvm::opt::ArgList &Args, |
| 205 | llvm::opt::OptSpecifier Id, int Default, |
| 206 | DiagnosticsEngine *Diags = nullptr); |
| 207 | |
| 208 | inline int getLastArgIntValue(const llvm::opt::ArgList &Args, |
| 209 | llvm::opt::OptSpecifier Id, int Default, |
| 210 | DiagnosticsEngine &Diags) { |
| 211 | return getLastArgIntValue(Args, Id, Default, &Diags); |
| 212 | } |
| 213 | |
| 214 | uint64_t getLastArgUInt64Value(const llvm::opt::ArgList &Args, |
| 215 | llvm::opt::OptSpecifier Id, uint64_t Default, |
| 216 | DiagnosticsEngine *Diags = nullptr); |
| 217 | |
| 218 | inline uint64_t getLastArgUInt64Value(const llvm::opt::ArgList &Args, |
| 219 | llvm::opt::OptSpecifier Id, |
| 220 | uint64_t Default, |
| 221 | DiagnosticsEngine &Diags) { |
| 222 | return getLastArgUInt64Value(Args, Id, Default, &Diags); |
| 223 | } |
| 224 | |
| 225 | // Frontend timing utils |
| 226 | |
| 227 | /// If the user specifies the -ftime-report argument on an Clang command line |
| 228 | /// then the value of this boolean will be true, otherwise false. |
| 229 | extern bool FrontendTimesIsEnabled; |
| 230 | |
| 231 | } // namespace clang |
| 232 | |
| 233 | #endif // LLVM_CLANG_FRONTEND_UTILS_H |