blob: 1e2ad46b93a647d4302a3183a2e197a78fb88082 [file] [log] [blame]
Argyrios Kyrtzidis3a08ec12009-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 Kyrtzidisce379752009-06-20 08:08:23 +000014#include "clang/Frontend/ASTUnit.h"
15#include "clang/Frontend/PCHReader.h"
Argyrios Kyrtzidisce379752009-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 Dunbar7cd285f2009-09-21 03:03:39 +000027ASTUnit::ASTUnit(Diagnostic &_Diags) : Diags(_Diags) { }
Argyrios Kyrtzidisce379752009-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 Stump11289f42009-09-09 15:08:12 +000040
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000041 unsigned NumHeaderInfos;
Mike Stump11289f42009-09-09 15:08:12 +000042
Argyrios Kyrtzidisce379752009-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 Stump11289f42009-09-09 15:08:12 +000049
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000050 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
51 LangOpt = LangOpts;
52 return false;
53 }
Mike Stump11289f42009-09-09 15:08:12 +000054
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000055 virtual bool ReadTargetTriple(const std::string &Triple) {
56 TargetTriple = Triple;
57 return false;
58 }
Mike Stump11289f42009-09-09 15:08:12 +000059
60 virtual bool ReadPredefinesBuffer(const char *PCHPredef,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000061 unsigned PCHPredefLen,
62 FileID PCHBufferID,
63 std::string &SuggestedPredefines) {
64 Predefines = PCHPredef;
65 return false;
66 }
Mike Stump11289f42009-09-09 15:08:12 +000067
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000068 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) {
69 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
70 }
Mike Stump11289f42009-09-09 15:08:12 +000071
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000072 virtual void ReadCounter(unsigned Value) {
73 Counter = Value;
74 }
75};
76
77} // anonymous namespace
78
Steve Naroffc0683b92009-09-03 18:19:54 +000079const std::string &ASTUnit::getOriginalSourceFileName() {
80 return dyn_cast<PCHReader>(Ctx->getExternalSource())->getOriginalSourceFile();
81}
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000082
Steve Naroffef9618b2009-09-04 15:44:05 +000083FileManager &ASTUnit::getFileManager() {
84 return HeaderInfo->getFileMgr();
85}
86
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000087ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
Daniel Dunbar7cd285f2009-09-21 03:03:39 +000088 Diagnostic &Diags,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000089 FileManager &FileMgr,
90 std::string *ErrMsg) {
Daniel Dunbar7cd285f2009-09-21 03:03:39 +000091 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(Diags));
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000092
93 AST->HeaderInfo.reset(new HeaderSearch(FileMgr));
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000094
95 // Gather Info for preprocessor construction later on.
Mike Stump11289f42009-09-09 15:08:12 +000096
Argyrios Kyrtzidisce379752009-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 Dunbar3a0637b2009-09-03 05:59:50 +0000103 llvm::OwningPtr<PCHReader> Reader;
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000104 llvm::OwningPtr<ExternalASTSource> Source;
105
Daniel Dunbar7cd285f2009-09-21 03:03:39 +0000106 Reader.reset(new PCHReader(AST->getSourceManager(), FileMgr, AST->Diags));
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000107 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
108 Predefines, Counter));
109
110 switch (Reader->ReadPCH(Filename)) {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000111 case PCHReader::Success:
112 break;
Mike Stump11289f42009-09-09 15:08:12 +0000113
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000114 case PCHReader::Failure:
Argyrios Kyrtzidis55c34112009-06-25 18:22:30 +0000115 case PCHReader::IgnorePCH:
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000116 if (ErrMsg)
117 *ErrMsg = "Could not load PCH file";
118 return NULL;
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000119 }
Mike Stump11289f42009-09-09 15:08:12 +0000120
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000121 // PCH loaded successfully. Now create the preprocessor.
Mike Stump11289f42009-09-09 15:08:12 +0000122
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000123 // Get information about the target being compiled for.
124 AST->Target.reset(TargetInfo::CreateTargetInfo(TargetTriple));
Daniel Dunbar7cd285f2009-09-21 03:03:39 +0000125 AST->PP.reset(new Preprocessor(AST->Diags, LangInfo, *AST->Target.get(),
126 AST->getSourceManager(), HeaderInfo));
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000127 Preprocessor &PP = *AST->PP.get();
128
129 PP.setPredefines(Predefines);
130 PP.setCounterValue(Counter);
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000131 Reader->setPreprocessor(PP);
Mike Stump11289f42009-09-09 15:08:12 +0000132
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000133 // Create and initialize the ASTContext.
134
135 AST->Ctx.reset(new ASTContext(LangInfo,
Daniel Dunbar7cd285f2009-09-21 03:03:39 +0000136 AST->getSourceManager(),
Argyrios Kyrtzidisce379752009-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 Stump11289f42009-09-09 15:08:12 +0000144
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000145 Reader->InitializeContext(Context);
Mike Stump11289f42009-09-09 15:08:12 +0000146
Argyrios Kyrtzidisce379752009-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 Dunbar2d9c7402009-09-03 05:59:35 +0000150 Source.reset(Reader.take());
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000151 Context.setExternalSource(Source);
152
Mike Stump11289f42009-09-09 15:08:12 +0000153 return AST.take();
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000154}