ASTUnit.h revision 6bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89
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/Serialization/ASTBitCodes.h"
2928019772db70d4547be05a042eb950bc910f134fDouglas Gregor#include "llvm/ADT/IntrusiveRefCntPtr.h"
30a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor#include "llvm/ADT/SmallVector.h"
31cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor#include "llvm/ADT/StringMap.h"
32651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "llvm/Support/MD5.h"
3303013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer#include "llvm/Support/Path.h"
3430a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include <cassert>
35788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor#include <map>
36651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include <memory>
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 {
470b5ca510fb00eeb19ab82ebfd3c2585404bc9aa8David Blaikieclass Sema;
48521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass ASTContext;
49f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregorclass ASTReader;
501abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregorclass CodeCompleteConsumer;
51521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass CompilerInvocation;
52e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidisclass CompilerInstance;
53521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Decl;
54d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikieclass DiagnosticsEngine;
55521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass FileEntry;
56521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass FileManager;
57521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass HeaderSearch;
58521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Preprocessor;
59521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass SourceManager;
60521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass TargetInfo;
61d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidisclass ASTFrontendAction;
62900ab95e12bb7483971640a91075699eec391804Argyrios Kyrtzidisclass ASTDeserializationListener;
630853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
643c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl/// \brief Utility class for loading a ASTContext from an AST file.
650853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis///
666aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregorclass ASTUnit : public ModuleLoader {
67651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinespublic:
68651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  struct StandaloneFixIt {
69651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    std::pair<unsigned, unsigned> RemoveRange;
70651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    std::pair<unsigned, unsigned> InsertFromRange;
71651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    std::string CodeToInsert;
72651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    bool BeforePreviousInsertions;
73651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  };
74651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
75651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  struct StandaloneDiagnostic {
76651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    unsigned ID;
77651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    DiagnosticsEngine::Level Level;
78651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    std::string Message;
79651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    std::string Filename;
80651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    unsigned LocOffset;
81651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    std::vector<std::pair<unsigned, unsigned> > Ranges;
82651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    std::vector<StandaloneFixIt> FixIts;
83651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  };
84651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
85788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregorprivate:
86c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<LangOptions>         LangOpts;
87c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<DiagnosticsEngine>   Diagnostics;
88c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<FileManager>         FileMgr;
89c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<SourceManager>       SourceMgr;
90651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::unique_ptr<HeaderSearch>           HeaderInfo;
91c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<TargetInfo>          Target;
92c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<Preprocessor>        PP;
93c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<ASTContext>          Ctx;
94c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<TargetOptions>       TargetOpts;
95c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts;
96651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  IntrusiveRefCntPtr<ASTReader> Reader;
973b7deda7137e62810a810ce25b062927a9fc7c71Argyrios Kyrtzidis  bool HadModuleLoaderFatalFailure;
98389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
99900ab95e12bb7483971640a91075699eec391804Argyrios Kyrtzidis  struct ASTWriterData;
100651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::unique_ptr<ASTWriterData> WriterData;
101900ab95e12bb7483971640a91075699eec391804Argyrios Kyrtzidis
102389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  FileSystemOptions FileSystemOpts;
103389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
104914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// \brief The AST consumer that received information about the translation
105914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// unit as it was parsed or loaded.
106651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::unique_ptr<ASTConsumer> Consumer;
107651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
108914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// \brief The semantic analysis object used to type-check the translation
109914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// unit.
110651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::unique_ptr<Sema> TheSema;
111651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
112807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// Optional owned invocation, just used to make the invocation used in
113807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// LoadFromCommandLine available.
114c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith  IntrusiveRefCntPtr<CompilerInvocation> Invocation;
115914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
1167d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // OnlyLocalDecls - when true, walking this AST should only visit declarations
1177d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // that come from the AST itself, not from included precompiled headers.
1187d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // FIXME: This is temporary; eventually, CIndex will always do this.
1197d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool                              OnlyLocalDecls;
120c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
121abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \brief Whether to capture any diagnostics produced.
122abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  bool CaptureDiagnostics;
123e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor
124df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  /// \brief Track whether the main file was loaded from an AST or not.
125c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool MainFileIsAST;
126c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
127467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  /// \brief What kind of translation unit this AST represents.
128467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  TranslationUnitKind TUKind;
129df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
130213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor  /// \brief Whether we should time each operation.
131213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor  bool WantTiming;
13215727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis
13315727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  /// \brief Whether the ASTUnit should delete the remapped buffers.
13415727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  bool OwnsRemappedFileBuffers;
135213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor
136f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// Track the top-level decls which appeared in an ASTUnit which was loaded
137f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// from a source file.
138f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  //
139f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // FIXME: This is just an optimization hack to avoid deserializing large parts
140f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // of a PCH file when using the Index library on an ASTUnit loaded from
141f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // source. In the long term we should make the Index library use efficient and
142f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // more scalable search mechanisms.
143f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  std::vector<Decl*> TopLevelDecls;
144332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis
145332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  /// \brief Sorted (by file offset) vector of pairs of file offset/Decl.
146332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  typedef SmallVector<std::pair<unsigned, Decl *>, 64> LocDeclsTy;
147332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  typedef llvm::DenseMap<FileID, LocDeclsTy *> FileDeclsTy;
148332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis
149332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  /// \brief Map from FileID to the file-level declarations that it contains.
150332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  /// The files and decls are only local (and non-preamble) ones.
151332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  FileDeclsTy FileDecls;
15289d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
15368d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  /// The name of the original source file used to generate this ASTUnit.
15468d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  std::string OriginalSourceFile;
15568d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar
156f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief The set of diagnostics produced when creating the preamble.
157651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  SmallVector<StandaloneDiagnostic, 4> PreambleDiagnostics;
158f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
159a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  /// \brief The set of diagnostics produced when creating this
160a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  /// translation unit.
161686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
162a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
163e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// \brief The set of diagnostics produced when failing to parse, e.g. due
164e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// to failure to load the PCH.
165e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  SmallVector<StoredDiagnostic, 4> FailedParseDiagnostics;
166e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis
1674cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \brief The number of stored diagnostics that come from the driver
1684cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// itself.
1694cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  ///
1704cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// Diagnostics that come from the driver are retained from one parse to
1714cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// the next.
1724cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  unsigned NumStoredDiagnosticsFromDriver;
1734cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor
174eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// \brief Counter that determines when we want to try building a
175eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// precompiled preamble.
176eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  ///
177eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// If zero, we will never build a precompiled preamble. Otherwise,
178eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// it's treated as a counter that decrements each time we reparse
179eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// without the benefit of a precompiled preamble. When it hits 1,
180eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// we'll attempt to rebuild the precompiled header. This way, if
181eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// building the precompiled preamble fails, we won't try again for
182eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// some number of calls.
183eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  unsigned PreambleRebuildCounter;
1841872b3153a388b3a548c9f699cbc348128059be9Ted Kremenek
185ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidispublic:
186507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  class PreambleData {
187507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    const FileEntry *File;
188507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    std::vector<char> Buffer;
189507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    mutable unsigned NumLines;
190507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
191507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  public:
1926bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    PreambleData() : File(nullptr), NumLines(0) { }
193507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
194507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    void assign(const FileEntry *F, const char *begin, const char *end) {
195507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      File = F;
196507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      Buffer.assign(begin, end);
197507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      NumLines = 0;
198507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    }
199507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
2006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    void clear() { Buffer.clear(); File = nullptr; NumLines = 0; }
201507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
202507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    size_t size() const { return Buffer.size(); }
203507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    bool empty() const { return Buffer.empty(); }
204507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
205507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    const char *getBufferStart() const { return &Buffer[0]; }
206507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
207507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    unsigned getNumLines() const {
208507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      if (NumLines)
209507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis        return NumLines;
210507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      countLines();
211507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      return NumLines;
212507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    }
213507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
214ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis    SourceRange getSourceRange(const SourceManager &SM) const {
215ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis      SourceLocation FileLoc = SM.getLocForStartOfFile(SM.getPreambleFileID());
216ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis      return SourceRange(FileLoc, FileLoc.getLocWithOffset(size()-1));
217ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis    }
218ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
219507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  private:
220507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    void countLines() const;
221507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  };
222507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
223ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  const PreambleData &getPreambleData() const {
224ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis    return Preamble;
225ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  }
226ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
227651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// Data used to determine if a file used in the preamble has been changed.
228651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  struct PreambleFileHash {
229651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// All files have size set.
230651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    off_t Size;
231651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
232651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// Modification time is set for files that are on disk.  For memory
233651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// buffers it is zero.
234651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    time_t ModTime;
235651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
236651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// Memory buffers have MD5 instead of modification time.  We don't
237651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// compute MD5 for on-disk files because we hope that modification time is
238651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// enough to tell if the file was changed.
239651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    llvm::MD5::MD5Result MD5;
240651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
241651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    static PreambleFileHash createForFile(off_t Size, time_t ModTime);
242651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    static PreambleFileHash
243651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    createForMemoryBuffer(const llvm::MemoryBuffer *Buffer);
244651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
245651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    friend bool operator==(const PreambleFileHash &LHS,
246651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                           const PreambleFileHash &RHS);
247651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
248651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    friend bool operator!=(const PreambleFileHash &LHS,
249651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                           const PreambleFileHash &RHS) {
250651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return !(LHS == RHS);
251651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
252651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  };
253ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
254651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesprivate:
255175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \brief The contents of the preamble that has been precompiled to
256175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \c PreambleFile.
257507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  PreambleData Preamble;
258175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
259f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// \brief Whether the preamble ends at the start of a new line.
260f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  ///
261f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// Used to inform the lexer as to whether it's starting at the beginning of
262f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// a line after skipping the preamble.
263f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  bool PreambleEndsAtStartOfLine;
264cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
265cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// \brief Keeps track of the files that were used when computing the
266cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// preamble, with both their buffer size and their modification time.
267cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  ///
268cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// If any of the files have changed from one compile to the next,
269cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// the preamble must be thrown away.
270651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::StringMap<PreambleFileHash> FilesInPreamble;
271cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
27228233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// \brief When non-NULL, this is the buffer used to store the contents of
27328233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// the main file when it has been padded for use with the precompiled
27428233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// preamble.
27528233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  llvm::MemoryBuffer *SavedMainFileBuffer;
276c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
277671947b18dba342f9aba022ee992babef325a833Douglas Gregor  /// \brief When non-NULL, this is the buffer used to store the
278671947b18dba342f9aba022ee992babef325a833Douglas Gregor  /// contents of the preamble when it has been padded to build the
279671947b18dba342f9aba022ee992babef325a833Douglas Gregor  /// precompiled preamble.
280671947b18dba342f9aba022ee992babef325a833Douglas Gregor  llvm::MemoryBuffer *PreambleBuffer;
281671947b18dba342f9aba022ee992babef325a833Douglas Gregor
282c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// \brief The number of warnings that occurred while parsing the preamble.
283c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  ///
284d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  /// This value will be used to restore the state of the \c DiagnosticsEngine
285d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  /// object when re-using the precompiled preamble. Note that only the
286c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// number of warnings matters, since we will not save the preamble
287c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// when any errors are present.
288c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  unsigned NumWarningsInPreamble;
289c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
2903c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \brief A list of the serialization ID numbers for each of the top-level
291eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// declarations parsed within the precompiled preamble.
2928538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
29389d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
29487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief Whether we should be caching code-completion results.
295d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  bool ShouldCacheCodeCompletionResults : 1;
296d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko
297d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  /// \brief Whether to include brief documentation within the set of code
298d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  /// completions cached.
299d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  bool IncludeBriefCommentsInCodeCompletion : 1;
300ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis
301ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis  /// \brief True if non-system source files should be treated as volatile
302ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis  /// (likely to change while trying to use them).
303ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis  bool UserFilesAreVolatile : 1;
3043e3cd93b2fd9644e970c389e715c13883faf68b6Douglas Gregor
3053e3cd93b2fd9644e970c389e715c13883faf68b6Douglas Gregor  /// \brief The language options used when we load an AST file.
3063e3cd93b2fd9644e970c389e715c13883faf68b6Douglas Gregor  LangOptions ASTFileLangOpts;
3073e3cd93b2fd9644e970c389e715c13883faf68b6Douglas Gregor
308c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith  static void ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> &Diags,
3090b53cf834346d78985aaa9e7300445a39c245614Douglas Gregor                             const char **ArgBegin, const char **ArgEnd,
310e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor                             ASTUnit &AST, bool CaptureDiagnostics);
311e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor
312651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void TranslateStoredDiagnostics(FileManager &FileMgr,
313f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                                  SourceManager &SrcMan,
314651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                      const SmallVectorImpl<StandaloneDiagnostic> &Diags,
315686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                            SmallVectorImpl<StoredDiagnostic> &Out);
316f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
317332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  void clearFileLevelDecls();
318332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis
31987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregorpublic:
32087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief A cached code-completion result, which may be introduced in one of
32187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// many different contexts.
32287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  struct CachedCodeCompletionResult {
32387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief The code-completion string corresponding to this completion
32487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// result.
32587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    CodeCompletionString *Completion;
32687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
32787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief A bitmask that indicates which code-completion contexts should
32887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// contain this completion result.
32987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    ///
330026b3588b9916f3fcb20ad2f02ca5472cbbdc5d7Richard Smith    /// The bits in the bitmask correspond to the values of
331026b3588b9916f3fcb20ad2f02ca5472cbbdc5d7Richard Smith    /// CodeCompleteContext::Kind. To map from a completion context kind to a
332026b3588b9916f3fcb20ad2f02ca5472cbbdc5d7Richard Smith    /// bit, shift 1 by that number of bits. Many completions can occur in
333026b3588b9916f3fcb20ad2f02ca5472cbbdc5d7Richard Smith    /// several different contexts.
334026b3588b9916f3fcb20ad2f02ca5472cbbdc5d7Richard Smith    uint64_t ShowInContexts;
33587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
33687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief The priority given to this code-completion result.
33787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    unsigned Priority;
33887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
33987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief The libclang cursor kind corresponding to this code-completion
34087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// result.
34187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    CXCursorKind Kind;
3421827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor
34358ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor    /// \brief The availability of this code-completion result.
34458ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor    CXAvailabilityKind Availability;
34558ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor
3461827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor    /// \brief The simplified type class for a non-macro completion result.
3471827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor    SimplifiedTypeClass TypeClass;
348f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
349f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// \brief The type of a non-macro completion result, stored as a unique
350f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// integer used by the string map of cached completion types.
351f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    ///
352f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// This value will be zero if the type is not known, or a unique value
353f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// determined by the formatted type string. Se \c CachedCompletionTypes
354f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// for more information.
355f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    unsigned Type;
35687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  };
35787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
358f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// \brief Retrieve the mapping from formatted type names to unique type
359f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// identifiers.
360f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  llvm::StringMap<unsigned> &getCachedCompletionTypes() {
361f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    return CachedCompletionTypes;
362f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  }
363f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
36448601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  /// \brief Retrieve the allocator used to cache global code completions.
365c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith  IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
36648601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  getCachedCompletionAllocator() {
36748601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor    return CachedCompletionAllocator;
36848601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  }
36928a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis
37028a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis  CodeCompletionTUInfo &getCodeCompletionTUInfo() {
37128a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis    if (!CCTUInfo)
37228a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis      CCTUInfo.reset(new CodeCompletionTUInfo(
37328a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis                                            new GlobalCodeCompletionAllocator));
37428a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis    return *CCTUInfo;
3758fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor  }
37628a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis
37787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregorprivate:
378218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  /// \brief Allocator used to store cached code completions.
379c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith  IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
38048601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor    CachedCompletionAllocator;
381651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
382651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::unique_ptr<CodeCompletionTUInfo> CCTUInfo;
383218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor
38487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief The set of cached code-completion results.
38587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  std::vector<CachedCodeCompletionResult> CachedCompletionResults;
38687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
387f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// \brief A mapping from the formatted type name to a unique number for that
388f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// type, which is used for type equality comparisons.
389f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  llvm::StringMap<unsigned> CachedCompletionTypes;
390f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
3919b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief A string hash of the top-level declaration and macro definition
3929b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// names processed the last time that we reparsed the file.
393727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  ///
3949b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// This hash value is used to determine when we need to refresh the
3959b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// global code-completion cache.
3969b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned CompletionCacheTopLevelHashValue;
397727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor
3989b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief A string hash of the top-level declaration and macro definition
3999b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// names processed the last time that we reparsed the precompiled preamble.
400727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  ///
4019b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// This hash value is used to determine when we need to refresh the
4029b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// global code-completion cache after a rebuild of the precompiled preamble.
4039b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned PreambleTopLevelHashValue;
404ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
4059b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief The current hash value for the top-level declaration and macro
4069b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// definition names
4079b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned CurrentTopLevelHashValue;
4089b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor
409ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  /// \brief Bit used by CIndex to mark when a translation unit may be in an
410ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  /// inconsistent state, and is not safe to free.
411ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  unsigned UnsafeToFree : 1;
412ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
413c4421e966d77a18f815284175b3fcbb46f36fa39Douglas Gregor  /// \brief Cache any "global" code-completion results, so that we can avoid
414c4421e966d77a18f815284175b3fcbb46f36fa39Douglas Gregor  /// recomputing them with each completion.
41587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  void CacheCodeCompletionResults();
41687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
41787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief Clear out and deallocate
41887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  void ClearCachedCompletionResults();
41987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
420f56faa01936b9cf909623d7f06e3c2569ca4a78eDmitri Gribenko  ASTUnit(const ASTUnit &) LLVM_DELETED_FUNCTION;
421f56faa01936b9cf909623d7f06e3c2569ca4a78eDmitri Gribenko  void operator=(const ASTUnit &) LLVM_DELETED_FUNCTION;
422bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
4233687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor  explicit ASTUnit(bool MainFileIsAST);
4248b96253907c47141af0b7b2a44a368748d006a87Douglas Gregor
425abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  void CleanTemporaryFiles();
426754f3490c5b0f5d83361f001bc87944f23644abbDouglas Gregor  bool Parse(llvm::MemoryBuffer *OverrideMainBuffer);
427175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
428f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
429df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  ComputePreamble(CompilerInvocation &Invocation,
430df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                  unsigned MaxLines, bool &CreatedBuffer);
431175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
432df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  llvm::MemoryBuffer *getMainBufferWithPrecompiledPreamble(
43301b6e31a62e2265849f4388b9be6be0a5d13348dDouglas Gregor                               const CompilerInvocation &PreambleInvocationIn,
434df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                                     bool AllowRebuild = true,
435df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                                        unsigned MaxLines = 0);
436eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  void RealizeTopLevelDeclsFromPreamble();
437e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis
438e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// \brief Transfers ownership of the objects (like SourceManager) from
439e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// \param CI to this ASTUnit.
440e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  void transferASTDataFromCompilerInstance(CompilerInstance &CI);
441e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis
442a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// \brief Allows us to assert that ASTUnit is not being used concurrently,
443a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// which is not supported.
444a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  ///
445a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// Clients should create instances of the ConcurrencyCheck class whenever
446a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// using the ASTUnit in a way that isn't intended to be concurrent, which is
447a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// just about any usage.
448a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// Becomes a noop in release mode; only useful for debug mode checking.
449a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  class ConcurrencyState {
450a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    void *Mutex; // a llvm::sys::MutexImpl in debug;
451a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis
452a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  public:
453a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    ConcurrencyState();
454a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    ~ConcurrencyState();
455a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis
456a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    void start();
457a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    void finish();
458a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  };
459a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  ConcurrencyState ConcurrencyCheckValue;
460a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis
4610853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidispublic:
462bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  class ConcurrencyCheck {
463a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    ASTUnit &Self;
464bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
465bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  public:
466bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    explicit ConcurrencyCheck(ASTUnit &Self)
467bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      : Self(Self)
468bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    {
469a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis      Self.ConcurrencyCheckValue.start();
470bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    }
471bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    ~ConcurrencyCheck() {
472a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis      Self.ConcurrencyCheckValue.finish();
473bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    }
474bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  };
475bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  friend class ConcurrencyCheck;
476bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
4770853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ~ASTUnit();
4780853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
479c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool isMainFileAST() const { return MainFileIsAST; }
480c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
481ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  bool isUnsafeToFree() const { return UnsafeToFree; }
482ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  void setUnsafeToFree(bool Value) { UnsafeToFree = Value; }
483ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
484d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  const DiagnosticsEngine &getDiagnostics() const { return *Diagnostics; }
485d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine &getDiagnostics()             { return *Diagnostics; }
4863687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor
487405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  const SourceManager &getSourceManager() const { return *SourceMgr; }
488405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor        SourceManager &getSourceManager()       { return *SourceMgr; }
4890853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
4904f32786ac45210143654390177105eb749b614e9Ted Kremenek  const Preprocessor &getPreprocessor() const { return *PP; }
4914f32786ac45210143654390177105eb749b614e9Ted Kremenek        Preprocessor &getPreprocessor()       { return *PP; }
4921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4934f32786ac45210143654390177105eb749b614e9Ted Kremenek  const ASTContext &getASTContext() const { return *Ctx; }
4944f32786ac45210143654390177105eb749b614e9Ted Kremenek        ASTContext &getASTContext()       { return *Ctx; }
4950853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
496abb5afadd11b19568359880faa5d966a4e33551cArgyrios Kyrtzidis  void setASTContext(ASTContext *ctx) { Ctx = ctx; }
4977fe90f3bfa6bd7f779ecfd4ba201a296a3a1c8cbArgyrios Kyrtzidis  void setPreprocessor(Preprocessor *pp);
498abb5afadd11b19568359880faa5d966a4e33551cArgyrios Kyrtzidis
499651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool hasSema() const { return (bool)TheSema; }
500914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  Sema &getSema() const {
501914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor    assert(TheSema && "ASTUnit does not have a Sema object!");
5026bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return *TheSema;
5036bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
5046bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
5056bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const LangOptions &getLangOpts() const {
5066bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    assert(LangOpts && " ASTUnit does not have language options");
5076bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return *LangOpts;
508914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  }
509914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
510405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  const FileManager &getFileManager() const { return *FileMgr; }
511405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor        FileManager &getFileManager()       { return *FileMgr; }
512f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
513389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
514389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
515942fbddccc1b5540336674e577abb59e5c231778Rafael Espindola  StringRef getOriginalSourceFileName() {
516badb3f6971bec4a7341ed152d367f27d572682e0Rafael Espindola    return OriginalSourceFile;
517badb3f6971bec4a7341ed152d367f27d572682e0Rafael Espindola  }
518b85bca2676b433ae555db09de4dd2823ff13b856Zhongxing Xu
5197eca8d2262a26eedb2ff3cc0fd11be2494b17cc1Argyrios Kyrtzidis  ASTMutationListener *getASTMutationListener();
520900ab95e12bb7483971640a91075699eec391804Argyrios Kyrtzidis  ASTDeserializationListener *getDeserializationListener();
521900ab95e12bb7483971640a91075699eec391804Argyrios Kyrtzidis
522313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// \brief Add a temporary file that the ASTUnit depends on.
523313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  ///
524313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// This file will be erased when the ASTUnit is destroyed.
525b804cb315cfeebc4d1c4d2d023ffb08440f77254Rafael Espindola  void addTemporaryFile(StringRef TempFile);
526b804cb315cfeebc4d1c4d2d023ffb08440f77254Rafael Espindola
5277d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
528f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
52915727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  bool getOwnsRemappedFileBuffers() const { return OwnsRemappedFileBuffers; }
53015727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  void setOwnsRemappedFileBuffers(bool val) { OwnsRemappedFileBuffers = val; }
53115727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis
532686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef getMainFileName() const;
533213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor
53444f65a587ee39c20e715c9df411c864d328c71b5Argyrios Kyrtzidis  /// \brief If this ASTUnit came from an AST file, returns the filename for it.
53544f65a587ee39c20e715c9df411c864d328c71b5Argyrios Kyrtzidis  StringRef getASTFileName() const;
53644f65a587ee39c20e715c9df411c864d328c71b5Argyrios Kyrtzidis
537eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  typedef std::vector<Decl *>::iterator top_level_iterator;
538eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
539eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  top_level_iterator top_level_begin() {
540f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
541eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    if (!TopLevelDeclsInPreamble.empty())
542eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor      RealizeTopLevelDeclsFromPreamble();
543eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDecls.begin();
544f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  }
545eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
546eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  top_level_iterator top_level_end() {
547f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
548eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    if (!TopLevelDeclsInPreamble.empty())
549eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor      RealizeTopLevelDeclsFromPreamble();
550eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDecls.end();
551eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
552eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
553eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  std::size_t top_level_size() const {
554eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
555eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDeclsInPreamble.size() + TopLevelDecls.size();
556eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
557eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
558eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  bool top_level_empty() const {
559eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
560eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty();
561eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
562eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
563eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// \brief Add a new top-level declaration.
564eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  void addTopLevelDecl(Decl *D) {
565eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    TopLevelDecls.push_back(D);
566eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
567eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
568332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  /// \brief Add a new local file-level declaration.
569332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  void addFileLevelDecl(Decl *D);
570332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis
571dfb332d0081c6641d1dbae6a2aeff757c99cc740Argyrios Kyrtzidis  /// \brief Get the decls that are contained in a file in the Offset/Length
5721824d54df85a462ada812dadda18130f951d40f3Dmitri Gribenko  /// range. \p Length can be 0 to indicate a point at \p Offset instead of
573dfb332d0081c6641d1dbae6a2aeff757c99cc740Argyrios Kyrtzidis  /// a range.
574dfb332d0081c6641d1dbae6a2aeff757c99cc740Argyrios Kyrtzidis  void findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
575dfb332d0081c6641d1dbae6a2aeff757c99cc740Argyrios Kyrtzidis                           SmallVectorImpl<Decl *> &Decls);
576dfb332d0081c6641d1dbae6a2aeff757c99cc740Argyrios Kyrtzidis
577eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// \brief Add a new top-level declaration, identified by its ID in
578eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// the precompiled preamble.
5798538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  void addTopLevelDeclFromPreamble(serialization::DeclID D) {
580eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    TopLevelDeclsInPreamble.push_back(D);
581f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  }
582f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
5839b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief Retrieve a reference to the current top-level name hash value.
5849b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  ///
5859b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// Note: This is used internally by the top-level tracking action
5869b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned &getCurrentTopLevelHashValue() { return CurrentTopLevelHashValue; }
587507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
588507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// \brief Get the source location for the given file:line:col triplet.
589507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  ///
590507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// The difference with SourceManager::getLocation is that this method checks
591507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// whether the requested location points inside the precompiled preamble
592507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// in which case the returned source location will be a "loaded" one.
593507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  SourceLocation getLocation(const FileEntry *File,
594507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis                             unsigned Line, unsigned Col) const;
595507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
596507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// \brief Get the source location for the given file:offset pair.
597507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  SourceLocation getLocation(const FileEntry *File, unsigned Offset) const;
598ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
5991824d54df85a462ada812dadda18130f951d40f3Dmitri Gribenko  /// \brief If \p Loc is a loaded location from the preamble, returns
600ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  /// the corresponding local location of the main file, otherwise it returns
6011824d54df85a462ada812dadda18130f951d40f3Dmitri Gribenko  /// \p Loc.
602ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  SourceLocation mapLocationFromPreamble(SourceLocation Loc);
603ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
6041824d54df85a462ada812dadda18130f951d40f3Dmitri Gribenko  /// \brief If \p Loc is a local location of the main file but inside the
605ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  /// preamble chunk, returns the corresponding loaded location from the
6061824d54df85a462ada812dadda18130f951d40f3Dmitri Gribenko  /// preamble, otherwise it returns \p Loc.
607ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  SourceLocation mapLocationToPreamble(SourceLocation Loc);
608ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
609f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  bool isInPreambleFileID(SourceLocation Loc);
610f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  bool isInMainFileID(SourceLocation Loc);
611f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  SourceLocation getStartOfMainFileID();
612f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  SourceLocation getEndOfPreambleFileID();
613f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis
614e23fb90712233bdfa04387e48b54a7168e23cb3eDmitri Gribenko  /// \see mapLocationFromPreamble.
615ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  SourceRange mapRangeFromPreamble(SourceRange R) {
616ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis    return SourceRange(mapLocationFromPreamble(R.getBegin()),
617ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis                       mapLocationFromPreamble(R.getEnd()));
618ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  }
619ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
620e23fb90712233bdfa04387e48b54a7168e23cb3eDmitri Gribenko  /// \see mapLocationToPreamble.
621ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  SourceRange mapRangeToPreamble(SourceRange R) {
622ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis    return SourceRange(mapLocationToPreamble(R.getBegin()),
623ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis                       mapLocationToPreamble(R.getEnd()));
624ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  }
625788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor
626a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  // Retrieve the diagnostics associated with this AST
6273e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  typedef StoredDiagnostic *stored_diag_iterator;
6283e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  typedef const StoredDiagnostic *stored_diag_const_iterator;
6293e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  stored_diag_const_iterator stored_diag_begin() const {
630405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics.begin();
631405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  }
6323e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  stored_diag_iterator stored_diag_begin() {
6333e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis    return StoredDiagnostics.begin();
6343e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  }
6353e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  stored_diag_const_iterator stored_diag_end() const {
6363e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis    return StoredDiagnostics.end();
6373e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  }
6383e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  stored_diag_iterator stored_diag_end() {
639405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics.end();
640405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  }
641405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  unsigned stored_diag_size() const { return StoredDiagnostics.size(); }
6423e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis
6433e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  stored_diag_iterator stored_diag_afterDriver_begin() {
6443e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis    if (NumStoredDiagnosticsFromDriver > StoredDiagnostics.size())
6453e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis      NumStoredDiagnosticsFromDriver = 0;
6463e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis    return StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver;
647a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  }
648a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
64987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  typedef std::vector<CachedCodeCompletionResult>::iterator
65087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    cached_completion_iterator;
65187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
65287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  cached_completion_iterator cached_completion_begin() {
65387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    return CachedCompletionResults.begin();
65487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
65587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
65687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  cached_completion_iterator cached_completion_end() {
65787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    return CachedCompletionResults.end();
65887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
65987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
66087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  unsigned cached_completion_size() const {
66187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    return CachedCompletionResults.size();
66287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
663389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
664632dcc92f60ab7f806a89c5bca3a0951763a9219Argyrios Kyrtzidis  /// \brief Returns an iterator range for the local preprocessing entities
665632dcc92f60ab7f806a89c5bca3a0951763a9219Argyrios Kyrtzidis  /// of the local Preprocessor, if this is a parsed source file, or the loaded
666632dcc92f60ab7f806a89c5bca3a0951763a9219Argyrios Kyrtzidis  /// preprocessing entities of the primary module if this is an AST file.
667632dcc92f60ab7f806a89c5bca3a0951763a9219Argyrios Kyrtzidis  std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
668632dcc92f60ab7f806a89c5bca3a0951763a9219Argyrios Kyrtzidis    getLocalPreprocessingEntities() const;
669632dcc92f60ab7f806a89c5bca3a0951763a9219Argyrios Kyrtzidis
6702093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis  /// \brief Type for a function iterating over a number of declarations.
6712093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis  /// \returns true to continue iteration and false to abort.
67295c579cae01118eadd311d445ff7f491d0011fb0Argyrios Kyrtzidis  typedef bool (*DeclVisitorFn)(void *context, const Decl *D);
6732093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis
6742093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis  /// \brief Iterate over local declarations (locally parsed if this is a parsed
6752093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis  /// source file or the loaded declarations of the primary module if this is an
6762093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis  /// AST file).
6772093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis  /// \returns true if the iteration was complete or false if it was aborted.
67895c579cae01118eadd311d445ff7f491d0011fb0Argyrios Kyrtzidis  bool visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn);
6792093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis
6803da76bfbd24b2747ceb72d1918bbbe3123dac841Argyrios Kyrtzidis  /// \brief Get the PCH file if one was included.
6813da76bfbd24b2747ceb72d1918bbbe3123dac841Argyrios Kyrtzidis  const FileEntry *getPCHFile();
6823da76bfbd24b2747ceb72d1918bbbe3123dac841Argyrios Kyrtzidis
68362288edde26ff4af9fc079c979a0e1bdc577ce9dArgyrios Kyrtzidis  /// \brief Returns true if the ASTUnit was constructed from a serialized
68462288edde26ff4af9fc079c979a0e1bdc577ce9dArgyrios Kyrtzidis  /// module file.
68562288edde26ff4af9fc079c979a0e1bdc577ce9dArgyrios Kyrtzidis  bool isModuleFile();
68662288edde26ff4af9fc079c979a0e1bdc577ce9dArgyrios Kyrtzidis
687686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  llvm::MemoryBuffer *getBufferForFile(StringRef Filename,
6886bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                       std::string *ErrorStr = nullptr);
689389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
690467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  /// \brief Determine what kind of translation unit this AST represents.
691467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  TranslationUnitKind getTranslationUnitKind() const { return TUKind; }
692df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
6934db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  /// \brief A mapping from a file name to the memory buffer that stores the
6944db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  /// remapped contents of that file.
695651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  typedef std::pair<std::string, const llvm::MemoryBuffer *> RemappedFile;
696b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis
697b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  /// \brief Create a ASTUnit. Gets ownership of the passed CompilerInvocation.
698b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  static ASTUnit *create(CompilerInvocation *CI,
699c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith                         IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
700ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis                         bool CaptureDiagnostics,
701ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis                         bool UserFilesAreVolatile);
702b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis
7033c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \brief Create a ASTUnit from an AST file.
7040853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
7053c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \param Filename - The AST file to load.
7060853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
7075262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
7085262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
70931b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ///
7103c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \returns - The initialized ASTUnit or null if the AST failed to load.
7113c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  static ASTUnit *LoadFromASTFile(const std::string &Filename,
712c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith                              IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
713389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis                                  const FileSystemOptions &FileSystemOpts,
7145cf48766d626ff6b223acc9d4b7e415ca8480836Ted Kremenek                                  bool OnlyLocalDecls = false,
715651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                  ArrayRef<RemappedFile> RemappedFiles = None,
716bef35c91b594f66216f4aab303b71a6c5ab7abcfArgyrios Kyrtzidis                                  bool CaptureDiagnostics = false,
717ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis                                  bool AllowPCHWithCompilerErrors = false,
718ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis                                  bool UserFilesAreVolatile = false);
719521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
7204cd912aa94656697a44c3ebb159f05060300524eDouglas Gregorprivate:
7214cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \brief Helper function for \c LoadFromCompilerInvocation() and
7224cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \c LoadFromCommandLine(), which loads an AST from a compiler invocation.
7234cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  ///
7244cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \param PrecompilePreamble Whether to precompile the preamble of this
7254cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// translation unit, to improve the performance of reparsing.
7264cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  ///
7274cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \returns \c true if a catastrophic failure occurred (which means that the
7284cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \c ASTUnit itself is invalid), or \c false otherwise.
7294cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  bool LoadFromCompilerInvocation(bool PrecompilePreamble);
7304cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor
7314cd912aa94656697a44c3ebb159f05060300524eDouglas Gregorpublic:
7324cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor
733d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// \brief Create an ASTUnit from a source file, via a CompilerInvocation
734d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// object, by invoking the optionally provided ASTFrontendAction.
735d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  ///
736d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// \param CI - The compiler invocation to use; it must have exactly one input
737d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
738d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  ///
739d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// \param Diags - The diagnostics engine to use for reporting errors; its
740d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// lifetime is expected to extend past that of the returned ASTUnit.
741d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  ///
742d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// \param Action - The ASTFrontendAction to invoke. Its ownership is not
743063820655db8121f0022a7c51458463c7250324cBenjamin Kramer  /// transferred.
744abb5afadd11b19568359880faa5d966a4e33551cArgyrios Kyrtzidis  ///
745abb5afadd11b19568359880faa5d966a4e33551cArgyrios Kyrtzidis  /// \param Unit - optionally an already created ASTUnit. Its ownership is not
746063820655db8121f0022a7c51458463c7250324cBenjamin Kramer  /// transferred.
7476f3ce979a7748fd117e6473d6272b16d643b6262Argyrios Kyrtzidis  ///
7486f3ce979a7748fd117e6473d6272b16d643b6262Argyrios Kyrtzidis  /// \param Persistent - if true the returned ASTUnit will be complete.
7496f3ce979a7748fd117e6473d6272b16d643b6262Argyrios Kyrtzidis  /// false means the caller is only interested in getting info through the
7506f3ce979a7748fd117e6473d6272b16d643b6262Argyrios Kyrtzidis  /// provided \see Action.
751e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  ///
752e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// \param ErrAST - If non-null and parsing failed without any AST to return
753e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// (e.g. because the PCH could not be loaded), this accepts the ASTUnit
754e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// mainly to allow the caller to see the diagnostics.
755e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// This will only receive an ASTUnit if a new one was created. If an already
75670517ca5c07c4b41ff8662b94ee22047b0299f8cDmitri Gribenko  /// created ASTUnit was passed in \p Unit then the caller can check that.
757e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  ///
758651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  static ASTUnit *LoadFromCompilerInvocationAction(
759651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      CompilerInvocation *CI, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
7606bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      ASTFrontendAction *Action = nullptr, ASTUnit *Unit = nullptr,
7616bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      bool Persistent = true, StringRef ResourceFilesPath = StringRef(),
7626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      bool OnlyLocalDecls = false, bool CaptureDiagnostics = false,
7636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      bool PrecompilePreamble = false, bool CacheCodeCompletionResults = false,
764651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      bool IncludeBriefCommentsInCodeCompletion = false,
7656bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      bool UserFilesAreVolatile = false,
7666bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      std::unique_ptr<ASTUnit> *ErrAST = nullptr);
767d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis
768521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
769521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// CompilerInvocation object.
770521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
771521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// \param CI - The compiler invocation to use; it must have exactly one input
772f7acc37450d59ef751df73acb91de73850cc6517Daniel Dunbar  /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
773521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
7745262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
7755262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
776521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  //
777521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
778521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // shouldn't need to specify them at construction time.
7796bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  static std::unique_ptr<ASTUnit> LoadFromCompilerInvocation(
7806bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      CompilerInvocation *CI, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
7816bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      bool OnlyLocalDecls = false, bool CaptureDiagnostics = false,
7826bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      bool PrecompilePreamble = false, TranslationUnitKind TUKind = TU_Complete,
7836bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      bool CacheCodeCompletionResults = false,
7846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      bool IncludeBriefCommentsInCodeCompletion = false,
7856bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      bool UserFilesAreVolatile = false);
786521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
7877b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
7887b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// arguments, which must specify exactly one source file.
7897b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
7907b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgBegin - The beginning of the argument vector.
7917b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
7927b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgEnd - The end of the argument vector.
7937b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
7945262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
7955262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
7967b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
797869824e87940f97b87064db2df2861e82e08a8c6Daniel Dunbar  /// \param ResourceFilesPath - The path to the compiler resource files.
798e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  ///
799e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// \param ErrAST - If non-null and parsing failed without any AST to return
800e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// (e.g. because the PCH could not be loaded), this accepts the ASTUnit
801e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// mainly to allow the caller to see the diagnostics.
802e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  ///
8037b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
8047b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // shouldn't need to specify them at construction time.
805651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  static ASTUnit *LoadFromCommandLine(
806651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      const char **ArgBegin, const char **ArgEnd,
807651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath,
808651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      bool OnlyLocalDecls = false, bool CaptureDiagnostics = false,
809651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      ArrayRef<RemappedFile> RemappedFiles = None,
810651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      bool RemappedFilesKeepOriginalName = true,
811651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      bool PrecompilePreamble = false, TranslationUnitKind TUKind = TU_Complete,
812651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      bool CacheCodeCompletionResults = false,
813651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      bool IncludeBriefCommentsInCodeCompletion = false,
814651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      bool AllowPCHWithCompilerErrors = false, bool SkipFunctionBodies = false,
815651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      bool UserFilesAreVolatile = false, bool ForSerialization = false,
8166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      std::unique_ptr<ASTUnit> *ErrAST = nullptr);
817651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
818abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \brief Reparse the source files using the same command-line options that
819abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// were originally used to produce this translation unit.
820abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  ///
821abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \returns True if a failure occurred that causes the ASTUnit not to
822abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// contain any translation-unit information, false otherwise.
823651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool Reparse(ArrayRef<RemappedFile> RemappedFiles = None);
8241abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor
8251abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  /// \brief Perform code completion at the given file, line, and
8261abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  /// column within this translation unit.
8271abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  ///
828cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param File The file in which code completion will occur.
829cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
830cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param Line The line at which code completion will occur.
831cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
832cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param Column The column at which code completion will occur.
833cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
834cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param IncludeMacros Whether to include macros in the code-completion
835cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// results.
836cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
837cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param IncludeCodePatterns Whether to include code patterns (such as a
838cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// for loop) in the code-completion results.
8391abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  ///
840d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  /// \param IncludeBriefComments Whether to include brief documentation within
841d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  /// the set of code completions returned.
842d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  ///
8432283d79155a3e82442fce124ce5fd704ca138801Douglas Gregor  /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, StoredDiagnostics, and
8442283d79155a3e82442fce124ce5fd704ca138801Douglas Gregor  /// OwnedBuffers parameters are all disgusting hacks. They will go away.
845686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void CodeComplete(StringRef File, unsigned Line, unsigned Column,
846651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                    ArrayRef<RemappedFile> RemappedFiles,
847cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor                    bool IncludeMacros, bool IncludeCodePatterns,
848d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko                    bool IncludeBriefComments,
8491abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    CodeCompleteConsumer &Consumer,
850d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                    DiagnosticsEngine &Diag, LangOptions &LangOpts,
8511abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    SourceManager &SourceMgr, FileManager &FileMgr,
852686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                    SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
853686775deca8b8685eb90801495880e3abdd844c2Chris Lattner              SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers);
8547ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor
8557ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor  /// \brief Save this translation unit to a file with the given name.
8567ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor  ///
857e6d22027bc22d3767a2acdc80299c76b91159867Argyrios Kyrtzidis  /// \returns true if there was a file error or false if the save was
858e6d22027bc22d3767a2acdc80299c76b91159867Argyrios Kyrtzidis  /// successful.
859e6d22027bc22d3767a2acdc80299c76b91159867Argyrios Kyrtzidis  bool Save(StringRef File);
860b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis
861b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  /// \brief Serialize this translation unit with the given output stream.
862b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  ///
863b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  /// \returns True if an error occurred, false otherwise.
8648cc488fefb2fb04bc8d5398da29f0182f97934cfChris Lattner  bool serialize(raw_ostream &OS);
865651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
866651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path,
867651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                              Module::NameVisibilityKind Visibility,
868651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                              bool IsInclusionDirective) override {
8696aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    // ASTUnit doesn't know how to load modules (not that this matters).
870463d90986ec54c62bf8fe31193ef5db701db48a5Douglas Gregor    return ModuleLoadResult();
8716aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  }
872ca2ab45341c448284cf93770018c717810575f86Douglas Gregor
873651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void makeModuleVisible(Module *Mod, Module::NameVisibilityKind Visibility,
874651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                         SourceLocation ImportLoc, bool Complain) override {}
875ca2ab45341c448284cf93770018c717810575f86Douglas Gregor
8766bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
8776bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    { return nullptr; }
8786bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
8796bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    { return 0; };
8800853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis};
8810853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
8820853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis} // namespace clang
8830853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
8840853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#endif
885