ASTUnit.h revision 68ea2ac7fd98a5a44c7a5b04c54076cf794531cb
1e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott//===--- ASTUnit.h - ASTUnit utility ----------------------------*- C++ -*-===// 2e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// 3e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// The LLVM Compiler Infrastructure 4e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// 5e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// This file is distributed under the University of Illinois Open Source 6e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// License. See LICENSE.TXT for details. 7e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// 8e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott//===----------------------------------------------------------------------===// 9e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// 10e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// ASTUnit utility class. 11e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// 12e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott//===----------------------------------------------------------------------===// 13e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 14e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#ifndef LLVM_CLANG_FRONTEND_ASTUNIT_H 15e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#define LLVM_CLANG_FRONTEND_ASTUNIT_H 16e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 17e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#include "clang/Basic/SourceManager.h" 18e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#include "llvm/ADT/OwningPtr.h" 19e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#include "clang/Frontend/TextDiagnosticBuffer.h" 20e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#include "clang/Basic/FileManager.h" 21e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#include "clang/Index/ASTLocation.h" 22e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#include <string> 23e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 24e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottnamespace clang { 25e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottclass ASTContext; 26e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottclass CompilerInvocation; 27e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottclass Decl; 28e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottclass Diagnostic; 29e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottclass FileEntry; 30e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottclass FileManager; 31e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottclass HeaderSearch; 32e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottclass Preprocessor; 33e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottclass SourceManager; 34e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottclass TargetInfo; 35e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottclass TextDiagnosticBuffer; 36e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 37e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottusing namespace idx; 38e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 39e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott/// \brief Utility class for loading a ASTContext from a PCH file. 40e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott/// 41e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottclass ASTUnit { 42e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott Diagnostic Diags; 43e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott FileManager FileMgr; 44e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 45e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott SourceManager SourceMgr; 46e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott llvm::OwningPtr<HeaderSearch> HeaderInfo; 47e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott llvm::OwningPtr<TargetInfo> Target; 48e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott llvm::OwningPtr<Preprocessor> PP; 49e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott llvm::OwningPtr<ASTContext> Ctx; 50e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott bool tempFile; 51e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 52e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott // OnlyLocalDecls - when true, walking this AST should only visit declarations 53e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott // that come from the AST itself, not from included precompiled headers. 54e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott // FIXME: This is temporary; eventually, CIndex will always do this. 55e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott bool OnlyLocalDecls; 56e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 57e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott /// The name of the original source file used to generate this ASTUnit. 58e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott std::string OriginalSourceFile; 59e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 60e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott // Critical optimization when using clang_getCursor(). 61e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott ASTLocation LastLoc; 62e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 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 127 /// LoadFromCommandLine - Create an ASTUnit from a vector of command line 128 /// arguments, which must specify exactly one source file. 129 /// 130 /// \param ArgBegin - The beginning of the argument vector. 131 /// 132 /// \param ArgEnd - The end of the argument vector. 133 /// 134 /// \param Diags - The diagnostics engine to use for reporting errors. 135 /// 136 /// \param Argv0 - The program path (from argv[0]), for finding the builtin 137 /// compiler path. 138 /// 139 /// \param MainAddr - The address of main (or some other function in the main 140 /// executable), for finding the builtin compiler path. 141 // 142 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we 143 // shouldn't need to specify them at construction time. 144 static ASTUnit *LoadFromCommandLine(const char **ArgBegin, 145 const char **ArgEnd, 146 Diagnostic &Diags, 147 const char *Arg0, 148 void *MainAddr, 149 bool OnlyLocalDecls = false, 150 bool UseBumpAllocator = false); 151}; 152 153} // namespace clang 154 155#endif 156