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
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 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  /// \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, const llvm::MemoryBuffer *> >
106    RemappedFileBuffers;
107
108  /// \brief Whether the compiler instance should retain (i.e., not free)
109  /// the buffers associated with remapped files.
110  ///
111  /// This flag defaults to false; it can be set true only through direct
112  /// manipulation of the compiler invocation object, in cases where the
113  /// compiler invocation and its buffers will be reused.
114  bool RetainRemappedFileBuffers;
115
116  /// \brief The Objective-C++ ARC standard library that we should support,
117  /// by providing appropriate definitions to retrofit the standard library
118  /// with support for lifetime-qualified pointers.
119  ObjCXXARCStandardLibraryKind ObjCXXARCStandardLibrary;
120
121  /// \brief Records the set of modules
122  class FailedModulesSet : public RefCountedBase<FailedModulesSet> {
123    llvm::StringSet<> Failed;
124
125  public:
126    bool hasAlreadyFailed(StringRef module) {
127      return Failed.count(module) > 0;
128    }
129
130    void addFailed(StringRef module) {
131      Failed.insert(module);
132    }
133  };
134
135  /// \brief The set of modules that failed to build.
136  ///
137  /// This pointer will be shared among all of the compiler instances created
138  /// to (re)build modules, so that once a module fails to build anywhere,
139  /// other instances will see that the module has failed and won't try to
140  /// build it again.
141  IntrusiveRefCntPtr<FailedModulesSet> FailedModules;
142
143  typedef std::vector<std::pair<std::string, std::string> >::iterator
144    remapped_file_iterator;
145  typedef std::vector<std::pair<std::string, std::string> >::const_iterator
146    const_remapped_file_iterator;
147  remapped_file_iterator remapped_file_begin() {
148    return RemappedFiles.begin();
149  }
150  const_remapped_file_iterator remapped_file_begin() const {
151    return RemappedFiles.begin();
152  }
153  remapped_file_iterator remapped_file_end() {
154    return RemappedFiles.end();
155  }
156  const_remapped_file_iterator remapped_file_end() const {
157    return RemappedFiles.end();
158  }
159
160  typedef std::vector<std::pair<std::string, const llvm::MemoryBuffer *> >::
161                                  iterator remapped_file_buffer_iterator;
162  typedef std::vector<std::pair<std::string, const llvm::MemoryBuffer *> >::
163                            const_iterator const_remapped_file_buffer_iterator;
164  remapped_file_buffer_iterator remapped_file_buffer_begin() {
165    return RemappedFileBuffers.begin();
166  }
167  const_remapped_file_buffer_iterator remapped_file_buffer_begin() const {
168    return RemappedFileBuffers.begin();
169  }
170  remapped_file_buffer_iterator remapped_file_buffer_end() {
171    return RemappedFileBuffers.end();
172  }
173  const_remapped_file_buffer_iterator remapped_file_buffer_end() const {
174    return RemappedFileBuffers.end();
175  }
176
177public:
178  PreprocessorOptions() : UsePredefines(true), DetailedRecord(false),
179                          DisablePCHValidation(false),
180                          AllowPCHWithCompilerErrors(false),
181                          DumpDeserializedPCHDecls(false),
182                          PrecompiledPreambleBytes(0, true),
183                          RemappedFilesKeepOriginalName(true),
184                          RetainRemappedFileBuffers(false),
185                          ObjCXXARCStandardLibrary(ARCXX_nolib) { }
186
187  void addMacroDef(StringRef Name) {
188    Macros.push_back(std::make_pair(Name, false));
189  }
190  void addMacroUndef(StringRef Name) {
191    Macros.push_back(std::make_pair(Name, true));
192  }
193  void addRemappedFile(StringRef From, StringRef To) {
194    RemappedFiles.push_back(std::make_pair(From, To));
195  }
196
197  remapped_file_iterator eraseRemappedFile(remapped_file_iterator Remapped) {
198    return RemappedFiles.erase(Remapped);
199  }
200
201  void addRemappedFile(StringRef From, const llvm::MemoryBuffer * To) {
202    RemappedFileBuffers.push_back(std::make_pair(From, To));
203  }
204
205  remapped_file_buffer_iterator
206  eraseRemappedFile(remapped_file_buffer_iterator Remapped) {
207    return RemappedFileBuffers.erase(Remapped);
208  }
209
210  void clearRemappedFiles() {
211    RemappedFiles.clear();
212    RemappedFileBuffers.clear();
213  }
214
215  /// \brief Reset any options that are not considered when building a
216  /// module.
217  void resetNonModularOptions() {
218    Includes.clear();
219    MacroIncludes.clear();
220    ChainedIncludes.clear();
221    DumpDeserializedPCHDecls = false;
222    ImplicitPCHInclude.clear();
223    ImplicitPTHInclude.clear();
224    TokenCache.clear();
225    RetainRemappedFileBuffers = true;
226    PrecompiledPreambleBytes.first = 0;
227    PrecompiledPreambleBytes.second = 0;
228  }
229};
230
231} // end namespace clang
232
233#endif
234