ASTUnit.h revision 68d40e2d16b9fadba386853d6bbb60089291fdc5
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/Frontend/TextDiagnosticBuffer.h" 20#include "clang/Basic/FileManager.h" 21#include "clang/Index/ASTLocation.h" 22#include <string> 23 24namespace clang { 25class ASTContext; 26class CompilerInvocation; 27class Decl; 28class Diagnostic; 29class FileEntry; 30class FileManager; 31class HeaderSearch; 32class Preprocessor; 33class SourceManager; 34class TargetInfo; 35class TextDiagnosticBuffer; 36 37using namespace idx; 38 39/// \brief Utility class for loading a ASTContext from a PCH file. 40/// 41class ASTUnit { 42 Diagnostic Diags; 43 FileManager FileMgr; 44 45 SourceManager SourceMgr; 46 llvm::OwningPtr<HeaderSearch> HeaderInfo; 47 llvm::OwningPtr<TargetInfo> Target; 48 llvm::OwningPtr<Preprocessor> PP; 49 llvm::OwningPtr<ASTContext> Ctx; 50 bool tempFile; 51 52 // OnlyLocalDecls - when true, walking this AST should only visit declarations 53 // that come from the AST itself, not from included precompiled headers. 54 // FIXME: This is temporary; eventually, CIndex will always do this. 55 bool OnlyLocalDecls; 56 57 /// The name of the original source file used to generate this ASTUnit. 58 std::string OriginalSourceFile; 59 60 // Critical optimization when using clang_getCursor(). 61 ASTLocation LastLoc; 62 63 ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT 64 ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT 65 66public: 67 ASTUnit(DiagnosticClient *diagClient = NULL); 68 ~ASTUnit(); 69 70 const SourceManager &getSourceManager() const { return SourceMgr; } 71 SourceManager &getSourceManager() { return SourceMgr; } 72 73 const Preprocessor &getPreprocessor() const { return *PP.get(); } 74 Preprocessor &getPreprocessor() { return *PP.get(); } 75 76 const ASTContext &getASTContext() const { return *Ctx.get(); } 77 ASTContext &getASTContext() { return *Ctx.get(); } 78 79 const Diagnostic &getDiagnostic() const { return Diags; } 80 Diagnostic &getDiagnostic() { return Diags; } 81 82 const FileManager &getFileManager() const { return FileMgr; } 83 FileManager &getFileManager() { return FileMgr; } 84 85 const std::string &getOriginalSourceFileName(); 86 const std::string &getPCHFileName(); 87 88 void unlinkTemporaryFile() { tempFile = true; } 89 90 bool getOnlyLocalDecls() const { return OnlyLocalDecls; } 91 92 void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; } 93 ASTLocation getLastASTLocation() const { return LastLoc; } 94 95 /// \brief Create a ASTUnit from a PCH file. 96 /// 97 /// \param Filename - The PCH file to load. 98 /// 99 /// \param DiagClient - The diagnostics client to use. Specify NULL 100 /// to use a default client that emits warnings/errors to standard error. 101 /// The ASTUnit objects takes ownership of this object. 102 /// 103 /// \param ErrMsg - Error message to report if the PCH file could not be 104 /// loaded. 105 /// 106 /// \returns - The initialized ASTUnit or null if the PCH failed to load. 107 static ASTUnit *LoadFromPCHFile(const std::string &Filename, 108 std::string *ErrMsg = 0, 109 DiagnosticClient *DiagClient = NULL, 110 bool OnlyLocalDecls = false, 111 bool UseBumpAllocator = false); 112 113 /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a 114 /// CompilerInvocation object. 115 /// 116 /// \param CI - The compiler invocation to use; it must have exactly one input 117 /// source file. 118 /// 119 /// \param Diags - The diagnostics engine to use for reporting errors. 120 // 121 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we 122 // shouldn't need to specify them at construction time. 123 static ASTUnit *LoadFromCompilerInvocation(const CompilerInvocation &CI, 124 Diagnostic &Diags, 125 bool OnlyLocalDecls = false, 126 bool UseBumpAllocator = false); 127 128 /// LoadFromCommandLine - Create an ASTUnit from a vector of command line 129 /// arguments, which must specify exactly one source file. 130 /// 131 /// \param ArgBegin - The beginning of the argument vector. 132 /// 133 /// \param ArgEnd - The end of the argument vector. 134 /// 135 /// \param Diags - The diagnostics engine to use for reporting errors. 136 /// 137 /// \param Argv0 - The program path (from argv[0]), for finding the builtin 138 /// compiler path. 139 /// 140 /// \param MainAddr - The address of main (or some other function in the main 141 /// executable), for finding the builtin compiler path. 142 // 143 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we 144 // shouldn't need to specify them at construction time. 145 static ASTUnit *LoadFromCommandLine(const char **ArgBegin, 146 const char **ArgEnd, 147 Diagnostic &Diags, 148 const char *Arg0, 149 void *MainAddr, 150 bool OnlyLocalDecls = false, 151 bool UseBumpAllocator = false); 152}; 153 154} // namespace clang 155 156#endif 157