ASTUnit.h revision f226ff9fe8c8db6c5b74a61ce649eda1491c3502
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;
49d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikieclass DiagnosticsEngine;
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 Gregorprivate:
72d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics;
73d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  llvm::IntrusiveRefCntPtr<FileManager>       FileMgr;
74d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  llvm::IntrusiveRefCntPtr<SourceManager>     SourceMgr;
75d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  llvm::OwningPtr<HeaderSearch>               HeaderInfo;
76d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  llvm::IntrusiveRefCntPtr<TargetInfo>        Target;
77d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  llvm::IntrusiveRefCntPtr<Preprocessor>      PP;
78d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  llvm::IntrusiveRefCntPtr<ASTContext>        Ctx;
79389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
80389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  FileSystemOptions FileSystemOpts;
81389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
82914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// \brief The AST consumer that received information about the translation
83914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// unit as it was parsed or loaded.
84914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  llvm::OwningPtr<ASTConsumer> Consumer;
85914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
86914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// \brief The semantic analysis object used to type-check the translation
87914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// unit.
88914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  llvm::OwningPtr<Sema> TheSema;
89914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
90807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// Optional owned invocation, just used to make the invocation used in
91807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// LoadFromCommandLine available.
924f32786ac45210143654390177105eb749b614e9Ted Kremenek  llvm::IntrusiveRefCntPtr<CompilerInvocation> Invocation;
93914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
941aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor  /// \brief The set of target features.
951aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor  ///
961aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor  /// FIXME: each time we reparse, we need to restore the set of target
971aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor  /// features from this vector, because TargetInfo::CreateTargetInfo()
981aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor  /// mangles the target options in place. Yuck!
991aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor  std::vector<std::string> TargetFeatures;
1001aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor
1017d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // OnlyLocalDecls - when true, walking this AST should only visit declarations
1027d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // that come from the AST itself, not from included precompiled headers.
1037d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // FIXME: This is temporary; eventually, CIndex will always do this.
1047d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool                              OnlyLocalDecls;
105c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
106abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \brief Whether to capture any diagnostics produced.
107abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  bool CaptureDiagnostics;
108e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor
109df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  /// \brief Track whether the main file was loaded from an AST or not.
110c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool MainFileIsAST;
111c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
112467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  /// \brief What kind of translation unit this AST represents.
113467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  TranslationUnitKind TUKind;
114df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
115213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor  /// \brief Whether we should time each operation.
116213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor  bool WantTiming;
11715727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis
11815727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  /// \brief Whether the ASTUnit should delete the remapped buffers.
11915727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  bool OwnsRemappedFileBuffers;
120213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor
121f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// Track the top-level decls which appeared in an ASTUnit which was loaded
122f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// from a source file.
123f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  //
124f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // FIXME: This is just an optimization hack to avoid deserializing large parts
125f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // of a PCH file when using the Index library on an ASTUnit loaded from
126f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // source. In the long term we should make the Index library use efficient and
127f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // more scalable search mechanisms.
128f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  std::vector<Decl*> TopLevelDecls;
12989d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
13068d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  /// The name of the original source file used to generate this ASTUnit.
13168d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  std::string OriginalSourceFile;
13268d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar
133f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  // Critical optimization when using clang_getCursor().
134f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  ASTLocation LastLoc;
135f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
136f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief The set of diagnostics produced when creating the preamble.
137686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<StoredDiagnostic, 4> PreambleDiagnostics;
138f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
139a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  /// \brief The set of diagnostics produced when creating this
140a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  /// translation unit.
141686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
142a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
1434cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \brief The number of stored diagnostics that come from the driver
1444cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// itself.
1454cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  ///
1464cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// Diagnostics that come from the driver are retained from one parse to
1474cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// the next.
1484cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  unsigned NumStoredDiagnosticsFromDriver;
1494cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor
150313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// \brief Temporary files that should be removed when the ASTUnit is
151313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// destroyed.
152686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<llvm::sys::Path, 4> TemporaryFiles;
153788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor
154eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// \brief Counter that determines when we want to try building a
155eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// precompiled preamble.
156eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  ///
157eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// If zero, we will never build a precompiled preamble. Otherwise,
158eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// it's treated as a counter that decrements each time we reparse
159eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// without the benefit of a precompiled preamble. When it hits 1,
160eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// we'll attempt to rebuild the precompiled header. This way, if
161eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// building the precompiled preamble fails, we won't try again for
162eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// some number of calls.
163eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  unsigned PreambleRebuildCounter;
164eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor
165175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \brief The file in which the precompiled preamble is stored.
166385103b79c5338a2be5da0ca70652400bc267371Douglas Gregor  std::string PreambleFile;
167175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
168ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidispublic:
169507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  class PreambleData {
170507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    const FileEntry *File;
171507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    std::vector<char> Buffer;
172507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    mutable unsigned NumLines;
173507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
174507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  public:
175507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    PreambleData() : File(0), NumLines(0) { }
176507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
177507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    void assign(const FileEntry *F, const char *begin, const char *end) {
178507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      File = F;
179507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      Buffer.assign(begin, end);
180507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      NumLines = 0;
181507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    }
182507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
183507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    void clear() { Buffer.clear(); File = 0; NumLines = 0; }
184507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
185507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    size_t size() const { return Buffer.size(); }
186507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    bool empty() const { return Buffer.empty(); }
187507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
188507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    const char *getBufferStart() const { return &Buffer[0]; }
189507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
190507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    unsigned getNumLines() const {
191507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      if (NumLines)
192507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis        return NumLines;
193507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      countLines();
194507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      return NumLines;
195507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    }
196507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
197ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis    SourceRange getSourceRange(const SourceManager &SM) const {
198ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis      SourceLocation FileLoc = SM.getLocForStartOfFile(SM.getPreambleFileID());
199ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis      return SourceRange(FileLoc, FileLoc.getLocWithOffset(size()-1));
200ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis    }
201ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
202507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  private:
203507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    void countLines() const;
204507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  };
205507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
206ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  const PreambleData &getPreambleData() const {
207ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis    return Preamble;
208ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  }
209ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
210ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidisprivate:
211ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
212175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \brief The contents of the preamble that has been precompiled to
213175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \c PreambleFile.
214507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  PreambleData Preamble;
215175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
216f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// \brief Whether the preamble ends at the start of a new line.
217f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  ///
218f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// Used to inform the lexer as to whether it's starting at the beginning of
219f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// a line after skipping the preamble.
220f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  bool PreambleEndsAtStartOfLine;
221f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor
222175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \brief The size of the source buffer that we've reserved for the main
223175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// file within the precompiled preamble.
224175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  unsigned PreambleReservedSize;
225cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
226cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// \brief Keeps track of the files that were used when computing the
227cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// preamble, with both their buffer size and their modification time.
228cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  ///
229cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// If any of the files have changed from one compile to the next,
230cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// the preamble must be thrown away.
231cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  llvm::StringMap<std::pair<off_t, time_t> > FilesInPreamble;
232cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
23328233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// \brief When non-NULL, this is the buffer used to store the contents of
23428233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// the main file when it has been padded for use with the precompiled
23528233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// preamble.
23628233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  llvm::MemoryBuffer *SavedMainFileBuffer;
237c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
238671947b18dba342f9aba022ee992babef325a833Douglas Gregor  /// \brief When non-NULL, this is the buffer used to store the
239671947b18dba342f9aba022ee992babef325a833Douglas Gregor  /// contents of the preamble when it has been padded to build the
240671947b18dba342f9aba022ee992babef325a833Douglas Gregor  /// precompiled preamble.
241671947b18dba342f9aba022ee992babef325a833Douglas Gregor  llvm::MemoryBuffer *PreambleBuffer;
242671947b18dba342f9aba022ee992babef325a833Douglas Gregor
243c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// \brief The number of warnings that occurred while parsing the preamble.
244c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  ///
245d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  /// This value will be used to restore the state of the \c DiagnosticsEngine
246d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  /// object when re-using the precompiled preamble. Note that only the
247c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// number of warnings matters, since we will not save the preamble
248c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// when any errors are present.
249c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  unsigned NumWarningsInPreamble;
250c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
2513c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \brief A list of the serialization ID numbers for each of the top-level
252eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// declarations parsed within the precompiled preamble.
2538538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
25489d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
25587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief Whether we should be caching code-completion results.
25687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  bool ShouldCacheCodeCompletionResults;
25787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
258ba7537febdf1bc1cc617e1f1746f2644feba6274Chandler Carruth  /// \brief Whether we want to include nested macro expansions in the
259dca8ee8b7bc86076916a3a80f553f7a4e98c14afDouglas Gregor  /// detailed preprocessing record.
260ba7537febdf1bc1cc617e1f1746f2644feba6274Chandler Carruth  bool NestedMacroExpansions;
2613e3cd93b2fd9644e970c389e715c13883faf68b6Douglas Gregor
2623e3cd93b2fd9644e970c389e715c13883faf68b6Douglas Gregor  /// \brief The language options used when we load an AST file.
2633e3cd93b2fd9644e970c389e715c13883faf68b6Douglas Gregor  LangOptions ASTFileLangOpts;
2643e3cd93b2fd9644e970c389e715c13883faf68b6Douglas Gregor
265d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  static void ConfigureDiags(llvm::IntrusiveRefCntPtr<DiagnosticsEngine> &Diags,
2660b53cf834346d78985aaa9e7300445a39c245614Douglas Gregor                             const char **ArgBegin, const char **ArgEnd,
267e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor                             ASTUnit &AST, bool CaptureDiagnostics);
268e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor
269686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void TranslateStoredDiagnostics(ASTReader *MMan, StringRef ModName,
270f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                                  SourceManager &SrcMan,
271686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                      const SmallVectorImpl<StoredDiagnostic> &Diags,
272686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                            SmallVectorImpl<StoredDiagnostic> &Out);
273f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
27487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregorpublic:
27587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief A cached code-completion result, which may be introduced in one of
27687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// many different contexts.
27787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  struct CachedCodeCompletionResult {
27887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief The code-completion string corresponding to this completion
27987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// result.
28087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    CodeCompletionString *Completion;
28187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
28287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief A bitmask that indicates which code-completion contexts should
28387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// contain this completion result.
28487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    ///
28587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// The bits in the bitmask correspond to the values of
28687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// CodeCompleteContext::Kind. To map from a completion context kind to a
28787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// bit, subtract one from the completion context kind and shift 1 by that
28887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// number of bits. Many completions can occur in several different
28987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// contexts.
29087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    unsigned ShowInContexts;
29187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
29287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief The priority given to this code-completion result.
29387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    unsigned Priority;
29487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
29587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief The libclang cursor kind corresponding to this code-completion
29687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// result.
29787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    CXCursorKind Kind;
2981827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor
29958ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor    /// \brief The availability of this code-completion result.
30058ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor    CXAvailabilityKind Availability;
30158ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor
3021827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor    /// \brief The simplified type class for a non-macro completion result.
3031827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor    SimplifiedTypeClass TypeClass;
304f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
305f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// \brief The type of a non-macro completion result, stored as a unique
306f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// integer used by the string map of cached completion types.
307f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    ///
308f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// This value will be zero if the type is not known, or a unique value
309f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// determined by the formatted type string. Se \c CachedCompletionTypes
310f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// for more information.
311f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    unsigned Type;
31287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  };
31387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
314f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// \brief Retrieve the mapping from formatted type names to unique type
315f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// identifiers.
316f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  llvm::StringMap<unsigned> &getCachedCompletionTypes() {
317f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    return CachedCompletionTypes;
318f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  }
319f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
32048601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  /// \brief Retrieve the allocator used to cache global code completions.
32148601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  llvm::IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
32248601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  getCachedCompletionAllocator() {
32348601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor    return CachedCompletionAllocator;
32448601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  }
32548601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor
3268fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor  /// \brief Retrieve the allocator used to cache global code completions.
3278fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor  /// Creates the allocator if it doesn't already exist.
3288fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor  llvm::IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
3298fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor  getCursorCompletionAllocator() {
3308fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor    if (!CursorCompletionAllocator.getPtr()) {
3318fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor      CursorCompletionAllocator = new GlobalCodeCompletionAllocator;
3328fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor    }
3338fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor    return CursorCompletionAllocator;
3348fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor  }
3358fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor
33687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregorprivate:
337218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  /// \brief Allocator used to store cached code completions.
33848601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  llvm::IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
33948601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor    CachedCompletionAllocator;
3408fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor
3418fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor  /// \brief Allocator used to store code completions for arbitrary cursors.
3428fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor  llvm::IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
3438fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor    CursorCompletionAllocator;
344218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor
34587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief The set of cached code-completion results.
34687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  std::vector<CachedCodeCompletionResult> CachedCompletionResults;
34787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
348f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// \brief A mapping from the formatted type name to a unique number for that
349f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// type, which is used for type equality comparisons.
350f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  llvm::StringMap<unsigned> CachedCompletionTypes;
351f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
3529b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief A string hash of the top-level declaration and macro definition
3539b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// names processed the last time that we reparsed the file.
354727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  ///
3559b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// This hash value is used to determine when we need to refresh the
3569b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// global code-completion cache.
3579b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned CompletionCacheTopLevelHashValue;
358727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor
3599b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief A string hash of the top-level declaration and macro definition
3609b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// names processed the last time that we reparsed the precompiled preamble.
361727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  ///
3629b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// This hash value is used to determine when we need to refresh the
3639b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// global code-completion cache after a rebuild of the precompiled preamble.
3649b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned PreambleTopLevelHashValue;
365ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
3669b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief The current hash value for the top-level declaration and macro
3679b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// definition names
3689b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned CurrentTopLevelHashValue;
3699b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor
370ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  /// \brief Bit used by CIndex to mark when a translation unit may be in an
371ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  /// inconsistent state, and is not safe to free.
372ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  unsigned UnsafeToFree : 1;
373ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
374c4421e966d77a18f815284175b3fcbb46f36fa39Douglas Gregor  /// \brief Cache any "global" code-completion results, so that we can avoid
375c4421e966d77a18f815284175b3fcbb46f36fa39Douglas Gregor  /// recomputing them with each completion.
37687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  void CacheCodeCompletionResults();
37787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
37887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief Clear out and deallocate
37987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  void ClearCachedCompletionResults();
38087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
38131b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
38231b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
383bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
3843687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor  explicit ASTUnit(bool MainFileIsAST);
3858b96253907c47141af0b7b2a44a368748d006a87Douglas Gregor
386abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  void CleanTemporaryFiles();
387754f3490c5b0f5d83361f001bc87944f23644abbDouglas Gregor  bool Parse(llvm::MemoryBuffer *OverrideMainBuffer);
388175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
389f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
390df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  ComputePreamble(CompilerInvocation &Invocation,
391df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                  unsigned MaxLines, bool &CreatedBuffer);
392175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
393df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  llvm::MemoryBuffer *getMainBufferWithPrecompiledPreamble(
39401b6e31a62e2265849f4388b9be6be0a5d13348dDouglas Gregor                               const CompilerInvocation &PreambleInvocationIn,
395df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                                     bool AllowRebuild = true,
396df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                                        unsigned MaxLines = 0);
397eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  void RealizeTopLevelDeclsFromPreamble();
39889d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
399a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// \brief Allows us to assert that ASTUnit is not being used concurrently,
400a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// which is not supported.
401a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  ///
402a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// Clients should create instances of the ConcurrencyCheck class whenever
403a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// using the ASTUnit in a way that isn't intended to be concurrent, which is
404a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// just about any usage.
405a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// Becomes a noop in release mode; only useful for debug mode checking.
406a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  class ConcurrencyState {
407a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    void *Mutex; // a llvm::sys::MutexImpl in debug;
408a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis
409a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  public:
410a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    ConcurrencyState();
411a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    ~ConcurrencyState();
412a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis
413a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    void start();
414a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    void finish();
415a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  };
416a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  ConcurrencyState ConcurrencyCheckValue;
417a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis
4180853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidispublic:
419bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  class ConcurrencyCheck {
420a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    ASTUnit &Self;
421bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
422bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  public:
423bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    explicit ConcurrencyCheck(ASTUnit &Self)
424bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      : Self(Self)
425bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    {
426a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis      Self.ConcurrencyCheckValue.start();
427bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    }
428bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    ~ConcurrencyCheck() {
429a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis      Self.ConcurrencyCheckValue.finish();
430bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    }
431bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  };
432bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  friend class ConcurrencyCheck;
433bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
4340853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ~ASTUnit();
4350853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
436c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool isMainFileAST() const { return MainFileIsAST; }
437c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
438ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  bool isUnsafeToFree() const { return UnsafeToFree; }
439ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  void setUnsafeToFree(bool Value) { UnsafeToFree = Value; }
440ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
441d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  const DiagnosticsEngine &getDiagnostics() const { return *Diagnostics; }
442d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine &getDiagnostics()             { return *Diagnostics; }
4433687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor
444405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  const SourceManager &getSourceManager() const { return *SourceMgr; }
445405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor        SourceManager &getSourceManager()       { return *SourceMgr; }
4460853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
4474f32786ac45210143654390177105eb749b614e9Ted Kremenek  const Preprocessor &getPreprocessor() const { return *PP; }
4484f32786ac45210143654390177105eb749b614e9Ted Kremenek        Preprocessor &getPreprocessor()       { return *PP; }
4491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4504f32786ac45210143654390177105eb749b614e9Ted Kremenek  const ASTContext &getASTContext() const { return *Ctx; }
4514f32786ac45210143654390177105eb749b614e9Ted Kremenek        ASTContext &getASTContext()       { return *Ctx; }
4520853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
453abb5afadd11b19568359880faa5d966a4e33551cArgyrios Kyrtzidis  void setASTContext(ASTContext *ctx) { Ctx = ctx; }
454abb5afadd11b19568359880faa5d966a4e33551cArgyrios Kyrtzidis
455914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  bool hasSema() const { return TheSema; }
456914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  Sema &getSema() const {
457914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor    assert(TheSema && "ASTUnit does not have a Sema object!");
458914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor    return *TheSema;
459914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  }
460914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
461405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  const FileManager &getFileManager() const { return *FileMgr; }
462405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor        FileManager &getFileManager()       { return *FileMgr; }
463f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
464389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
465389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
46677accc11f04ed4ff9afd4e27d430144d4714be56Steve Naroff  const std::string &getOriginalSourceFileName();
467b85bca2676b433ae555db09de4dd2823ff13b856Zhongxing Xu
468313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// \brief Add a temporary file that the ASTUnit depends on.
469313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  ///
470313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// This file will be erased when the ASTUnit is destroyed.
471313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  void addTemporaryFile(const llvm::sys::Path &TempFile) {
472313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor    TemporaryFiles.push_back(TempFile);
473313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  }
474313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor
4757d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
476f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
47715727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  bool getOwnsRemappedFileBuffers() const { return OwnsRemappedFileBuffers; }
47815727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  void setOwnsRemappedFileBuffers(bool val) { OwnsRemappedFileBuffers = val; }
47915727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis
480f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; }
481f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  ASTLocation getLastASTLocation() const { return LastLoc; }
482f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
483213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor
484686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef getMainFileName() const;
485213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor
486eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  typedef std::vector<Decl *>::iterator top_level_iterator;
487eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
488eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  top_level_iterator top_level_begin() {
489f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
490eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    if (!TopLevelDeclsInPreamble.empty())
491eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor      RealizeTopLevelDeclsFromPreamble();
492eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDecls.begin();
493f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  }
494eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
495eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  top_level_iterator top_level_end() {
496f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
497eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    if (!TopLevelDeclsInPreamble.empty())
498eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor      RealizeTopLevelDeclsFromPreamble();
499eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDecls.end();
500eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
501eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
502eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  std::size_t top_level_size() const {
503eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
504eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDeclsInPreamble.size() + TopLevelDecls.size();
505eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
506eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
507eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  bool top_level_empty() const {
508eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
509eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty();
510eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
511eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
512eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// \brief Add a new top-level declaration.
513eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  void addTopLevelDecl(Decl *D) {
514eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    TopLevelDecls.push_back(D);
515eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
516eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
517eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// \brief Add a new top-level declaration, identified by its ID in
518eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// the precompiled preamble.
5198538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  void addTopLevelDeclFromPreamble(serialization::DeclID D) {
520eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    TopLevelDeclsInPreamble.push_back(D);
521f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  }
522f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
5239b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief Retrieve a reference to the current top-level name hash value.
5249b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  ///
5259b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// Note: This is used internally by the top-level tracking action
5269b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned &getCurrentTopLevelHashValue() { return CurrentTopLevelHashValue; }
527507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
528507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// \brief Get the source location for the given file:line:col triplet.
529507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  ///
530507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// The difference with SourceManager::getLocation is that this method checks
531507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// whether the requested location points inside the precompiled preamble
532507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// in which case the returned source location will be a "loaded" one.
533507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  SourceLocation getLocation(const FileEntry *File,
534507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis                             unsigned Line, unsigned Col) const;
535507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
536507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// \brief Get the source location for the given file:offset pair.
537507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  SourceLocation getLocation(const FileEntry *File, unsigned Offset) const;
538ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
539ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  /// \brief If \arg Loc is a loaded location from the preamble, returns
540ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  /// the corresponding local location of the main file, otherwise it returns
541ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  /// \arg Loc.
542ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  SourceLocation mapLocationFromPreamble(SourceLocation Loc);
543ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
544ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  /// \brief If \arg Loc is a local location of the main file but inside the
545ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  /// preamble chunk, returns the corresponding loaded location from the
546ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  /// preamble, otherwise it returns \arg Loc.
547ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  SourceLocation mapLocationToPreamble(SourceLocation Loc);
548ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
549f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  bool isInPreambleFileID(SourceLocation Loc);
550f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  bool isInMainFileID(SourceLocation Loc);
551f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  SourceLocation getStartOfMainFileID();
552f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  SourceLocation getEndOfPreambleFileID();
553f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis
554ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  /// \brief \see mapLocationFromPreamble.
555ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  SourceRange mapRangeFromPreamble(SourceRange R) {
556ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis    return SourceRange(mapLocationFromPreamble(R.getBegin()),
557ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis                       mapLocationFromPreamble(R.getEnd()));
558ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  }
559ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
560ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  /// \brief \see mapLocationToPreamble.
561ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  SourceRange mapRangeToPreamble(SourceRange R) {
562ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis    return SourceRange(mapLocationToPreamble(R.getBegin()),
563ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis                       mapLocationToPreamble(R.getEnd()));
564ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  }
565788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor
566a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  // Retrieve the diagnostics associated with this AST
5673e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  typedef StoredDiagnostic *stored_diag_iterator;
5683e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  typedef const StoredDiagnostic *stored_diag_const_iterator;
5693e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  stored_diag_const_iterator stored_diag_begin() const {
570405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics.begin();
571405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  }
5723e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  stored_diag_iterator stored_diag_begin() {
5733e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis    return StoredDiagnostics.begin();
5743e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  }
5753e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  stored_diag_const_iterator stored_diag_end() const {
5763e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis    return StoredDiagnostics.end();
5773e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  }
5783e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  stored_diag_iterator stored_diag_end() {
579405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics.end();
580405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  }
581405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  unsigned stored_diag_size() const { return StoredDiagnostics.size(); }
5823e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis
5833e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  stored_diag_iterator stored_diag_afterDriver_begin() {
5843e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis    if (NumStoredDiagnosticsFromDriver > StoredDiagnostics.size())
5853e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis      NumStoredDiagnosticsFromDriver = 0;
5863e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis    return StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver;
587a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  }
588a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
58987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  typedef std::vector<CachedCodeCompletionResult>::iterator
59087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    cached_completion_iterator;
59187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
59287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  cached_completion_iterator cached_completion_begin() {
59387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    return CachedCompletionResults.begin();
59487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
59587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
59687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  cached_completion_iterator cached_completion_end() {
59787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    return CachedCompletionResults.end();
59887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
59987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
60087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  unsigned cached_completion_size() const {
60187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    return CachedCompletionResults.size();
60287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
603389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
604686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  llvm::MemoryBuffer *getBufferForFile(StringRef Filename,
60575dfb65c38d51772df9a00ce2d2feeefd55667adChris Lattner                                       std::string *ErrorStr = 0);
606389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
607467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  /// \brief Determine what kind of translation unit this AST represents.
608467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  TranslationUnitKind getTranslationUnitKind() const { return TUKind; }
609df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
610b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  typedef llvm::PointerUnion<const char *, const llvm::MemoryBuffer *>
611b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis      FilenameOrMemBuf;
6124db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  /// \brief A mapping from a file name to the memory buffer that stores the
6134db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  /// remapped contents of that file.
614b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  typedef std::pair<std::string, FilenameOrMemBuf> RemappedFile;
615b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis
616b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  /// \brief Create a ASTUnit. Gets ownership of the passed CompilerInvocation.
617b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  static ASTUnit *create(CompilerInvocation *CI,
618d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                         llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags);
619b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis
6203c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \brief Create a ASTUnit from an AST file.
6210853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
6223c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \param Filename - The AST file to load.
6230853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
6245262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
6255262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
62631b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ///
6273c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \returns - The initialized ASTUnit or null if the AST failed to load.
6283c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  static ASTUnit *LoadFromASTFile(const std::string &Filename,
629d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                              llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
630389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis                                  const FileSystemOptions &FileSystemOpts,
6315cf48766d626ff6b223acc9d4b7e415ca8480836Ted Kremenek                                  bool OnlyLocalDecls = false,
6324db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor                                  RemappedFile *RemappedFiles = 0,
633a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                  unsigned NumRemappedFiles = 0,
634a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                  bool CaptureDiagnostics = false);
635521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
6364cd912aa94656697a44c3ebb159f05060300524eDouglas Gregorprivate:
6374cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \brief Helper function for \c LoadFromCompilerInvocation() and
6384cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \c LoadFromCommandLine(), which loads an AST from a compiler invocation.
6394cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  ///
6404cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \param PrecompilePreamble Whether to precompile the preamble of this
6414cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// translation unit, to improve the performance of reparsing.
6424cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  ///
6434cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \returns \c true if a catastrophic failure occurred (which means that the
6444cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \c ASTUnit itself is invalid), or \c false otherwise.
6454cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  bool LoadFromCompilerInvocation(bool PrecompilePreamble);
6464cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor
6474cd912aa94656697a44c3ebb159f05060300524eDouglas Gregorpublic:
6484cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor
649d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// \brief Create an ASTUnit from a source file, via a CompilerInvocation
650d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// object, by invoking the optionally provided ASTFrontendAction.
651d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  ///
652d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// \param CI - The compiler invocation to use; it must have exactly one input
653d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
654d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  ///
655d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// \param Diags - The diagnostics engine to use for reporting errors; its
656d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// lifetime is expected to extend past that of the returned ASTUnit.
657d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  ///
658d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// \param Action - The ASTFrontendAction to invoke. Its ownership is not
659d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// transfered.
660abb5afadd11b19568359880faa5d966a4e33551cArgyrios Kyrtzidis  ///
661abb5afadd11b19568359880faa5d966a4e33551cArgyrios Kyrtzidis  /// \param Unit - optionally an already created ASTUnit. Its ownership is not
662abb5afadd11b19568359880faa5d966a4e33551cArgyrios Kyrtzidis  /// transfered.
663d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  static ASTUnit *LoadFromCompilerInvocationAction(CompilerInvocation *CI,
664d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                              llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
665abb5afadd11b19568359880faa5d966a4e33551cArgyrios Kyrtzidis                                             ASTFrontendAction *Action = 0,
666abb5afadd11b19568359880faa5d966a4e33551cArgyrios Kyrtzidis                                             ASTUnit *Unit = 0);
667d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis
668521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
669521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// CompilerInvocation object.
670521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
671521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// \param CI - The compiler invocation to use; it must have exactly one input
672f7acc37450d59ef751df73acb91de73850cc6517Daniel Dunbar  /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
673521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
6745262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
6755262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
676521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  //
677521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
678521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // shouldn't need to specify them at construction time.
679f7acc37450d59ef751df73acb91de73850cc6517Daniel Dunbar  static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI,
680d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                              llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
681a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                             bool OnlyLocalDecls = false,
68244c181aec37789f25f6c15543c164416f72e562aDouglas Gregor                                             bool CaptureDiagnostics = false,
683df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                             bool PrecompilePreamble = false,
684467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor                                      TranslationUnitKind TUKind = TU_Complete,
685dca8ee8b7bc86076916a3a80f553f7a4e98c14afDouglas Gregor                                       bool CacheCodeCompletionResults = false,
686ba7537febdf1bc1cc617e1f1746f2644feba6274Chandler Carruth                                       bool NestedMacroExpansions = true);
687521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
6887b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
6897b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// arguments, which must specify exactly one source file.
6907b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
6917b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgBegin - The beginning of the argument vector.
6927b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
6937b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgEnd - The end of the argument vector.
6947b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
6955262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
6965262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
6977b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
698869824e87940f97b87064db2df2861e82e08a8c6Daniel Dunbar  /// \param ResourceFilesPath - The path to the compiler resource files.
6997b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  //
7007b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
7017b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // shouldn't need to specify them at construction time.
7027b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  static ASTUnit *LoadFromCommandLine(const char **ArgBegin,
7037b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      const char **ArgEnd,
704d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                              llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
705686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                                      StringRef ResourceFilesPath,
7067b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      bool OnlyLocalDecls = false,
707e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor                                      bool CaptureDiagnostics = false,
7084db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor                                      RemappedFile *RemappedFiles = 0,
709a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                      unsigned NumRemappedFiles = 0,
710299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis                                      bool RemappedFilesKeepOriginalName = true,
711df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                      bool PrecompilePreamble = false,
712467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor                                      TranslationUnitKind TUKind = TU_Complete,
71399ba202f659e1885fa5ee114f97c97cf6a857491Douglas Gregor                                      bool CacheCodeCompletionResults = false,
714ba7537febdf1bc1cc617e1f1746f2644feba6274Chandler Carruth                                      bool NestedMacroExpansions = true);
715abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor
716abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \brief Reparse the source files using the same command-line options that
717abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// were originally used to produce this translation unit.
718abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  ///
719abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \returns True if a failure occurred that causes the ASTUnit not to
720abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// contain any translation-unit information, false otherwise.
721abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  bool Reparse(RemappedFile *RemappedFiles = 0,
722abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor               unsigned NumRemappedFiles = 0);
7231abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor
7241abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  /// \brief Perform code completion at the given file, line, and
7251abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  /// column within this translation unit.
7261abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  ///
727cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param File The file in which code completion will occur.
728cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
729cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param Line The line at which code completion will occur.
730cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
731cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param Column The column at which code completion will occur.
732cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
733cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param IncludeMacros Whether to include macros in the code-completion
734cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// results.
735cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
736cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param IncludeCodePatterns Whether to include code patterns (such as a
737cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// for loop) in the code-completion results.
7381abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  ///
7392283d79155a3e82442fce124ce5fd704ca138801Douglas Gregor  /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, StoredDiagnostics, and
7402283d79155a3e82442fce124ce5fd704ca138801Douglas Gregor  /// OwnedBuffers parameters are all disgusting hacks. They will go away.
741686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void CodeComplete(StringRef File, unsigned Line, unsigned Column,
7421abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    RemappedFile *RemappedFiles, unsigned NumRemappedFiles,
743cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor                    bool IncludeMacros, bool IncludeCodePatterns,
7441abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    CodeCompleteConsumer &Consumer,
745d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                    DiagnosticsEngine &Diag, LangOptions &LangOpts,
7461abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    SourceManager &SourceMgr, FileManager &FileMgr,
747686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                    SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
748686775deca8b8685eb90801495880e3abdd844c2Chris Lattner              SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers);
7497ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor
7507ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor  /// \brief Save this translation unit to a file with the given name.
7517ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor  ///
75239c411fa229b2a6747b92f945d1702ee674d3470Douglas Gregor  /// \returns An indication of whether the save was successful or not.
753686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  CXSaveError Save(StringRef File);
754b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis
755b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  /// \brief Serialize this translation unit with the given output stream.
756b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  ///
757b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  /// \returns True if an error occurred, false otherwise.
7588cc488fefb2fb04bc8d5398da29f0182f97934cfChris Lattner  bool serialize(raw_ostream &OS);
7596aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
7606aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  virtual ModuleKey loadModule(SourceLocation ImportLoc,
7616aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor                               IdentifierInfo &ModuleName,
7626aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor                               SourceLocation ModuleNameLoc) {
7636aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    // ASTUnit doesn't know how to load modules (not that this matters).
7646aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    return 0;
7656aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  }
7660853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis};
7670853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
7680853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis} // namespace clang
7690853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
7700853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#endif
771