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;
59a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarclass PCHContainerOperations;
60a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarclass PCHContainerReader;
61521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass SourceManager;
62521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass TargetInfo;
63d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidisclass ASTFrontendAction;
64900ab95e12bb7483971640a91075699eec391804Argyrios Kyrtzidisclass ASTDeserializationListener;
650853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
663c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl/// \brief Utility class for loading a ASTContext from an AST file.
670853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis///
686aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregorclass ASTUnit : public ModuleLoader {
69651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinespublic:
70651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  struct StandaloneFixIt {
71651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    std::pair<unsigned, unsigned> RemoveRange;
72651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    std::pair<unsigned, unsigned> InsertFromRange;
73651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    std::string CodeToInsert;
74651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    bool BeforePreviousInsertions;
75651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  };
76651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
77651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  struct StandaloneDiagnostic {
78651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    unsigned ID;
79651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    DiagnosticsEngine::Level Level;
80651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    std::string Message;
81651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    std::string Filename;
82651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    unsigned LocOffset;
83651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    std::vector<std::pair<unsigned, unsigned> > Ranges;
84651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    std::vector<StandaloneFixIt> FixIts;
85651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  };
86651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
87788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregorprivate:
88c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  std::shared_ptr<LangOptions>            LangOpts;
89c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<DiagnosticsEngine>   Diagnostics;
90c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<FileManager>         FileMgr;
91c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<SourceManager>       SourceMgr;
92651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::unique_ptr<HeaderSearch>           HeaderInfo;
93c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<TargetInfo>          Target;
94c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<Preprocessor>        PP;
95c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<ASTContext>          Ctx;
96c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  std::shared_ptr<TargetOptions>          TargetOpts;
97c042edd54face617a3b9d0b4b9d5a3ff229d0f48Douglas Gregor  IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts;
98651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  IntrusiveRefCntPtr<ASTReader> Reader;
993b7deda7137e62810a810ce25b062927a9fc7c71Argyrios Kyrtzidis  bool HadModuleLoaderFatalFailure;
100389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
101900ab95e12bb7483971640a91075699eec391804Argyrios Kyrtzidis  struct ASTWriterData;
102651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::unique_ptr<ASTWriterData> WriterData;
103900ab95e12bb7483971640a91075699eec391804Argyrios Kyrtzidis
104389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  FileSystemOptions FileSystemOpts;
105389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
106914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// \brief The AST consumer that received information about the translation
107914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// unit as it was parsed or loaded.
108651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::unique_ptr<ASTConsumer> Consumer;
109651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
110914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// \brief The semantic analysis object used to type-check the translation
111914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// unit.
112651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::unique_ptr<Sema> TheSema;
113651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
114807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// Optional owned invocation, just used to make the invocation used in
115807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// LoadFromCommandLine available.
116c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith  IntrusiveRefCntPtr<CompilerInvocation> Invocation;
117914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
1187d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // OnlyLocalDecls - when true, walking this AST should only visit declarations
1197d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // that come from the AST itself, not from included precompiled headers.
1207d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // FIXME: This is temporary; eventually, CIndex will always do this.
1217d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool                              OnlyLocalDecls;
122c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
123abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \brief Whether to capture any diagnostics produced.
124abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  bool CaptureDiagnostics;
125e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor
126df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  /// \brief Track whether the main file was loaded from an AST or not.
127c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool MainFileIsAST;
128c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
129467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  /// \brief What kind of translation unit this AST represents.
130467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  TranslationUnitKind TUKind;
131df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
132213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor  /// \brief Whether we should time each operation.
133213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor  bool WantTiming;
13415727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis
13515727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  /// \brief Whether the ASTUnit should delete the remapped buffers.
13615727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  bool OwnsRemappedFileBuffers;
137213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor
138f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// Track the top-level decls which appeared in an ASTUnit which was loaded
139f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// from a source file.
140f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  //
141f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // FIXME: This is just an optimization hack to avoid deserializing large parts
142f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // of a PCH file when using the Index library on an ASTUnit loaded from
143f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // source. In the long term we should make the Index library use efficient and
144f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // more scalable search mechanisms.
145f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  std::vector<Decl*> TopLevelDecls;
146332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis
147332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  /// \brief Sorted (by file offset) vector of pairs of file offset/Decl.
148332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  typedef SmallVector<std::pair<unsigned, Decl *>, 64> LocDeclsTy;
149332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  typedef llvm::DenseMap<FileID, LocDeclsTy *> FileDeclsTy;
150332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis
151332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  /// \brief Map from FileID to the file-level declarations that it contains.
152332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  /// The files and decls are only local (and non-preamble) ones.
153332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  FileDeclsTy FileDecls;
15489d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
15568d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  /// The name of the original source file used to generate this ASTUnit.
15668d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  std::string OriginalSourceFile;
15768d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar
158f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief The set of diagnostics produced when creating the preamble.
159651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  SmallVector<StandaloneDiagnostic, 4> PreambleDiagnostics;
160f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
161a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  /// \brief The set of diagnostics produced when creating this
162a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  /// translation unit.
163686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
164a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
165e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// \brief The set of diagnostics produced when failing to parse, e.g. due
166e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// to failure to load the PCH.
167e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  SmallVector<StoredDiagnostic, 4> FailedParseDiagnostics;
168e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis
1694cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \brief The number of stored diagnostics that come from the driver
1704cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// itself.
1714cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  ///
1724cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// Diagnostics that come from the driver are retained from one parse to
1734cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// the next.
1744cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  unsigned NumStoredDiagnosticsFromDriver;
1754cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor
176eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// \brief Counter that determines when we want to try building a
177eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// precompiled preamble.
178eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  ///
179eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// If zero, we will never build a precompiled preamble. Otherwise,
180eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// it's treated as a counter that decrements each time we reparse
181eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// without the benefit of a precompiled preamble. When it hits 1,
182eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// we'll attempt to rebuild the precompiled header. This way, if
183eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// building the precompiled preamble fails, we won't try again for
184eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// some number of calls.
185eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  unsigned PreambleRebuildCounter;
1861872b3153a388b3a548c9f699cbc348128059be9Ted Kremenek
187ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidispublic:
188507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  class PreambleData {
189507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    const FileEntry *File;
190507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    std::vector<char> Buffer;
191507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    mutable unsigned NumLines;
192507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
193507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  public:
1946bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    PreambleData() : File(nullptr), NumLines(0) { }
195507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
196507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    void assign(const FileEntry *F, const char *begin, const char *end) {
197507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      File = F;
198507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      Buffer.assign(begin, end);
199507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      NumLines = 0;
200507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    }
201507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
2026bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    void clear() { Buffer.clear(); File = nullptr; NumLines = 0; }
203507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
204507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    size_t size() const { return Buffer.size(); }
205507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    bool empty() const { return Buffer.empty(); }
206507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
207507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    const char *getBufferStart() const { return &Buffer[0]; }
208507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
209507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    unsigned getNumLines() const {
210507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      if (NumLines)
211507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis        return NumLines;
212507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      countLines();
213507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis      return NumLines;
214507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    }
215507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
216ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis    SourceRange getSourceRange(const SourceManager &SM) const {
217ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis      SourceLocation FileLoc = SM.getLocForStartOfFile(SM.getPreambleFileID());
218ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis      return SourceRange(FileLoc, FileLoc.getLocWithOffset(size()-1));
219ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis    }
220ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
221507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  private:
222507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    void countLines() const;
223507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  };
224507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
225ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  const PreambleData &getPreambleData() const {
226ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis    return Preamble;
227ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  }
228ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
229651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// Data used to determine if a file used in the preamble has been changed.
230651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  struct PreambleFileHash {
231651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// All files have size set.
232651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    off_t Size;
233651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
234651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// Modification time is set for files that are on disk.  For memory
235651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// buffers it is zero.
236651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    time_t ModTime;
237651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
238651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// Memory buffers have MD5 instead of modification time.  We don't
239651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// compute MD5 for on-disk files because we hope that modification time is
240651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// enough to tell if the file was changed.
241651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    llvm::MD5::MD5Result MD5;
242651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
243651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    static PreambleFileHash createForFile(off_t Size, time_t ModTime);
244651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    static PreambleFileHash
245651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    createForMemoryBuffer(const llvm::MemoryBuffer *Buffer);
246651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
247651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    friend bool operator==(const PreambleFileHash &LHS,
248651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                           const PreambleFileHash &RHS);
249651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
250651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    friend bool operator!=(const PreambleFileHash &LHS,
251651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                           const PreambleFileHash &RHS) {
252651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return !(LHS == RHS);
253651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
254651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  };
255ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
256651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesprivate:
257175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \brief The contents of the preamble that has been precompiled to
258175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \c PreambleFile.
259507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  PreambleData Preamble;
260175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
261f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// \brief Whether the preamble ends at the start of a new line.
262f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  ///
263f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// Used to inform the lexer as to whether it's starting at the beginning of
264f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// a line after skipping the preamble.
265f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  bool PreambleEndsAtStartOfLine;
266cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
267cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// \brief Keeps track of the files that were used when computing the
268cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// preamble, with both their buffer size and their modification time.
269cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  ///
270cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// If any of the files have changed from one compile to the next,
271cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// the preamble must be thrown away.
272651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::StringMap<PreambleFileHash> FilesInPreamble;
273cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
27428233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// \brief When non-NULL, this is the buffer used to store the contents of
27528233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// the main file when it has been padded for use with the precompiled
27628233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// preamble.
277176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  std::unique_ptr<llvm::MemoryBuffer> SavedMainFileBuffer;
278c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
279671947b18dba342f9aba022ee992babef325a833Douglas Gregor  /// \brief When non-NULL, this is the buffer used to store the
280671947b18dba342f9aba022ee992babef325a833Douglas Gregor  /// contents of the preamble when it has been padded to build the
281671947b18dba342f9aba022ee992babef325a833Douglas Gregor  /// precompiled preamble.
282176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  std::unique_ptr<llvm::MemoryBuffer> PreambleBuffer;
283671947b18dba342f9aba022ee992babef325a833Douglas Gregor
284c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// \brief The number of warnings that occurred while parsing the preamble.
285c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  ///
286d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  /// This value will be used to restore the state of the \c DiagnosticsEngine
287d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  /// object when re-using the precompiled preamble. Note that only the
288c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// number of warnings matters, since we will not save the preamble
289c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// when any errors are present.
290c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  unsigned NumWarningsInPreamble;
291c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
2923c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \brief A list of the serialization ID numbers for each of the top-level
293eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// declarations parsed within the precompiled preamble.
2948538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
29589d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
29687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief Whether we should be caching code-completion results.
297d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  bool ShouldCacheCodeCompletionResults : 1;
298d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko
299d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  /// \brief Whether to include brief documentation within the set of code
300d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  /// completions cached.
301d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  bool IncludeBriefCommentsInCodeCompletion : 1;
302ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis
303ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis  /// \brief True if non-system source files should be treated as volatile
304ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis  /// (likely to change while trying to use them).
305ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis  bool UserFilesAreVolatile : 1;
3063e3cd93b2fd9644e970c389e715c13883faf68b6Douglas Gregor
3073e3cd93b2fd9644e970c389e715c13883faf68b6Douglas Gregor  /// \brief The language options used when we load an AST file.
3083e3cd93b2fd9644e970c389e715c13883faf68b6Douglas Gregor  LangOptions ASTFileLangOpts;
3093e3cd93b2fd9644e970c389e715c13883faf68b6Douglas Gregor
310176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  static void ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
311e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor                             ASTUnit &AST, bool CaptureDiagnostics);
312e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor
313651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void TranslateStoredDiagnostics(FileManager &FileMgr,
314f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                                  SourceManager &SrcMan,
315651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                      const SmallVectorImpl<StandaloneDiagnostic> &Diags,
316686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                            SmallVectorImpl<StoredDiagnostic> &Out);
317f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
318332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  void clearFileLevelDecls();
319332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis
32087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregorpublic:
32187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief A cached code-completion result, which may be introduced in one of
32287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// many different contexts.
32387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  struct CachedCodeCompletionResult {
32487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief The code-completion string corresponding to this completion
32587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// result.
32687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    CodeCompletionString *Completion;
32787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
32887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief A bitmask that indicates which code-completion contexts should
32987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// contain this completion result.
33087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    ///
331026b3588b9916f3fcb20ad2f02ca5472cbbdc5d7Richard Smith    /// The bits in the bitmask correspond to the values of
332026b3588b9916f3fcb20ad2f02ca5472cbbdc5d7Richard Smith    /// CodeCompleteContext::Kind. To map from a completion context kind to a
333026b3588b9916f3fcb20ad2f02ca5472cbbdc5d7Richard Smith    /// bit, shift 1 by that number of bits. Many completions can occur in
334026b3588b9916f3fcb20ad2f02ca5472cbbdc5d7Richard Smith    /// several different contexts.
335026b3588b9916f3fcb20ad2f02ca5472cbbdc5d7Richard Smith    uint64_t ShowInContexts;
33687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
33787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief The priority given to this code-completion result.
33887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    unsigned Priority;
33987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
34087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief The libclang cursor kind corresponding to this code-completion
34187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// result.
34287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    CXCursorKind Kind;
3431827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor
34458ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor    /// \brief The availability of this code-completion result.
34558ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor    CXAvailabilityKind Availability;
34658ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor
3471827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor    /// \brief The simplified type class for a non-macro completion result.
3481827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor    SimplifiedTypeClass TypeClass;
349f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
350f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// \brief The type of a non-macro completion result, stored as a unique
351f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// integer used by the string map of cached completion types.
352f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    ///
353f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// This value will be zero if the type is not known, or a unique value
354f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// determined by the formatted type string. Se \c CachedCompletionTypes
355f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// for more information.
356f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    unsigned Type;
35787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  };
35887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
359f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// \brief Retrieve the mapping from formatted type names to unique type
360f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// identifiers.
361f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  llvm::StringMap<unsigned> &getCachedCompletionTypes() {
362f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    return CachedCompletionTypes;
363f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  }
364f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
36548601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  /// \brief Retrieve the allocator used to cache global code completions.
366c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith  IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
36748601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  getCachedCompletionAllocator() {
36848601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor    return CachedCompletionAllocator;
36948601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor  }
37028a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis
37128a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis  CodeCompletionTUInfo &getCodeCompletionTUInfo() {
37228a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis    if (!CCTUInfo)
37328a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis      CCTUInfo.reset(new CodeCompletionTUInfo(
37428a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis                                            new GlobalCodeCompletionAllocator));
37528a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis    return *CCTUInfo;
3768fa0a80b4482ad94e82c4a19e23de17fd69140b5Douglas Gregor  }
37728a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis
37887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregorprivate:
379218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  /// \brief Allocator used to store cached code completions.
380c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith  IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
38148601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor    CachedCompletionAllocator;
382651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
383651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::unique_ptr<CodeCompletionTUInfo> CCTUInfo;
384218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor
38587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief The set of cached code-completion results.
38687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  std::vector<CachedCodeCompletionResult> CachedCompletionResults;
38787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
388f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// \brief A mapping from the formatted type name to a unique number for that
389f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// type, which is used for type equality comparisons.
390f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  llvm::StringMap<unsigned> CachedCompletionTypes;
391f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
3929b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief A string hash of the top-level declaration and macro definition
3939b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// names processed the last time that we reparsed the file.
394727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  ///
3959b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// This hash value is used to determine when we need to refresh the
3969b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// global code-completion cache.
3979b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned CompletionCacheTopLevelHashValue;
398727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor
3999b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief A string hash of the top-level declaration and macro definition
4009b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// names processed the last time that we reparsed the precompiled preamble.
401727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  ///
4029b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// This hash value is used to determine when we need to refresh the
4039b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// global code-completion cache after a rebuild of the precompiled preamble.
4049b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned PreambleTopLevelHashValue;
405ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
4069b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief The current hash value for the top-level declaration and macro
4079b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// definition names
4089b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned CurrentTopLevelHashValue;
4099b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor
410ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  /// \brief Bit used by CIndex to mark when a translation unit may be in an
411ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  /// inconsistent state, and is not safe to free.
412ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  unsigned UnsafeToFree : 1;
413ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
414c4421e966d77a18f815284175b3fcbb46f36fa39Douglas Gregor  /// \brief Cache any "global" code-completion results, so that we can avoid
415c4421e966d77a18f815284175b3fcbb46f36fa39Douglas Gregor  /// recomputing them with each completion.
41687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  void CacheCodeCompletionResults();
41787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
41887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief Clear out and deallocate
41987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  void ClearCachedCompletionResults();
42087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
4210e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  ASTUnit(const ASTUnit &) = delete;
4220e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  void operator=(const ASTUnit &) = delete;
423bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
4243687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor  explicit ASTUnit(bool MainFileIsAST);
4258b96253907c47141af0b7b2a44a368748d006a87Douglas Gregor
426abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  void CleanTemporaryFiles();
427a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  bool Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
428a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar             std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer);
429176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
430176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  struct ComputedPreamble {
431176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    llvm::MemoryBuffer *Buffer;
432176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    std::unique_ptr<llvm::MemoryBuffer> Owner;
433176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    unsigned Size;
434176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    bool PreambleEndsAtStartOfLine;
435176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    ComputedPreamble(llvm::MemoryBuffer *Buffer,
436176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                     std::unique_ptr<llvm::MemoryBuffer> Owner, unsigned Size,
437176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                     bool PreambleEndsAtStartOfLine)
438176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        : Buffer(Buffer), Owner(std::move(Owner)), Size(Size),
439176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines          PreambleEndsAtStartOfLine(PreambleEndsAtStartOfLine) {}
440176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    ComputedPreamble(ComputedPreamble &&C)
441176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        : Buffer(C.Buffer), Owner(std::move(C.Owner)), Size(C.Size),
442176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines          PreambleEndsAtStartOfLine(C.PreambleEndsAtStartOfLine) {}
443176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  };
444176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ComputedPreamble ComputePreamble(CompilerInvocation &Invocation,
445176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                   unsigned MaxLines);
446176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
447176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  std::unique_ptr<llvm::MemoryBuffer> getMainBufferWithPrecompiledPreamble(
448a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      std::shared_ptr<PCHContainerOperations> PCHContainerOps,
449176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      const CompilerInvocation &PreambleInvocationIn, bool AllowRebuild = true,
450176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      unsigned MaxLines = 0);
451eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  void RealizeTopLevelDeclsFromPreamble();
452e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis
453e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// \brief Transfers ownership of the objects (like SourceManager) from
454e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// \param CI to this ASTUnit.
455e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  void transferASTDataFromCompilerInstance(CompilerInstance &CI);
456e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis
457a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// \brief Allows us to assert that ASTUnit is not being used concurrently,
458a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// which is not supported.
459a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  ///
460a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// Clients should create instances of the ConcurrencyCheck class whenever
461a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// using the ASTUnit in a way that isn't intended to be concurrent, which is
462a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// just about any usage.
463a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  /// Becomes a noop in release mode; only useful for debug mode checking.
464a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  class ConcurrencyState {
465a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    void *Mutex; // a llvm::sys::MutexImpl in debug;
466a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis
467a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  public:
468a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    ConcurrencyState();
469a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    ~ConcurrencyState();
470a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis
471a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    void start();
472a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    void finish();
473a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  };
474a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis  ConcurrencyState ConcurrencyCheckValue;
475a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis
4760853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidispublic:
477bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  class ConcurrencyCheck {
478a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis    ASTUnit &Self;
479bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
480bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  public:
481bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    explicit ConcurrencyCheck(ASTUnit &Self)
482bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      : Self(Self)
483bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    {
484a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis      Self.ConcurrencyCheckValue.start();
485bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    }
486bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    ~ConcurrencyCheck() {
487a696ece1ac449a2b77e7c0a693b55cb10e9e2068Argyrios Kyrtzidis      Self.ConcurrencyCheckValue.finish();
488bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    }
489bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  };
490bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  friend class ConcurrencyCheck;
49158878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar
49258878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar  ~ASTUnit() override;
4930853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
494c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool isMainFileAST() const { return MainFileIsAST; }
495c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
496ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  bool isUnsafeToFree() const { return UnsafeToFree; }
497ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  void setUnsafeToFree(bool Value) { UnsafeToFree = Value; }
498ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
499d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  const DiagnosticsEngine &getDiagnostics() const { return *Diagnostics; }
500d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine &getDiagnostics()             { return *Diagnostics; }
5013687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor
502405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  const SourceManager &getSourceManager() const { return *SourceMgr; }
503405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor        SourceManager &getSourceManager()       { return *SourceMgr; }
5040853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
5054f32786ac45210143654390177105eb749b614e9Ted Kremenek  const Preprocessor &getPreprocessor() const { return *PP; }
5064f32786ac45210143654390177105eb749b614e9Ted Kremenek        Preprocessor &getPreprocessor()       { return *PP; }
5071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5084f32786ac45210143654390177105eb749b614e9Ted Kremenek  const ASTContext &getASTContext() const { return *Ctx; }
5094f32786ac45210143654390177105eb749b614e9Ted Kremenek        ASTContext &getASTContext()       { return *Ctx; }
5100853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
511abb5afadd11b19568359880faa5d966a4e33551cArgyrios Kyrtzidis  void setASTContext(ASTContext *ctx) { Ctx = ctx; }
5127fe90f3bfa6bd7f779ecfd4ba201a296a3a1c8cbArgyrios Kyrtzidis  void setPreprocessor(Preprocessor *pp);
513abb5afadd11b19568359880faa5d966a4e33551cArgyrios Kyrtzidis
514651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool hasSema() const { return (bool)TheSema; }
515914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  Sema &getSema() const {
516914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor    assert(TheSema && "ASTUnit does not have a Sema object!");
5176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return *TheSema;
5186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
5196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
5206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const LangOptions &getLangOpts() const {
5216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    assert(LangOpts && " ASTUnit does not have language options");
5226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return *LangOpts;
523914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  }
524914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
525405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  const FileManager &getFileManager() const { return *FileMgr; }
526405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor        FileManager &getFileManager()       { return *FileMgr; }
527f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
528389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
529389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
530942fbddccc1b5540336674e577abb59e5c231778Rafael Espindola  StringRef getOriginalSourceFileName() {
531badb3f6971bec4a7341ed152d367f27d572682e0Rafael Espindola    return OriginalSourceFile;
532badb3f6971bec4a7341ed152d367f27d572682e0Rafael Espindola  }
533b85bca2676b433ae555db09de4dd2823ff13b856Zhongxing Xu
5347eca8d2262a26eedb2ff3cc0fd11be2494b17cc1Argyrios Kyrtzidis  ASTMutationListener *getASTMutationListener();
535900ab95e12bb7483971640a91075699eec391804Argyrios Kyrtzidis  ASTDeserializationListener *getDeserializationListener();
536900ab95e12bb7483971640a91075699eec391804Argyrios Kyrtzidis
537313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// \brief Add a temporary file that the ASTUnit depends on.
538313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  ///
539313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// This file will be erased when the ASTUnit is destroyed.
540b804cb315cfeebc4d1c4d2d023ffb08440f77254Rafael Espindola  void addTemporaryFile(StringRef TempFile);
541b804cb315cfeebc4d1c4d2d023ffb08440f77254Rafael Espindola
5427d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
543f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
54415727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  bool getOwnsRemappedFileBuffers() const { return OwnsRemappedFileBuffers; }
54515727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis  void setOwnsRemappedFileBuffers(bool val) { OwnsRemappedFileBuffers = val; }
54615727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis
547686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef getMainFileName() const;
548213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor
54944f65a587ee39c20e715c9df411c864d328c71b5Argyrios Kyrtzidis  /// \brief If this ASTUnit came from an AST file, returns the filename for it.
55044f65a587ee39c20e715c9df411c864d328c71b5Argyrios Kyrtzidis  StringRef getASTFileName() const;
55144f65a587ee39c20e715c9df411c864d328c71b5Argyrios Kyrtzidis
552eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  typedef std::vector<Decl *>::iterator top_level_iterator;
553eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
554eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  top_level_iterator top_level_begin() {
555f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
556eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    if (!TopLevelDeclsInPreamble.empty())
557eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor      RealizeTopLevelDeclsFromPreamble();
558eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDecls.begin();
559f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  }
560eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
561eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  top_level_iterator top_level_end() {
562f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
563eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    if (!TopLevelDeclsInPreamble.empty())
564eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor      RealizeTopLevelDeclsFromPreamble();
565eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDecls.end();
566eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
567eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
568eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  std::size_t top_level_size() const {
569eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
570eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDeclsInPreamble.size() + TopLevelDecls.size();
571eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
572eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
573eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  bool top_level_empty() const {
574eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
575eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty();
576eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
577eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
578eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// \brief Add a new top-level declaration.
579eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  void addTopLevelDecl(Decl *D) {
580eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    TopLevelDecls.push_back(D);
581eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
582eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
583332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  /// \brief Add a new local file-level declaration.
584332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis  void addFileLevelDecl(Decl *D);
585332cb9be88fd2d1b294a2fbc299c354e93be673cArgyrios Kyrtzidis
586dfb332d0081c6641d1dbae6a2aeff757c99cc740Argyrios Kyrtzidis  /// \brief Get the decls that are contained in a file in the Offset/Length
5871824d54df85a462ada812dadda18130f951d40f3Dmitri Gribenko  /// range. \p Length can be 0 to indicate a point at \p Offset instead of
588dfb332d0081c6641d1dbae6a2aeff757c99cc740Argyrios Kyrtzidis  /// a range.
589dfb332d0081c6641d1dbae6a2aeff757c99cc740Argyrios Kyrtzidis  void findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
590dfb332d0081c6641d1dbae6a2aeff757c99cc740Argyrios Kyrtzidis                           SmallVectorImpl<Decl *> &Decls);
591dfb332d0081c6641d1dbae6a2aeff757c99cc740Argyrios Kyrtzidis
592eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// \brief Add a new top-level declaration, identified by its ID in
593eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// the precompiled preamble.
5948538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  void addTopLevelDeclFromPreamble(serialization::DeclID D) {
595eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    TopLevelDeclsInPreamble.push_back(D);
596f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  }
597f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
5989b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// \brief Retrieve a reference to the current top-level name hash value.
5999b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  ///
6009b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  /// Note: This is used internally by the top-level tracking action
6019b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor  unsigned &getCurrentTopLevelHashValue() { return CurrentTopLevelHashValue; }
602507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
603507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// \brief Get the source location for the given file:line:col triplet.
604507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  ///
605507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// The difference with SourceManager::getLocation is that this method checks
606507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// whether the requested location points inside the precompiled preamble
607507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// in which case the returned source location will be a "loaded" one.
608507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  SourceLocation getLocation(const FileEntry *File,
609507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis                             unsigned Line, unsigned Col) const;
610507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
611507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// \brief Get the source location for the given file:offset pair.
612507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  SourceLocation getLocation(const FileEntry *File, unsigned Offset) const;
613ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
6141824d54df85a462ada812dadda18130f951d40f3Dmitri Gribenko  /// \brief If \p Loc is a loaded location from the preamble, returns
615ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  /// the corresponding local location of the main file, otherwise it returns
6161824d54df85a462ada812dadda18130f951d40f3Dmitri Gribenko  /// \p Loc.
617ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  SourceLocation mapLocationFromPreamble(SourceLocation Loc);
618ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
6191824d54df85a462ada812dadda18130f951d40f3Dmitri Gribenko  /// \brief If \p Loc is a local location of the main file but inside the
620ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  /// preamble chunk, returns the corresponding loaded location from the
6211824d54df85a462ada812dadda18130f951d40f3Dmitri Gribenko  /// preamble, otherwise it returns \p Loc.
622ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  SourceLocation mapLocationToPreamble(SourceLocation Loc);
623ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
624f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  bool isInPreambleFileID(SourceLocation Loc);
625f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  bool isInMainFileID(SourceLocation Loc);
626f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  SourceLocation getStartOfMainFileID();
627f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  SourceLocation getEndOfPreambleFileID();
628f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis
629e23fb90712233bdfa04387e48b54a7168e23cb3eDmitri Gribenko  /// \see mapLocationFromPreamble.
630ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  SourceRange mapRangeFromPreamble(SourceRange R) {
631ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis    return SourceRange(mapLocationFromPreamble(R.getBegin()),
632ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis                       mapLocationFromPreamble(R.getEnd()));
633ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  }
634ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis
635e23fb90712233bdfa04387e48b54a7168e23cb3eDmitri Gribenko  /// \see mapLocationToPreamble.
636ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  SourceRange mapRangeToPreamble(SourceRange R) {
637ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis    return SourceRange(mapLocationToPreamble(R.getBegin()),
638ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis                       mapLocationToPreamble(R.getEnd()));
639ee0f84fc84ed7de7975e102668d8e53a778f7a8cArgyrios Kyrtzidis  }
640788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor
641a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  // Retrieve the diagnostics associated with this AST
6423e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  typedef StoredDiagnostic *stored_diag_iterator;
6433e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  typedef const StoredDiagnostic *stored_diag_const_iterator;
6443e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  stored_diag_const_iterator stored_diag_begin() const {
645405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics.begin();
646405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  }
6473e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  stored_diag_iterator stored_diag_begin() {
6483e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis    return StoredDiagnostics.begin();
6493e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  }
6503e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  stored_diag_const_iterator stored_diag_end() const {
6513e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis    return StoredDiagnostics.end();
6523e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  }
6533e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  stored_diag_iterator stored_diag_end() {
654405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics.end();
655405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  }
656405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  unsigned stored_diag_size() const { return StoredDiagnostics.size(); }
6573e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis
6583e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis  stored_diag_iterator stored_diag_afterDriver_begin() {
6593e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis    if (NumStoredDiagnosticsFromDriver > StoredDiagnostics.size())
6603e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis      NumStoredDiagnosticsFromDriver = 0;
6613e9d32656a8a3a787f64f9beeef66d2d700eb85dArgyrios Kyrtzidis    return StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver;
662a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  }
663a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
66487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  typedef std::vector<CachedCodeCompletionResult>::iterator
66587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    cached_completion_iterator;
66687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
66787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  cached_completion_iterator cached_completion_begin() {
66887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    return CachedCompletionResults.begin();
66987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
67087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
67187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  cached_completion_iterator cached_completion_end() {
67287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    return CachedCompletionResults.end();
67387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
67487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
67587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  unsigned cached_completion_size() const {
67687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    return CachedCompletionResults.size();
67787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
678389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
679632dcc92f60ab7f806a89c5bca3a0951763a9219Argyrios Kyrtzidis  /// \brief Returns an iterator range for the local preprocessing entities
680632dcc92f60ab7f806a89c5bca3a0951763a9219Argyrios Kyrtzidis  /// of the local Preprocessor, if this is a parsed source file, or the loaded
681632dcc92f60ab7f806a89c5bca3a0951763a9219Argyrios Kyrtzidis  /// preprocessing entities of the primary module if this is an AST file.
6820e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  llvm::iterator_range<PreprocessingRecord::iterator>
6830e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  getLocalPreprocessingEntities() const;
684632dcc92f60ab7f806a89c5bca3a0951763a9219Argyrios Kyrtzidis
6852093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis  /// \brief Type for a function iterating over a number of declarations.
6862093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis  /// \returns true to continue iteration and false to abort.
68795c579cae01118eadd311d445ff7f491d0011fb0Argyrios Kyrtzidis  typedef bool (*DeclVisitorFn)(void *context, const Decl *D);
6882093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis
6892093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis  /// \brief Iterate over local declarations (locally parsed if this is a parsed
6902093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis  /// source file or the loaded declarations of the primary module if this is an
6912093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis  /// AST file).
6922093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis  /// \returns true if the iteration was complete or false if it was aborted.
69395c579cae01118eadd311d445ff7f491d0011fb0Argyrios Kyrtzidis  bool visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn);
6942093e0bc4e436b1b2791d5423fb3274dd37231b8Argyrios Kyrtzidis
6953da76bfbd24b2747ceb72d1918bbbe3123dac841Argyrios Kyrtzidis  /// \brief Get the PCH file if one was included.
6963da76bfbd24b2747ceb72d1918bbbe3123dac841Argyrios Kyrtzidis  const FileEntry *getPCHFile();
6973da76bfbd24b2747ceb72d1918bbbe3123dac841Argyrios Kyrtzidis
69862288edde26ff4af9fc079c979a0e1bdc577ce9dArgyrios Kyrtzidis  /// \brief Returns true if the ASTUnit was constructed from a serialized
69962288edde26ff4af9fc079c979a0e1bdc577ce9dArgyrios Kyrtzidis  /// module file.
70062288edde26ff4af9fc079c979a0e1bdc577ce9dArgyrios Kyrtzidis  bool isModuleFile();
70162288edde26ff4af9fc079c979a0e1bdc577ce9dArgyrios Kyrtzidis
702176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  std::unique_ptr<llvm::MemoryBuffer>
703176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  getBufferForFile(StringRef Filename, std::string *ErrorStr = nullptr);
704389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
705467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  /// \brief Determine what kind of translation unit this AST represents.
706467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  TranslationUnitKind getTranslationUnitKind() const { return TUKind; }
707df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
7084db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  /// \brief A mapping from a file name to the memory buffer that stores the
7094db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  /// remapped contents of that file.
710c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  typedef std::pair<std::string, llvm::MemoryBuffer *> RemappedFile;
711b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis
712b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  /// \brief Create a ASTUnit. Gets ownership of the passed CompilerInvocation.
713b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  static ASTUnit *create(CompilerInvocation *CI,
714c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith                         IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
715ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis                         bool CaptureDiagnostics,
716ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis                         bool UserFilesAreVolatile);
717b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis
7183c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \brief Create a ASTUnit from an AST file.
7190853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
7203c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \param Filename - The AST file to load.
7210853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
722a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  /// \param PCHContainerRdr - The PCHContainerOperations to use for loading and
723a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  /// creating modules.
7245262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
7255262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
72631b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ///
7273c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \returns - The initialized ASTUnit or null if the AST failed to load.
728176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  static std::unique_ptr<ASTUnit> LoadFromASTFile(
729a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      const std::string &Filename, const PCHContainerReader &PCHContainerRdr,
730a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
731a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      const FileSystemOptions &FileSystemOpts, bool UseDebugInfo = false,
732a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      bool OnlyLocalDecls = false, ArrayRef<RemappedFile> RemappedFiles = None,
733176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      bool CaptureDiagnostics = false, bool AllowPCHWithCompilerErrors = false,
734176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      bool UserFilesAreVolatile = false);
735521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
7364cd912aa94656697a44c3ebb159f05060300524eDouglas Gregorprivate:
7374cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \brief Helper function for \c LoadFromCompilerInvocation() and
7384cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \c LoadFromCommandLine(), which loads an AST from a compiler invocation.
7394cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  ///
740a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  /// \param PrecompilePreambleAfterNParses After how many parses the preamble
741a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  /// of this translation unit should be precompiled, to improve the performance
742a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  /// of reparsing. Set to zero to disable preambles.
7434cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  ///
7444cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \returns \c true if a catastrophic failure occurred (which means that the
7454cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \c ASTUnit itself is invalid), or \c false otherwise.
746a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  bool LoadFromCompilerInvocation(
747a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      std::shared_ptr<PCHContainerOperations> PCHContainerOps,
748a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      unsigned PrecompilePreambleAfterNParses);
749a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
7504cd912aa94656697a44c3ebb159f05060300524eDouglas Gregorpublic:
7514cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor
752d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// \brief Create an ASTUnit from a source file, via a CompilerInvocation
753d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// object, by invoking the optionally provided ASTFrontendAction.
754d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  ///
755d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// \param CI - The compiler invocation to use; it must have exactly one input
756d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
757d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  ///
758a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  /// \param PCHContainerOps - The PCHContainerOperations to use for loading and
759a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  /// creating modules.
760a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  ///
761d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// \param Diags - The diagnostics engine to use for reporting errors; its
762d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// lifetime is expected to extend past that of the returned ASTUnit.
763d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  ///
764d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis  /// \param Action - The ASTFrontendAction to invoke. Its ownership is not
765063820655db8121f0022a7c51458463c7250324cBenjamin Kramer  /// transferred.
766abb5afadd11b19568359880faa5d966a4e33551cArgyrios Kyrtzidis  ///
767abb5afadd11b19568359880faa5d966a4e33551cArgyrios Kyrtzidis  /// \param Unit - optionally an already created ASTUnit. Its ownership is not
768063820655db8121f0022a7c51458463c7250324cBenjamin Kramer  /// transferred.
7696f3ce979a7748fd117e6473d6272b16d643b6262Argyrios Kyrtzidis  ///
7706f3ce979a7748fd117e6473d6272b16d643b6262Argyrios Kyrtzidis  /// \param Persistent - if true the returned ASTUnit will be complete.
7716f3ce979a7748fd117e6473d6272b16d643b6262Argyrios Kyrtzidis  /// false means the caller is only interested in getting info through the
7726f3ce979a7748fd117e6473d6272b16d643b6262Argyrios Kyrtzidis  /// provided \see Action.
773e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  ///
774e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// \param ErrAST - If non-null and parsing failed without any AST to return
775e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// (e.g. because the PCH could not be loaded), this accepts the ASTUnit
776e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// mainly to allow the caller to see the diagnostics.
777e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// This will only receive an ASTUnit if a new one was created. If an already
77870517ca5c07c4b41ff8662b94ee22047b0299f8cDmitri Gribenko  /// created ASTUnit was passed in \p Unit then the caller can check that.
779e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  ///
780651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  static ASTUnit *LoadFromCompilerInvocationAction(
781a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      CompilerInvocation *CI,
782a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      std::shared_ptr<PCHContainerOperations> PCHContainerOps,
783a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
7846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      ASTFrontendAction *Action = nullptr, ASTUnit *Unit = nullptr,
7856bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      bool Persistent = true, StringRef ResourceFilesPath = StringRef(),
7866bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      bool OnlyLocalDecls = false, bool CaptureDiagnostics = false,
787a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      unsigned PrecompilePreambleAfterNParses = 0,
788a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      bool CacheCodeCompletionResults = false,
789651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      bool IncludeBriefCommentsInCodeCompletion = false,
7906bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      bool UserFilesAreVolatile = false,
7916bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      std::unique_ptr<ASTUnit> *ErrAST = nullptr);
792d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis
793521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
794521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// CompilerInvocation object.
795521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
796521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// \param CI - The compiler invocation to use; it must have exactly one input
797f7acc37450d59ef751df73acb91de73850cc6517Daniel Dunbar  /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
798521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
799a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  /// \param PCHContainerOps - The PCHContainerOperations to use for loading and
800a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  /// creating modules.
801a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  ///
8025262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
8035262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
804521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  //
805521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
806521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // shouldn't need to specify them at construction time.
8076bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  static std::unique_ptr<ASTUnit> LoadFromCompilerInvocation(
808a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      CompilerInvocation *CI,
809a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      std::shared_ptr<PCHContainerOperations> PCHContainerOps,
810a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr,
8116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      bool OnlyLocalDecls = false, bool CaptureDiagnostics = false,
812a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      unsigned PrecompilePreambleAfterNParses = 0,
813a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      TranslationUnitKind TUKind = TU_Complete,
8146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      bool CacheCodeCompletionResults = false,
8156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      bool IncludeBriefCommentsInCodeCompletion = false,
8166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      bool UserFilesAreVolatile = false);
817521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
8187b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
8197b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// arguments, which must specify exactly one source file.
8207b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
8217b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgBegin - The beginning of the argument vector.
8227b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
8237b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgEnd - The end of the argument vector.
8247b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
825a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  /// \param PCHContainerOps - The PCHContainerOperations to use for loading and
826a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  /// creating modules.
827a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  ///
8285262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
8295262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
8307b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
831869824e87940f97b87064db2df2861e82e08a8c6Daniel Dunbar  /// \param ResourceFilesPath - The path to the compiler resource files.
832e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  ///
833a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  /// \param ModuleFormat - If provided, uses the specific module format.
834a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  ///
835e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// \param ErrAST - If non-null and parsing failed without any AST to return
836e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// (e.g. because the PCH could not be loaded), this accepts the ASTUnit
837e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  /// mainly to allow the caller to see the diagnostics.
838e722ed6f5464232e23be52f4976312ef526fae99Argyrios Kyrtzidis  ///
8397b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
8407b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // shouldn't need to specify them at construction time.
841651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  static ASTUnit *LoadFromCommandLine(
842651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      const char **ArgBegin, const char **ArgEnd,
843a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      std::shared_ptr<PCHContainerOperations> PCHContainerOps,
844651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath,
845651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      bool OnlyLocalDecls = false, bool CaptureDiagnostics = false,
846651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      ArrayRef<RemappedFile> RemappedFiles = None,
847651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      bool RemappedFilesKeepOriginalName = true,
848a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      unsigned PrecompilePreambleAfterNParses = 0,
849a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      TranslationUnitKind TUKind = TU_Complete,
850651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      bool CacheCodeCompletionResults = false,
851651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      bool IncludeBriefCommentsInCodeCompletion = false,
852651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      bool AllowPCHWithCompilerErrors = false, bool SkipFunctionBodies = false,
853651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      bool UserFilesAreVolatile = false, bool ForSerialization = false,
854a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      llvm::Optional<StringRef> ModuleFormat = llvm::None,
8556bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      std::unique_ptr<ASTUnit> *ErrAST = nullptr);
856651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
857abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \brief Reparse the source files using the same command-line options that
858abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// were originally used to produce this translation unit.
859abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  ///
860abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \returns True if a failure occurred that causes the ASTUnit not to
861a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  /// contain any translation-unit information, false otherwise.
862a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  bool Reparse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
863a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar               ArrayRef<RemappedFile> RemappedFiles = None);
8641abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor
8651abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  /// \brief Perform code completion at the given file, line, and
8661abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  /// column within this translation unit.
8671abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  ///
868cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param File The file in which code completion will occur.
869cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
870cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param Line The line at which code completion will occur.
871cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
872cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param Column The column at which code completion will occur.
873cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
874cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param IncludeMacros Whether to include macros in the code-completion
875cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// results.
876cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
877cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param IncludeCodePatterns Whether to include code patterns (such as a
878cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// for loop) in the code-completion results.
8791abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  ///
880d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  /// \param IncludeBriefComments Whether to include brief documentation within
881d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  /// the set of code completions returned.
882d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  ///
8832283d79155a3e82442fce124ce5fd704ca138801Douglas Gregor  /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, StoredDiagnostics, and
8842283d79155a3e82442fce124ce5fd704ca138801Douglas Gregor  /// OwnedBuffers parameters are all disgusting hacks. They will go away.
885686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void CodeComplete(StringRef File, unsigned Line, unsigned Column,
886a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar                    ArrayRef<RemappedFile> RemappedFiles, bool IncludeMacros,
887a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar                    bool IncludeCodePatterns, bool IncludeBriefComments,
8881abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    CodeCompleteConsumer &Consumer,
889a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar                    std::shared_ptr<PCHContainerOperations> PCHContainerOps,
890d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                    DiagnosticsEngine &Diag, LangOptions &LangOpts,
8911abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    SourceManager &SourceMgr, FileManager &FileMgr,
892686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                    SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
893a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar                    SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers);
8947ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor
8957ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor  /// \brief Save this translation unit to a file with the given name.
8967ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor  ///
897e6d22027bc22d3767a2acdc80299c76b91159867Argyrios Kyrtzidis  /// \returns true if there was a file error or false if the save was
898e6d22027bc22d3767a2acdc80299c76b91159867Argyrios Kyrtzidis  /// successful.
899e6d22027bc22d3767a2acdc80299c76b91159867Argyrios Kyrtzidis  bool Save(StringRef File);
900b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis
901b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  /// \brief Serialize this translation unit with the given output stream.
902b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  ///
903b0f4b9a558933b307073f7cd7753602f94354ae9Argyrios Kyrtzidis  /// \returns True if an error occurred, false otherwise.
9048cc488fefb2fb04bc8d5398da29f0182f97934cfChris Lattner  bool serialize(raw_ostream &OS);
905651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
906651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path,
907651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                              Module::NameVisibilityKind Visibility,
908651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                              bool IsInclusionDirective) override {
9096aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    // ASTUnit doesn't know how to load modules (not that this matters).
910463d90986ec54c62bf8fe31193ef5db701db48a5Douglas Gregor    return ModuleLoadResult();
9116aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  }
912ca2ab45341c448284cf93770018c717810575f86Douglas Gregor
913651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void makeModuleVisible(Module *Mod, Module::NameVisibilityKind Visibility,
914b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar                         SourceLocation ImportLoc) override {}
915ca2ab45341c448284cf93770018c717810575f86Douglas Gregor
9166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
9176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    { return nullptr; }
9186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
919a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    { return 0; }
9200853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis};
9210853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
9220853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis} // namespace clang
9230853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
9240853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#endif
925