clang API Documentation

Utils.h
Go to the documentation of this file.
00001 //===--- Utils.h - Misc utilities for the front-end -------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This header contains miscellaneous utilities for various front-end actions.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CLANG_FRONTEND_UTILS_H
00015 #define LLVM_CLANG_FRONTEND_UTILS_H
00016 
00017 #include "clang/Basic/Diagnostic.h"
00018 #include "clang/Basic/VirtualFileSystem.h"
00019 #include "llvm/ADT/IntrusiveRefCntPtr.h"
00020 #include "llvm/ADT/StringRef.h"
00021 #include "llvm/ADT/StringSet.h"
00022 #include "llvm/Option/OptSpecifier.h"
00023 
00024 namespace llvm {
00025 class raw_fd_ostream;
00026 class Triple;
00027 
00028 namespace opt {
00029 class ArgList;
00030 }
00031 }
00032 
00033 namespace clang {
00034 class ASTConsumer;
00035 class ASTReader;
00036 class CompilerInstance;
00037 class CompilerInvocation;
00038 class Decl;
00039 class DependencyOutputOptions;
00040 class DiagnosticsEngine;
00041 class DiagnosticOptions;
00042 class ExternalSemaSource;
00043 class FileManager;
00044 class HeaderSearch;
00045 class HeaderSearchOptions;
00046 class IdentifierTable;
00047 class LangOptions;
00048 class Preprocessor;
00049 class PreprocessorOptions;
00050 class PreprocessorOutputOptions;
00051 class SourceManager;
00052 class Stmt;
00053 class TargetInfo;
00054 class FrontendOptions;
00055 
00056 /// Apply the header search options to get given HeaderSearch object.
00057 void ApplyHeaderSearchOptions(HeaderSearch &HS,
00058                               const HeaderSearchOptions &HSOpts,
00059                               const LangOptions &Lang,
00060                               const llvm::Triple &triple);
00061 
00062 /// InitializePreprocessor - Initialize the preprocessor getting it and the
00063 /// environment ready to process a single file.
00064 void InitializePreprocessor(Preprocessor &PP,
00065                             const PreprocessorOptions &PPOpts,
00066                             const FrontendOptions &FEOpts);
00067 
00068 /// DoPrintPreprocessedInput - Implement -E mode.
00069 void DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream* OS,
00070                               const PreprocessorOutputOptions &Opts);
00071 
00072 /// An interface for collecting the dependencies of a compilation. Users should
00073 /// use \c attachToPreprocessor and \c attachToASTReader to get all of the
00074 /// dependencies.
00075 // FIXME: Migrate DependencyFileGen, DependencyGraphGen, ModuleDepCollectory to
00076 // use this interface.
00077 class DependencyCollector {
00078 public:
00079   void attachToPreprocessor(Preprocessor &PP);
00080   void attachToASTReader(ASTReader &R);
00081   llvm::ArrayRef<std::string> getDependencies() const { return Dependencies; }
00082 
00083   /// Called when a new file is seen. Return true if \p Filename should be added
00084   /// to the list of dependencies.
00085   ///
00086   /// The default implementation ignores <built-in> and system files.
00087   virtual bool sawDependency(StringRef Filename, bool FromModule,
00088                              bool IsSystem, bool IsModuleFile, bool IsMissing);
00089   /// Called when the end of the main file is reached.
00090   virtual void finishedMainFile() { }
00091   /// Return true if system files should be passed to sawDependency().
00092   virtual bool needSystemDependencies() { return false; }
00093   virtual ~DependencyCollector();
00094 
00095 public: // implementation detail
00096   /// Add a dependency \p Filename if it has not been seen before and
00097   /// sawDependency() returns true.
00098   void maybeAddDependency(StringRef Filename, bool FromModule, bool IsSystem,
00099                           bool IsModuleFile, bool IsMissing);
00100 private:
00101   llvm::StringSet<> Seen;
00102   std::vector<std::string> Dependencies;
00103 };
00104 
00105 /// Builds a depdenency file when attached to a Preprocessor (for includes) and
00106 /// ASTReader (for module imports), and writes it out at the end of processing
00107 /// a source file.  Users should attach to the ast reader whenever a module is
00108 /// loaded.
00109 class DependencyFileGenerator {
00110   void *Impl; // Opaque implementation
00111   DependencyFileGenerator(void *Impl);
00112 public:
00113   static DependencyFileGenerator *CreateAndAttachToPreprocessor(
00114     Preprocessor &PP, const DependencyOutputOptions &Opts);
00115   void AttachToASTReader(ASTReader &R);
00116 };
00117 
00118 /// Collects the dependencies for imported modules into a directory.  Users
00119 /// should attach to the AST reader whenever a module is loaded.
00120 class ModuleDependencyCollector {
00121   std::string DestDir;
00122   bool HasErrors;
00123   llvm::StringSet<> Seen;
00124   vfs::YAMLVFSWriter VFSWriter;
00125 
00126 public:
00127   StringRef getDest() { return DestDir; }
00128   bool insertSeen(StringRef Filename) { return Seen.insert(Filename); }
00129   void setHasErrors() { HasErrors = true; }
00130   void addFileMapping(StringRef VPath, StringRef RPath) {
00131     VFSWriter.addFileMapping(VPath, RPath);
00132   }
00133 
00134   void attachToASTReader(ASTReader &R);
00135   void writeFileMap();
00136   bool hasErrors() { return HasErrors; }
00137   ModuleDependencyCollector(std::string DestDir)
00138       : DestDir(DestDir), HasErrors(false) {}
00139   ~ModuleDependencyCollector() { writeFileMap(); }
00140 };
00141 
00142 /// AttachDependencyGraphGen - Create a dependency graph generator, and attach
00143 /// it to the given preprocessor.
00144   void AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile,
00145                                 StringRef SysRoot);
00146 
00147 /// AttachHeaderIncludeGen - Create a header include list generator, and attach
00148 /// it to the given preprocessor.
00149 ///
00150 /// \param ShowAllHeaders - If true, show all header information instead of just
00151 /// headers following the predefines buffer. This is useful for making sure
00152 /// includes mentioned on the command line are also reported, but differs from
00153 /// the default behavior used by -H.
00154 /// \param OutputPath - If non-empty, a path to write the header include
00155 /// information to, instead of writing to stderr.
00156 /// \param ShowDepth - Whether to indent to show the nesting of the includes.
00157 /// \param MSStyle - Whether to print in cl.exe /showIncludes style.
00158 void AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders = false,
00159                             StringRef OutputPath = "",
00160                             bool ShowDepth = true, bool MSStyle = false);
00161 
00162 /// CacheTokens - Cache tokens for use with PCH. Note that this requires
00163 /// a seekable stream.
00164 void CacheTokens(Preprocessor &PP, llvm::raw_fd_ostream* OS);
00165 
00166 /// The ChainedIncludesSource class converts headers to chained PCHs in
00167 /// memory, mainly for testing.
00168 IntrusiveRefCntPtr<ExternalSemaSource>
00169 createChainedIncludesSource(CompilerInstance &CI,
00170                             IntrusiveRefCntPtr<ExternalSemaSource> &Reader);
00171 
00172 /// createInvocationFromCommandLine - Construct a compiler invocation object for
00173 /// a command line argument vector.
00174 ///
00175 /// \return A CompilerInvocation, or 0 if none was built for the given
00176 /// argument vector.
00177 CompilerInvocation *
00178 createInvocationFromCommandLine(ArrayRef<const char *> Args,
00179                             IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
00180                                 IntrusiveRefCntPtr<DiagnosticsEngine>());
00181 
00182 /// Return the value of the last argument as an integer, or a default. If Diags
00183 /// is non-null, emits an error if the argument is given, but non-integral.
00184 int getLastArgIntValue(const llvm::opt::ArgList &Args,
00185                        llvm::opt::OptSpecifier Id, int Default,
00186                        DiagnosticsEngine *Diags = nullptr);
00187 
00188 inline int getLastArgIntValue(const llvm::opt::ArgList &Args,
00189                               llvm::opt::OptSpecifier Id, int Default,
00190                               DiagnosticsEngine &Diags) {
00191   return getLastArgIntValue(Args, Id, Default, &Diags);
00192 }
00193 
00194 uint64_t getLastArgUInt64Value(const llvm::opt::ArgList &Args,
00195                                llvm::opt::OptSpecifier Id, uint64_t Default,
00196                                DiagnosticsEngine *Diags = nullptr);
00197 
00198 inline uint64_t getLastArgUInt64Value(const llvm::opt::ArgList &Args,
00199                                       llvm::opt::OptSpecifier Id,
00200                                       uint64_t Default,
00201                                       DiagnosticsEngine &Diags) {
00202   return getLastArgUInt64Value(Args, Id, Default, &Diags);
00203 }
00204 
00205 // When Clang->getFrontendOpts().DisableFree is set we don't delete some of the
00206 // global objects, but we don't want LeakDetectors to complain, so we bury them
00207 // in a globally visible array.
00208 void BuryPointer(const void *Ptr);
00209 template <typename T> void BuryPointer(std::unique_ptr<T> Ptr) {
00210   BuryPointer(Ptr.release());
00211 }
00212 
00213 } // end namespace clang
00214 
00215 #endif