ASTUnit.h revision b0f4b9a558933b307073f7cd7753602f94354ae9
14b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//===--- ASTUnit.h - ASTUnit utility ----------------------------*- C++ -*-===//
24b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//
34b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//                     The LLVM Compiler Infrastructure
44b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//
54b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis// This file is distributed under the University of Illinois Open Source
64b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis// License. See LICENSE.TXT for details.
74b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//
84b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
94b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//
104b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis// ASTUnit utility class.
114b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//
124b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
134b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis
140853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#ifndef LLVM_CLANG_FRONTEND_ASTUNIT_H
150853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#define LLVM_CLANG_FRONTEND_ASTUNIT_H
160853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
17eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor#include "clang/Index/ASTLocation.h"
1805a07605322dfef2b017781042043a261c5a89cdSebastian Redl#include "clang/Serialization/ASTBitCodes.h"
19914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor#include "clang/Sema/Sema.h"
200a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall#include "clang/Sema/CodeCompleteConsumer.h"
214ae8f298b1ea51b4c2234f9148e2e4349c9bdd23Douglas Gregor#include "clang/Lex/PreprocessingRecord.h"
2231b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar#include "clang/Basic/SourceManager.h"
23eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor#include "clang/Basic/FileManager.h"
24389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis#include "clang/Basic/FileSystemOptions.h"
2587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor#include "clang-c/Index.h"
2628019772db70d4547be05a042eb950bc910f134fDouglas Gregor#include "llvm/ADT/IntrusiveRefCntPtr.h"
270853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#include "llvm/ADT/OwningPtr.h"
28a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor#include "llvm/ADT/SmallVector.h"
29cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor#include "llvm/ADT/StringMap.h"
3003013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer#include "llvm/Support/Path.h"
31788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor#include <map>
320853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#include <string>
33f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar#include <vector>
34f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar#include <cassert>
354db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor#include <utility>
36cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor#include <sys/types.h>
374db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor
384db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregornamespace llvm {
394db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  class MemoryBuffer;
404db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor}
410853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
420853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidisnamespace clang {
43521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass ASTContext;
441abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregorclass CodeCompleteConsumer;
45521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass CompilerInvocation;
46521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Decl;
47521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Diagnostic;
48521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass FileEntry;
49521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass FileManager;
50521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass HeaderSearch;
51521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Preprocessor;
52521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass SourceManager;
53521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass TargetInfo;
540853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
55f96b524306ccfa623235d375deee79637bd38f29Steve Naroffusing namespace idx;
5644c181aec37789f25f6c15543c164416f72e562aDouglas Gregor
5748601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor/// \brief Allocator for a cached set of global code completions.
5848601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregorclass GlobalCodeCompletionAllocator
5948601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  : public CodeCompletionAllocator,
6048601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor    public llvm::RefCountedBase<GlobalCodeCompletionAllocator>
6148601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor{
6248601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor
6348601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor};
6448601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor
653c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl/// \brief Utility class for loading a ASTContext from an AST file.
660853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis///
670853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidisclass ASTUnit {
68788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregorpublic:
69788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  typedef std::map<FileID, std::vector<PreprocessedEntity *> >
70788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor    PreprocessedEntitiesByFileMap;
71bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor
72788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregorprivate:
7328019772db70d4547be05a042eb950bc910f134fDouglas Gregor  llvm::IntrusiveRefCntPtr<Diagnostic> Diagnostics;
74405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  llvm::OwningPtr<FileManager>      FileMgr;
75405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  llvm::OwningPtr<SourceManager>    SourceMgr;
760853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  llvm::OwningPtr<HeaderSearch>     HeaderInfo;
770853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  llvm::OwningPtr<TargetInfo>       Target;
780853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  llvm::OwningPtr<Preprocessor>     PP;
790853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  llvm::OwningPtr<ASTContext>       Ctx;
80389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
81389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  FileSystemOptions FileSystemOpts;
82389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
83914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// \brief The AST consumer that received information about the translation
84914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// unit as it was parsed or loaded.
85914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  llvm::OwningPtr<ASTConsumer> Consumer;
86914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
87914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// \brief The semantic analysis object used to type-check the translation
88914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// unit.
89914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  llvm::OwningPtr<Sema> TheSema;
90914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
91807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// Optional owned invocation, just used to make the invocation used in
92807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// LoadFromCommandLine available.
93807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  llvm::OwningPtr<CompilerInvocation> Invocation;
94914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
951aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor  /// \brief The set of target features.
961aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor  ///
971aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor  /// FIXME: each time we reparse, we need to restore the set of target
981aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor  /// features from this vector, because TargetInfo::CreateTargetInfo()
991aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor  /// mangles the target options in place. Yuck!
1001aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor  std::vector<std::string> TargetFeatures;
1011aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor
1027d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // OnlyLocalDecls - when true, walking this AST should only visit declarations
1037d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // that come from the AST itself, not from included precompiled headers.
1047d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // FIXME: This is temporary; eventually, CIndex will always do this.
1057d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool                              OnlyLocalDecls;
106c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
107abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \brief Whether to capture any diagnostics produced.
108abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  bool CaptureDiagnostics;
109e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor
110df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  /// \brief Track whether the main file was loaded from an AST or not.
111c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool MainFileIsAST;
112c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
113df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  /// \brief Whether this AST represents a complete translation unit.
114df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  bool CompleteTranslationUnit;
115df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
116213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor  /// \brief Whether we should time each operation.
117213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor  bool WantTiming;
11815727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis
11915727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  /// \brief Whether the ASTUnit should delete the remapped buffers.
12015727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  bool OwnsRemappedFileBuffers;
121213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor
122f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// Track the top-level decls which appeared in an ASTUnit which was loaded
123f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// from a source file.
124f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  //
125f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // FIXME: This is just an optimization hack to avoid deserializing large parts
126f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // of a PCH file when using the Index library on an ASTUnit loaded from
127f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // source. In the long term we should make the Index library use efficient and
128f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // more scalable search mechanisms.
129f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  std::vector<Decl*> TopLevelDecls;
130f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
13189d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  /// \brief The list of preprocessed entities which appeared when the ASTUnit
13289d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  /// was loaded.
13389d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  ///
13489d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  /// FIXME: This is just an optimization hack to avoid deserializing large
13589d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  /// parts of a PCH file while performing a walk or search. In the long term,
13689d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  /// we should provide more scalable search mechanisms.
13789d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  std::vector<PreprocessedEntity *> PreprocessedEntities;
13889d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
13968d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  /// The name of the original source file used to generate this ASTUnit.
14068d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  std::string OriginalSourceFile;
14168d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar
142f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  // Critical optimization when using clang_getCursor().
143f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  ASTLocation LastLoc;
144f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
145a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  /// \brief The set of diagnostics produced when creating this
146a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  /// translation unit.
147405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  llvm::SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
148a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
1494cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \brief The number of stored diagnostics that come from the driver
1504cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// itself.
1514cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  ///
1524cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// Diagnostics that come from the driver are retained from one parse to
1534cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// the next.
1544cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  unsigned NumStoredDiagnosticsFromDriver;
1554cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor
156313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// \brief Temporary files that should be removed when the ASTUnit is
157313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// destroyed.
158313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  llvm::SmallVector<llvm::sys::Path, 4> TemporaryFiles;
159bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
160788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// \brief A mapping from file IDs to the set of preprocessed entities
161788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// stored in that file.
162788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  ///
163788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// FIXME: This is just an optimization hack to avoid searching through
164788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// many preprocessed entities during cursor traversal in the CIndex library.
165788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// Ideally, we would just be able to perform a binary search within the
166788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// list of preprocessed entities.
167788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  PreprocessedEntitiesByFileMap PreprocessedEntitiesByFile;
168788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor
169bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// \brief Simple hack to allow us to assert that ASTUnit is not being
170bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// used concurrently, which is not supported.
171bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  ///
172bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// Clients should create instances of the ConcurrencyCheck class whenever
173bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// using the ASTUnit in a way that isn't intended to be concurrent, which is
174bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// just about any usage.
175bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  unsigned int ConcurrencyCheckValue;
176bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  static const unsigned int CheckLocked = 28573289;
177bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  static const unsigned int CheckUnlocked = 9803453;
178175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
179eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// \brief Counter that determines when we want to try building a
180eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// precompiled preamble.
181eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  ///
182eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// If zero, we will never build a precompiled preamble. Otherwise,
183eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// it's treated as a counter that decrements each time we reparse
184eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// without the benefit of a precompiled preamble. When it hits 1,
185eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// we'll attempt to rebuild the precompiled header. This way, if
186eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// building the precompiled preamble fails, we won't try again for
187eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// some number of calls.
188eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  unsigned PreambleRebuildCounter;
189eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor
190175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \brief The file in which the precompiled preamble is stored.
191385103b79c5338a2be5da0ca70652400bc267371Douglas Gregor  std::string PreambleFile;
192175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
193175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \brief The contents of the preamble that has been precompiled to
194175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \c PreambleFile.
195175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  std::vector<char> Preamble;
196175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
197f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// \brief Whether the preamble ends at the start of a new line.
198f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  ///
199f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// Used to inform the lexer as to whether it's starting at the beginning of
200f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// a line after skipping the preamble.
201f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  bool PreambleEndsAtStartOfLine;
202f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor
203175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \brief The size of the source buffer that we've reserved for the main
204175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// file within the precompiled preamble.
205175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  unsigned PreambleReservedSize;
206cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
207cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// \brief Keeps track of the files that were used when computing the
208cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// preamble, with both their buffer size and their modification time.
209cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  ///
210cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// If any of the files have changed from one compile to the next,
211cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// the preamble must be thrown away.
212cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  llvm::StringMap<std::pair<off_t, time_t> > FilesInPreamble;
213cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
21428233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// \brief When non-NULL, this is the buffer used to store the contents of
21528233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// the main file when it has been padded for use with the precompiled
21628233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// preamble.
21728233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  llvm::MemoryBuffer *SavedMainFileBuffer;
218c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
219671947b18dba342f9aba022ee992babef325a833Douglas Gregor  /// \brief When non-NULL, this is the buffer used to store the
220671947b18dba342f9aba022ee992babef325a833Douglas Gregor  /// contents of the preamble when it has been padded to build the
221671947b18dba342f9aba022ee992babef325a833Douglas Gregor  /// precompiled preamble.
222671947b18dba342f9aba022ee992babef325a833Douglas Gregor  llvm::MemoryBuffer *PreambleBuffer;
223671947b18dba342f9aba022ee992babef325a833Douglas Gregor
224c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// \brief The number of warnings that occurred while parsing the preamble.
225c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  ///
226c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// This value will be used to restore the state of the \c Diagnostic object
227c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// when re-using the precompiled preamble. Note that only the
228c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// number of warnings matters, since we will not save the preamble
229c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// when any errors are present.
230c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  unsigned NumWarningsInPreamble;
231c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
232c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// \brief The number of diagnostics that were stored when parsing
233c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// the precompiled preamble.
234c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  ///
235c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// This value is used to determine how many of the stored
236c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// diagnostics should be retained when reparsing in the presence of
237c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// a precompiled preamble.
238c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  unsigned NumStoredDiagnosticsInPreamble;
239c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
2403c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \brief A list of the serialization ID numbers for each of the top-level
241eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// declarations parsed within the precompiled preamble.
2428538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
243eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
24489d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  /// \brief A list of the offsets into the precompiled preamble which
24589d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  /// correspond to preprocessed entities.
24689d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  std::vector<uint64_t> PreprocessedEntitiesInPreamble;
24789d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
24887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief Whether we should be caching code-completion results.
24987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  bool ShouldCacheCodeCompletionResults;
25087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
251e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor  static void ConfigureDiags(llvm::IntrusiveRefCntPtr<Diagnostic> &Diags,
2520b53cf834346d78985aaa9e7300445a39c245614Douglas Gregor                             const char **ArgBegin, const char **ArgEnd,
253e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor                             ASTUnit &AST, bool CaptureDiagnostics);
254e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor
25587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregorpublic:
25687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief A cached code-completion result, which may be introduced in one of
25787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// many different contexts.
25887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  struct CachedCodeCompletionResult {
25987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief The code-completion string corresponding to this completion
26087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// result.
26187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    CodeCompletionString *Completion;
26287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
26387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief A bitmask that indicates which code-completion contexts should
26487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// contain this completion result.
26587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    ///
26687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// The bits in the bitmask correspond to the values of
26787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// CodeCompleteContext::Kind. To map from a completion context kind to a
26887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// bit, subtract one from the completion context kind and shift 1 by that
26987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// number of bits. Many completions can occur in several different
27087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// contexts.
27187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    unsigned ShowInContexts;
27287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
27387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief The priority given to this code-completion result.
27487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    unsigned Priority;
27587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
27687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief The libclang cursor kind corresponding to this code-completion
27787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// result.
27887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    CXCursorKind Kind;
2791827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor
28058ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor    /// \brief The availability of this code-completion result.
28158ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor    CXAvailabilityKind Availability;
28258ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor
2831827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor    /// \brief The simplified type class for a non-macro completion result.
2841827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor    SimplifiedTypeClass TypeClass;
285f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
286f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// \brief The type of a non-macro completion result, stored as a unique
287f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// integer used by the string map of cached completion types.
288f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    ///
289f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// This value will be zero if the type is not known, or a unique value
290f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// determined by the formatted type string. Se \c CachedCompletionTypes
291f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// for more information.
292f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    unsigned Type;
29387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  };
29487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
295f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// \brief Retrieve the mapping from formatted type names to unique type
296f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// identifiers.
297f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  llvm::StringMap<unsigned> &getCachedCompletionTypes() {
298f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    return CachedCompletionTypes;
299f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  }
300f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
30148601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  /// \brief Retrieve the allocator used to cache global code completions.
30248601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  llvm::IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
30348601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  getCachedCompletionAllocator() {
30448601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor    return CachedCompletionAllocator;
30548601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  }
30648601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor
30787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregorprivate:
308218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  /// \brief Allocator used to store cached code completions.
30948601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  llvm::IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
31048601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor    CachedCompletionAllocator;
311218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor
31287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief The set of cached code-completion results.
31387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  std::vector<CachedCodeCompletionResult> CachedCompletionResults;
31487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
315f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// \brief A mapping from the formatted type name to a unique number for that
316f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// type, which is used for type equality comparisons.
317f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  llvm::StringMap<unsigned> CachedCompletionTypes;
318f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
3199b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief A string hash of the top-level declaration and macro definition
3209b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// names processed the last time that we reparsed the file.
321727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  ///
3229b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// This hash value is used to determine when we need to refresh the
3239b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// global code-completion cache.
3249b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned CompletionCacheTopLevelHashValue;
325727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor
3269b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief A string hash of the top-level declaration and macro definition
3279b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// names processed the last time that we reparsed the precompiled preamble.
328727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  ///
3299b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// This hash value is used to determine when we need to refresh the
3309b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// global code-completion cache after a rebuild of the precompiled preamble.
3319b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned PreambleTopLevelHashValue;
332ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
3339b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief The current hash value for the top-level declaration and macro
3349b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// definition names
3359b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned CurrentTopLevelHashValue;
3369b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor
337ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  /// \brief Bit used by CIndex to mark when a translation unit may be in an
338ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  /// inconsistent state, and is not safe to free.
339ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  unsigned UnsafeToFree : 1;
340ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
341c4421e966d77a18f815284175b3fcbb46f36fa39Douglas Gregor  /// \brief Cache any "global" code-completion results, so that we can avoid
342c4421e966d77a18f815284175b3fcbb46f36fa39Douglas Gregor  /// recomputing them with each completion.
34387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  void CacheCodeCompletionResults();
34487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
34587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief Clear out and deallocate
34687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  void ClearCachedCompletionResults();
34787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
34831b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
34931b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
350bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
3513687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor  explicit ASTUnit(bool MainFileIsAST);
3528b96253907c47141af0b7b2a44a368748d006a87Douglas Gregor
353abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  void CleanTemporaryFiles();
354754f3490c5b0f5d83361f001bc87944f23644abbDouglas Gregor  bool Parse(llvm::MemoryBuffer *OverrideMainBuffer);
355175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
356f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
357df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  ComputePreamble(CompilerInvocation &Invocation,
358df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                  unsigned MaxLines, bool &CreatedBuffer);
359175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
360df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  llvm::MemoryBuffer *getMainBufferWithPrecompiledPreamble(
3612283d79155a3e82442fce124ce5fd704ca138801Douglas Gregor                                         CompilerInvocation PreambleInvocation,
362df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                                     bool AllowRebuild = true,
363df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                                        unsigned MaxLines = 0);
364eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  void RealizeTopLevelDeclsFromPreamble();
36589d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  void RealizePreprocessedEntitiesFromPreamble();
36689d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
3670853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidispublic:
368bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  class ConcurrencyCheck {
369bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    volatile ASTUnit &Self;
370bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
371bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  public:
372bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    explicit ConcurrencyCheck(ASTUnit &Self)
373bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      : Self(Self)
374bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    {
375bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      assert(Self.ConcurrencyCheckValue == CheckUnlocked &&
376bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor             "Concurrent access to ASTUnit!");
377bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      Self.ConcurrencyCheckValue = CheckLocked;
378bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    }
379bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
380bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    ~ConcurrencyCheck() {
381bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      Self.ConcurrencyCheckValue = CheckUnlocked;
382bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    }
383bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  };
384bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  friend class ConcurrencyCheck;
385bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
3860853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ~ASTUnit();
3870853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
388c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool isMainFileAST() const { return MainFileIsAST; }
389c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
390ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  bool isUnsafeToFree() const { return UnsafeToFree; }
391ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  void setUnsafeToFree(bool Value) { UnsafeToFree = Value; }
392ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
3933687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor  const Diagnostic &getDiagnostics() const { return *Diagnostics; }
3943687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor  Diagnostic &getDiagnostics()             { return *Diagnostics; }
3953687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor
396405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  const SourceManager &getSourceManager() const { return *SourceMgr; }
397405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor        SourceManager &getSourceManager()       { return *SourceMgr; }
3980853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
3990853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  const Preprocessor &getPreprocessor() const { return *PP.get(); }
4000853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis        Preprocessor &getPreprocessor()       { return *PP.get(); }
4011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4020853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  const ASTContext &getASTContext() const { return *Ctx.get(); }
4030853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis        ASTContext &getASTContext()       { return *Ctx.get(); }
4040853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
405914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  bool hasSema() const { return TheSema; }
406914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  Sema &getSema() const {
407914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor    assert(TheSema && "ASTUnit does not have a Sema object!");
408914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor    return *TheSema;
409914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  }
410914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
411405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  const FileManager &getFileManager() const { return *FileMgr; }
412405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor        FileManager &getFileManager()       { return *FileMgr; }
413f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
414389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
415389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
41677accc11f04ed4ff9afd4e27d430144d4714be56Steve Naroff  const std::string &getOriginalSourceFileName();
4173c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  const std::string &getASTFileName();
418b85bca2676b433ae555db09de4dd2823ff13b856Zhongxing Xu
419313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// \brief Add a temporary file that the ASTUnit depends on.
420313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  ///
421313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// This file will be erased when the ASTUnit is destroyed.
422313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  void addTemporaryFile(const llvm::sys::Path &TempFile) {
423313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor    TemporaryFiles.push_back(TempFile);
424313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  }
425313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor
4267d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
427f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
42815727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  bool getOwnsRemappedFileBuffers() const { return OwnsRemappedFileBuffers; }
42915727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  void setOwnsRemappedFileBuffers(bool val) { OwnsRemappedFileBuffers = val; }
43015727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis
431eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// \brief Retrieve the maximum PCH level of declarations that a
432eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// traversal of the translation unit should consider.
433eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  unsigned getMaxPCHLevel() const;
434eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
435f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; }
436f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  ASTLocation getLastASTLocation() const { return LastLoc; }
437f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
438213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor
439213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor  llvm::StringRef getMainFileName() const;
440213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor
441eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  typedef std::vector<Decl *>::iterator top_level_iterator;
442eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
443eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  top_level_iterator top_level_begin() {
444f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
445eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    if (!TopLevelDeclsInPreamble.empty())
446eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor      RealizeTopLevelDeclsFromPreamble();
447eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDecls.begin();
448f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  }
449eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
450eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  top_level_iterator top_level_end() {
451f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
452eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    if (!TopLevelDeclsInPreamble.empty())
453eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor      RealizeTopLevelDeclsFromPreamble();
454eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDecls.end();
455eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
456eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
457eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  std::size_t top_level_size() const {
458eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
459eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDeclsInPreamble.size() + TopLevelDecls.size();
460eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
461eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
462eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  bool top_level_empty() const {
463eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
464eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty();
465eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
466eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
467eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// \brief Add a new top-level declaration.
468eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  void addTopLevelDecl(Decl *D) {
469eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    TopLevelDecls.push_back(D);
470eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
471eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
472eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// \brief Add a new top-level declaration, identified by its ID in
473eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// the precompiled preamble.
4748538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  void addTopLevelDeclFromPreamble(serialization::DeclID D) {
475eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    TopLevelDeclsInPreamble.push_back(D);
476f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  }
477f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
4789b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief Retrieve a reference to the current top-level name hash value.
4799b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  ///
4809b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// Note: This is used internally by the top-level tracking action
4819b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned &getCurrentTopLevelHashValue() { return CurrentTopLevelHashValue; }
4829b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor
48389d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  typedef std::vector<PreprocessedEntity *>::iterator pp_entity_iterator;
48489d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
48589d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  pp_entity_iterator pp_entity_begin();
48689d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  pp_entity_iterator pp_entity_end();
48789d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
48889d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  /// \brief Add a new preprocessed entity that's stored at the given offset
48989d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  /// in the precompiled preamble.
49089d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  void addPreprocessedEntityFromPreamble(uint64_t Offset) {
49189d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor    PreprocessedEntitiesInPreamble.push_back(Offset);
49289d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  }
49389d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
494788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// \brief Retrieve the mapping from File IDs to the preprocessed entities
495788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// within that file.
496788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  PreprocessedEntitiesByFileMap &getPreprocessedEntitiesByFile() {
497788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor    return PreprocessedEntitiesByFile;
498788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  }
499788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor
500a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  // Retrieve the diagnostics associated with this AST
501405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  typedef const StoredDiagnostic *stored_diag_iterator;
502405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  stored_diag_iterator stored_diag_begin() const {
503405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics.begin();
504405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  }
505405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  stored_diag_iterator stored_diag_end() const {
506405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics.end();
507405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  }
508405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  unsigned stored_diag_size() const { return StoredDiagnostics.size(); }
509405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor
510405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  llvm::SmallVector<StoredDiagnostic, 4> &getStoredDiagnostics() {
511405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics;
512a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  }
513a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
51487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  typedef std::vector<CachedCodeCompletionResult>::iterator
51587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    cached_completion_iterator;
51687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
51787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  cached_completion_iterator cached_completion_begin() {
51887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    return CachedCompletionResults.begin();
51987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
52087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
52187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  cached_completion_iterator cached_completion_end() {
52287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    return CachedCompletionResults.end();
52387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
52487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
52587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  unsigned cached_completion_size() const {
52687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    return CachedCompletionResults.size();
52787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
528389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
529389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  llvm::MemoryBuffer *getBufferForFile(llvm::StringRef Filename,
53075dfb65c38d51772df9a00ce2d2feeefd55667adChris Lattner                                       std::string *ErrorStr = 0);
531389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
532df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  /// \brief Whether this AST represents a complete translation unit.
533df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  ///
534df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  /// If false, this AST is only a partial translation unit, e.g., one
535df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  /// that might still be used as a precompiled header or preamble.
536df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  bool isCompleteTranslationUnit() const { return CompleteTranslationUnit; }
537df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
538b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  typedef llvm::PointerUnion<const char *, const llvm::MemoryBuffer *>
539b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis      FilenameOrMemBuf;
5404db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  /// \brief A mapping from a file name to the memory buffer that stores the
5414db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  /// remapped contents of that file.
542b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  typedef std::pair<std::string, FilenameOrMemBuf> RemappedFile;
543b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis
544b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  /// \brief Create a ASTUnit. Gets ownership of the passed CompilerInvocation.
545b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  static ASTUnit *create(CompilerInvocation *CI,
546b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis                         llvm::IntrusiveRefCntPtr<Diagnostic> Diags);
547b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis
5483c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \brief Create a ASTUnit from an AST file.
5490853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
5503c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \param Filename - The AST file to load.
5510853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
5525262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
5535262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
55431b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ///
5553c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \returns - The initialized ASTUnit or null if the AST failed to load.
5563c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  static ASTUnit *LoadFromASTFile(const std::string &Filename,
55728019772db70d4547be05a042eb950bc910f134fDouglas Gregor                                  llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
558389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis                                  const FileSystemOptions &FileSystemOpts,
5595cf48766d626ff6b223acc9d4b7e415ca8480836Ted Kremenek                                  bool OnlyLocalDecls = false,
5604db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor                                  RemappedFile *RemappedFiles = 0,
561a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                  unsigned NumRemappedFiles = 0,
562a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                  bool CaptureDiagnostics = false);
563521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
5644cd912aa94656697a44c3ebb159f05060300524eDouglas Gregorprivate:
5654cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \brief Helper function for \c LoadFromCompilerInvocation() and
5664cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \c LoadFromCommandLine(), which loads an AST from a compiler invocation.
5674cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  ///
5684cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \param PrecompilePreamble Whether to precompile the preamble of this
5694cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// translation unit, to improve the performance of reparsing.
5704cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  ///
5714cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \returns \c true if a catastrophic failure occurred (which means that the
5724cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \c ASTUnit itself is invalid), or \c false otherwise.
5734cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  bool LoadFromCompilerInvocation(bool PrecompilePreamble);
5744cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor
5754cd912aa94656697a44c3ebb159f05060300524eDouglas Gregorpublic:
5764cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor
577521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
578521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// CompilerInvocation object.
579521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
580521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// \param CI - The compiler invocation to use; it must have exactly one input
581f7acc37450d59ef751df73acb91de73850cc6517Daniel Dunbar  /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
582521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
5835262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
5845262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
585521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  //
586521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
587521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // shouldn't need to specify them at construction time.
588f7acc37450d59ef751df73acb91de73850cc6517Daniel Dunbar  static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI,
58928019772db70d4547be05a042eb950bc910f134fDouglas Gregor                                     llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
590a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                             bool OnlyLocalDecls = false,
59144c181aec37789f25f6c15543c164416f72e562aDouglas Gregor                                             bool CaptureDiagnostics = false,
592df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                             bool PrecompilePreamble = false,
59387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor                                          bool CompleteTranslationUnit = true,
59487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor                                       bool CacheCodeCompletionResults = false);
595521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
5967b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
5977b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// arguments, which must specify exactly one source file.
5987b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
5997b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgBegin - The beginning of the argument vector.
6007b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
6017b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgEnd - The end of the argument vector.
6027b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
6035262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
6045262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
6057b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
606869824e87940f97b87064db2df2861e82e08a8c6Daniel Dunbar  /// \param ResourceFilesPath - The path to the compiler resource files.
6077b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  //
6087b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
6097b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // shouldn't need to specify them at construction time.
6107b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  static ASTUnit *LoadFromCommandLine(const char **ArgBegin,
6117b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      const char **ArgEnd,
61228019772db70d4547be05a042eb950bc910f134fDouglas Gregor                                    llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
613869824e87940f97b87064db2df2861e82e08a8c6Daniel Dunbar                                      llvm::StringRef ResourceFilesPath,
6147b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      bool OnlyLocalDecls = false,
615e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor                                      bool CaptureDiagnostics = false,
6164db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor                                      RemappedFile *RemappedFiles = 0,
617a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                      unsigned NumRemappedFiles = 0,
618299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis                                      bool RemappedFilesKeepOriginalName = true,
619df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                      bool PrecompilePreamble = false,
62087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor                                      bool CompleteTranslationUnit = true,
62199ba202f659e1885fa5ee114f97c97cf6a857491Douglas Gregor                                      bool CacheCodeCompletionResults = false,
62299ba202f659e1885fa5ee114f97c97cf6a857491Douglas Gregor                                      bool CXXPrecompilePreamble = false,
62399ba202f659e1885fa5ee114f97c97cf6a857491Douglas Gregor                                      bool CXXChainedPCH = false);
624abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor
625abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \brief Reparse the source files using the same command-line options that
626abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// were originally used to produce this translation unit.
627abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  ///
628abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \returns True if a failure occurred that causes the ASTUnit not to
629abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// contain any translation-unit information, false otherwise.
630abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  bool Reparse(RemappedFile *RemappedFiles = 0,
631abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor               unsigned NumRemappedFiles = 0);
6321abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor
6331abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  /// \brief Perform code completion at the given file, line, and
6341abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  /// column within this translation unit.
6351abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  ///
636cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param File The file in which code completion will occur.
637cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
638cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param Line The line at which code completion will occur.
639cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
640cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param Column The column at which code completion will occur.
641cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
642cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param IncludeMacros Whether to include macros in the code-completion
643cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// results.
644cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
645cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param IncludeCodePatterns Whether to include code patterns (such as a
646cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// for loop) in the code-completion results.
6471abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  ///
6482283d79155a3e82442fce124ce5fd704ca138801Douglas Gregor  /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, StoredDiagnostics, and
6492283d79155a3e82442fce124ce5fd704ca138801Douglas Gregor  /// OwnedBuffers parameters are all disgusting hacks. They will go away.
6501abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  void CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column,
6511abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    RemappedFile *RemappedFiles, unsigned NumRemappedFiles,
652cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor                    bool IncludeMacros, bool IncludeCodePatterns,
6531abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    CodeCompleteConsumer &Consumer,
6541abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    Diagnostic &Diag, LangOptions &LangOpts,
6551abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    SourceManager &SourceMgr, FileManager &FileMgr,
6562283d79155a3e82442fce124ce5fd704ca138801Douglas Gregor                    llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
6572283d79155a3e82442fce124ce5fd704ca138801Douglas Gregor              llvm::SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers);
6587ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor
6597ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor  /// \brief Save this translation unit to a file with the given name.
6607ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor  ///
6617ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor  /// \returns True if an error occurred, false otherwise.
6627ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor  bool Save(llvm::StringRef File);
663b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis
664b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  /// \brief Serialize this translation unit with the given output stream.
665b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  ///
666b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  /// \returns True if an error occurred, false otherwise.
667b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  bool serialize(llvm::raw_ostream &OS);
6680853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis};
6690853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
6700853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis} // namespace clang
6710853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
6720853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#endif
673