ASTUnit.h revision 6aa52ec6b969faabf3764baf79d89810b8249a7e
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"
216aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor#include "clang/Lex/ModuleLoader.h"
224ae8f298b1ea51b4c2234f9148e2e4349c9bdd23Douglas Gregor#include "clang/Lex/PreprocessingRecord.h"
2331b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar#include "clang/Basic/SourceManager.h"
24eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor#include "clang/Basic/FileManager.h"
25389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis#include "clang/Basic/FileSystemOptions.h"
2687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor#include "clang-c/Index.h"
2728019772db70d4547be05a042eb950bc910f134fDouglas Gregor#include "llvm/ADT/IntrusiveRefCntPtr.h"
280853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#include "llvm/ADT/OwningPtr.h"
29a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor#include "llvm/ADT/SmallVector.h"
30cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor#include "llvm/ADT/StringMap.h"
3103013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer#include "llvm/Support/Path.h"
32788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor#include <map>
330853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#include <string>
34f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar#include <vector>
35f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar#include <cassert>
364db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor#include <utility>
37cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor#include <sys/types.h>
384db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor
394db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregornamespace llvm {
404db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  class MemoryBuffer;
414db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor}
420853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
430853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidisnamespace clang {
44521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass ASTContext;
45f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregorclass ASTReader;
461abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregorclass CodeCompleteConsumer;
47521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass CompilerInvocation;
48521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Decl;
49521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Diagnostic;
50521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass FileEntry;
51521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass FileManager;
52521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass HeaderSearch;
53521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Preprocessor;
54521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass SourceManager;
55521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass TargetInfo;
56d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidisclass ASTFrontendAction;
570853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
58f96b524306ccfa623235d375deee79637bd38f29Steve Naroffusing namespace idx;
5944c181aec37789f25f6c15543c164416f72e562aDouglas Gregor
6048601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor/// \brief Allocator for a cached set of global code completions.
6148601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregorclass GlobalCodeCompletionAllocator
6248601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  : public CodeCompletionAllocator,
6348601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor    public llvm::RefCountedBase<GlobalCodeCompletionAllocator>
6448601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor{
6548601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor
6648601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor};
6748601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor
683c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl/// \brief Utility class for loading a ASTContext from an AST file.
690853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis///
706aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregorclass ASTUnit : public ModuleLoader {
71788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregorpublic:
72788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  typedef std::map<FileID, std::vector<PreprocessedEntity *> >
73788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor    PreprocessedEntitiesByFileMap;
74bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor
75788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregorprivate:
7628019772db70d4547be05a042eb950bc910f134fDouglas Gregor  llvm::IntrusiveRefCntPtr<Diagnostic> Diagnostics;
774f32786ac45210143654390177105eb749b614e9Ted Kremenek  llvm::IntrusiveRefCntPtr<FileManager>      FileMgr;
784f32786ac45210143654390177105eb749b614e9Ted Kremenek  llvm::IntrusiveRefCntPtr<SourceManager>    SourceMgr;
790853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  llvm::OwningPtr<HeaderSearch>     HeaderInfo;
804f32786ac45210143654390177105eb749b614e9Ted Kremenek  llvm::IntrusiveRefCntPtr<TargetInfo>       Target;
814f32786ac45210143654390177105eb749b614e9Ted Kremenek  llvm::IntrusiveRefCntPtr<Preprocessor>     PP;
824f32786ac45210143654390177105eb749b614e9Ted Kremenek  llvm::IntrusiveRefCntPtr<ASTContext>       Ctx;
83389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
84389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  FileSystemOptions FileSystemOpts;
85389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
86914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// \brief The AST consumer that received information about the translation
87914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// unit as it was parsed or loaded.
88914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  llvm::OwningPtr<ASTConsumer> Consumer;
89914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
90914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// \brief The semantic analysis object used to type-check the translation
91914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// unit.
92914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  llvm::OwningPtr<Sema> TheSema;
93914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
94807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// Optional owned invocation, just used to make the invocation used in
95807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// LoadFromCommandLine available.
964f32786ac45210143654390177105eb749b614e9Ted Kremenek  llvm::IntrusiveRefCntPtr<CompilerInvocation> Invocation;
97914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
981aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor  /// \brief The set of target features.
991aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor  ///
1001aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor  /// FIXME: each time we reparse, we need to restore the set of target
1011aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor  /// features from this vector, because TargetInfo::CreateTargetInfo()
1021aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor  /// mangles the target options in place. Yuck!
1031aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor  std::vector<std::string> TargetFeatures;
1041aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor
1057d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // OnlyLocalDecls - when true, walking this AST should only visit declarations
1067d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // that come from the AST itself, not from included precompiled headers.
1077d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // FIXME: This is temporary; eventually, CIndex will always do this.
1087d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool                              OnlyLocalDecls;
109c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
110abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \brief Whether to capture any diagnostics produced.
111abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  bool CaptureDiagnostics;
112e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor
113df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  /// \brief Track whether the main file was loaded from an AST or not.
114c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool MainFileIsAST;
115c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
116467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  /// \brief What kind of translation unit this AST represents.
117467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  TranslationUnitKind TUKind;
118df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
119213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor  /// \brief Whether we should time each operation.
120213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor  bool WantTiming;
12115727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis
12215727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  /// \brief Whether the ASTUnit should delete the remapped buffers.
12315727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  bool OwnsRemappedFileBuffers;
124213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor
125f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// Track the top-level decls which appeared in an ASTUnit which was loaded
126f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// from a source file.
127f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  //
128f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // FIXME: This is just an optimization hack to avoid deserializing large parts
129f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // of a PCH file when using the Index library on an ASTUnit loaded from
130f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // source. In the long term we should make the Index library use efficient and
131f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // more scalable search mechanisms.
132f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  std::vector<Decl*> TopLevelDecls;
133f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
13489d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  /// \brief The list of preprocessed entities which appeared when the ASTUnit
13589d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  /// was loaded.
13689d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  ///
13789d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  /// FIXME: This is just an optimization hack to avoid deserializing large
13889d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  /// parts of a PCH file while performing a walk or search. In the long term,
13989d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  /// we should provide more scalable search mechanisms.
14089d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  std::vector<PreprocessedEntity *> PreprocessedEntities;
14189d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
14268d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  /// The name of the original source file used to generate this ASTUnit.
14368d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  std::string OriginalSourceFile;
14468d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar
145f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  // Critical optimization when using clang_getCursor().
146f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  ASTLocation LastLoc;
147f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
148f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief The set of diagnostics produced when creating the preamble.
149686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<StoredDiagnostic, 4> PreambleDiagnostics;
150f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
151a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  /// \brief The set of diagnostics produced when creating this
152a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  /// translation unit.
153686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
154a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
1554cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \brief The number of stored diagnostics that come from the driver
1564cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// itself.
1574cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  ///
1584cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// Diagnostics that come from the driver are retained from one parse to
1594cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// the next.
1604cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  unsigned NumStoredDiagnosticsFromDriver;
1614cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor
162313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// \brief Temporary files that should be removed when the ASTUnit is
163313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// destroyed.
164686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<llvm::sys::Path, 4> TemporaryFiles;
165bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
166788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// \brief A mapping from file IDs to the set of preprocessed entities
167788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// stored in that file.
168788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  ///
169788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// FIXME: This is just an optimization hack to avoid searching through
170788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// many preprocessed entities during cursor traversal in the CIndex library.
171788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// Ideally, we would just be able to perform a binary search within the
172788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// list of preprocessed entities.
173788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  PreprocessedEntitiesByFileMap PreprocessedEntitiesByFile;
174788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor
175bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// \brief Simple hack to allow us to assert that ASTUnit is not being
176bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// used concurrently, which is not supported.
177bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  ///
178bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// Clients should create instances of the ConcurrencyCheck class whenever
179bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// using the ASTUnit in a way that isn't intended to be concurrent, which is
180bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// just about any usage.
181bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  unsigned int ConcurrencyCheckValue;
182bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  static const unsigned int CheckLocked = 28573289;
183bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  static const unsigned int CheckUnlocked = 9803453;
184175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
185eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// \brief Counter that determines when we want to try building a
186eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// precompiled preamble.
187eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  ///
188eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// If zero, we will never build a precompiled preamble. Otherwise,
189eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// it's treated as a counter that decrements each time we reparse
190eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// without the benefit of a precompiled preamble. When it hits 1,
191eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// we'll attempt to rebuild the precompiled header. This way, if
192eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// building the precompiled preamble fails, we won't try again for
193eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// some number of calls.
194eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  unsigned PreambleRebuildCounter;
195eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor
196175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \brief The file in which the precompiled preamble is stored.
197385103b79c5338a2be5da0ca70652400bc267371Douglas Gregor  std::string PreambleFile;
198175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
199175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \brief The contents of the preamble that has been precompiled to
200175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \c PreambleFile.
201175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  std::vector<char> Preamble;
202175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
203f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// \brief Whether the preamble ends at the start of a new line.
204f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  ///
205f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// Used to inform the lexer as to whether it's starting at the beginning of
206f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// a line after skipping the preamble.
207f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  bool PreambleEndsAtStartOfLine;
208f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor
209175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \brief The size of the source buffer that we've reserved for the main
210175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// file within the precompiled preamble.
211175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  unsigned PreambleReservedSize;
212cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
213cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// \brief Keeps track of the files that were used when computing the
214cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// preamble, with both their buffer size and their modification time.
215cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  ///
216cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// If any of the files have changed from one compile to the next,
217cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// the preamble must be thrown away.
218cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  llvm::StringMap<std::pair<off_t, time_t> > FilesInPreamble;
219cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
22028233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// \brief When non-NULL, this is the buffer used to store the contents of
22128233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// the main file when it has been padded for use with the precompiled
22228233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// preamble.
22328233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  llvm::MemoryBuffer *SavedMainFileBuffer;
224c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
225671947b18dba342f9aba022ee992babef325a833Douglas Gregor  /// \brief When non-NULL, this is the buffer used to store the
226671947b18dba342f9aba022ee992babef325a833Douglas Gregor  /// contents of the preamble when it has been padded to build the
227671947b18dba342f9aba022ee992babef325a833Douglas Gregor  /// precompiled preamble.
228671947b18dba342f9aba022ee992babef325a833Douglas Gregor  llvm::MemoryBuffer *PreambleBuffer;
229671947b18dba342f9aba022ee992babef325a833Douglas Gregor
230c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// \brief The number of warnings that occurred while parsing the preamble.
231c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  ///
232c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// This value will be used to restore the state of the \c Diagnostic object
233c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// when re-using the precompiled preamble. Note that only the
234c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// number of warnings matters, since we will not save the preamble
235c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// when any errors are present.
236c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  unsigned NumWarningsInPreamble;
237c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
2383c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \brief A list of the serialization ID numbers for each of the top-level
239eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// declarations parsed within the precompiled preamble.
2408538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
241eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
24289d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  /// \brief A list of the offsets into the precompiled preamble which
24389d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  /// correspond to preprocessed entities.
24489d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  std::vector<uint64_t> PreprocessedEntitiesInPreamble;
24589d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
24687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief Whether we should be caching code-completion results.
24787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  bool ShouldCacheCodeCompletionResults;
24887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
249ba7537febdf1bc1cc617e1f1746f2644feba6274Chandler Carruth  /// \brief Whether we want to include nested macro expansions in the
250dca8ee8b7bc86076916a3a80f553f7a4e98c14afDouglas Gregor  /// detailed preprocessing record.
251ba7537febdf1bc1cc617e1f1746f2644feba6274Chandler Carruth  bool NestedMacroExpansions;
252dca8ee8b7bc86076916a3a80f553f7a4e98c14afDouglas Gregor
253e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor  static void ConfigureDiags(llvm::IntrusiveRefCntPtr<Diagnostic> &Diags,
2540b53cf834346d78985aaa9e7300445a39c245614Douglas Gregor                             const char **ArgBegin, const char **ArgEnd,
255e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor                             ASTUnit &AST, bool CaptureDiagnostics);
256e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor
257686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void TranslateStoredDiagnostics(ASTReader *MMan, StringRef ModName,
258f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                                  SourceManager &SrcMan,
259686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                      const SmallVectorImpl<StoredDiagnostic> &Diags,
260686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                            SmallVectorImpl<StoredDiagnostic> &Out);
261f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
26287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregorpublic:
26387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief A cached code-completion result, which may be introduced in one of
26487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// many different contexts.
26587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  struct CachedCodeCompletionResult {
26687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief The code-completion string corresponding to this completion
26787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// result.
26887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    CodeCompletionString *Completion;
26987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
27087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief A bitmask that indicates which code-completion contexts should
27187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// contain this completion result.
27287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    ///
27387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// The bits in the bitmask correspond to the values of
27487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// CodeCompleteContext::Kind. To map from a completion context kind to a
27587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// bit, subtract one from the completion context kind and shift 1 by that
27687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// number of bits. Many completions can occur in several different
27787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// contexts.
27887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    unsigned ShowInContexts;
27987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
28087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief The priority given to this code-completion result.
28187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    unsigned Priority;
28287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
28387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief The libclang cursor kind corresponding to this code-completion
28487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// result.
28587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    CXCursorKind Kind;
2861827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor
28758ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor    /// \brief The availability of this code-completion result.
28858ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor    CXAvailabilityKind Availability;
28958ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor
2901827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor    /// \brief The simplified type class for a non-macro completion result.
2911827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor    SimplifiedTypeClass TypeClass;
292f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
293f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// \brief The type of a non-macro completion result, stored as a unique
294f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// integer used by the string map of cached completion types.
295f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    ///
296f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// This value will be zero if the type is not known, or a unique value
297f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// determined by the formatted type string. Se \c CachedCompletionTypes
298f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// for more information.
299f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    unsigned Type;
30087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  };
30187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
302f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// \brief Retrieve the mapping from formatted type names to unique type
303f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// identifiers.
304f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  llvm::StringMap<unsigned> &getCachedCompletionTypes() {
305f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    return CachedCompletionTypes;
306f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  }
307f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
30848601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  /// \brief Retrieve the allocator used to cache global code completions.
30948601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  llvm::IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
31048601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  getCachedCompletionAllocator() {
31148601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor    return CachedCompletionAllocator;
31248601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  }
31348601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor
3148fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor  /// \brief Retrieve the allocator used to cache global code completions.
3158fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor  /// Creates the allocator if it doesn't already exist.
3168fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor  llvm::IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
3178fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor  getCursorCompletionAllocator() {
3188fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor    if (!CursorCompletionAllocator.getPtr()) {
3198fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor      CursorCompletionAllocator = new GlobalCodeCompletionAllocator;
3208fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor    }
3218fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor    return CursorCompletionAllocator;
3228fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor  }
3238fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor
32487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregorprivate:
325218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  /// \brief Allocator used to store cached code completions.
32648601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  llvm::IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
32748601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor    CachedCompletionAllocator;
3288fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor
3298fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor  /// \brief Allocator used to store code completions for arbitrary cursors.
3308fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor  llvm::IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
3318fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor    CursorCompletionAllocator;
332218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor
33387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief The set of cached code-completion results.
33487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  std::vector<CachedCodeCompletionResult> CachedCompletionResults;
33587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
336f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// \brief A mapping from the formatted type name to a unique number for that
337f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// type, which is used for type equality comparisons.
338f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  llvm::StringMap<unsigned> CachedCompletionTypes;
339f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
3409b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief A string hash of the top-level declaration and macro definition
3419b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// names processed the last time that we reparsed the file.
342727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  ///
3439b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// This hash value is used to determine when we need to refresh the
3449b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// global code-completion cache.
3459b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned CompletionCacheTopLevelHashValue;
346727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor
3479b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief A string hash of the top-level declaration and macro definition
3489b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// names processed the last time that we reparsed the precompiled preamble.
349727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  ///
3509b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// This hash value is used to determine when we need to refresh the
3519b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// global code-completion cache after a rebuild of the precompiled preamble.
3529b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned PreambleTopLevelHashValue;
353ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
3549b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief The current hash value for the top-level declaration and macro
3559b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// definition names
3569b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned CurrentTopLevelHashValue;
3579b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor
358ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  /// \brief Bit used by CIndex to mark when a translation unit may be in an
359ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  /// inconsistent state, and is not safe to free.
360ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  unsigned UnsafeToFree : 1;
361ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
362c4421e966d77a18f815284175b3fcbb46f36fa39Douglas Gregor  /// \brief Cache any "global" code-completion results, so that we can avoid
363c4421e966d77a18f815284175b3fcbb46f36fa39Douglas Gregor  /// recomputing them with each completion.
36487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  void CacheCodeCompletionResults();
36587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
36687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief Clear out and deallocate
36787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  void ClearCachedCompletionResults();
36887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
36931b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
37031b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
371bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
3723687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor  explicit ASTUnit(bool MainFileIsAST);
3738b96253907c47141af0b7b2a44a368748d006a87Douglas Gregor
374abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  void CleanTemporaryFiles();
375754f3490c5b0f5d83361f001bc87944f23644abbDouglas Gregor  bool Parse(llvm::MemoryBuffer *OverrideMainBuffer);
376175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
377f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
378df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  ComputePreamble(CompilerInvocation &Invocation,
379df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                  unsigned MaxLines, bool &CreatedBuffer);
380175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
381df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  llvm::MemoryBuffer *getMainBufferWithPrecompiledPreamble(
38201b6e31a62e2265849f4388b9be6be0a5d13348dDouglas Gregor                               const CompilerInvocation &PreambleInvocationIn,
383df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                                     bool AllowRebuild = true,
384df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                                        unsigned MaxLines = 0);
385eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  void RealizeTopLevelDeclsFromPreamble();
38689d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  void RealizePreprocessedEntitiesFromPreamble();
38789d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
3880853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidispublic:
389bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  class ConcurrencyCheck {
390bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    volatile ASTUnit &Self;
391bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
392bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  public:
393bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    explicit ConcurrencyCheck(ASTUnit &Self)
394bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      : Self(Self)
395bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    {
396bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      assert(Self.ConcurrencyCheckValue == CheckUnlocked &&
397bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor             "Concurrent access to ASTUnit!");
398bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      Self.ConcurrencyCheckValue = CheckLocked;
399bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    }
400bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
401bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    ~ConcurrencyCheck() {
402bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      Self.ConcurrencyCheckValue = CheckUnlocked;
403bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    }
404bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  };
405bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  friend class ConcurrencyCheck;
406bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
4070853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ~ASTUnit();
4080853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
409c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool isMainFileAST() const { return MainFileIsAST; }
410c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
411ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  bool isUnsafeToFree() const { return UnsafeToFree; }
412ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  void setUnsafeToFree(bool Value) { UnsafeToFree = Value; }
413ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
4143687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor  const Diagnostic &getDiagnostics() const { return *Diagnostics; }
4153687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor  Diagnostic &getDiagnostics()             { return *Diagnostics; }
4163687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor
417405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  const SourceManager &getSourceManager() const { return *SourceMgr; }
418405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor        SourceManager &getSourceManager()       { return *SourceMgr; }
4190853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
4204f32786ac45210143654390177105eb749b614e9Ted Kremenek  const Preprocessor &getPreprocessor() const { return *PP; }
4214f32786ac45210143654390177105eb749b614e9Ted Kremenek        Preprocessor &getPreprocessor()       { return *PP; }
4221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4234f32786ac45210143654390177105eb749b614e9Ted Kremenek  const ASTContext &getASTContext() const { return *Ctx; }
4244f32786ac45210143654390177105eb749b614e9Ted Kremenek        ASTContext &getASTContext()       { return *Ctx; }
4250853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
426914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  bool hasSema() const { return TheSema; }
427914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  Sema &getSema() const {
428914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor    assert(TheSema && "ASTUnit does not have a Sema object!");
429914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor    return *TheSema;
430914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  }
431914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
432405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  const FileManager &getFileManager() const { return *FileMgr; }
433405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor        FileManager &getFileManager()       { return *FileMgr; }
434f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
435389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
436389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
43777accc11f04ed4ff9afd4e27d430144d4714be56Steve Naroff  const std::string &getOriginalSourceFileName();
438b85bca2676b433ae555db09de4dd2823ff13b856Zhongxing Xu
439313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// \brief Add a temporary file that the ASTUnit depends on.
440313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  ///
441313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// This file will be erased when the ASTUnit is destroyed.
442313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  void addTemporaryFile(const llvm::sys::Path &TempFile) {
443313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor    TemporaryFiles.push_back(TempFile);
444313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  }
445313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor
4467d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
447f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
44815727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  bool getOwnsRemappedFileBuffers() const { return OwnsRemappedFileBuffers; }
44915727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  void setOwnsRemappedFileBuffers(bool val) { OwnsRemappedFileBuffers = val; }
45015727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis
451eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// \brief Retrieve the maximum PCH level of declarations that a
452eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// traversal of the translation unit should consider.
453eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  unsigned getMaxPCHLevel() const;
454eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
455f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; }
456f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  ASTLocation getLastASTLocation() const { return LastLoc; }
457f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
458213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor
459686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef getMainFileName() const;
460213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor
461eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  typedef std::vector<Decl *>::iterator top_level_iterator;
462eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
463eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  top_level_iterator top_level_begin() {
464f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
465eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    if (!TopLevelDeclsInPreamble.empty())
466eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor      RealizeTopLevelDeclsFromPreamble();
467eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDecls.begin();
468f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  }
469eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
470eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  top_level_iterator top_level_end() {
471f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
472eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    if (!TopLevelDeclsInPreamble.empty())
473eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor      RealizeTopLevelDeclsFromPreamble();
474eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDecls.end();
475eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
476eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
477eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  std::size_t top_level_size() const {
478eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
479eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDeclsInPreamble.size() + TopLevelDecls.size();
480eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
481eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
482eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  bool top_level_empty() const {
483eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
484eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty();
485eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
486eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
487eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// \brief Add a new top-level declaration.
488eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  void addTopLevelDecl(Decl *D) {
489eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    TopLevelDecls.push_back(D);
490eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
491eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
492eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// \brief Add a new top-level declaration, identified by its ID in
493eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// the precompiled preamble.
4948538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  void addTopLevelDeclFromPreamble(serialization::DeclID D) {
495eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    TopLevelDeclsInPreamble.push_back(D);
496f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  }
497f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
4989b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief Retrieve a reference to the current top-level name hash value.
4999b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  ///
5009b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// Note: This is used internally by the top-level tracking action
5019b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned &getCurrentTopLevelHashValue() { return CurrentTopLevelHashValue; }
5029b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor
50389d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  typedef std::vector<PreprocessedEntity *>::iterator pp_entity_iterator;
50489d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
50589d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  pp_entity_iterator pp_entity_begin();
50689d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  pp_entity_iterator pp_entity_end();
50789d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
50889d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  /// \brief Add a new preprocessed entity that's stored at the given offset
50989d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  /// in the precompiled preamble.
51089d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  void addPreprocessedEntityFromPreamble(uint64_t Offset) {
51189d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor    PreprocessedEntitiesInPreamble.push_back(Offset);
51289d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  }
51389d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
514788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// \brief Retrieve the mapping from File IDs to the preprocessed entities
515788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// within that file.
516788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  PreprocessedEntitiesByFileMap &getPreprocessedEntitiesByFile() {
517788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor    return PreprocessedEntitiesByFile;
518788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  }
519788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor
520a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  // Retrieve the diagnostics associated with this AST
521405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  typedef const StoredDiagnostic *stored_diag_iterator;
522405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  stored_diag_iterator stored_diag_begin() const {
523405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics.begin();
524405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  }
525405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  stored_diag_iterator stored_diag_end() const {
526405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics.end();
527405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  }
528405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  unsigned stored_diag_size() const { return StoredDiagnostics.size(); }
529405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor
530686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<StoredDiagnostic, 4> &getStoredDiagnostics() {
531405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics;
532a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  }
533a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
53487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  typedef std::vector<CachedCodeCompletionResult>::iterator
53587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    cached_completion_iterator;
53687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
53787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  cached_completion_iterator cached_completion_begin() {
53887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    return CachedCompletionResults.begin();
53987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
54087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
54187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  cached_completion_iterator cached_completion_end() {
54287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    return CachedCompletionResults.end();
54387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
54487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
54587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  unsigned cached_completion_size() const {
54687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    return CachedCompletionResults.size();
54787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
548389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
549686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  llvm::MemoryBuffer *getBufferForFile(StringRef Filename,
55075dfb65c38d51772df9a00ce2d2feeefd55667adChris Lattner                                       std::string *ErrorStr = 0);
551389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
552467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  /// \brief Determine what kind of translation unit this AST represents.
553467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  TranslationUnitKind getTranslationUnitKind() const { return TUKind; }
554df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
555b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  typedef llvm::PointerUnion<const char *, const llvm::MemoryBuffer *>
556b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis      FilenameOrMemBuf;
5574db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  /// \brief A mapping from a file name to the memory buffer that stores the
5584db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  /// remapped contents of that file.
559b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  typedef std::pair<std::string, FilenameOrMemBuf> RemappedFile;
560b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis
561b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  /// \brief Create a ASTUnit. Gets ownership of the passed CompilerInvocation.
562b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  static ASTUnit *create(CompilerInvocation *CI,
563b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis                         llvm::IntrusiveRefCntPtr<Diagnostic> Diags);
564b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis
5653c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \brief Create a ASTUnit from an AST file.
5660853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
5673c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \param Filename - The AST file to load.
5680853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
5695262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
5705262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
57131b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ///
5723c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \returns - The initialized ASTUnit or null if the AST failed to load.
5733c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  static ASTUnit *LoadFromASTFile(const std::string &Filename,
57428019772db70d4547be05a042eb950bc910f134fDouglas Gregor                                  llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
575389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis                                  const FileSystemOptions &FileSystemOpts,
5765cf48766d626ff6b223acc9d4b7e415ca8480836Ted Kremenek                                  bool OnlyLocalDecls = false,
5774db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor                                  RemappedFile *RemappedFiles = 0,
578a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                  unsigned NumRemappedFiles = 0,
579a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                  bool CaptureDiagnostics = false);
580521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
5814cd912aa94656697a44c3ebb159f05060300524eDouglas Gregorprivate:
5824cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \brief Helper function for \c LoadFromCompilerInvocation() and
5834cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \c LoadFromCommandLine(), which loads an AST from a compiler invocation.
5844cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  ///
5854cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \param PrecompilePreamble Whether to precompile the preamble of this
5864cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// translation unit, to improve the performance of reparsing.
5874cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  ///
5884cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \returns \c true if a catastrophic failure occurred (which means that the
5894cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \c ASTUnit itself is invalid), or \c false otherwise.
5904cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  bool LoadFromCompilerInvocation(bool PrecompilePreamble);
5914cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor
5924cd912aa94656697a44c3ebb159f05060300524eDouglas Gregorpublic:
5934cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor
594d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// \brief Create an ASTUnit from a source file, via a CompilerInvocation
595d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// object, by invoking the optionally provided ASTFrontendAction.
596d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  ///
597d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// \param CI - The compiler invocation to use; it must have exactly one input
598d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
599d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  ///
600d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// \param Diags - The diagnostics engine to use for reporting errors; its
601d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// lifetime is expected to extend past that of the returned ASTUnit.
602d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  ///
603d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// \param Action - The ASTFrontendAction to invoke. Its ownership is not
604d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// transfered.
605d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  static ASTUnit *LoadFromCompilerInvocationAction(CompilerInvocation *CI,
606d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis                                     llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
607d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis                                             ASTFrontendAction *Action = 0);
608d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis
609521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
610521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// CompilerInvocation object.
611521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
612521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// \param CI - The compiler invocation to use; it must have exactly one input
613f7acc37450d59ef751df73acb91de73850cc6517Daniel Dunbar  /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
614521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
6155262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
6165262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
617521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  //
618521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
619521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // shouldn't need to specify them at construction time.
620f7acc37450d59ef751df73acb91de73850cc6517Daniel Dunbar  static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI,
62128019772db70d4547be05a042eb950bc910f134fDouglas Gregor                                     llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
622a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                             bool OnlyLocalDecls = false,
62344c181aec37789f25f6c15543c164416f72e562aDouglas Gregor                                             bool CaptureDiagnostics = false,
624df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                             bool PrecompilePreamble = false,
625467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor                                      TranslationUnitKind TUKind = TU_Complete,
626dca8ee8b7bc86076916a3a80f553f7a4e98c14afDouglas Gregor                                       bool CacheCodeCompletionResults = false,
627ba7537febdf1bc1cc617e1f1746f2644feba6274Chandler Carruth                                       bool NestedMacroExpansions = true);
628521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
6297b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
6307b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// arguments, which must specify exactly one source file.
6317b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
6327b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgBegin - The beginning of the argument vector.
6337b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
6347b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgEnd - The end of the argument vector.
6357b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
6365262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
6375262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
6387b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
639869824e87940f97b87064db2df2861e82e08a8c6Daniel Dunbar  /// \param ResourceFilesPath - The path to the compiler resource files.
6407b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  //
6417b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
6427b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // shouldn't need to specify them at construction time.
6437b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  static ASTUnit *LoadFromCommandLine(const char **ArgBegin,
6447b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      const char **ArgEnd,
64528019772db70d4547be05a042eb950bc910f134fDouglas Gregor                                    llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
646686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                                      StringRef ResourceFilesPath,
6477b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      bool OnlyLocalDecls = false,
648e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor                                      bool CaptureDiagnostics = false,
6494db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor                                      RemappedFile *RemappedFiles = 0,
650a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                      unsigned NumRemappedFiles = 0,
651299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis                                      bool RemappedFilesKeepOriginalName = true,
652df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                      bool PrecompilePreamble = false,
653467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor                                      TranslationUnitKind TUKind = TU_Complete,
65499ba202f659e1885fa5ee114f97c97cf6a857491Douglas Gregor                                      bool CacheCodeCompletionResults = false,
655ba7537febdf1bc1cc617e1f1746f2644feba6274Chandler Carruth                                      bool NestedMacroExpansions = true);
656abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor
657abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \brief Reparse the source files using the same command-line options that
658abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// were originally used to produce this translation unit.
659abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  ///
660abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \returns True if a failure occurred that causes the ASTUnit not to
661abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// contain any translation-unit information, false otherwise.
662abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  bool Reparse(RemappedFile *RemappedFiles = 0,
663abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor               unsigned NumRemappedFiles = 0);
6641abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor
6651abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  /// \brief Perform code completion at the given file, line, and
6661abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  /// column within this translation unit.
6671abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  ///
668cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param File The file in which code completion will occur.
669cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
670cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param Line The line at which code completion will occur.
671cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
672cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param Column The column at which code completion will occur.
673cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
674cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param IncludeMacros Whether to include macros in the code-completion
675cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// results.
676cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
677cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param IncludeCodePatterns Whether to include code patterns (such as a
678cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// for loop) in the code-completion results.
6791abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  ///
6802283d79155a3e82442fce124ce5fd704ca138801Douglas Gregor  /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, StoredDiagnostics, and
6812283d79155a3e82442fce124ce5fd704ca138801Douglas Gregor  /// OwnedBuffers parameters are all disgusting hacks. They will go away.
682686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void CodeComplete(StringRef File, unsigned Line, unsigned Column,
6831abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    RemappedFile *RemappedFiles, unsigned NumRemappedFiles,
684cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor                    bool IncludeMacros, bool IncludeCodePatterns,
6851abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    CodeCompleteConsumer &Consumer,
6861abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    Diagnostic &Diag, LangOptions &LangOpts,
6871abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    SourceManager &SourceMgr, FileManager &FileMgr,
688686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                    SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
689686775deca8b8685eb90801495880e3abdd844c2Chris Lattner              SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers);
6907ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor
6917ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor  /// \brief Save this translation unit to a file with the given name.
6927ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor  ///
69339c411fa229b2a6747b92f945d1702ee674d3470Douglas Gregor  /// \returns An indication of whether the save was successful or not.
694686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  CXSaveError Save(StringRef File);
695b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis
696b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  /// \brief Serialize this translation unit with the given output stream.
697b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  ///
698b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  /// \returns True if an error occurred, false otherwise.
6998cc488fefb2fb04bc8d5398da29f0182f97934cfChris Lattner  bool serialize(raw_ostream &OS);
7006aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
7016aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  virtual ModuleKey loadModule(SourceLocation ImportLoc,
7026aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor                               IdentifierInfo &ModuleName,
7036aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor                               SourceLocation ModuleNameLoc) {
7046aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    // ASTUnit doesn't know how to load modules (not that this matters).
7056aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    return 0;
7066aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  }
7070853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis};
7080853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
7090853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis} // namespace clang
7100853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
7110853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#endif
712