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