ASTUnit.h revision a88084b78fd4ca5d3d858c14b02414f8cc399f02
1//===--- ASTUnit.h - ASTUnit utility ----------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// ASTUnit utility class. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_FRONTEND_ASTUNIT_H 15#define LLVM_CLANG_FRONTEND_ASTUNIT_H 16 17#include "clang/Basic/SourceManager.h" 18#include "llvm/ADT/OwningPtr.h" 19#include "clang/Basic/FileManager.h" 20#include "clang/Index/ASTLocation.h" 21#include "llvm/ADT/SmallVector.h" 22#include <string> 23#include <vector> 24#include <cassert> 25#include <utility> 26 27namespace llvm { 28 class MemoryBuffer; 29} 30 31namespace clang { 32class ASTContext; 33class CompilerInvocation; 34class Decl; 35class Diagnostic; 36class FileEntry; 37class FileManager; 38class HeaderSearch; 39class Preprocessor; 40class SourceManager; 41class TargetInfo; 42 43using namespace idx; 44 45/// \brief Utility class for loading a ASTContext from a PCH file. 46/// 47class ASTUnit { 48 FileManager FileMgr; 49 50 SourceManager SourceMgr; 51 llvm::OwningPtr<HeaderSearch> HeaderInfo; 52 llvm::OwningPtr<TargetInfo> Target; 53 llvm::OwningPtr<Preprocessor> PP; 54 llvm::OwningPtr<ASTContext> Ctx; 55 bool tempFile; 56 57 /// Optional owned invocation, just used to make the invocation used in 58 /// LoadFromCommandLine available. 59 llvm::OwningPtr<CompilerInvocation> Invocation; 60 61 // OnlyLocalDecls - when true, walking this AST should only visit declarations 62 // that come from the AST itself, not from included precompiled headers. 63 // FIXME: This is temporary; eventually, CIndex will always do this. 64 bool OnlyLocalDecls; 65 66 /// Track whether the main file was loaded from an AST or not. 67 bool MainFileIsAST; 68 69 /// Track the top-level decls which appeared in an ASTUnit which was loaded 70 /// from a source file. 71 // 72 // FIXME: This is just an optimization hack to avoid deserializing large parts 73 // of a PCH file when using the Index library on an ASTUnit loaded from 74 // source. In the long term we should make the Index library use efficient and 75 // more scalable search mechanisms. 76 std::vector<Decl*> TopLevelDecls; 77 78 /// The name of the original source file used to generate this ASTUnit. 79 std::string OriginalSourceFile; 80 81 // Critical optimization when using clang_getCursor(). 82 ASTLocation LastLoc; 83 84 /// \brief The set of diagnostics produced when creating this 85 /// translation unit. 86 llvm::SmallVector<StoredDiagnostic, 4> Diagnostics; 87 88 ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT 89 ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT 90 91public: 92 ASTUnit(bool MainFileIsAST); 93 ~ASTUnit(); 94 95 bool isMainFileAST() const { return MainFileIsAST; } 96 97 const SourceManager &getSourceManager() const { return SourceMgr; } 98 SourceManager &getSourceManager() { return SourceMgr; } 99 100 const Preprocessor &getPreprocessor() const { return *PP.get(); } 101 Preprocessor &getPreprocessor() { return *PP.get(); } 102 103 const ASTContext &getASTContext() const { return *Ctx.get(); } 104 ASTContext &getASTContext() { return *Ctx.get(); } 105 106 const FileManager &getFileManager() const { return FileMgr; } 107 FileManager &getFileManager() { return FileMgr; } 108 109 const std::string &getOriginalSourceFileName(); 110 const std::string &getPCHFileName(); 111 112 void unlinkTemporaryFile() { tempFile = true; } 113 114 bool getOnlyLocalDecls() const { return OnlyLocalDecls; } 115 116 void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; } 117 ASTLocation getLastASTLocation() const { return LastLoc; } 118 119 std::vector<Decl*> &getTopLevelDecls() { 120 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 121 return TopLevelDecls; 122 } 123 const std::vector<Decl*> &getTopLevelDecls() const { 124 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 125 return TopLevelDecls; 126 } 127 128 // Retrieve the diagnostics associated with this AST 129 typedef const StoredDiagnostic * diag_iterator; 130 diag_iterator diag_begin() const { return Diagnostics.begin(); } 131 diag_iterator diag_end() const { return Diagnostics.end(); } 132 unsigned diag_size() const { return Diagnostics.size(); } 133 llvm::SmallVector<StoredDiagnostic, 4> &getDiagnostics() { 134 return Diagnostics; 135 } 136 137 /// \brief A mapping from a file name to the memory buffer that stores the 138 /// remapped contents of that file. 139 typedef std::pair<std::string, const llvm::MemoryBuffer *> RemappedFile; 140 141 /// \brief Create a ASTUnit from a PCH file. 142 /// 143 /// \param Filename - The PCH file to load. 144 /// 145 /// \param Diags - The diagnostics engine to use for reporting errors; its 146 /// lifetime is expected to extend past that of the returned ASTUnit. 147 /// 148 /// \returns - The initialized ASTUnit or null if the PCH failed to load. 149 static ASTUnit *LoadFromPCHFile(const std::string &Filename, 150 Diagnostic &Diags, 151 bool OnlyLocalDecls = false, 152 RemappedFile *RemappedFiles = 0, 153 unsigned NumRemappedFiles = 0, 154 bool CaptureDiagnostics = false); 155 156 /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a 157 /// CompilerInvocation object. 158 /// 159 /// \param CI - The compiler invocation to use; it must have exactly one input 160 /// source file. The ASTUnit takes ownership of the CompilerInvocation object. 161 /// 162 /// \param Diags - The diagnostics engine to use for reporting errors; its 163 /// lifetime is expected to extend past that of the returned ASTUnit. 164 // 165 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we 166 // shouldn't need to specify them at construction time. 167 static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI, 168 Diagnostic &Diags, 169 bool OnlyLocalDecls = false, 170 bool CaptureDiagnostics = false); 171 172 /// LoadFromCommandLine - Create an ASTUnit from a vector of command line 173 /// arguments, which must specify exactly one source file. 174 /// 175 /// \param ArgBegin - The beginning of the argument vector. 176 /// 177 /// \param ArgEnd - The end of the argument vector. 178 /// 179 /// \param Diags - The diagnostics engine to use for reporting errors; its 180 /// lifetime is expected to extend past that of the returned ASTUnit. 181 /// 182 /// \param ResourceFilesPath - The path to the compiler resource files. 183 // 184 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we 185 // shouldn't need to specify them at construction time. 186 static ASTUnit *LoadFromCommandLine(const char **ArgBegin, 187 const char **ArgEnd, 188 Diagnostic &Diags, 189 llvm::StringRef ResourceFilesPath, 190 bool OnlyLocalDecls = false, 191 RemappedFile *RemappedFiles = 0, 192 unsigned NumRemappedFiles = 0, 193 bool CaptureDiagnostics = false); 194}; 195 196} // namespace clang 197 198#endif 199