blob: 3b2aa75008e5b136e730fcb61cc86ff7a1a03968 [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
Steve Naroff44cd60e2009-10-15 22:23:48 +000027ASTUnit::ASTUnit(Diagnostic &_Diags) : Diags(_Diags), tempFile(false) { }
28ASTUnit::~ASTUnit() {
29 if (tempFile)
30 unlink(getPCHFileName().c_str());
31}
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000032
33namespace {
34
35/// \brief Gathers information from PCHReader that will be used to initialize
36/// a Preprocessor.
37class VISIBILITY_HIDDEN PCHInfoCollector : public PCHReaderListener {
38 LangOptions &LangOpt;
39 HeaderSearch &HSI;
40 std::string &TargetTriple;
41 std::string &Predefines;
42 unsigned &Counter;
Mike Stump11289f42009-09-09 15:08:12 +000043
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000044 unsigned NumHeaderInfos;
Mike Stump11289f42009-09-09 15:08:12 +000045
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000046public:
47 PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
48 std::string &TargetTriple, std::string &Predefines,
49 unsigned &Counter)
50 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
51 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
Mike Stump11289f42009-09-09 15:08:12 +000052
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000053 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
54 LangOpt = LangOpts;
55 return false;
56 }
Mike Stump11289f42009-09-09 15:08:12 +000057
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000058 virtual bool ReadTargetTriple(const std::string &Triple) {
59 TargetTriple = Triple;
60 return false;
61 }
Mike Stump11289f42009-09-09 15:08:12 +000062
63 virtual bool ReadPredefinesBuffer(const char *PCHPredef,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000064 unsigned PCHPredefLen,
65 FileID PCHBufferID,
66 std::string &SuggestedPredefines) {
67 Predefines = PCHPredef;
68 return false;
69 }
Mike Stump11289f42009-09-09 15:08:12 +000070
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000071 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) {
72 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
73 }
Mike Stump11289f42009-09-09 15:08:12 +000074
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000075 virtual void ReadCounter(unsigned Value) {
76 Counter = Value;
77 }
78};
79
80} // anonymous namespace
81
Steve Naroffc0683b92009-09-03 18:19:54 +000082const std::string &ASTUnit::getOriginalSourceFileName() {
83 return dyn_cast<PCHReader>(Ctx->getExternalSource())->getOriginalSourceFile();
84}
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000085
Steve Naroff44cd60e2009-10-15 22:23:48 +000086const std::string &ASTUnit::getPCHFileName() {
87 return dyn_cast<PCHReader>(Ctx->getExternalSource())->getFileName();
88}
89
Steve Naroffef9618b2009-09-04 15:44:05 +000090FileManager &ASTUnit::getFileManager() {
91 return HeaderInfo->getFileMgr();
92}
93
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000094ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
Daniel Dunbar7cd285f2009-09-21 03:03:39 +000095 Diagnostic &Diags,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000096 FileManager &FileMgr,
97 std::string *ErrMsg) {
Daniel Dunbar7cd285f2009-09-21 03:03:39 +000098 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(Diags));
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000099
100 AST->HeaderInfo.reset(new HeaderSearch(FileMgr));
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000101
102 // Gather Info for preprocessor construction later on.
Mike Stump11289f42009-09-09 15:08:12 +0000103
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000104 LangOptions LangInfo;
105 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
106 std::string TargetTriple;
107 std::string Predefines;
108 unsigned Counter;
109
Daniel Dunbar3a0637b2009-09-03 05:59:50 +0000110 llvm::OwningPtr<PCHReader> Reader;
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000111 llvm::OwningPtr<ExternalASTSource> Source;
112
Daniel Dunbar7cd285f2009-09-21 03:03:39 +0000113 Reader.reset(new PCHReader(AST->getSourceManager(), FileMgr, AST->Diags));
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000114 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
115 Predefines, Counter));
116
117 switch (Reader->ReadPCH(Filename)) {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000118 case PCHReader::Success:
119 break;
Mike Stump11289f42009-09-09 15:08:12 +0000120
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000121 case PCHReader::Failure:
Argyrios Kyrtzidis55c34112009-06-25 18:22:30 +0000122 case PCHReader::IgnorePCH:
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000123 if (ErrMsg)
124 *ErrMsg = "Could not load PCH file";
125 return NULL;
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000126 }
Mike Stump11289f42009-09-09 15:08:12 +0000127
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000128 // PCH loaded successfully. Now create the preprocessor.
Mike Stump11289f42009-09-09 15:08:12 +0000129
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000130 // Get information about the target being compiled for.
131 AST->Target.reset(TargetInfo::CreateTargetInfo(TargetTriple));
Daniel Dunbar7cd285f2009-09-21 03:03:39 +0000132 AST->PP.reset(new Preprocessor(AST->Diags, LangInfo, *AST->Target.get(),
133 AST->getSourceManager(), HeaderInfo));
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000134 Preprocessor &PP = *AST->PP.get();
135
Daniel Dunbarb7bbfdd2009-09-21 03:03:47 +0000136 PP.setPredefines(Reader->getSuggestedPredefines());
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000137 PP.setCounterValue(Counter);
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000138 Reader->setPreprocessor(PP);
Mike Stump11289f42009-09-09 15:08:12 +0000139
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000140 // Create and initialize the ASTContext.
141
142 AST->Ctx.reset(new ASTContext(LangInfo,
Daniel Dunbar7cd285f2009-09-21 03:03:39 +0000143 AST->getSourceManager(),
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000144 *AST->Target.get(),
145 PP.getIdentifierTable(),
146 PP.getSelectorTable(),
147 PP.getBuiltinInfo(),
148 /* FreeMemory = */ true,
149 /* size_reserve = */0));
150 ASTContext &Context = *AST->Ctx.get();
Mike Stump11289f42009-09-09 15:08:12 +0000151
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000152 Reader->InitializeContext(Context);
Mike Stump11289f42009-09-09 15:08:12 +0000153
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000154 // Attach the PCH reader to the AST context as an external AST
155 // source, so that declarations will be deserialized from the
156 // PCH file as needed.
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000157 Source.reset(Reader.take());
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000158 Context.setExternalSource(Source);
159
Mike Stump11289f42009-09-09 15:08:12 +0000160 return AST.take();
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000161}