1 //===--- ASTUnit.cpp - ASTUnit utility ------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // ASTUnit Implementation.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Frontend/ASTUnit.h"
15 #include "clang/Frontend/PCHReader.h"
16 #include "clang/Frontend/TextDiagnosticBuffer.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclVisitor.h"
19 #include "clang/AST/StmtVisitor.h"
20 #include "clang/Lex/HeaderSearch.h"
21 #include "clang/Lex/Preprocessor.h"
22 #include "clang/Basic/TargetInfo.h"
23 #include "clang/Basic/Diagnostic.h"
24 #include "llvm/Support/Compiler.h"
26 using namespace clang;
28 ASTUnit::ASTUnit() { }
29 ASTUnit::~ASTUnit() { }
33 /// \brief Gathers information from PCHReader that will be used to initialize
35 class VISIBILITY_HIDDEN PCHInfoCollector : public PCHReaderListener {
38 std::string &TargetTriple;
39 std::string &Predefines;
42 unsigned NumHeaderInfos;
45 PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
46 std::string &TargetTriple, std::string &Predefines,
48 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
49 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
51 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
56 virtual bool ReadTargetTriple(const std::string &Triple) {
57 TargetTriple = Triple;
61 virtual bool ReadPredefinesBuffer(const char *PCHPredef,
62 unsigned PCHPredefLen,
64 std::string &SuggestedPredefines) {
65 Predefines = PCHPredef;
69 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) {
70 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
73 virtual void ReadCounter(unsigned Value) {
78 } // anonymous namespace
81 ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
83 std::string *ErrMsg) {
85 llvm::OwningPtr<ASTUnit> AST(new ASTUnit());
87 AST->DiagClient.reset(new TextDiagnosticBuffer());
88 AST->Diags.reset(new Diagnostic(AST->DiagClient.get()));
90 AST->HeaderInfo.reset(new HeaderSearch(FileMgr));
91 AST->SourceMgr.reset(new SourceManager());
93 Diagnostic &Diags = *AST->Diags.get();
94 SourceManager &SourceMgr = *AST->SourceMgr.get();
96 // Gather Info for preprocessor construction later on.
99 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
100 std::string TargetTriple;
101 std::string Predefines;
104 llvm::OwningPtr<PCHReader> Reader;
105 llvm::OwningPtr<ExternalASTSource> Source;
107 Reader.reset(new PCHReader(SourceMgr, FileMgr, Diags));
108 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
109 Predefines, Counter));
111 switch (Reader->ReadPCH(Filename)) {
112 case PCHReader::Success:
115 case PCHReader::Failure:
116 case PCHReader::IgnorePCH:
118 *ErrMsg = "Could not load PCH file";
122 // PCH loaded successfully. Now create the preprocessor.
124 // Get information about the target being compiled for.
125 AST->Target.reset(TargetInfo::CreateTargetInfo(TargetTriple));
126 AST->PP.reset(new Preprocessor(Diags, LangInfo, *AST->Target.get(),
127 SourceMgr, HeaderInfo));
128 Preprocessor &PP = *AST->PP.get();
130 PP.setPredefines(Predefines);
131 PP.setCounterValue(Counter);
132 Reader->setPreprocessor(PP);
134 // Create and initialize the ASTContext.
136 AST->Ctx.reset(new ASTContext(LangInfo,
139 PP.getIdentifierTable(),
140 PP.getSelectorTable(),
142 /* FreeMemory = */ true,
143 /* size_reserve = */0));
144 ASTContext &Context = *AST->Ctx.get();
146 Reader->InitializeContext(Context);
148 // Attach the PCH reader to the AST context as an external AST
149 // source, so that declarations will be deserialized from the
150 // PCH file as needed.
151 Source.reset(Reader.take());
152 Context.setExternalSource(Source);