ASTUnit.h revision 30a2e16f6c27f888dd11eba6bbbae1e980078fcb
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
1730a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang-c/Index.h"
18478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer#include "clang/AST/ASTContext.h"
19eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor#include "clang/Basic/FileManager.h"
20389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis#include "clang/Basic/FileSystemOptions.h"
2130a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/Basic/LangOptions.h"
2230a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/Basic/SourceManager.h"
239a022bb007a3e77e1ac1330f955a239cfb1dd0fbDouglas Gregor#include "clang/Basic/TargetOptions.h"
2430a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/Lex/HeaderSearchOptions.h"
2530a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/Lex/ModuleLoader.h"
2630a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/Lex/PreprocessingRecord.h"
2730a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/Sema/CodeCompleteConsumer.h"
2830a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/Sema/Sema.h"
2930a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/Serialization/ASTBitCodes.h"
3028019772db70d4547be05a042eb950bc910f134fDouglas Gregor#include "llvm/ADT/IntrusiveRefCntPtr.h"
310853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#include "llvm/ADT/OwningPtr.h"
32a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor#include "llvm/ADT/SmallVector.h"
33cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor#include "llvm/ADT/StringMap.h"
3403013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer#include "llvm/Support/Path.h"
3530a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include <cassert>
36788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor#include <map>
370853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#include <string>
38cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor#include <sys/types.h>
3930a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include <utility>
4030a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include <vector>
414db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor
424db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregornamespace llvm {
434db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  class MemoryBuffer;
444db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor}
450853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
460853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidisnamespace clang {
47521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass ASTContext;
48f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregorclass ASTReader;
491abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregorclass CodeCompleteConsumer;
50521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass CompilerInvocation;
51e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidisclass CompilerInstance;
52521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Decl;
53d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikieclass DiagnosticsEngine;
54521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass FileEntry;
55521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass FileManager;
56521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass HeaderSearch;
57521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Preprocessor;
58521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass SourceManager;
59521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass TargetInfo;
60d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidisclass ASTFrontendAction;
61900ab95e12bb7483971640a91075699eec391804Argyrios Kyrtzidisclass ASTDeserializationListener;
620853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
633c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl/// \brief Utility class for loading a ASTContext from an AST file.
640853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis///
656aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregorclass ASTUnit : public ModuleLoader {
66788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregorprivate:
67c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<LangOptions>         LangOpts;
68c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<DiagnosticsEngine>   Diagnostics;
69c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<FileManager>         FileMgr;
70c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<SourceManager>       SourceMgr;
71c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  OwningPtr<HeaderSearch>                 HeaderInfo;
72c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<TargetInfo>          Target;
73c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<Preprocessor>        PP;
74c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<ASTContext>          Ctx;
75c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<TargetOptions>       TargetOpts;
76c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts;
7762ba9f61af6177992d8cd2e3306257e1213b1bd3Argyrios Kyrtzidis  ASTReader *Reader;
78389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
79900ab95e12bb7483971640a91075699eec391804Argyrios Kyrtzidis  struct ASTWriterData;
80900ab95e12bb7483971640a91075699eec391804Argyrios Kyrtzidis  OwningPtr<ASTWriterData> WriterData;
81900ab95e12bb7483971640a91075699eec391804Argyrios Kyrtzidis
82389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  FileSystemOptions FileSystemOpts;
83389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
84914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// \brief The AST consumer that received information about the translation
85914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// unit as it was parsed or loaded.
866f42b62b6194f53bcbc349f5d17388e1936535d7Dylan Noblesmith  OwningPtr<ASTConsumer> Consumer;
87914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
88914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// \brief The semantic analysis object used to type-check the translation
89914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// unit.
906f42b62b6194f53bcbc349f5d17388e1936535d7Dylan Noblesmith  OwningPtr<Sema> TheSema;
91914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
92807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// Optional owned invocation, just used to make the invocation used in
93807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// LoadFromCommandLine available.
94c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith  IntrusiveRefCntPtr<CompilerInvocation> Invocation;
95914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
967d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // OnlyLocalDecls - when true, walking this AST should only visit declarations
977d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // that come from the AST itself, not from included precompiled headers.
987d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // FIXME: This is temporary; eventually, CIndex will always do this.
997d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool                              OnlyLocalDecls;
100c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
101abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \brief Whether to capture any diagnostics produced.
102abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  bool CaptureDiagnostics;
103e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor
104df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  /// \brief Track whether the main file was loaded from an AST or not.
105c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool MainFileIsAST;
106c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
107467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  /// \brief What kind of translation unit this AST represents.
108467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  TranslationUnitKind TUKind;
109df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
110213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor  /// \brief Whether we should time each operation.
111213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor  bool WantTiming;
11215727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis
11315727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  /// \brief Whether the ASTUnit should delete the remapped buffers.
11415727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  bool OwnsRemappedFileBuffers;
115213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor
116f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// Track the top-level decls which appeared in an ASTUnit which was loaded
117f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// from a source file.
118f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  //
119f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // FIXME: This is just an optimization hack to avoid deserializing large parts
120f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // of a PCH file when using the Index library on an ASTUnit loaded from
121f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // source. In the long term we should make the Index library use efficient and
122f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // more scalable search mechanisms.
123f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  std::vector<Decl*> TopLevelDecls;
124332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis
125332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  /// \brief Sorted (by file offset) vector of pairs of file offset/Decl.
126332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  typedef SmallVector<std::pair<unsigned, Decl *>, 64> LocDeclsTy;
127332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  typedef llvm::DenseMap<FileID, LocDeclsTy *> FileDeclsTy;
128332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis
129332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  /// \brief Map from FileID to the file-level declarations that it contains.
130332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  /// The files and decls are only local (and non-preamble) ones.
131332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  FileDeclsTy FileDecls;
13289d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
13368d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  /// The name of the original source file used to generate this ASTUnit.
13468d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  std::string OriginalSourceFile;
13568d40e2d16b9fadba386853d6bbb60089291fdc5Daniel 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
143e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// \brief The set of diagnostics produced when failing to parse, e.g. due
144e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// to failure to load the PCH.
145e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  SmallVector<StoredDiagnostic, 4> FailedParseDiagnostics;
146e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis
1474cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \brief The number of stored diagnostics that come from the driver
1484cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// itself.
1494cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  ///
1504cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// Diagnostics that come from the driver are retained from one parse to
1514cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// the next.
1524cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  unsigned NumStoredDiagnosticsFromDriver;
1534cd912aa94656697a44c3ebb159f05060300524eDouglas 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;
1641872b3153a388b3a548c9f699cbc348128059be9Ted Kremenek
165ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidispublic:
166507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  class PreambleData {
167507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    const FileEntry *File;
168507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    std::vector<char> Buffer;
169507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    mutable unsigned NumLines;
170507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
171507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  public:
172507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    PreambleData() : File(0), NumLines(0) { }
173507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
174507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    void assign(const FileEntry *F, const char *begin, const char *end) {
175507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      File = F;
176507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      Buffer.assign(begin, end);
177507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      NumLines = 0;
178507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    }
179507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
180507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    void clear() { Buffer.clear(); File = 0; NumLines = 0; }
181507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
182507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    size_t size() const { return Buffer.size(); }
183507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    bool empty() const { return Buffer.empty(); }
184507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
185507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    const char *getBufferStart() const { return &Buffer[0]; }
186507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
187507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    unsigned getNumLines() const {
188507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      if (NumLines)
189507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis        return NumLines;
190507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      countLines();
191507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      return NumLines;
192507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    }
193507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
194ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis    SourceRange getSourceRange(const SourceManager &SM) const {
195ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis      SourceLocation FileLoc = SM.getLocForStartOfFile(SM.getPreambleFileID());
196ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis      return SourceRange(FileLoc, FileLoc.getLocWithOffset(size()-1));
197ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis    }
198ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
199507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  private:
200507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    void countLines() const;
201507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  };
202507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
203ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  const PreambleData &getPreambleData() const {
204ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis    return Preamble;
205ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  }
206ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
207ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidisprivate:
208ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
209175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \brief The contents of the preamble that has been precompiled to
210175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \c PreambleFile.
211507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  PreambleData Preamble;
212175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
213f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// \brief Whether the preamble ends at the start of a new line.
214f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  ///
215f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// Used to inform the lexer as to whether it's starting at the beginning of
216f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// a line after skipping the preamble.
217f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  bool PreambleEndsAtStartOfLine;
218f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor
219175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \brief The size of the source buffer that we've reserved for the main
220175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// file within the precompiled preamble.
221175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  unsigned PreambleReservedSize;
222cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
223cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// \brief Keeps track of the files that were used when computing the
224cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// preamble, with both their buffer size and their modification time.
225cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  ///
226cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// If any of the files have changed from one compile to the next,
227cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// the preamble must be thrown away.
228cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  llvm::StringMap<std::pair<off_t, time_t> > FilesInPreamble;
229cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
23028233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// \brief When non-NULL, this is the buffer used to store the contents of
23128233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// the main file when it has been padded for use with the precompiled
23228233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// preamble.
23328233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  llvm::MemoryBuffer *SavedMainFileBuffer;
234c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
235671947b18dba342f9aba022ee992babef325a833Douglas Gregor  /// \brief When non-NULL, this is the buffer used to store the
236671947b18dba342f9aba022ee992babef325a833Douglas Gregor  /// contents of the preamble when it has been padded to build the
237671947b18dba342f9aba022ee992babef325a833Douglas Gregor  /// precompiled preamble.
238671947b18dba342f9aba022ee992babef325a833Douglas Gregor  llvm::MemoryBuffer *PreambleBuffer;
239671947b18dba342f9aba022ee992babef325a833Douglas Gregor
240c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// \brief The number of warnings that occurred while parsing the preamble.
241c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  ///
242d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  /// This value will be used to restore the state of the \c DiagnosticsEngine
243d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  /// object when re-using the precompiled preamble. Note that only the
244c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// number of warnings matters, since we will not save the preamble
245c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// when any errors are present.
246c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  unsigned NumWarningsInPreamble;
247c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
2483c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \brief A list of the serialization ID numbers for each of the top-level
249eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// declarations parsed within the precompiled preamble.
2508538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
25189d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
25287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief Whether we should be caching code-completion results.
253d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  bool ShouldCacheCodeCompletionResults : 1;
254d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko
255d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  /// \brief Whether to include brief documentation within the set of code
256d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  /// completions cached.
257d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  bool IncludeBriefCommentsInCodeCompletion : 1;
258ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis
259ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis  /// \brief True if non-system source files should be treated as volatile
260ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis  /// (likely to change while trying to use them).
261ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis  bool UserFilesAreVolatile : 1;
2623e3cd93b2fd9644e970c389e715c13883faf68b6Douglas Gregor
2633e3cd93b2fd9644e970c389e715c13883faf68b6Douglas Gregor  /// \brief The language options used when we load an AST file.
2643e3cd93b2fd9644e970c389e715c13883faf68b6Douglas Gregor  LangOptions ASTFileLangOpts;
2653e3cd93b2fd9644e970c389e715c13883faf68b6Douglas Gregor
266c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith  static void ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> &Diags,
2670b53cf834346d78985aaa9e7300445a39c245614Douglas Gregor                             const char **ArgBegin, const char **ArgEnd,
268e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor                             ASTUnit &AST, bool CaptureDiagnostics);
269e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor
270686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void TranslateStoredDiagnostics(ASTReader *MMan, StringRef ModName,
271f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                                  SourceManager &SrcMan,
272686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                      const SmallVectorImpl<StoredDiagnostic> &Diags,
273686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                            SmallVectorImpl<StoredDiagnostic> &Out);
274f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
275332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  void clearFileLevelDecls();
276332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis
27787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregorpublic:
27887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief A cached code-completion result, which may be introduced in one of
27987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// many different contexts.
28087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  struct CachedCodeCompletionResult {
28187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief The code-completion string corresponding to this completion
28287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// result.
28387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    CodeCompletionString *Completion;
28487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
28587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief A bitmask that indicates which code-completion contexts should
28687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// contain this completion result.
28787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    ///
288026b3588b9916f3fcb20ad2f02ca5472cbbdc5d7Richard Smith    /// The bits in the bitmask correspond to the values of
289026b3588b9916f3fcb20ad2f02ca5472cbbdc5d7Richard Smith    /// CodeCompleteContext::Kind. To map from a completion context kind to a
290026b3588b9916f3fcb20ad2f02ca5472cbbdc5d7Richard Smith    /// bit, shift 1 by that number of bits. Many completions can occur in
291026b3588b9916f3fcb20ad2f02ca5472cbbdc5d7Richard Smith    /// several different contexts.
292026b3588b9916f3fcb20ad2f02ca5472cbbdc5d7Richard Smith    uint64_t ShowInContexts;
29387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
29487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief The priority given to this code-completion result.
29587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    unsigned Priority;
29687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
29787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief The libclang cursor kind corresponding to this code-completion
29887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// result.
29987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    CXCursorKind Kind;
3001827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor
30158ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor    /// \brief The availability of this code-completion result.
30258ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor    CXAvailabilityKind Availability;
30358ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor
3041827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor    /// \brief The simplified type class for a non-macro completion result.
3051827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor    SimplifiedTypeClass TypeClass;
306f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
307f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// \brief The type of a non-macro completion result, stored as a unique
308f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// integer used by the string map of cached completion types.
309f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    ///
310f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// This value will be zero if the type is not known, or a unique value
311f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// determined by the formatted type string. Se \c CachedCompletionTypes
312f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// for more information.
313f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    unsigned Type;
31487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  };
31587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
316f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// \brief Retrieve the mapping from formatted type names to unique type
317f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// identifiers.
318f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  llvm::StringMap<unsigned> &getCachedCompletionTypes() {
319f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    return CachedCompletionTypes;
320f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  }
321f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
32248601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  /// \brief Retrieve the allocator used to cache global code completions.
323c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith  IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
32448601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  getCachedCompletionAllocator() {
32548601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor    return CachedCompletionAllocator;
32648601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  }
32728a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis
32828a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis  CodeCompletionTUInfo &getCodeCompletionTUInfo() {
32928a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis    if (!CCTUInfo)
33028a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis      CCTUInfo.reset(new CodeCompletionTUInfo(
33128a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis                                            new GlobalCodeCompletionAllocator));
33228a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis    return *CCTUInfo;
3338fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor  }
33428a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis
33587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregorprivate:
336218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  /// \brief Allocator used to store cached code completions.
337c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith  IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
33848601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor    CachedCompletionAllocator;
3398fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor
34028a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis  OwningPtr<CodeCompletionTUInfo> CCTUInfo;
341218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor
34287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief The set of cached code-completion results.
34387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  std::vector<CachedCodeCompletionResult> CachedCompletionResults;
34487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
345f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// \brief A mapping from the formatted type name to a unique number for that
346f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// type, which is used for type equality comparisons.
347f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  llvm::StringMap<unsigned> CachedCompletionTypes;
348f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
3499b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief A string hash of the top-level declaration and macro definition
3509b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// names processed the last time that we reparsed the file.
351727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  ///
3529b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// This hash value is used to determine when we need to refresh the
3539b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// global code-completion cache.
3549b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned CompletionCacheTopLevelHashValue;
355727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor
3569b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief A string hash of the top-level declaration and macro definition
3579b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// names processed the last time that we reparsed the precompiled preamble.
358727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  ///
3599b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// This hash value is used to determine when we need to refresh the
3609b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// global code-completion cache after a rebuild of the precompiled preamble.
3619b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned PreambleTopLevelHashValue;
362ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
3639b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief The current hash value for the top-level declaration and macro
3649b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// definition names
3659b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned CurrentTopLevelHashValue;
3669b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor
367ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  /// \brief Bit used by CIndex to mark when a translation unit may be in an
368ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  /// inconsistent state, and is not safe to free.
369ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  unsigned UnsafeToFree : 1;
370ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
371c4421e966d77a18f815284175b3fcbb46f36fa39Douglas Gregor  /// \brief Cache any "global" code-completion results, so that we can avoid
372c4421e966d77a18f815284175b3fcbb46f36fa39Douglas Gregor  /// recomputing them with each completion.
37387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  void CacheCodeCompletionResults();
37487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
37587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief Clear out and deallocate
37687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  void ClearCachedCompletionResults();
37787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
378f56faa01936b9cf909623d7f06e3c2569ca4a78eDmitri Gribenko  ASTUnit(const ASTUnit &) LLVM_DELETED_FUNCTION;
379f56faa01936b9cf909623d7f06e3c2569ca4a78eDmitri Gribenko  void operator=(const ASTUnit &) LLVM_DELETED_FUNCTION;
380bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
3813687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor  explicit ASTUnit(bool MainFileIsAST);
3828b96253907c47141af0b7b2a44a368748d006a87Douglas Gregor
383abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  void CleanTemporaryFiles();
384754f3490c5b0f5d83361f001bc87944f23644abbDouglas Gregor  bool Parse(llvm::MemoryBuffer *OverrideMainBuffer);
385175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
386f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
387df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  ComputePreamble(CompilerInvocation &Invocation,
388df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                  unsigned MaxLines, bool &CreatedBuffer);
389175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
390df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  llvm::MemoryBuffer *getMainBufferWithPrecompiledPreamble(
39101b6e31a62e2265849f4388b9be6be0a5d13348dDouglas Gregor                               const CompilerInvocation &PreambleInvocationIn,
392df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                                     bool AllowRebuild = true,
393df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                                        unsigned MaxLines = 0);
394eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  void RealizeTopLevelDeclsFromPreamble();
395e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis
396e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// \brief Transfers ownership of the objects (like SourceManager) from
397e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// \param CI to this ASTUnit.
398e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  void transferASTDataFromCompilerInstance(CompilerInstance &CI);
399e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis
400a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// \brief Allows us to assert that ASTUnit is not being used concurrently,
401a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// which is not supported.
402a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  ///
403a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// Clients should create instances of the ConcurrencyCheck class whenever
404a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// using the ASTUnit in a way that isn't intended to be concurrent, which is
405a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// just about any usage.
406a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// Becomes a noop in release mode; only useful for debug mode checking.
407a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  class ConcurrencyState {
408d3cb28bef1e1d397b35126029465f2b7e8e8dc1fBenjamin Kramer#ifndef NDEBUG
409a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    void *Mutex; // a llvm::sys::MutexImpl in debug;
410d3cb28bef1e1d397b35126029465f2b7e8e8dc1fBenjamin Kramer#endif
411a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis
412a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  public:
413a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    ConcurrencyState();
414a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    ~ConcurrencyState();
415a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis
416a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    void start();
417a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    void finish();
418a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  };
419a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  ConcurrencyState ConcurrencyCheckValue;
420a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis
4210853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidispublic:
422bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  class ConcurrencyCheck {
423a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    ASTUnit &Self;
424bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
425bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  public:
426bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    explicit ConcurrencyCheck(ASTUnit &Self)
427bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      : Self(Self)
428bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    {
429a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis      Self.ConcurrencyCheckValue.start();
430bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    }
431bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    ~ConcurrencyCheck() {
432a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis      Self.ConcurrencyCheckValue.finish();
433bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    }
434bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  };
435bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  friend class ConcurrencyCheck;
436bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
4370853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ~ASTUnit();
4380853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
439c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool isMainFileAST() const { return MainFileIsAST; }
440c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
441ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  bool isUnsafeToFree() const { return UnsafeToFree; }
442ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  void setUnsafeToFree(bool Value) { UnsafeToFree = Value; }
443ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
444d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  const DiagnosticsEngine &getDiagnostics() const { return *Diagnostics; }
445d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine &getDiagnostics()             { return *Diagnostics; }
4463687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor
447405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  const SourceManager &getSourceManager() const { return *SourceMgr; }
448405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor        SourceManager &getSourceManager()       { return *SourceMgr; }
4490853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
4504f32786ac45210143654390177105eb749b614e9Ted Kremenek  const Preprocessor &getPreprocessor() const { return *PP; }
4514f32786ac45210143654390177105eb749b614e9Ted Kremenek        Preprocessor &getPreprocessor()       { return *PP; }
4521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4534f32786ac45210143654390177105eb749b614e9Ted Kremenek  const ASTContext &getASTContext() const { return *Ctx; }
4544f32786ac45210143654390177105eb749b614e9Ted Kremenek        ASTContext &getASTContext()       { return *Ctx; }
4550853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
456abb5afadd11b19568359880faa5d966a4e33551cArgyrios Kyrtzidis  void setASTContext(ASTContext *ctx) { Ctx = ctx; }
4577fe90f3bfa6bd7f779ecfd4ba201a296a3a1c8cbArgyrios Kyrtzidis  void setPreprocessor(Preprocessor *pp);
458abb5afadd11b19568359880faa5d966a4e33551cArgyrios Kyrtzidis
459914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  bool hasSema() const { return TheSema; }
460914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  Sema &getSema() const {
461914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor    assert(TheSema && "ASTUnit does not have a Sema object!");
462914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor    return *TheSema;
463914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  }
464914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
465405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  const FileManager &getFileManager() const { return *FileMgr; }
466405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor        FileManager &getFileManager()       { return *FileMgr; }
467f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
468389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
469389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
470942fbddccc1b5540336674e577abb59e5c231778Rafael Espindola  StringRef getOriginalSourceFileName() {
471badb3f6971bec4a7341ed152d367f27d572682e0Rafael Espindola    return OriginalSourceFile;
472badb3f6971bec4a7341ed152d367f27d572682e0Rafael Espindola  }
473b85bca2676b433ae555db09de4dd2823ff13b856Zhongxing Xu
474900ab95e12bb7483971640a91075699eec391804Argyrios Kyrtzidis  ASTDeserializationListener *getDeserializationListener();
475900ab95e12bb7483971640a91075699eec391804Argyrios Kyrtzidis
476313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// \brief Add a temporary file that the ASTUnit depends on.
477313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  ///
478313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// This file will be erased when the ASTUnit is destroyed.
4791872b3153a388b3a548c9f699cbc348128059be9Ted Kremenek  void addTemporaryFile(const llvm::sys::Path &TempFile);
480313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor
4817d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
482f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
48315727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  bool getOwnsRemappedFileBuffers() const { return OwnsRemappedFileBuffers; }
48415727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  void setOwnsRemappedFileBuffers(bool val) { OwnsRemappedFileBuffers = val; }
48515727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis
486686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef getMainFileName() const;
487213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor
488eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  typedef std::vector<Decl *>::iterator top_level_iterator;
489eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
490eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  top_level_iterator top_level_begin() {
491f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
492eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    if (!TopLevelDeclsInPreamble.empty())
493eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor      RealizeTopLevelDeclsFromPreamble();
494eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDecls.begin();
495f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  }
496eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
497eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  top_level_iterator top_level_end() {
498f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
499eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    if (!TopLevelDeclsInPreamble.empty())
500eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor      RealizeTopLevelDeclsFromPreamble();
501eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDecls.end();
502eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
503eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
504eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  std::size_t top_level_size() const {
505eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
506eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDeclsInPreamble.size() + TopLevelDecls.size();
507eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
508eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
509eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  bool top_level_empty() const {
510eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
511eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty();
512eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
513eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
514eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// \brief Add a new top-level declaration.
515eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  void addTopLevelDecl(Decl *D) {
516eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    TopLevelDecls.push_back(D);
517eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
518eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
519332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  /// \brief Add a new local file-level declaration.
520332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  void addFileLevelDecl(Decl *D);
521332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis
522dfb332d0081c6641d1dbae6a2aeff757c99cc740Argyrios Kyrtzidis  /// \brief Get the decls that are contained in a file in the Offset/Length
5231824d54df85a462ada812dadda18130f951d40f3Dmitri Gribenko  /// range. \p Length can be 0 to indicate a point at \p Offset instead of
524dfb332d0081c6641d1dbae6a2aeff757c99cc740Argyrios Kyrtzidis  /// a range.
525dfb332d0081c6641d1dbae6a2aeff757c99cc740Argyrios Kyrtzidis  void findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
526dfb332d0081c6641d1dbae6a2aeff757c99cc740Argyrios Kyrtzidis                           SmallVectorImpl<Decl *> &Decls);
527dfb332d0081c6641d1dbae6a2aeff757c99cc740Argyrios Kyrtzidis
528eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// \brief Add a new top-level declaration, identified by its ID in
529eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// the precompiled preamble.
5308538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  void addTopLevelDeclFromPreamble(serialization::DeclID D) {
531eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    TopLevelDeclsInPreamble.push_back(D);
532f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  }
533f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
5349b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief Retrieve a reference to the current top-level name hash value.
5359b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  ///
5369b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// Note: This is used internally by the top-level tracking action
5379b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned &getCurrentTopLevelHashValue() { return CurrentTopLevelHashValue; }
538507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
539507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// \brief Get the source location for the given file:line:col triplet.
540507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  ///
541507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// The difference with SourceManager::getLocation is that this method checks
542507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// whether the requested location points inside the precompiled preamble
543507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// in which case the returned source location will be a "loaded" one.
544507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  SourceLocation getLocation(const FileEntry *File,
545507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis                             unsigned Line, unsigned Col) const;
546507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
547507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// \brief Get the source location for the given file:offset pair.
548507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  SourceLocation getLocation(const FileEntry *File, unsigned Offset) const;
549ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
5501824d54df85a462ada812dadda18130f951d40f3Dmitri Gribenko  /// \brief If \p Loc is a loaded location from the preamble, returns
551ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  /// the corresponding local location of the main file, otherwise it returns
5521824d54df85a462ada812dadda18130f951d40f3Dmitri Gribenko  /// \p Loc.
553ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  SourceLocation mapLocationFromPreamble(SourceLocation Loc);
554ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
5551824d54df85a462ada812dadda18130f951d40f3Dmitri Gribenko  /// \brief If \p Loc is a local location of the main file but inside the
556ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  /// preamble chunk, returns the corresponding loaded location from the
5571824d54df85a462ada812dadda18130f951d40f3Dmitri Gribenko  /// preamble, otherwise it returns \p Loc.
558ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  SourceLocation mapLocationToPreamble(SourceLocation Loc);
559ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
560f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  bool isInPreambleFileID(SourceLocation Loc);
561f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  bool isInMainFileID(SourceLocation Loc);
562f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  SourceLocation getStartOfMainFileID();
563f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  SourceLocation getEndOfPreambleFileID();
564f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis
565e23fb90712233bdfa04387e48b54a7168e23cb3eDmitri Gribenko  /// \see mapLocationFromPreamble.
566ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  SourceRange mapRangeFromPreamble(SourceRange R) {
567ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis    return SourceRange(mapLocationFromPreamble(R.getBegin()),
568ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis                       mapLocationFromPreamble(R.getEnd()));
569ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  }
570ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
571e23fb90712233bdfa04387e48b54a7168e23cb3eDmitri Gribenko  /// \see mapLocationToPreamble.
572ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  SourceRange mapRangeToPreamble(SourceRange R) {
573ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis    return SourceRange(mapLocationToPreamble(R.getBegin()),
574ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis                       mapLocationToPreamble(R.getEnd()));
575ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  }
576788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor
577a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  // Retrieve the diagnostics associated with this AST
5783e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  typedef StoredDiagnostic *stored_diag_iterator;
5793e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  typedef const StoredDiagnostic *stored_diag_const_iterator;
5803e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  stored_diag_const_iterator stored_diag_begin() const {
581405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics.begin();
582405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  }
5833e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  stored_diag_iterator stored_diag_begin() {
5843e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis    return StoredDiagnostics.begin();
5853e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  }
5863e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  stored_diag_const_iterator stored_diag_end() const {
5873e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis    return StoredDiagnostics.end();
5883e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  }
5893e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  stored_diag_iterator stored_diag_end() {
590405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics.end();
591405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  }
592405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  unsigned stored_diag_size() const { return StoredDiagnostics.size(); }
5933e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis
5943e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  stored_diag_iterator stored_diag_afterDriver_begin() {
5953e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis    if (NumStoredDiagnosticsFromDriver > StoredDiagnostics.size())
5963e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis      NumStoredDiagnosticsFromDriver = 0;
5973e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis    return StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver;
598a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  }
599a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
60087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  typedef std::vector<CachedCodeCompletionResult>::iterator
60187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    cached_completion_iterator;
60287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
60387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  cached_completion_iterator cached_completion_begin() {
60487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    return CachedCompletionResults.begin();
60587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
60687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
60787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  cached_completion_iterator cached_completion_end() {
60887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    return CachedCompletionResults.end();
60987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
61087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
61187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  unsigned cached_completion_size() const {
61287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    return CachedCompletionResults.size();
61387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
614389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
615632dcc92f60ab7f806a89c5bca3a0951763a9219Argyrios Kyrtzidis  /// \brief Returns an iterator range for the local preprocessing entities
616632dcc92f60ab7f806a89c5bca3a0951763a9219Argyrios Kyrtzidis  /// of the local Preprocessor, if this is a parsed source file, or the loaded
617632dcc92f60ab7f806a89c5bca3a0951763a9219Argyrios Kyrtzidis  /// preprocessing entities of the primary module if this is an AST file.
618632dcc92f60ab7f806a89c5bca3a0951763a9219Argyrios Kyrtzidis  std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
619632dcc92f60ab7f806a89c5bca3a0951763a9219Argyrios Kyrtzidis    getLocalPreprocessingEntities() const;
620632dcc92f60ab7f806a89c5bca3a0951763a9219Argyrios Kyrtzidis
6212093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis  /// \brief Type for a function iterating over a number of declarations.
6222093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis  /// \returns true to continue iteration and false to abort.
62395c579cae01118eadd311d445ff7f491d0011fb0Argyrios Kyrtzidis  typedef bool (*DeclVisitorFn)(void *context, const Decl *D);
6242093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis
6252093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis  /// \brief Iterate over local declarations (locally parsed if this is a parsed
6262093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis  /// source file or the loaded declarations of the primary module if this is an
6272093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis  /// AST file).
6282093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis  /// \returns true if the iteration was complete or false if it was aborted.
62995c579cae01118eadd311d445ff7f491d0011fb0Argyrios Kyrtzidis  bool visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn);
6302093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis
6313da76bfbd24b2747ceb72d1918bbbe3123dac841Argyrios Kyrtzidis  /// \brief Get the PCH file if one was included.
6323da76bfbd24b2747ceb72d1918bbbe3123dac841Argyrios Kyrtzidis  const FileEntry *getPCHFile();
6333da76bfbd24b2747ceb72d1918bbbe3123dac841Argyrios Kyrtzidis
63462288edde26ff4af9fc079c979a0e1bdc577ce9dArgyrios Kyrtzidis  /// \brief Returns true if the ASTUnit was constructed from a serialized
63562288edde26ff4af9fc079c979a0e1bdc577ce9dArgyrios Kyrtzidis  /// module file.
63662288edde26ff4af9fc079c979a0e1bdc577ce9dArgyrios Kyrtzidis  bool isModuleFile();
63762288edde26ff4af9fc079c979a0e1bdc577ce9dArgyrios Kyrtzidis
638686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  llvm::MemoryBuffer *getBufferForFile(StringRef Filename,
63975dfb65c38d51772df9a00ce2d2feeefd55667adChris Lattner                                       std::string *ErrorStr = 0);
640389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
641467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  /// \brief Determine what kind of translation unit this AST represents.
642467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  TranslationUnitKind getTranslationUnitKind() const { return TUKind; }
643df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
644b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  typedef llvm::PointerUnion<const char *, const llvm::MemoryBuffer *>
645b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis      FilenameOrMemBuf;
6464db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  /// \brief A mapping from a file name to the memory buffer that stores the
6474db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  /// remapped contents of that file.
648b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  typedef std::pair<std::string, FilenameOrMemBuf> RemappedFile;
649b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis
650b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  /// \brief Create a ASTUnit. Gets ownership of the passed CompilerInvocation.
651b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  static ASTUnit *create(CompilerInvocation *CI,
652c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith                         IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
653ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis                         bool CaptureDiagnostics,
654ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis                         bool UserFilesAreVolatile);
655b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis
6563c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \brief Create a ASTUnit from an AST file.
6570853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
6583c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \param Filename - The AST file to load.
6590853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
6605262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
6615262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
66231b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ///
6633c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \returns - The initialized ASTUnit or null if the AST failed to load.
6643c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  static ASTUnit *LoadFromASTFile(const std::string &Filename,
665c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith                              IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
666389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis                                  const FileSystemOptions &FileSystemOpts,
6675cf48766d626ff6b223acc9d4b7e415ca8480836Ted Kremenek                                  bool OnlyLocalDecls = false,
6684db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor                                  RemappedFile *RemappedFiles = 0,
669a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                  unsigned NumRemappedFiles = 0,
670bef35c91b594f66216f4aab303b71a6c5ab7abcfArgyrios Kyrtzidis                                  bool CaptureDiagnostics = false,
671ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis                                  bool AllowPCHWithCompilerErrors = false,
672ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis                                  bool UserFilesAreVolatile = false);
673521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
6744cd912aa94656697a44c3ebb159f05060300524eDouglas Gregorprivate:
6754cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \brief Helper function for \c LoadFromCompilerInvocation() and
6764cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \c LoadFromCommandLine(), which loads an AST from a compiler invocation.
6774cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  ///
6784cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \param PrecompilePreamble Whether to precompile the preamble of this
6794cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// translation unit, to improve the performance of reparsing.
6804cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  ///
6814cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \returns \c true if a catastrophic failure occurred (which means that the
6824cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \c ASTUnit itself is invalid), or \c false otherwise.
6834cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  bool LoadFromCompilerInvocation(bool PrecompilePreamble);
6844cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor
6854cd912aa94656697a44c3ebb159f05060300524eDouglas Gregorpublic:
6864cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor
687d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// \brief Create an ASTUnit from a source file, via a CompilerInvocation
688d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// object, by invoking the optionally provided ASTFrontendAction.
689d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  ///
690d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// \param CI - The compiler invocation to use; it must have exactly one input
691d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
692d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  ///
693d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// \param Diags - The diagnostics engine to use for reporting errors; its
694d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// lifetime is expected to extend past that of the returned ASTUnit.
695d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  ///
696d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// \param Action - The ASTFrontendAction to invoke. Its ownership is not
697d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// transfered.
698abb5afadd11b19568359880faa5d966a4e33551cArgyrios Kyrtzidis  ///
699abb5afadd11b19568359880faa5d966a4e33551cArgyrios Kyrtzidis  /// \param Unit - optionally an already created ASTUnit. Its ownership is not
700abb5afadd11b19568359880faa5d966a4e33551cArgyrios Kyrtzidis  /// transfered.
7016f3ce979a7748fd117e6473d6272b16d643b6262Argyrios Kyrtzidis  ///
7026f3ce979a7748fd117e6473d6272b16d643b6262Argyrios Kyrtzidis  /// \param Persistent - if true the returned ASTUnit will be complete.
7036f3ce979a7748fd117e6473d6272b16d643b6262Argyrios Kyrtzidis  /// false means the caller is only interested in getting info through the
7046f3ce979a7748fd117e6473d6272b16d643b6262Argyrios Kyrtzidis  /// provided \see Action.
705e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  ///
706e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// \param ErrAST - If non-null and parsing failed without any AST to return
707e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// (e.g. because the PCH could not be loaded), this accepts the ASTUnit
708e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// mainly to allow the caller to see the diagnostics.
709e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// This will only receive an ASTUnit if a new one was created. If an already
71070517ca5c07c4b41ff8662b94ee22047b0299f8cDmitri Gribenko  /// created ASTUnit was passed in \p Unit then the caller can check that.
711e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  ///
712d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  static ASTUnit *LoadFromCompilerInvocationAction(CompilerInvocation *CI,
713c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith                              IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
714abb5afadd11b19568359880faa5d966a4e33551cArgyrios Kyrtzidis                                             ASTFrontendAction *Action = 0,
7156f3ce979a7748fd117e6473d6272b16d643b6262Argyrios Kyrtzidis                                             ASTUnit *Unit = 0,
7166f3ce979a7748fd117e6473d6272b16d643b6262Argyrios Kyrtzidis                                             bool Persistent = true,
7176f3ce979a7748fd117e6473d6272b16d643b6262Argyrios Kyrtzidis                                      StringRef ResourceFilesPath = StringRef(),
7186f3ce979a7748fd117e6473d6272b16d643b6262Argyrios Kyrtzidis                                             bool OnlyLocalDecls = false,
7196f3ce979a7748fd117e6473d6272b16d643b6262Argyrios Kyrtzidis                                             bool CaptureDiagnostics = false,
7206f3ce979a7748fd117e6473d6272b16d643b6262Argyrios Kyrtzidis                                             bool PrecompilePreamble = false,
721e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis                                       bool CacheCodeCompletionResults = false,
722d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko                              bool IncludeBriefCommentsInCodeCompletion = false,
723ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis                                       bool UserFilesAreVolatile = false,
724e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis                                       OwningPtr<ASTUnit> *ErrAST = 0);
725d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis
726521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
727521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// CompilerInvocation object.
728521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
729521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// \param CI - The compiler invocation to use; it must have exactly one input
730f7acc37450d59ef751df73acb91de73850cc6517Daniel Dunbar  /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
731521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
7325262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
7335262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
734521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  //
735521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
736521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // shouldn't need to specify them at construction time.
737f7acc37450d59ef751df73acb91de73850cc6517Daniel Dunbar  static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI,
738c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith                              IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
739a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                             bool OnlyLocalDecls = false,
74044c181aec37789f25f6c15543c164416f72e562aDouglas Gregor                                             bool CaptureDiagnostics = false,
741df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                             bool PrecompilePreamble = false,
742467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor                                      TranslationUnitKind TUKind = TU_Complete,
743d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko                                       bool CacheCodeCompletionResults = false,
744ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis                            bool IncludeBriefCommentsInCodeCompletion = false,
745ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis                                             bool UserFilesAreVolatile = false);
746521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
7477b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
7487b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// arguments, which must specify exactly one source file.
7497b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
7507b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgBegin - The beginning of the argument vector.
7517b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
7527b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgEnd - The end of the argument vector.
7537b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
7545262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
7555262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
7567b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
757869824e87940f97b87064db2df2861e82e08a8c6Daniel Dunbar  /// \param ResourceFilesPath - The path to the compiler resource files.
758e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  ///
759e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// \param ErrAST - If non-null and parsing failed without any AST to return
760e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// (e.g. because the PCH could not be loaded), this accepts the ASTUnit
761e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// mainly to allow the caller to see the diagnostics.
762e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  ///
7637b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
7647b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // shouldn't need to specify them at construction time.
7657b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  static ASTUnit *LoadFromCommandLine(const char **ArgBegin,
7667b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      const char **ArgEnd,
767c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith                              IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
768686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                                      StringRef ResourceFilesPath,
7697b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      bool OnlyLocalDecls = false,
770e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor                                      bool CaptureDiagnostics = false,
7714db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor                                      RemappedFile *RemappedFiles = 0,
772a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                      unsigned NumRemappedFiles = 0,
773299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis                                      bool RemappedFilesKeepOriginalName = true,
774df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                      bool PrecompilePreamble = false,
775467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor                                      TranslationUnitKind TUKind = TU_Complete,
776bef35c91b594f66216f4aab303b71a6c5ab7abcfArgyrios Kyrtzidis                                      bool CacheCodeCompletionResults = false,
777d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko                            bool IncludeBriefCommentsInCodeCompletion = false,
778e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis                                      bool AllowPCHWithCompilerErrors = false,
7796a91d385618ea4d28236c496f540a26877c95525Erik Verbruggen                                      bool SkipFunctionBodies = false,
780ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis                                      bool UserFilesAreVolatile = false,
781900ab95e12bb7483971640a91075699eec391804Argyrios Kyrtzidis                                      bool ForSerialization = false,
782e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis                                      OwningPtr<ASTUnit> *ErrAST = 0);
783abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor
784abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \brief Reparse the source files using the same command-line options that
785abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// were originally used to produce this translation unit.
786abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  ///
787abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \returns True if a failure occurred that causes the ASTUnit not to
788abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// contain any translation-unit information, false otherwise.
789abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  bool Reparse(RemappedFile *RemappedFiles = 0,
790abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor               unsigned NumRemappedFiles = 0);
7911abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor
7921abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  /// \brief Perform code completion at the given file, line, and
7931abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  /// column within this translation unit.
7941abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  ///
795cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param File The file in which code completion will occur.
796cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
797cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param Line The line at which code completion will occur.
798cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
799cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param Column The column at which code completion will occur.
800cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
801cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param IncludeMacros Whether to include macros in the code-completion
802cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// results.
803cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
804cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param IncludeCodePatterns Whether to include code patterns (such as a
805cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// for loop) in the code-completion results.
8061abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  ///
807d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  /// \param IncludeBriefComments Whether to include brief documentation within
808d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  /// the set of code completions returned.
809d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  ///
8102283d79155a3e82442fce124ce5fd704ca138801Douglas Gregor  /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, StoredDiagnostics, and
8112283d79155a3e82442fce124ce5fd704ca138801Douglas Gregor  /// OwnedBuffers parameters are all disgusting hacks. They will go away.
812686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void CodeComplete(StringRef File, unsigned Line, unsigned Column,
8131abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    RemappedFile *RemappedFiles, unsigned NumRemappedFiles,
814cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor                    bool IncludeMacros, bool IncludeCodePatterns,
815d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko                    bool IncludeBriefComments,
8161abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    CodeCompleteConsumer &Consumer,
817d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                    DiagnosticsEngine &Diag, LangOptions &LangOpts,
8181abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    SourceManager &SourceMgr, FileManager &FileMgr,
819686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                    SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
820686775deca8b8685eb90801495880e3abdd844c2Chris Lattner              SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers);
8217ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor
8227ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor  /// \brief Save this translation unit to a file with the given name.
8237ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor  ///
824e6d22027bc22d3767a2acdc80299c76b91159867Argyrios Kyrtzidis  /// \returns true if there was a file error or false if the save was
825e6d22027bc22d3767a2acdc80299c76b91159867Argyrios Kyrtzidis  /// successful.
826e6d22027bc22d3767a2acdc80299c76b91159867Argyrios Kyrtzidis  bool Save(StringRef File);
827b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis
828b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  /// \brief Serialize this translation unit with the given output stream.
829b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  ///
830b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  /// \returns True if an error occurred, false otherwise.
8318cc488fefb2fb04bc8d5398da29f0182f97934cfChris Lattner  bool serialize(raw_ostream &OS);
8326aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
833463d90986ec54c62bf8fe31193ef5db701db48a5Douglas Gregor  virtual ModuleLoadResult loadModule(SourceLocation ImportLoc,
834463d90986ec54c62bf8fe31193ef5db701db48a5Douglas Gregor                                      ModuleIdPath Path,
835463d90986ec54c62bf8fe31193ef5db701db48a5Douglas Gregor                                      Module::NameVisibilityKind Visibility,
836463d90986ec54c62bf8fe31193ef5db701db48a5Douglas Gregor                                      bool IsInclusionDirective) {
8376aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    // ASTUnit doesn't know how to load modules (not that this matters).
838463d90986ec54c62bf8fe31193ef5db701db48a5Douglas Gregor    return ModuleLoadResult();
8396aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  }
8400853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis};
8410853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
8420853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis} // namespace clang
8430853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
8440853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#endif
845