1243791Sdim//===--- PreprocessorOptions.h ----------------------------------*- C++ -*-===//
2243791Sdim//
3243791Sdim//                     The LLVM Compiler Infrastructure
4243791Sdim//
5243791Sdim// This file is distributed under the University of Illinois Open Source
6243791Sdim// License. See LICENSE.TXT for details.
7243791Sdim//
8243791Sdim//===----------------------------------------------------------------------===//
9243791Sdim
10243791Sdim#ifndef LLVM_CLANG_LEX_PREPROCESSOROPTIONS_H_
11243791Sdim#define LLVM_CLANG_LEX_PREPROCESSOROPTIONS_H_
12243791Sdim
13249423Sdim#include "clang/Basic/SourceLocation.h"
14243791Sdim#include "llvm/ADT/IntrusiveRefCntPtr.h"
15243791Sdim#include "llvm/ADT/SmallVector.h"
16243791Sdim#include "llvm/ADT/StringRef.h"
17249423Sdim#include "llvm/ADT/StringSet.h"
18243791Sdim#include <cassert>
19249423Sdim#include <set>
20243791Sdim#include <string>
21243791Sdim#include <utility>
22243791Sdim#include <vector>
23243791Sdim
24243791Sdimnamespace llvm {
25243791Sdim  class MemoryBuffer;
26243791Sdim}
27243791Sdim
28243791Sdimnamespace clang {
29243791Sdim
30243791Sdimclass Preprocessor;
31243791Sdimclass LangOptions;
32243791Sdim
33243791Sdim/// \brief Enumerate the kinds of standard library that
34243791Sdimenum ObjCXXARCStandardLibraryKind {
35243791Sdim  ARCXX_nolib,
36243791Sdim  /// \brief libc++
37243791Sdim  ARCXX_libcxx,
38243791Sdim  /// \brief libstdc++
39243791Sdim  ARCXX_libstdcxx
40243791Sdim};
41243791Sdim
42243791Sdim/// PreprocessorOptions - This class is used for passing the various options
43243791Sdim/// used in preprocessor initialization to InitializePreprocessor().
44249423Sdimclass PreprocessorOptions : public RefCountedBase<PreprocessorOptions> {
45243791Sdimpublic:
46243791Sdim  std::vector<std::pair<std::string, bool/*isUndef*/> > Macros;
47243791Sdim  std::vector<std::string> Includes;
48243791Sdim  std::vector<std::string> MacroIncludes;
49243791Sdim
50249423Sdim  /// \brief Initialize the preprocessor with the compiler and target specific
51249423Sdim  /// predefines.
52249423Sdim  unsigned UsePredefines : 1;
53243791Sdim
54249423Sdim  /// \brief Whether we should maintain a detailed record of all macro
55249423Sdim  /// definitions and expansions.
56249423Sdim  unsigned DetailedRecord : 1;
57249423Sdim
58243791Sdim  /// The implicit PCH included at the start of the translation unit, or empty.
59243791Sdim  std::string ImplicitPCHInclude;
60243791Sdim
61243791Sdim  /// \brief Headers that will be converted to chained PCHs in memory.
62243791Sdim  std::vector<std::string> ChainedIncludes;
63243791Sdim
64243791Sdim  /// \brief When true, disables most of the normal validation performed on
65243791Sdim  /// precompiled headers.
66243791Sdim  bool DisablePCHValidation;
67243791Sdim
68243791Sdim  /// \brief When true, a PCH with compiler errors will not be rejected.
69243791Sdim  bool AllowPCHWithCompilerErrors;
70243791Sdim
71243791Sdim  /// \brief Dump declarations that are deserialized from PCH, for testing.
72243791Sdim  bool DumpDeserializedPCHDecls;
73243791Sdim
74243791Sdim  /// \brief This is a set of names for decls that we do not want to be
75243791Sdim  /// deserialized, and we emit an error if they are; for testing purposes.
76243791Sdim  std::set<std::string> DeserializedPCHDeclsToErrorOn;
77243791Sdim
78243791Sdim  /// \brief If non-zero, the implicit PCH include is actually a precompiled
79243791Sdim  /// preamble that covers this number of bytes in the main source file.
80243791Sdim  ///
81243791Sdim  /// The boolean indicates whether the preamble ends at the start of a new
82243791Sdim  /// line.
83243791Sdim  std::pair<unsigned, bool> PrecompiledPreambleBytes;
84243791Sdim
85243791Sdim  /// The implicit PTH input included at the start of the translation unit, or
86243791Sdim  /// empty.
87243791Sdim  std::string ImplicitPTHInclude;
88243791Sdim
89243791Sdim  /// If given, a PTH cache file to use for speeding up header parsing.
90243791Sdim  std::string TokenCache;
91243791Sdim
92243791Sdim  /// \brief True if the SourceManager should report the original file name for
93243791Sdim  /// contents of files that were remapped to other files. Defaults to true.
94243791Sdim  bool RemappedFilesKeepOriginalName;
95243791Sdim
96243791Sdim  /// \brief The set of file remappings, which take existing files on
97243791Sdim  /// the system (the first part of each pair) and gives them the
98243791Sdim  /// contents of other files on the system (the second part of each
99243791Sdim  /// pair).
100243791Sdim  std::vector<std::pair<std::string, std::string> >  RemappedFiles;
101243791Sdim
102243791Sdim  /// \brief The set of file-to-buffer remappings, which take existing files
103243791Sdim  /// on the system (the first part of each pair) and gives them the contents
104243791Sdim  /// of the specified memory buffer (the second part of each pair).
105243791Sdim  std::vector<std::pair<std::string, const llvm::MemoryBuffer *> >
106243791Sdim    RemappedFileBuffers;
107243791Sdim
108243791Sdim  /// \brief Whether the compiler instance should retain (i.e., not free)
109243791Sdim  /// the buffers associated with remapped files.
110243791Sdim  ///
111243791Sdim  /// This flag defaults to false; it can be set true only through direct
112243791Sdim  /// manipulation of the compiler invocation object, in cases where the
113243791Sdim  /// compiler invocation and its buffers will be reused.
114243791Sdim  bool RetainRemappedFileBuffers;
115243791Sdim
116243791Sdim  /// \brief The Objective-C++ ARC standard library that we should support,
117243791Sdim  /// by providing appropriate definitions to retrofit the standard library
118243791Sdim  /// with support for lifetime-qualified pointers.
119243791Sdim  ObjCXXARCStandardLibraryKind ObjCXXARCStandardLibrary;
120243791Sdim
121249423Sdim  /// \brief Records the set of modules
122249423Sdim  class FailedModulesSet : public RefCountedBase<FailedModulesSet> {
123249423Sdim    llvm::StringSet<> Failed;
124249423Sdim
125249423Sdim  public:
126249423Sdim    bool hasAlreadyFailed(StringRef module) {
127249423Sdim      return Failed.count(module) > 0;
128249423Sdim    }
129249423Sdim
130249423Sdim    void addFailed(StringRef module) {
131249423Sdim      Failed.insert(module);
132249423Sdim    }
133249423Sdim  };
134249423Sdim
135249423Sdim  /// \brief The set of modules that failed to build.
136243791Sdim  ///
137249423Sdim  /// This pointer will be shared among all of the compiler instances created
138249423Sdim  /// to (re)build modules, so that once a module fails to build anywhere,
139249423Sdim  /// other instances will see that the module has failed and won't try to
140249423Sdim  /// build it again.
141249423Sdim  IntrusiveRefCntPtr<FailedModulesSet> FailedModules;
142249423Sdim
143243791Sdim  typedef std::vector<std::pair<std::string, std::string> >::iterator
144243791Sdim    remapped_file_iterator;
145243791Sdim  typedef std::vector<std::pair<std::string, std::string> >::const_iterator
146243791Sdim    const_remapped_file_iterator;
147243791Sdim  remapped_file_iterator remapped_file_begin() {
148243791Sdim    return RemappedFiles.begin();
149243791Sdim  }
150243791Sdim  const_remapped_file_iterator remapped_file_begin() const {
151243791Sdim    return RemappedFiles.begin();
152243791Sdim  }
153243791Sdim  remapped_file_iterator remapped_file_end() {
154243791Sdim    return RemappedFiles.end();
155243791Sdim  }
156243791Sdim  const_remapped_file_iterator remapped_file_end() const {
157243791Sdim    return RemappedFiles.end();
158243791Sdim  }
159243791Sdim
160243791Sdim  typedef std::vector<std::pair<std::string, const llvm::MemoryBuffer *> >::
161243791Sdim                                  iterator remapped_file_buffer_iterator;
162243791Sdim  typedef std::vector<std::pair<std::string, const llvm::MemoryBuffer *> >::
163243791Sdim                            const_iterator const_remapped_file_buffer_iterator;
164243791Sdim  remapped_file_buffer_iterator remapped_file_buffer_begin() {
165243791Sdim    return RemappedFileBuffers.begin();
166243791Sdim  }
167243791Sdim  const_remapped_file_buffer_iterator remapped_file_buffer_begin() const {
168243791Sdim    return RemappedFileBuffers.begin();
169243791Sdim  }
170243791Sdim  remapped_file_buffer_iterator remapped_file_buffer_end() {
171243791Sdim    return RemappedFileBuffers.end();
172243791Sdim  }
173243791Sdim  const_remapped_file_buffer_iterator remapped_file_buffer_end() const {
174243791Sdim    return RemappedFileBuffers.end();
175243791Sdim  }
176243791Sdim
177243791Sdimpublic:
178243791Sdim  PreprocessorOptions() : UsePredefines(true), DetailedRecord(false),
179243791Sdim                          DisablePCHValidation(false),
180243791Sdim                          AllowPCHWithCompilerErrors(false),
181243791Sdim                          DumpDeserializedPCHDecls(false),
182243791Sdim                          PrecompiledPreambleBytes(0, true),
183243791Sdim                          RemappedFilesKeepOriginalName(true),
184243791Sdim                          RetainRemappedFileBuffers(false),
185243791Sdim                          ObjCXXARCStandardLibrary(ARCXX_nolib) { }
186243791Sdim
187243791Sdim  void addMacroDef(StringRef Name) {
188243791Sdim    Macros.push_back(std::make_pair(Name, false));
189243791Sdim  }
190243791Sdim  void addMacroUndef(StringRef Name) {
191243791Sdim    Macros.push_back(std::make_pair(Name, true));
192243791Sdim  }
193243791Sdim  void addRemappedFile(StringRef From, StringRef To) {
194243791Sdim    RemappedFiles.push_back(std::make_pair(From, To));
195243791Sdim  }
196243791Sdim
197243791Sdim  remapped_file_iterator eraseRemappedFile(remapped_file_iterator Remapped) {
198243791Sdim    return RemappedFiles.erase(Remapped);
199243791Sdim  }
200243791Sdim
201243791Sdim  void addRemappedFile(StringRef From, const llvm::MemoryBuffer * To) {
202243791Sdim    RemappedFileBuffers.push_back(std::make_pair(From, To));
203243791Sdim  }
204243791Sdim
205243791Sdim  remapped_file_buffer_iterator
206243791Sdim  eraseRemappedFile(remapped_file_buffer_iterator Remapped) {
207243791Sdim    return RemappedFileBuffers.erase(Remapped);
208243791Sdim  }
209243791Sdim
210243791Sdim  void clearRemappedFiles() {
211243791Sdim    RemappedFiles.clear();
212243791Sdim    RemappedFileBuffers.clear();
213243791Sdim  }
214243791Sdim
215243791Sdim  /// \brief Reset any options that are not considered when building a
216243791Sdim  /// module.
217243791Sdim  void resetNonModularOptions() {
218243791Sdim    Includes.clear();
219243791Sdim    MacroIncludes.clear();
220243791Sdim    ChainedIncludes.clear();
221243791Sdim    DumpDeserializedPCHDecls = false;
222243791Sdim    ImplicitPCHInclude.clear();
223243791Sdim    ImplicitPTHInclude.clear();
224243791Sdim    TokenCache.clear();
225243791Sdim    RetainRemappedFileBuffers = true;
226243791Sdim    PrecompiledPreambleBytes.first = 0;
227243791Sdim    PrecompiledPreambleBytes.second = 0;
228243791Sdim  }
229243791Sdim};
230243791Sdim
231243791Sdim} // end namespace clang
232243791Sdim
233243791Sdim#endif
234