ASTUnit.h revision 313e26c4e81f0e467490a530548450f4c824a6c4
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 "llvm/System/Path.h" 23#include <string> 24#include <vector> 25#include <cassert> 26#include <utility> 27 28namespace llvm { 29 class MemoryBuffer; 30} 31 32namespace clang { 33class ASTContext; 34class CompilerInvocation; 35class Decl; 36class Diagnostic; 37class FileEntry; 38class FileManager; 39class HeaderSearch; 40class Preprocessor; 41class SourceManager; 42class TargetInfo; 43 44using namespace idx; 45 46/// \brief Utility class for loading a ASTContext from a PCH file. 47/// 48class ASTUnit { 49 FileManager FileMgr; 50 51 SourceManager SourceMgr; 52 llvm::OwningPtr<HeaderSearch> HeaderInfo; 53 llvm::OwningPtr<TargetInfo> Target; 54 llvm::OwningPtr<Preprocessor> PP; 55 llvm::OwningPtr<ASTContext> Ctx; 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 /// \brief Temporary files that should be removed when the ASTUnit is 89 /// destroyed. 90 llvm::SmallVector<llvm::sys::Path, 4> TemporaryFiles; 91 92 ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT 93 ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT 94 95public: 96 ASTUnit(bool MainFileIsAST); 97 ~ASTUnit(); 98 99 bool isMainFileAST() const { return MainFileIsAST; } 100 101 const SourceManager &getSourceManager() const { return SourceMgr; } 102 SourceManager &getSourceManager() { return SourceMgr; } 103 104 const Preprocessor &getPreprocessor() const { return *PP.get(); } 105 Preprocessor &getPreprocessor() { return *PP.get(); } 106 107 const ASTContext &getASTContext() const { return *Ctx.get(); } 108 ASTContext &getASTContext() { return *Ctx.get(); } 109 110 const FileManager &getFileManager() const { return FileMgr; } 111 FileManager &getFileManager() { return FileMgr; } 112 113 const std::string &getOriginalSourceFileName(); 114 const std::string &getPCHFileName(); 115 116 /// \brief Add a temporary file that the ASTUnit depends on. 117 /// 118 /// This file will be erased when the ASTUnit is destroyed. 119 void addTemporaryFile(const llvm::sys::Path &TempFile) { 120 TemporaryFiles.push_back(TempFile); 121 } 122 123 bool getOnlyLocalDecls() const { return OnlyLocalDecls; } 124 125 void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; } 126 ASTLocation getLastASTLocation() const { return LastLoc; } 127 128 std::vector<Decl*> &getTopLevelDecls() { 129 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 130 return TopLevelDecls; 131 } 132 const std::vector<Decl*> &getTopLevelDecls() const { 133 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 134 return TopLevelDecls; 135 } 136 137 // Retrieve the diagnostics associated with this AST 138 typedef const StoredDiagnostic * diag_iterator; 139 diag_iterator diag_begin() const { return Diagnostics.begin(); } 140 diag_iterator diag_end() const { return Diagnostics.end(); } 141 unsigned diag_size() const { return Diagnostics.size(); } 142 llvm::SmallVector<StoredDiagnostic, 4> &getDiagnostics() { 143 return Diagnostics; 144 } 145 146 /// \brief A mapping from a file name to the memory buffer that stores the 147 /// remapped contents of that file. 148 typedef std::pair<std::string, const llvm::MemoryBuffer *> RemappedFile; 149 150 /// \brief Create a ASTUnit from a PCH file. 151 /// 152 /// \param Filename - The PCH file to load. 153 /// 154 /// \param Diags - The diagnostics engine to use for reporting errors; its 155 /// lifetime is expected to extend past that of the returned ASTUnit. 156 /// 157 /// \returns - The initialized ASTUnit or null if the PCH failed to load. 158 static ASTUnit *LoadFromPCHFile(const std::string &Filename, 159 Diagnostic &Diags, 160 bool OnlyLocalDecls = false, 161 RemappedFile *RemappedFiles = 0, 162 unsigned NumRemappedFiles = 0, 163 bool CaptureDiagnostics = false); 164 165 /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a 166 /// CompilerInvocation object. 167 /// 168 /// \param CI - The compiler invocation to use; it must have exactly one input 169 /// source file. The ASTUnit takes ownership of the CompilerInvocation object. 170 /// 171 /// \param Diags - The diagnostics engine to use for reporting errors; its 172 /// lifetime is expected to extend past that of the returned ASTUnit. 173 // 174 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we 175 // shouldn't need to specify them at construction time. 176 static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI, 177 Diagnostic &Diags, 178 bool OnlyLocalDecls = false, 179 bool CaptureDiagnostics = false); 180 181 /// LoadFromCommandLine - Create an ASTUnit from a vector of command line 182 /// arguments, which must specify exactly one source file. 183 /// 184 /// \param ArgBegin - The beginning of the argument vector. 185 /// 186 /// \param ArgEnd - The end of the argument vector. 187 /// 188 /// \param Diags - The diagnostics engine to use for reporting errors; its 189 /// lifetime is expected to extend past that of the returned ASTUnit. 190 /// 191 /// \param ResourceFilesPath - The path to the compiler resource files. 192 // 193 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we 194 // shouldn't need to specify them at construction time. 195 static ASTUnit *LoadFromCommandLine(const char **ArgBegin, 196 const char **ArgEnd, 197 Diagnostic &Diags, 198 llvm::StringRef ResourceFilesPath, 199 bool OnlyLocalDecls = false, 200 RemappedFile *RemappedFiles = 0, 201 unsigned NumRemappedFiles = 0, 202 bool CaptureDiagnostics = false); 203}; 204 205} // namespace clang 206 207#endif 208