blob: d3475b5236d926ca7b127d61b4b51786b49e1d83 [file] [log] [blame]
Argyrios Kyrtzidis4b562cf2009-06-20 08:27:14 +00001//===--- ASTUnit.cpp - ASTUnit utility ------------------------------------===//
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 Implementation.
11//
12//===----------------------------------------------------------------------===//
13
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000014#include "clang/Frontend/ASTUnit.h"
15#include "clang/Frontend/PCHReader.h"
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/DeclVisitor.h"
18#include "clang/AST/StmtVisitor.h"
19#include "clang/Lex/HeaderSearch.h"
20#include "clang/Lex/Preprocessor.h"
21#include "clang/Basic/TargetInfo.h"
22#include "clang/Basic/Diagnostic.h"
23#include "llvm/Support/Compiler.h"
24
25using namespace clang;
26
Daniel Dunbar31b87d82009-09-21 03:03:39 +000027ASTUnit::ASTUnit(Diagnostic &_Diags) : Diags(_Diags) { }
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000028ASTUnit::~ASTUnit() { }
29
30namespace {
31
32/// \brief Gathers information from PCHReader that will be used to initialize
33/// a Preprocessor.
34class VISIBILITY_HIDDEN PCHInfoCollector : public PCHReaderListener {
35 LangOptions &LangOpt;
36 HeaderSearch &HSI;
37 std::string &TargetTriple;
38 std::string &Predefines;
39 unsigned &Counter;
Mike Stump1eb44332009-09-09 15:08:12 +000040
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000041 unsigned NumHeaderInfos;
Mike Stump1eb44332009-09-09 15:08:12 +000042
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000043public:
44 PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
45 std::string &TargetTriple, std::string &Predefines,
46 unsigned &Counter)
47 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
48 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +000049
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000050 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
51 LangOpt = LangOpts;
52 return false;
53 }
Mike Stump1eb44332009-09-09 15:08:12 +000054
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000055 virtual bool ReadTargetTriple(const std::string &Triple) {
56 TargetTriple = Triple;
57 return false;
58 }
Mike Stump1eb44332009-09-09 15:08:12 +000059
60 virtual bool ReadPredefinesBuffer(const char *PCHPredef,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000061 unsigned PCHPredefLen,
62 FileID PCHBufferID,
63 std::string &SuggestedPredefines) {
64 Predefines = PCHPredef;
65 return false;
66 }
Mike Stump1eb44332009-09-09 15:08:12 +000067
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000068 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) {
69 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
70 }
Mike Stump1eb44332009-09-09 15:08:12 +000071
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000072 virtual void ReadCounter(unsigned Value) {
73 Counter = Value;
74 }
75};
76
77} // anonymous namespace
78
Steve Naroff77accc12009-09-03 18:19:54 +000079const std::string &ASTUnit::getOriginalSourceFileName() {
80 return dyn_cast<PCHReader>(Ctx->getExternalSource())->getOriginalSourceFile();
81}
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000082
Steve Naroff9efa7672009-09-04 15:44:05 +000083FileManager &ASTUnit::getFileManager() {
84 return HeaderInfo->getFileMgr();
85}
86
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000087ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
Daniel Dunbar31b87d82009-09-21 03:03:39 +000088 Diagnostic &Diags,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000089 FileManager &FileMgr,
90 std::string *ErrMsg) {
Daniel Dunbar31b87d82009-09-21 03:03:39 +000091 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(Diags));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000092
93 AST->HeaderInfo.reset(new HeaderSearch(FileMgr));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000094
95 // Gather Info for preprocessor construction later on.
Mike Stump1eb44332009-09-09 15:08:12 +000096
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000097 LangOptions LangInfo;
98 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
99 std::string TargetTriple;
100 std::string Predefines;
101 unsigned Counter;
102
Daniel Dunbarbce6f622009-09-03 05:59:50 +0000103 llvm::OwningPtr<PCHReader> Reader;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000104 llvm::OwningPtr<ExternalASTSource> Source;
105
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000106 Reader.reset(new PCHReader(AST->getSourceManager(), FileMgr, AST->Diags));
Daniel Dunbarcc318932009-09-03 05:59:35 +0000107 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
108 Predefines, Counter));
109
110 switch (Reader->ReadPCH(Filename)) {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000111 case PCHReader::Success:
112 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000114 case PCHReader::Failure:
Argyrios Kyrtzidis106c9982009-06-25 18:22:30 +0000115 case PCHReader::IgnorePCH:
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000116 if (ErrMsg)
117 *ErrMsg = "Could not load PCH file";
118 return NULL;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000119 }
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000121 // PCH loaded successfully. Now create the preprocessor.
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000123 // Get information about the target being compiled for.
124 AST->Target.reset(TargetInfo::CreateTargetInfo(TargetTriple));
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000125 AST->PP.reset(new Preprocessor(AST->Diags, LangInfo, *AST->Target.get(),
126 AST->getSourceManager(), HeaderInfo));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000127 Preprocessor &PP = *AST->PP.get();
128
Daniel Dunbard5b61262009-09-21 03:03:47 +0000129 PP.setPredefines(Reader->getSuggestedPredefines());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000130 PP.setCounterValue(Counter);
Daniel Dunbarcc318932009-09-03 05:59:35 +0000131 Reader->setPreprocessor(PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000133 // Create and initialize the ASTContext.
134
135 AST->Ctx.reset(new ASTContext(LangInfo,
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000136 AST->getSourceManager(),
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000137 *AST->Target.get(),
138 PP.getIdentifierTable(),
139 PP.getSelectorTable(),
140 PP.getBuiltinInfo(),
141 /* FreeMemory = */ true,
142 /* size_reserve = */0));
143 ASTContext &Context = *AST->Ctx.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Daniel Dunbarcc318932009-09-03 05:59:35 +0000145 Reader->InitializeContext(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000147 // Attach the PCH reader to the AST context as an external AST
148 // source, so that declarations will be deserialized from the
149 // PCH file as needed.
Daniel Dunbarcc318932009-09-03 05:59:35 +0000150 Source.reset(Reader.take());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000151 Context.setExternalSource(Source);
152
Mike Stump1eb44332009-09-09 15:08:12 +0000153 return AST.take();
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000154}