ASTUnit.h revision fc0622155fa61349698a8fd0053773c37d9f7ac4
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 <string> 22 23namespace clang { 24 class FileManager; 25 class FileEntry; 26 class SourceManager; 27 class Diagnostic; 28 class TextDiagnosticBuffer; 29 class HeaderSearch; 30 class TargetInfo; 31 class Preprocessor; 32 class ASTContext; 33 class Decl; 34 35/// \brief Utility class for loading a ASTContext from a PCH file. 36/// 37class ASTUnit { 38 Diagnostic Diags; 39 FileManager FileMgr; 40 41 SourceManager SourceMgr; 42 llvm::OwningPtr<HeaderSearch> HeaderInfo; 43 llvm::OwningPtr<TargetInfo> Target; 44 llvm::OwningPtr<Preprocessor> PP; 45 llvm::OwningPtr<ASTContext> Ctx; 46 bool tempFile; 47 48 // OnlyLocalDecls - when true, walking this AST should only visit declarations 49 // that come from the AST itself, not from included precompiled headers. 50 // FIXME: This is temporary; eventually, CIndex will always do this. 51 bool OnlyLocalDecls; 52 53 ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT 54 ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT 55 ASTUnit(); 56 57public: 58 ASTUnit(DiagnosticClient *diagClient = NULL); 59 ~ASTUnit(); 60 61 const SourceManager &getSourceManager() const { return SourceMgr; } 62 SourceManager &getSourceManager() { return SourceMgr; } 63 64 const Preprocessor &getPreprocessor() const { return *PP.get(); } 65 Preprocessor &getPreprocessor() { return *PP.get(); } 66 67 const ASTContext &getASTContext() const { return *Ctx.get(); } 68 ASTContext &getASTContext() { return *Ctx.get(); } 69 70 const Diagnostic &getDiagnostic() const { return Diags; } 71 Diagnostic &getDiagnostic() { return Diags; } 72 73 const FileManager &getFileManager() const { return FileMgr; } 74 FileManager &getFileManager() { return FileMgr; } 75 76 const std::string &getOriginalSourceFileName(); 77 const std::string &getPCHFileName(); 78 79 void unlinkTemporaryFile() { tempFile = true; } 80 81 bool getOnlyLocalDecls() const { return OnlyLocalDecls; } 82 83 /// \brief Create a ASTUnit from a PCH file. 84 /// 85 /// \param Filename - The PCH file to load. 86 /// 87 /// \param diagClient - The diagnostics client to use. Specify NULL 88 /// to use a default client that emits warnings/errors to standard error. 89 /// The ASTUnit objects takes ownership of this object. 90 /// 91 /// \param FileMgr - The FileManager to use. 92 /// 93 /// \param ErrMsg - Error message to report if the PCH file could not be 94 /// loaded. 95 /// 96 /// \returns - The initialized ASTUnit or null if the PCH failed to load. 97 static ASTUnit *LoadFromPCHFile(const std::string &Filename, 98 std::string *ErrMsg = 0, 99 DiagnosticClient *diagClient = NULL, 100 bool OnlyLocalDecls = false, 101 bool UseBumpAllocator = false); 102}; 103 104} // namespace clang 105 106#endif 107