ASTUnit.h revision 44c181aec37789f25f6c15543c164416f72e562a
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
174ae8f298b1ea51b4c2234f9148e2e4349c9bdd23Douglas Gregor#include "clang/Lex/PreprocessingRecord.h"
1831b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar#include "clang/Basic/SourceManager.h"
1928019772db70d4547be05a042eb950bc910f134fDouglas Gregor#include "llvm/ADT/IntrusiveRefCntPtr.h"
200853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#include "llvm/ADT/OwningPtr.h"
2136c4464ba6cfc2a63dc67c493ef2f5ab2aea09ccSteve Naroff#include "clang/Basic/FileManager.h"
22f96b524306ccfa623235d375deee79637bd38f29Steve Naroff#include "clang/Index/ASTLocation.h"
23a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor#include "llvm/ADT/SmallVector.h"
24313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor#include "llvm/System/Path.h"
25788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor#include <map>
260853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#include <string>
27f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar#include <vector>
28f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar#include <cassert>
294db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor#include <utility>
304db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor
314db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregornamespace llvm {
324db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  class MemoryBuffer;
334db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor}
340853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
350853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidisnamespace clang {
36521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass ASTContext;
37521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass CompilerInvocation;
38521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Decl;
39521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Diagnostic;
40521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass FileEntry;
41521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass FileManager;
42521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass HeaderSearch;
43521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Preprocessor;
44521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass SourceManager;
45521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass TargetInfo;
460853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
47f96b524306ccfa623235d375deee79637bd38f29Steve Naroffusing namespace idx;
48f96b524306ccfa623235d375deee79637bd38f29Steve Naroff
4944c181aec37789f25f6c15543c164416f72e562aDouglas Gregorclass PrecompiledPreamble {
5044c181aec37789f25f6c15543c164416f72e562aDouglas Gregor  llvm::sys::Path PreambleFile;
5144c181aec37789f25f6c15543c164416f72e562aDouglas Gregor
5244c181aec37789f25f6c15543c164416f72e562aDouglas Gregorpublic:
5344c181aec37789f25f6c15543c164416f72e562aDouglas Gregor  ~PrecompiledPreamble();
5444c181aec37789f25f6c15543c164416f72e562aDouglas Gregor
5544c181aec37789f25f6c15543c164416f72e562aDouglas Gregor};
5644c181aec37789f25f6c15543c164416f72e562aDouglas Gregor
570853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis/// \brief Utility class for loading a ASTContext from a PCH file.
580853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis///
590853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidisclass ASTUnit {
60788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregorpublic:
61788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  typedef std::map<FileID, std::vector<PreprocessedEntity *> >
62788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor    PreprocessedEntitiesByFileMap;
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
72807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// Optional owned invocation, just used to make the invocation used in
73807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// LoadFromCommandLine available.
74807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  llvm::OwningPtr<CompilerInvocation> Invocation;
75807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar
767d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // OnlyLocalDecls - when true, walking this AST should only visit declarations
777d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // that come from the AST itself, not from included precompiled headers.
787d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // FIXME: This is temporary; eventually, CIndex will always do this.
797d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool                              OnlyLocalDecls;
80c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
81abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \brief Whether to capture any diagnostics produced.
82abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  bool CaptureDiagnostics;
83abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor
84f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// Track whether the main file was loaded from an AST or not.
85c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool MainFileIsAST;
86c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
87f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// Track the top-level decls which appeared in an ASTUnit which was loaded
88f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// from a source file.
89f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  //
90f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // FIXME: This is just an optimization hack to avoid deserializing large parts
91f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // of a PCH file when using the Index library on an ASTUnit loaded from
92f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // source. In the long term we should make the Index library use efficient and
93f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // more scalable search mechanisms.
94f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  std::vector<Decl*> TopLevelDecls;
95f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
9668d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  /// The name of the original source file used to generate this ASTUnit.
9768d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  std::string OriginalSourceFile;
9868d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar
99f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  // Critical optimization when using clang_getCursor().
100f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  ASTLocation LastLoc;
101f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
102a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  /// \brief The set of diagnostics produced when creating this
103a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  /// translation unit.
104405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  llvm::SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
105a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
106313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// \brief Temporary files that should be removed when the ASTUnit is
107313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// destroyed.
108313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  llvm::SmallVector<llvm::sys::Path, 4> TemporaryFiles;
109bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
110788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// \brief A mapping from file IDs to the set of preprocessed entities
111788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// stored in that file.
112788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  ///
113788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// FIXME: This is just an optimization hack to avoid searching through
114788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// many preprocessed entities during cursor traversal in the CIndex library.
115788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// Ideally, we would just be able to perform a binary search within the
116788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// list of preprocessed entities.
117788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  PreprocessedEntitiesByFileMap PreprocessedEntitiesByFile;
118788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor
119bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// \brief Simple hack to allow us to assert that ASTUnit is not being
120bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// used concurrently, which is not supported.
121bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  ///
122bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// Clients should create instances of the ConcurrencyCheck class whenever
123bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// using the ASTUnit in a way that isn't intended to be concurrent, which is
124bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// just about any usage.
125bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  unsigned int ConcurrencyCheckValue;
126bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  static const unsigned int CheckLocked = 28573289;
127bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  static const unsigned int CheckUnlocked = 9803453;
128313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor
12931b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
13031b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
131bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
1323687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor  explicit ASTUnit(bool MainFileIsAST);
1338b96253907c47141af0b7b2a44a368748d006a87Douglas Gregor
134abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  void CleanTemporaryFiles();
135abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  bool Parse();
13644c181aec37789f25f6c15543c164416f72e562aDouglas Gregor  void BuildPrecompiledPreamble();
137abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor
1380853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidispublic:
139bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  class ConcurrencyCheck {
140bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    volatile ASTUnit &Self;
141bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
142bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  public:
143bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    explicit ConcurrencyCheck(ASTUnit &Self)
144bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      : Self(Self)
145bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    {
146bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      assert(Self.ConcurrencyCheckValue == CheckUnlocked &&
147bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor             "Concurrent access to ASTUnit!");
148bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      Self.ConcurrencyCheckValue = CheckLocked;
149bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    }
150bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
151bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    ~ConcurrencyCheck() {
152bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      Self.ConcurrencyCheckValue = CheckUnlocked;
153bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    }
154bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  };
155bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  friend class ConcurrencyCheck;
156bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
1570853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ~ASTUnit();
1580853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
159c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool isMainFileAST() const { return MainFileIsAST; }
160c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
1613687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor  const Diagnostic &getDiagnostics() const { return *Diagnostics; }
1623687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor  Diagnostic &getDiagnostics()             { return *Diagnostics; }
1633687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor
164405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  const SourceManager &getSourceManager() const { return *SourceMgr; }
165405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor        SourceManager &getSourceManager()       { return *SourceMgr; }
1660853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
1670853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  const Preprocessor &getPreprocessor() const { return *PP.get(); }
1680853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis        Preprocessor &getPreprocessor()       { return *PP.get(); }
1691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1700853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  const ASTContext &getASTContext() const { return *Ctx.get(); }
1710853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis        ASTContext &getASTContext()       { return *Ctx.get(); }
1720853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
173405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  const FileManager &getFileManager() const { return *FileMgr; }
174405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor        FileManager &getFileManager()       { return *FileMgr; }
175f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
17677accc11f04ed4ff9afd4e27d430144d4714be56Steve Naroff  const std::string &getOriginalSourceFileName();
177e19944c93961b7618f4f3f3185f698f46369ea54Steve Naroff  const std::string &getPCHFileName();
178b85bca2676b433ae555db09de4dd2823ff13b856Zhongxing Xu
179313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// \brief Add a temporary file that the ASTUnit depends on.
180313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  ///
181313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// This file will be erased when the ASTUnit is destroyed.
182313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  void addTemporaryFile(const llvm::sys::Path &TempFile) {
183313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor    TemporaryFiles.push_back(TempFile);
184313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  }
185313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor
1867d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
187f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
188f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; }
189f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  ASTLocation getLastASTLocation() const { return LastLoc; }
190f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
191f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  std::vector<Decl*> &getTopLevelDecls() {
192f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
193f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    return TopLevelDecls;
194f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  }
195f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  const std::vector<Decl*> &getTopLevelDecls() const {
196f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
197f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    return TopLevelDecls;
198f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  }
199f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
200788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// \brief Retrieve the mapping from File IDs to the preprocessed entities
201788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// within that file.
202788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  PreprocessedEntitiesByFileMap &getPreprocessedEntitiesByFile() {
203788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor    return PreprocessedEntitiesByFile;
204788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  }
205788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor
206a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  // Retrieve the diagnostics associated with this AST
207405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  typedef const StoredDiagnostic *stored_diag_iterator;
208405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  stored_diag_iterator stored_diag_begin() const {
209405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics.begin();
210405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  }
211405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  stored_diag_iterator stored_diag_end() const {
212405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics.end();
213405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  }
214405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  unsigned stored_diag_size() const { return StoredDiagnostics.size(); }
215405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor
216405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  llvm::SmallVector<StoredDiagnostic, 4> &getStoredDiagnostics() {
217405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics;
218a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  }
219a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
2204db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  /// \brief A mapping from a file name to the memory buffer that stores the
2214db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  /// remapped contents of that file.
2224db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  typedef std::pair<std::string, const llvm::MemoryBuffer *> RemappedFile;
2234db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor
2240853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  /// \brief Create a ASTUnit from a PCH file.
2250853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
22631b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  /// \param Filename - The PCH file to load.
2270853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
2285262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
2295262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
23031b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ///
23131b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  /// \returns - The initialized ASTUnit or null if the PCH failed to load.
2320853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  static ASTUnit *LoadFromPCHFile(const std::string &Filename,
23328019772db70d4547be05a042eb950bc910f134fDouglas Gregor                                  llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
2345cf48766d626ff6b223acc9d4b7e415ca8480836Ted Kremenek                                  bool OnlyLocalDecls = false,
2354db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor                                  RemappedFile *RemappedFiles = 0,
236a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                  unsigned NumRemappedFiles = 0,
237a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                  bool CaptureDiagnostics = false);
238521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
239521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
240521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// CompilerInvocation object.
241521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
242521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// \param CI - The compiler invocation to use; it must have exactly one input
243f7acc37450d59ef751df73acb91de73850cc6517Daniel Dunbar  /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
244521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
2455262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
2465262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
247521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  //
248521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
249521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // shouldn't need to specify them at construction time.
250f7acc37450d59ef751df73acb91de73850cc6517Daniel Dunbar  static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI,
25128019772db70d4547be05a042eb950bc910f134fDouglas Gregor                                     llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
252a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                             bool OnlyLocalDecls = false,
25344c181aec37789f25f6c15543c164416f72e562aDouglas Gregor                                             bool CaptureDiagnostics = false,
25444c181aec37789f25f6c15543c164416f72e562aDouglas Gregor                                             bool PrecompilePreamble = false);
255521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
2567b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
2577b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// arguments, which must specify exactly one source file.
2587b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
2597b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgBegin - The beginning of the argument vector.
2607b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
2617b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgEnd - The end of the argument vector.
2627b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
2635262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
2645262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
2657b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
266869824e87940f97b87064db2df2861e82e08a8c6Daniel Dunbar  /// \param ResourceFilesPath - The path to the compiler resource files.
2677b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  //
2687b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
2697b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // shouldn't need to specify them at construction time.
2707b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  static ASTUnit *LoadFromCommandLine(const char **ArgBegin,
2717b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      const char **ArgEnd,
27228019772db70d4547be05a042eb950bc910f134fDouglas Gregor                                    llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
273869824e87940f97b87064db2df2861e82e08a8c6Daniel Dunbar                                      llvm::StringRef ResourceFilesPath,
2747b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      bool OnlyLocalDecls = false,
2754db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor                                      RemappedFile *RemappedFiles = 0,
276a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                      unsigned NumRemappedFiles = 0,
27744c181aec37789f25f6c15543c164416f72e562aDouglas Gregor                                      bool CaptureDiagnostics = false,
27844c181aec37789f25f6c15543c164416f72e562aDouglas Gregor                                      bool PrecompilePreamble = false);
279abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor
280abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \brief Reparse the source files using the same command-line options that
281abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// were originally used to produce this translation unit.
282abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  ///
283abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \returns True if a failure occurred that causes the ASTUnit not to
284abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// contain any translation-unit information, false otherwise.
285abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  bool Reparse(RemappedFile *RemappedFiles = 0,
286abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor               unsigned NumRemappedFiles = 0);
2870853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis};
2880853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
2890853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis} // namespace clang
2900853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
2910853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#endif
292