ASTUnit.h revision f7acc37450d59ef751df73acb91de73850cc6517
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 bool UseBumpAllocator = false, 139 RemappedFile *RemappedFiles = 0, 140 unsigned NumRemappedFiles = 0); 141 142 /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a 143 /// CompilerInvocation object. 144 /// 145 /// \param CI - The compiler invocation to use; it must have exactly one input 146 /// source file. The ASTUnit takes ownership of the CompilerInvocation object. 147 /// 148 /// \param Diags - The diagnostics engine to use for reporting errors; its 149 /// lifetime is expected to extend past that of the returned ASTUnit. 150 // 151 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we 152 // shouldn't need to specify them at construction time. 153 static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI, 154 Diagnostic &Diags, 155 bool OnlyLocalDecls = false); 156 157 /// LoadFromCommandLine - Create an ASTUnit from a vector of command line 158 /// arguments, which must specify exactly one source file. 159 /// 160 /// \param ArgBegin - The beginning of the argument vector. 161 /// 162 /// \param ArgEnd - The end of the argument vector. 163 /// 164 /// \param Diags - The diagnostics engine to use for reporting errors; its 165 /// lifetime is expected to extend past that of the returned ASTUnit. 166 /// 167 /// \param ResourceFilesPath - The path to the compiler resource files. 168 // 169 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we 170 // shouldn't need to specify them at construction time. 171 static ASTUnit *LoadFromCommandLine(const char **ArgBegin, 172 const char **ArgEnd, 173 Diagnostic &Diags, 174 llvm::StringRef ResourceFilesPath, 175 bool OnlyLocalDecls = false, 176 bool UseBumpAllocator = false, 177 RemappedFile *RemappedFiles = 0, 178 unsigned NumRemappedFiles = 0); 179}; 180 181} // namespace clang 182 183#endif 184