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