PreprocessorOptions.h revision 37ed12720a35b7bfa1c4de73ad6f1c6c1c88ee17
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 <string>
20#include <utility>
21#include <vector>
22#include <set>
23
24namespace llvm {
25  class MemoryBuffer;
26}
27
28namespace clang {
29
30class Preprocessor;
31class LangOptions;
32
33/// \brief Enumerate the kinds of standard library that
34enum 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().
44class PreprocessorOptions : public llvm::RefCountedBase<PreprocessorOptions> {
45public:
46  std::vector<std::pair<std::string, bool/*isUndef*/> > Macros;
47  std::vector<std::string> Includes;
48  std::vector<std::string> MacroIncludes;
49
50  unsigned UsePredefines : 1; /// Initialize the preprocessor with the compiler
51                              /// and target specific predefines.
52
53  unsigned DetailedRecord : 1; /// Whether we should maintain a detailed
54                               /// record of all macro definitions and
55                               /// expansions.
56
57  /// The implicit PCH included at the start of the translation unit, or empty.
58  std::string ImplicitPCHInclude;
59
60  /// \brief Headers that will be converted to chained PCHs in memory.
61  std::vector<std::string> ChainedIncludes;
62
63  /// \brief When true, disables most of the normal validation performed on
64  /// precompiled headers.
65  bool DisablePCHValidation;
66
67  /// \brief When true, a PCH with compiler errors will not be rejected.
68  bool AllowPCHWithCompilerErrors;
69
70  /// \brief Dump declarations that are deserialized from PCH, for testing.
71  bool DumpDeserializedPCHDecls;
72
73  /// \brief This is a set of names for decls that we do not want to be
74  /// deserialized, and we emit an error if they are; for testing purposes.
75  std::set<std::string> DeserializedPCHDeclsToErrorOn;
76
77  /// \brief If non-zero, the implicit PCH include is actually a precompiled
78  /// preamble that covers this number of bytes in the main source file.
79  ///
80  /// The boolean indicates whether the preamble ends at the start of a new
81  /// line.
82  std::pair<unsigned, bool> PrecompiledPreambleBytes;
83
84  /// The implicit PTH input included at the start of the translation unit, or
85  /// empty.
86  std::string ImplicitPTHInclude;
87
88  /// If given, a PTH cache file to use for speeding up header parsing.
89  std::string TokenCache;
90
91  /// \brief True if the SourceManager should report the original file name for
92  /// contents of files that were remapped to other files. Defaults to true.
93  bool RemappedFilesKeepOriginalName;
94
95  /// \brief The set of file remappings, which take existing files on
96  /// the system (the first part of each pair) and gives them the
97  /// contents of other files on the system (the second part of each
98  /// pair).
99  std::vector<std::pair<std::string, std::string> >  RemappedFiles;
100
101  /// \brief The set of file-to-buffer remappings, which take existing files
102  /// on the system (the first part of each pair) and gives them the contents
103  /// of the specified memory buffer (the second part of each pair).
104  std::vector<std::pair<std::string, const llvm::MemoryBuffer *> >
105    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 llvm::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  llvm::IntrusiveRefCntPtr<FailedModulesSet> FailedModules;
141
142  typedef std::vector<std::pair<std::string, std::string> >::iterator
143    remapped_file_iterator;
144  typedef std::vector<std::pair<std::string, std::string> >::const_iterator
145    const_remapped_file_iterator;
146  remapped_file_iterator remapped_file_begin() {
147    return RemappedFiles.begin();
148  }
149  const_remapped_file_iterator remapped_file_begin() const {
150    return RemappedFiles.begin();
151  }
152  remapped_file_iterator remapped_file_end() {
153    return RemappedFiles.end();
154  }
155  const_remapped_file_iterator remapped_file_end() const {
156    return RemappedFiles.end();
157  }
158
159  typedef std::vector<std::pair<std::string, const llvm::MemoryBuffer *> >::
160                                  iterator remapped_file_buffer_iterator;
161  typedef std::vector<std::pair<std::string, const llvm::MemoryBuffer *> >::
162                            const_iterator const_remapped_file_buffer_iterator;
163  remapped_file_buffer_iterator remapped_file_buffer_begin() {
164    return RemappedFileBuffers.begin();
165  }
166  const_remapped_file_buffer_iterator remapped_file_buffer_begin() const {
167    return RemappedFileBuffers.begin();
168  }
169  remapped_file_buffer_iterator remapped_file_buffer_end() {
170    return RemappedFileBuffers.end();
171  }
172  const_remapped_file_buffer_iterator remapped_file_buffer_end() const {
173    return RemappedFileBuffers.end();
174  }
175
176public:
177  PreprocessorOptions() : UsePredefines(true), DetailedRecord(false),
178                          DisablePCHValidation(false),
179                          AllowPCHWithCompilerErrors(false),
180                          DumpDeserializedPCHDecls(false),
181                          PrecompiledPreambleBytes(0, true),
182                          RemappedFilesKeepOriginalName(true),
183                          RetainRemappedFileBuffers(false),
184                          ObjCXXARCStandardLibrary(ARCXX_nolib) { }
185
186  void addMacroDef(StringRef Name) {
187    Macros.push_back(std::make_pair(Name, false));
188  }
189  void addMacroUndef(StringRef Name) {
190    Macros.push_back(std::make_pair(Name, true));
191  }
192  void addRemappedFile(StringRef From, StringRef To) {
193    RemappedFiles.push_back(std::make_pair(From, To));
194  }
195
196  remapped_file_iterator eraseRemappedFile(remapped_file_iterator Remapped) {
197    return RemappedFiles.erase(Remapped);
198  }
199
200  void addRemappedFile(StringRef From, const llvm::MemoryBuffer * To) {
201    RemappedFileBuffers.push_back(std::make_pair(From, To));
202  }
203
204  remapped_file_buffer_iterator
205  eraseRemappedFile(remapped_file_buffer_iterator Remapped) {
206    return RemappedFileBuffers.erase(Remapped);
207  }
208
209  void clearRemappedFiles() {
210    RemappedFiles.clear();
211    RemappedFileBuffers.clear();
212  }
213
214  /// \brief Reset any options that are not considered when building a
215  /// module.
216  void resetNonModularOptions() {
217    Includes.clear();
218    MacroIncludes.clear();
219    ChainedIncludes.clear();
220    DumpDeserializedPCHDecls = false;
221    ImplicitPCHInclude.clear();
222    ImplicitPTHInclude.clear();
223    TokenCache.clear();
224    RetainRemappedFileBuffers = true;
225    PrecompiledPreambleBytes.first = 0;
226    PrecompiledPreambleBytes.second = 0;
227  }
228};
229
230} // end namespace clang
231
232#endif
233