ASTUnit.h revision 213f18b3d654de7d1c7cf4a329ea9d3db1c50b6a
14b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//===--- ASTUnit.h - ASTUnit utility ----------------------------*- C++ -*-===//
24b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//
34b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//                     The LLVM Compiler Infrastructure
44b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//
54b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis// This file is distributed under the University of Illinois Open Source
64b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis// License. See LICENSE.TXT for details.
74b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//
84b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
94b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//
104b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis// ASTUnit utility class.
114b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//
124b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
134b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis
140853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#ifndef LLVM_CLANG_FRONTEND_ASTUNIT_H
150853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#define LLVM_CLANG_FRONTEND_ASTUNIT_H
160853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
17eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor#include "clang/Index/ASTLocation.h"
1805a07605322dfef2b017781042043a261c5a89cdSebastian Redl#include "clang/Serialization/ASTBitCodes.h"
19914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor#include "clang/Sema/Sema.h"
200a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall#include "clang/Sema/CodeCompleteConsumer.h"
214ae8f298b1ea51b4c2234f9148e2e4349c9bdd23Douglas Gregor#include "clang/Lex/PreprocessingRecord.h"
2231b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar#include "clang/Basic/SourceManager.h"
23eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor#include "clang/Basic/FileManager.h"
2487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor#include "clang-c/Index.h"
2528019772db70d4547be05a042eb950bc910f134fDouglas Gregor#include "llvm/ADT/IntrusiveRefCntPtr.h"
260853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#include "llvm/ADT/OwningPtr.h"
27a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor#include "llvm/ADT/SmallVector.h"
28cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor#include "llvm/ADT/StringMap.h"
29313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor#include "llvm/System/Path.h"
30788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor#include <map>
310853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#include <string>
32f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar#include <vector>
33f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar#include <cassert>
344db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor#include <utility>
35cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor#include <sys/types.h>
364db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor
374db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregornamespace llvm {
384db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  class MemoryBuffer;
394db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor}
400853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
410853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidisnamespace clang {
42521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass ASTContext;
431abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregorclass CodeCompleteConsumer;
44521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass CompilerInvocation;
45521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Decl;
46521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Diagnostic;
47521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass FileEntry;
48521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass FileManager;
49521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass HeaderSearch;
50521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Preprocessor;
51521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass SourceManager;
52521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass TargetInfo;
530853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
54f96b524306ccfa623235d375deee79637bd38f29Steve Naroffusing namespace idx;
5544c181aec37789f25f6c15543c164416f72e562aDouglas Gregor
563c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl/// \brief Utility class for loading a ASTContext from an AST file.
570853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis///
580853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidisclass ASTUnit {
59788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregorpublic:
60788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  typedef std::map<FileID, std::vector<PreprocessedEntity *> >
61788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor    PreprocessedEntitiesByFileMap;
62bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor
63788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregorprivate:
6428019772db70d4547be05a042eb950bc910f134fDouglas Gregor  llvm::IntrusiveRefCntPtr<Diagnostic> Diagnostics;
65405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  llvm::OwningPtr<FileManager>      FileMgr;
66405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  llvm::OwningPtr<SourceManager>    SourceMgr;
670853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  llvm::OwningPtr<HeaderSearch>     HeaderInfo;
680853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  llvm::OwningPtr<TargetInfo>       Target;
690853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  llvm::OwningPtr<Preprocessor>     PP;
700853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  llvm::OwningPtr<ASTContext>       Ctx;
714ae8f298b1ea51b4c2234f9148e2e4349c9bdd23Douglas Gregor
72914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// \brief The AST consumer that received information about the translation
73914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// unit as it was parsed or loaded.
74914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  llvm::OwningPtr<ASTConsumer> Consumer;
75914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
76914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// \brief The semantic analysis object used to type-check the translation
77914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  /// unit.
78914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  llvm::OwningPtr<Sema> TheSema;
79914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
80807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// Optional owned invocation, just used to make the invocation used in
81807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// LoadFromCommandLine available.
82807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  llvm::OwningPtr<CompilerInvocation> Invocation;
83914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
847d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // OnlyLocalDecls - when true, walking this AST should only visit declarations
857d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // that come from the AST itself, not from included precompiled headers.
867d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // FIXME: This is temporary; eventually, CIndex will always do this.
877d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool                              OnlyLocalDecls;
88c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
89abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \brief Whether to capture any diagnostics produced.
90abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  bool CaptureDiagnostics;
91abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor
92df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  /// \brief Track whether the main file was loaded from an AST or not.
93c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool MainFileIsAST;
94c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
95df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  /// \brief Whether this AST represents a complete translation unit.
96df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  bool CompleteTranslationUnit;
97df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
98213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor  /// \brief Whether we should time each operation.
99213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor  bool WantTiming;
100213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor
101f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// Track the top-level decls which appeared in an ASTUnit which was loaded
102f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// from a source file.
103f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  //
104f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // FIXME: This is just an optimization hack to avoid deserializing large parts
105f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // of a PCH file when using the Index library on an ASTUnit loaded from
106f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // source. In the long term we should make the Index library use efficient and
107f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // more scalable search mechanisms.
108f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  std::vector<Decl*> TopLevelDecls;
109f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
11068d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  /// The name of the original source file used to generate this ASTUnit.
11168d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  std::string OriginalSourceFile;
11268d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar
113f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  // Critical optimization when using clang_getCursor().
114f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  ASTLocation LastLoc;
115f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
116a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  /// \brief The set of diagnostics produced when creating this
117a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  /// translation unit.
118405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  llvm::SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
119a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
1204cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \brief The number of stored diagnostics that come from the driver
1214cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// itself.
1224cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  ///
1234cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// Diagnostics that come from the driver are retained from one parse to
1244cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// the next.
1254cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  unsigned NumStoredDiagnosticsFromDriver;
1264cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor
127313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// \brief Temporary files that should be removed when the ASTUnit is
128313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// destroyed.
129313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  llvm::SmallVector<llvm::sys::Path, 4> TemporaryFiles;
130bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
131788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// \brief A mapping from file IDs to the set of preprocessed entities
132788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// stored in that file.
133788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  ///
134788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// FIXME: This is just an optimization hack to avoid searching through
135788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// many preprocessed entities during cursor traversal in the CIndex library.
136788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// Ideally, we would just be able to perform a binary search within the
137788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// list of preprocessed entities.
138788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  PreprocessedEntitiesByFileMap PreprocessedEntitiesByFile;
139788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor
140bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// \brief Simple hack to allow us to assert that ASTUnit is not being
141bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// used concurrently, which is not supported.
142bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  ///
143bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// Clients should create instances of the ConcurrencyCheck class whenever
144bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// using the ASTUnit in a way that isn't intended to be concurrent, which is
145bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// just about any usage.
146bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  unsigned int ConcurrencyCheckValue;
147bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  static const unsigned int CheckLocked = 28573289;
148bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  static const unsigned int CheckUnlocked = 9803453;
149175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
150eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// \brief Counter that determines when we want to try building a
151eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// precompiled preamble.
152eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  ///
153eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// If zero, we will never build a precompiled preamble. Otherwise,
154eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// it's treated as a counter that decrements each time we reparse
155eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// without the benefit of a precompiled preamble. When it hits 1,
156eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// we'll attempt to rebuild the precompiled header. This way, if
157eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// building the precompiled preamble fails, we won't try again for
158eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  /// some number of calls.
159eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor  unsigned PreambleRebuildCounter;
160eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor
161175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \brief The file in which the precompiled preamble is stored.
162385103b79c5338a2be5da0ca70652400bc267371Douglas Gregor  std::string PreambleFile;
163175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
164175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \brief The contents of the preamble that has been precompiled to
165175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \c PreambleFile.
166175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  std::vector<char> Preamble;
167175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
168f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// \brief Whether the preamble ends at the start of a new line.
169f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  ///
170f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// Used to inform the lexer as to whether it's starting at the beginning of
171f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// a line after skipping the preamble.
172f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  bool PreambleEndsAtStartOfLine;
173f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor
174175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \brief The size of the source buffer that we've reserved for the main
175175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// file within the precompiled preamble.
176175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  unsigned PreambleReservedSize;
177cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
178cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// \brief Keeps track of the files that were used when computing the
179cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// preamble, with both their buffer size and their modification time.
180cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  ///
181cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// If any of the files have changed from one compile to the next,
182cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// the preamble must be thrown away.
183cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  llvm::StringMap<std::pair<off_t, time_t> > FilesInPreamble;
184cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
18528233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// \brief When non-NULL, this is the buffer used to store the contents of
18628233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// the main file when it has been padded for use with the precompiled
18728233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// preamble.
18828233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  llvm::MemoryBuffer *SavedMainFileBuffer;
189c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
190671947b18dba342f9aba022ee992babef325a833Douglas Gregor  /// \brief When non-NULL, this is the buffer used to store the
191671947b18dba342f9aba022ee992babef325a833Douglas Gregor  /// contents of the preamble when it has been padded to build the
192671947b18dba342f9aba022ee992babef325a833Douglas Gregor  /// precompiled preamble.
193671947b18dba342f9aba022ee992babef325a833Douglas Gregor  llvm::MemoryBuffer *PreambleBuffer;
194671947b18dba342f9aba022ee992babef325a833Douglas Gregor
195c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// \brief The number of warnings that occurred while parsing the preamble.
196c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  ///
197c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// This value will be used to restore the state of the \c Diagnostic object
198c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// when re-using the precompiled preamble. Note that only the
199c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// number of warnings matters, since we will not save the preamble
200c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// when any errors are present.
201c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  unsigned NumWarningsInPreamble;
202c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
203c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// \brief The number of diagnostics that were stored when parsing
204c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// the precompiled preamble.
205c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  ///
206c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// This value is used to determine how many of the stored
207c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// diagnostics should be retained when reparsing in the presence of
208c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  /// a precompiled preamble.
209c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor  unsigned NumStoredDiagnosticsInPreamble;
210c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
2113c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \brief A list of the serialization ID numbers for each of the top-level
212eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// declarations parsed within the precompiled preamble.
2138538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
214eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
21587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief Whether we should be caching code-completion results.
21687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  bool ShouldCacheCodeCompletionResults;
21787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
21887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregorpublic:
21987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief A cached code-completion result, which may be introduced in one of
22087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// many different contexts.
22187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  struct CachedCodeCompletionResult {
22287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief The code-completion string corresponding to this completion
22387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// result.
22487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    CodeCompletionString *Completion;
22587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
22687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief A bitmask that indicates which code-completion contexts should
22787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// contain this completion result.
22887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    ///
22987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// The bits in the bitmask correspond to the values of
23087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// CodeCompleteContext::Kind. To map from a completion context kind to a
23187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// bit, subtract one from the completion context kind and shift 1 by that
23287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// number of bits. Many completions can occur in several different
23387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// contexts.
23487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    unsigned ShowInContexts;
23587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
23687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief The priority given to this code-completion result.
23787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    unsigned Priority;
23887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
23987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// \brief The libclang cursor kind corresponding to this code-completion
24087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /// result.
24187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    CXCursorKind Kind;
2421827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor
24358ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor    /// \brief The availability of this code-completion result.
24458ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor    CXAvailabilityKind Availability;
24558ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor
2461827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor    /// \brief The simplified type class for a non-macro completion result.
2471827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor    SimplifiedTypeClass TypeClass;
248f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
249f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// \brief The type of a non-macro completion result, stored as a unique
250f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// integer used by the string map of cached completion types.
251f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    ///
252f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// This value will be zero if the type is not known, or a unique value
253f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// determined by the formatted type string. Se \c CachedCompletionTypes
254f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /// for more information.
255f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    unsigned Type;
25687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  };
25787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
258f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// \brief Retrieve the mapping from formatted type names to unique type
259f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// identifiers.
260f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  llvm::StringMap<unsigned> &getCachedCompletionTypes() {
261f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    return CachedCompletionTypes;
262f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  }
263f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
26487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregorprivate:
26587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief The set of cached code-completion results.
26687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  std::vector<CachedCodeCompletionResult> CachedCompletionResults;
26787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
268f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// \brief A mapping from the formatted type name to a unique number for that
269f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  /// type, which is used for type equality comparisons.
270f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor  llvm::StringMap<unsigned> CachedCompletionTypes;
271f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
272727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  /// \brief The number of top-level declarations present the last time we
273727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  /// cached code-completion results.
274727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  ///
275727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  /// The value is used to help detect when we should repopulate the global
276727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  /// completion cache.
277727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  unsigned NumTopLevelDeclsAtLastCompletionCache;
278727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor
279727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  /// \brief The number of reparses left until we'll consider updating the
280727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  /// code-completion cache.
281727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  ///
282727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  /// This is meant to avoid thrashing during reparsing, by not allowing the
283727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  /// code-completion cache to be updated on every reparse.
284727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor  unsigned CacheCodeCompletionCoolDown;
285ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
286ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  /// \brief Bit used by CIndex to mark when a translation unit may be in an
287ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  /// inconsistent state, and is not safe to free.
288ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  unsigned UnsafeToFree : 1;
289ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
290c4421e966d77a18f815284175b3fcbb46f36fa39Douglas Gregor  /// \brief Cache any "global" code-completion results, so that we can avoid
291c4421e966d77a18f815284175b3fcbb46f36fa39Douglas Gregor  /// recomputing them with each completion.
29287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  void CacheCodeCompletionResults();
29387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
29487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  /// \brief Clear out and deallocate
29587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  void ClearCachedCompletionResults();
29687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
29731b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
29831b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
299bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
3003687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor  explicit ASTUnit(bool MainFileIsAST);
3018b96253907c47141af0b7b2a44a368748d006a87Douglas Gregor
302abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  void CleanTemporaryFiles();
303754f3490c5b0f5d83361f001bc87944f23644abbDouglas Gregor  bool Parse(llvm::MemoryBuffer *OverrideMainBuffer);
304175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
305f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
306df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  ComputePreamble(CompilerInvocation &Invocation,
307df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                  unsigned MaxLines, bool &CreatedBuffer);
308175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
309df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  llvm::MemoryBuffer *getMainBufferWithPrecompiledPreamble(
3102283d79155a3e82442fce124ce5fd704ca138801Douglas Gregor                                         CompilerInvocation PreambleInvocation,
311df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                                     bool AllowRebuild = true,
312df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                                        unsigned MaxLines = 0);
313eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  void RealizeTopLevelDeclsFromPreamble();
314eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
3150853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidispublic:
316bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  class ConcurrencyCheck {
317bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    volatile ASTUnit &Self;
318bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
319bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  public:
320bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    explicit ConcurrencyCheck(ASTUnit &Self)
321bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      : Self(Self)
322bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    {
323bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      assert(Self.ConcurrencyCheckValue == CheckUnlocked &&
324bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor             "Concurrent access to ASTUnit!");
325bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      Self.ConcurrencyCheckValue = CheckLocked;
326bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    }
327bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
328bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    ~ConcurrencyCheck() {
329bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      Self.ConcurrencyCheckValue = CheckUnlocked;
330bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    }
331bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  };
332bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  friend class ConcurrencyCheck;
333bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
3340853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ~ASTUnit();
3350853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
336c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool isMainFileAST() const { return MainFileIsAST; }
337c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
338ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  bool isUnsafeToFree() const { return UnsafeToFree; }
339ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar  void setUnsafeToFree(bool Value) { UnsafeToFree = Value; }
340ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
3413687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor  const Diagnostic &getDiagnostics() const { return *Diagnostics; }
3423687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor  Diagnostic &getDiagnostics()             { return *Diagnostics; }
3433687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor
344405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  const SourceManager &getSourceManager() const { return *SourceMgr; }
345405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor        SourceManager &getSourceManager()       { return *SourceMgr; }
3460853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
3470853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  const Preprocessor &getPreprocessor() const { return *PP.get(); }
3480853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis        Preprocessor &getPreprocessor()       { return *PP.get(); }
3491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3500853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  const ASTContext &getASTContext() const { return *Ctx.get(); }
3510853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis        ASTContext &getASTContext()       { return *Ctx.get(); }
3520853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
353914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  bool hasSema() const { return TheSema; }
354914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  Sema &getSema() const {
355914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor    assert(TheSema && "ASTUnit does not have a Sema object!");
356914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor    return *TheSema;
357914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor  }
358914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
359405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  const FileManager &getFileManager() const { return *FileMgr; }
360405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor        FileManager &getFileManager()       { return *FileMgr; }
361f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
36277accc11f04ed4ff9afd4e27d430144d4714be56Steve Naroff  const std::string &getOriginalSourceFileName();
3633c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  const std::string &getASTFileName();
364b85bca2676b433ae555db09de4dd2823ff13b856Zhongxing Xu
365313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// \brief Add a temporary file that the ASTUnit depends on.
366313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  ///
367313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// This file will be erased when the ASTUnit is destroyed.
368313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  void addTemporaryFile(const llvm::sys::Path &TempFile) {
369313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor    TemporaryFiles.push_back(TempFile);
370313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  }
371313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor
3727d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
373f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
374eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// \brief Retrieve the maximum PCH level of declarations that a
375eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// traversal of the translation unit should consider.
376eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  unsigned getMaxPCHLevel() const;
377eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
378f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; }
379f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  ASTLocation getLastASTLocation() const { return LastLoc; }
380f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
381213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor
382213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor  llvm::StringRef getMainFileName() const;
383213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor
384eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  typedef std::vector<Decl *>::iterator top_level_iterator;
385eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
386eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  top_level_iterator top_level_begin() {
387f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
388eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    if (!TopLevelDeclsInPreamble.empty())
389eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor      RealizeTopLevelDeclsFromPreamble();
390eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDecls.begin();
391f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  }
392eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
393eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  top_level_iterator top_level_end() {
394f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
395eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    if (!TopLevelDeclsInPreamble.empty())
396eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor      RealizeTopLevelDeclsFromPreamble();
397eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDecls.end();
398eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
399eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
400eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  std::size_t top_level_size() const {
401eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
402eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDeclsInPreamble.size() + TopLevelDecls.size();
403eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
404eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
405eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  bool top_level_empty() const {
406eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
407eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty();
408eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
409eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
410eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// \brief Add a new top-level declaration.
411eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  void addTopLevelDecl(Decl *D) {
412eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    TopLevelDecls.push_back(D);
413eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  }
414eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor
415eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// \brief Add a new top-level declaration, identified by its ID in
416eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor  /// the precompiled preamble.
4178538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  void addTopLevelDeclFromPreamble(serialization::DeclID D) {
418eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    TopLevelDeclsInPreamble.push_back(D);
419f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  }
420f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
421788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// \brief Retrieve the mapping from File IDs to the preprocessed entities
422788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// within that file.
423788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  PreprocessedEntitiesByFileMap &getPreprocessedEntitiesByFile() {
424788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor    return PreprocessedEntitiesByFile;
425788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  }
426788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor
427a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  // Retrieve the diagnostics associated with this AST
428405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  typedef const StoredDiagnostic *stored_diag_iterator;
429405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  stored_diag_iterator stored_diag_begin() const {
430405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics.begin();
431405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  }
432405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  stored_diag_iterator stored_diag_end() const {
433405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics.end();
434405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  }
435405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  unsigned stored_diag_size() const { return StoredDiagnostics.size(); }
436405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor
437405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  llvm::SmallVector<StoredDiagnostic, 4> &getStoredDiagnostics() {
438405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics;
439a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  }
440a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
44187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  typedef std::vector<CachedCodeCompletionResult>::iterator
44287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    cached_completion_iterator;
44387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
44487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  cached_completion_iterator cached_completion_begin() {
44587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    return CachedCompletionResults.begin();
44687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
44787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
44887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  cached_completion_iterator cached_completion_end() {
44987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    return CachedCompletionResults.end();
45087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
45187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
45287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  unsigned cached_completion_size() const {
45387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    return CachedCompletionResults.size();
45487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
45587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
456df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  /// \brief Whether this AST represents a complete translation unit.
457df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  ///
458df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  /// If false, this AST is only a partial translation unit, e.g., one
459df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  /// that might still be used as a precompiled header or preamble.
460df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  bool isCompleteTranslationUnit() const { return CompleteTranslationUnit; }
461df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
4624db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  /// \brief A mapping from a file name to the memory buffer that stores the
4634db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  /// remapped contents of that file.
4644db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  typedef std::pair<std::string, const llvm::MemoryBuffer *> RemappedFile;
4654db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor
4663c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \brief Create a ASTUnit from an AST file.
4670853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
4683c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \param Filename - The AST file to load.
4690853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
4705262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
4715262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
47231b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ///
4733c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// \returns - The initialized ASTUnit or null if the AST failed to load.
4743c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  static ASTUnit *LoadFromASTFile(const std::string &Filename,
47528019772db70d4547be05a042eb950bc910f134fDouglas Gregor                                  llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
4765cf48766d626ff6b223acc9d4b7e415ca8480836Ted Kremenek                                  bool OnlyLocalDecls = false,
4774db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor                                  RemappedFile *RemappedFiles = 0,
478a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                  unsigned NumRemappedFiles = 0,
479a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                  bool CaptureDiagnostics = false);
480521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
4814cd912aa94656697a44c3ebb159f05060300524eDouglas Gregorprivate:
4824cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \brief Helper function for \c LoadFromCompilerInvocation() and
4834cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \c LoadFromCommandLine(), which loads an AST from a compiler invocation.
4844cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  ///
4854cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \param PrecompilePreamble Whether to precompile the preamble of this
4864cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// translation unit, to improve the performance of reparsing.
4874cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  ///
4884cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \returns \c true if a catastrophic failure occurred (which means that the
4894cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  /// \c ASTUnit itself is invalid), or \c false otherwise.
4904cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor  bool LoadFromCompilerInvocation(bool PrecompilePreamble);
4914cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor
4924cd912aa94656697a44c3ebb159f05060300524eDouglas Gregorpublic:
4934cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor
494521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
495521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// CompilerInvocation object.
496521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
497521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// \param CI - The compiler invocation to use; it must have exactly one input
498f7acc37450d59ef751df73acb91de73850cc6517Daniel Dunbar  /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
499521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
5005262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
5015262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
502521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  //
503521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
504521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // shouldn't need to specify them at construction time.
505f7acc37450d59ef751df73acb91de73850cc6517Daniel Dunbar  static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI,
50628019772db70d4547be05a042eb950bc910f134fDouglas Gregor                                     llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
507a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                             bool OnlyLocalDecls = false,
50844c181aec37789f25f6c15543c164416f72e562aDouglas Gregor                                             bool CaptureDiagnostics = false,
509df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                             bool PrecompilePreamble = false,
51087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor                                          bool CompleteTranslationUnit = true,
51187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor                                       bool CacheCodeCompletionResults = false);
512521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
5137b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
5147b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// arguments, which must specify exactly one source file.
5157b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
5167b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgBegin - The beginning of the argument vector.
5177b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
5187b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgEnd - The end of the argument vector.
5197b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
5205262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
5215262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
5227b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
523869824e87940f97b87064db2df2861e82e08a8c6Daniel Dunbar  /// \param ResourceFilesPath - The path to the compiler resource files.
5247b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  //
5257b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
5267b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // shouldn't need to specify them at construction time.
5277b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  static ASTUnit *LoadFromCommandLine(const char **ArgBegin,
5287b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      const char **ArgEnd,
52928019772db70d4547be05a042eb950bc910f134fDouglas Gregor                                    llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
530869824e87940f97b87064db2df2861e82e08a8c6Daniel Dunbar                                      llvm::StringRef ResourceFilesPath,
5317b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      bool OnlyLocalDecls = false,
5324db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor                                      RemappedFile *RemappedFiles = 0,
533a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                      unsigned NumRemappedFiles = 0,
53444c181aec37789f25f6c15543c164416f72e562aDouglas Gregor                                      bool CaptureDiagnostics = false,
535df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor                                      bool PrecompilePreamble = false,
53687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor                                      bool CompleteTranslationUnit = true,
53799ba202f659e1885fa5ee114f97c97cf6a857491Douglas Gregor                                      bool CacheCodeCompletionResults = false,
53899ba202f659e1885fa5ee114f97c97cf6a857491Douglas Gregor                                      bool CXXPrecompilePreamble = false,
53999ba202f659e1885fa5ee114f97c97cf6a857491Douglas Gregor                                      bool CXXChainedPCH = false);
540abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor
541abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \brief Reparse the source files using the same command-line options that
542abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// were originally used to produce this translation unit.
543abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  ///
544abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \returns True if a failure occurred that causes the ASTUnit not to
545abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// contain any translation-unit information, false otherwise.
546abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  bool Reparse(RemappedFile *RemappedFiles = 0,
547abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor               unsigned NumRemappedFiles = 0);
5481abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor
5491abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  /// \brief Perform code completion at the given file, line, and
5501abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  /// column within this translation unit.
5511abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  ///
552cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param File The file in which code completion will occur.
553cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
554cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param Line The line at which code completion will occur.
555cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
556cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param Column The column at which code completion will occur.
557cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
558cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param IncludeMacros Whether to include macros in the code-completion
559cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// results.
560cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  ///
561cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// \param IncludeCodePatterns Whether to include code patterns (such as a
562cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor  /// for loop) in the code-completion results.
5631abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  ///
5642283d79155a3e82442fce124ce5fd704ca138801Douglas Gregor  /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, StoredDiagnostics, and
5652283d79155a3e82442fce124ce5fd704ca138801Douglas Gregor  /// OwnedBuffers parameters are all disgusting hacks. They will go away.
5661abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  void CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column,
5671abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    RemappedFile *RemappedFiles, unsigned NumRemappedFiles,
568cee235cdf0b8047761ffac598c4c3a32ab7411a2Douglas Gregor                    bool IncludeMacros, bool IncludeCodePatterns,
5691abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    CodeCompleteConsumer &Consumer,
5701abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    Diagnostic &Diag, LangOptions &LangOpts,
5711abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                    SourceManager &SourceMgr, FileManager &FileMgr,
5722283d79155a3e82442fce124ce5fd704ca138801Douglas Gregor                    llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
5732283d79155a3e82442fce124ce5fd704ca138801Douglas Gregor              llvm::SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers);
5747ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor
5757ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor  /// \brief Save this translation unit to a file with the given name.
5767ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor  ///
5777ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor  /// \returns True if an error occurred, false otherwise.
5787ae2faafd30524ef5f863bb3b8701977888839bbDouglas Gregor  bool Save(llvm::StringRef File);
5790853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis};
5800853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
5810853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis} // namespace clang
5820853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
5830853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#endif
584