Home | History | Annotate | Download | only in Lex
      1 //===--- PreprocessorOptions.h ----------------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #ifndef LLVM_CLANG_LEX_PREPROCESSOROPTIONS_H_
     11 #define LLVM_CLANG_LEX_PREPROCESSOROPTIONS_H_
     12 
     13 #include "clang/Basic/SourceLocation.h"
     14 #include "llvm/ADT/IntrusiveRefCntPtr.h"
     15 #include "llvm/ADT/SmallVector.h"
     16 #include "llvm/ADT/StringRef.h"
     17 #include "llvm/ADT/StringSet.h"
     18 #include <cassert>
     19 #include <set>
     20 #include <string>
     21 #include <utility>
     22 #include <vector>
     23 
     24 namespace llvm {
     25   class MemoryBuffer;
     26 }
     27 
     28 namespace clang {
     29 
     30 class Preprocessor;
     31 class LangOptions;
     32 
     33 /// \brief Enumerate the kinds of standard library that
     34 enum ObjCXXARCStandardLibraryKind {
     35   ARCXX_nolib,
     36   /// \brief libc++
     37   ARCXX_libcxx,
     38   /// \brief libstdc++
     39   ARCXX_libstdcxx
     40 };
     41 
     42 /// PreprocessorOptions - This class is used for passing the various options
     43 /// used in preprocessor initialization to InitializePreprocessor().
     44 class PreprocessorOptions : public RefCountedBase<PreprocessorOptions> {
     45 public:
     46   std::vector<std::pair<std::string, bool/*isUndef*/> > Macros;
     47   std::vector<std::string> Includes;
     48   std::vector<std::string> MacroIncludes;
     49 
     50   /// \brief Initialize the preprocessor with the compiler and target specific
     51   /// predefines.
     52   unsigned UsePredefines : 1;
     53 
     54   /// \brief Whether we should maintain a detailed record of all macro
     55   /// definitions and expansions.
     56   unsigned DetailedRecord : 1;
     57 
     58   /// The implicit PCH included at the start of the translation unit, or empty.
     59   std::string ImplicitPCHInclude;
     60 
     61   /// \brief Headers that will be converted to chained PCHs in memory.
     62   std::vector<std::string> ChainedIncludes;
     63 
     64   /// \brief When true, disables most of the normal validation performed on
     65   /// precompiled headers.
     66   bool DisablePCHValidation;
     67 
     68   /// \brief When true, a PCH with compiler errors will not be rejected.
     69   bool AllowPCHWithCompilerErrors;
     70 
     71   /// \brief Dump declarations that are deserialized from PCH, for testing.
     72   bool DumpDeserializedPCHDecls;
     73 
     74   /// \brief This is a set of names for decls that we do not want to be
     75   /// deserialized, and we emit an error if they are; for testing purposes.
     76   std::set<std::string> DeserializedPCHDeclsToErrorOn;
     77 
     78   /// \brief If non-zero, the implicit PCH include is actually a precompiled
     79   /// preamble that covers this number of bytes in the main source file.
     80   ///
     81   /// The boolean indicates whether the preamble ends at the start of a new
     82   /// line.
     83   std::pair<unsigned, bool> PrecompiledPreambleBytes;
     84 
     85   /// The implicit PTH input included at the start of the translation unit, or
     86   /// empty.
     87   std::string ImplicitPTHInclude;
     88 
     89   /// If given, a PTH cache file to use for speeding up header parsing.
     90   std::string TokenCache;
     91 
     92   /// \brief True if the SourceManager should report the original file name for
     93   /// contents of files that were remapped to other files. Defaults to true.
     94   bool RemappedFilesKeepOriginalName;
     95 
     96   /// \brief The set of file remappings, which take existing files on
     97   /// the system (the first part of each pair) and gives them the
     98   /// contents of other files on the system (the second part of each
     99   /// pair).
    100   std::vector<std::pair<std::string, std::string>> RemappedFiles;
    101 
    102   /// \brief The set of file-to-buffer remappings, which take existing files
    103   /// on the system (the first part of each pair) and gives them the contents
    104   /// of the specified memory buffer (the second part of each pair).
    105   std::vector<std::pair<std::string, llvm::MemoryBuffer *>> RemappedFileBuffers;
    106 
    107   /// \brief Whether the compiler instance should retain (i.e., not free)
    108   /// the buffers associated with remapped files.
    109   ///
    110   /// This flag defaults to false; it can be set true only through direct
    111   /// manipulation of the compiler invocation object, in cases where the
    112   /// compiler invocation and its buffers will be reused.
    113   bool RetainRemappedFileBuffers;
    114 
    115   /// \brief The Objective-C++ ARC standard library that we should support,
    116   /// by providing appropriate definitions to retrofit the standard library
    117   /// with support for lifetime-qualified pointers.
    118   ObjCXXARCStandardLibraryKind ObjCXXARCStandardLibrary;
    119 
    120   /// \brief Records the set of modules
    121   class FailedModulesSet : public RefCountedBase<FailedModulesSet> {
    122     llvm::StringSet<> Failed;
    123 
    124   public:
    125     bool hasAlreadyFailed(StringRef module) {
    126       return Failed.count(module) > 0;
    127     }
    128 
    129     void addFailed(StringRef module) {
    130       Failed.insert(module);
    131     }
    132   };
    133 
    134   /// \brief The set of modules that failed to build.
    135   ///
    136   /// This pointer will be shared among all of the compiler instances created
    137   /// to (re)build modules, so that once a module fails to build anywhere,
    138   /// other instances will see that the module has failed and won't try to
    139   /// build it again.
    140   IntrusiveRefCntPtr<FailedModulesSet> FailedModules;
    141 
    142 public:
    143   PreprocessorOptions() : UsePredefines(true), DetailedRecord(false),
    144                           DisablePCHValidation(false),
    145                           AllowPCHWithCompilerErrors(false),
    146                           DumpDeserializedPCHDecls(false),
    147                           PrecompiledPreambleBytes(0, true),
    148                           RemappedFilesKeepOriginalName(true),
    149                           RetainRemappedFileBuffers(false),
    150                           ObjCXXARCStandardLibrary(ARCXX_nolib) { }
    151 
    152   void addMacroDef(StringRef Name) { Macros.emplace_back(Name, false); }
    153   void addMacroUndef(StringRef Name) { Macros.emplace_back(Name, true); }
    154   void addRemappedFile(StringRef From, StringRef To) {
    155     RemappedFiles.emplace_back(From, To);
    156   }
    157 
    158   void addRemappedFile(StringRef From, llvm::MemoryBuffer *To) {
    159     RemappedFileBuffers.emplace_back(From, To);
    160   }
    161 
    162   void clearRemappedFiles() {
    163     RemappedFiles.clear();
    164     RemappedFileBuffers.clear();
    165   }
    166 
    167   /// \brief Reset any options that are not considered when building a
    168   /// module.
    169   void resetNonModularOptions() {
    170     Includes.clear();
    171     MacroIncludes.clear();
    172     ChainedIncludes.clear();
    173     DumpDeserializedPCHDecls = false;
    174     ImplicitPCHInclude.clear();
    175     ImplicitPTHInclude.clear();
    176     TokenCache.clear();
    177     RetainRemappedFileBuffers = true;
    178     PrecompiledPreambleBytes.first = 0;
    179     PrecompiledPreambleBytes.second = 0;
    180   }
    181 };
    182 
    183 } // end namespace clang
    184 
    185 #endif
    186