blob: 7ee66486998881d36af9cf10d8c8efdf41a02971 [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
Steve Naroffe19944c2009-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 Kyrtzidis0853a022009-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 Stump1eb44332009-09-09 15:08:12 +000043
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000044 unsigned NumHeaderInfos;
Mike Stump1eb44332009-09-09 15:08:12 +000045
Argyrios Kyrtzidis0853a022009-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 Stump1eb44332009-09-09 15:08:12 +000052
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000053 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
54 LangOpt = LangOpts;
55 return false;
56 }
Mike Stump1eb44332009-09-09 15:08:12 +000057
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000058 virtual bool ReadTargetTriple(const std::string &Triple) {
59 TargetTriple = Triple;
60 return false;
61 }
Mike Stump1eb44332009-09-09 15:08:12 +000062
63 virtual bool ReadPredefinesBuffer(const char *PCHPredef,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000064 unsigned PCHPredefLen,
65 FileID PCHBufferID,
66 std::string &SuggestedPredefines) {
67 Predefines = PCHPredef;
68 return false;
69 }
Mike Stump1eb44332009-09-09 15:08:12 +000070
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000071 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) {
72 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
73 }
Mike Stump1eb44332009-09-09 15:08:12 +000074
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000075 virtual void ReadCounter(unsigned Value) {
76 Counter = Value;
77 }
78};
79
80} // anonymous namespace
81
Steve Naroff77accc12009-09-03 18:19:54 +000082const std::string &ASTUnit::getOriginalSourceFileName() {
83 return dyn_cast<PCHReader>(Ctx->getExternalSource())->getOriginalSourceFile();
84}
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000085
Steve Naroffe19944c2009-10-15 22:23:48 +000086const std::string &ASTUnit::getPCHFileName() {
87 return dyn_cast<PCHReader>(Ctx->getExternalSource())->getFileName();
88}
89
Steve Naroff9efa7672009-09-04 15:44:05 +000090FileManager &ASTUnit::getFileManager() {
91 return HeaderInfo->getFileMgr();
92}
93
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000094ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
Daniel Dunbar31b87d82009-09-21 03:03:39 +000095 Diagnostic &Diags,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000096 FileManager &FileMgr,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000097 std::string *ErrMsg,
Ted Kremenek5cf48762009-10-17 00:34:24 +000098 bool OnlyLocalDecls,
99 bool UseBumpAllocator) {
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000100 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(Diags));
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000101 AST->OnlyLocalDecls = OnlyLocalDecls;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000102 AST->HeaderInfo.reset(new HeaderSearch(FileMgr));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000103
104 // Gather Info for preprocessor construction later on.
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000106 LangOptions LangInfo;
107 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
108 std::string TargetTriple;
109 std::string Predefines;
110 unsigned Counter;
111
Daniel Dunbarbce6f622009-09-03 05:59:50 +0000112 llvm::OwningPtr<PCHReader> Reader;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000113 llvm::OwningPtr<ExternalASTSource> Source;
114
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000115 Reader.reset(new PCHReader(AST->getSourceManager(), FileMgr, AST->Diags));
Daniel Dunbarcc318932009-09-03 05:59:35 +0000116 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
117 Predefines, Counter));
118
119 switch (Reader->ReadPCH(Filename)) {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000120 case PCHReader::Success:
121 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000123 case PCHReader::Failure:
Argyrios Kyrtzidis106c9982009-06-25 18:22:30 +0000124 case PCHReader::IgnorePCH:
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000125 if (ErrMsg)
126 *ErrMsg = "Could not load PCH file";
127 return NULL;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000128 }
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000130 // PCH loaded successfully. Now create the preprocessor.
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000132 // Get information about the target being compiled for.
133 AST->Target.reset(TargetInfo::CreateTargetInfo(TargetTriple));
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000134 AST->PP.reset(new Preprocessor(AST->Diags, LangInfo, *AST->Target.get(),
135 AST->getSourceManager(), HeaderInfo));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000136 Preprocessor &PP = *AST->PP.get();
137
Daniel Dunbard5b61262009-09-21 03:03:47 +0000138 PP.setPredefines(Reader->getSuggestedPredefines());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000139 PP.setCounterValue(Counter);
Daniel Dunbarcc318932009-09-03 05:59:35 +0000140 Reader->setPreprocessor(PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000142 // Create and initialize the ASTContext.
143
144 AST->Ctx.reset(new ASTContext(LangInfo,
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000145 AST->getSourceManager(),
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000146 *AST->Target.get(),
147 PP.getIdentifierTable(),
148 PP.getSelectorTable(),
149 PP.getBuiltinInfo(),
Ted Kremenek5cf48762009-10-17 00:34:24 +0000150 /* FreeMemory = */ !UseBumpAllocator,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000151 /* size_reserve = */0));
152 ASTContext &Context = *AST->Ctx.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Daniel Dunbarcc318932009-09-03 05:59:35 +0000154 Reader->InitializeContext(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000156 // Attach the PCH reader to the AST context as an external AST
157 // source, so that declarations will be deserialized from the
158 // PCH file as needed.
Daniel Dunbarcc318932009-09-03 05:59:35 +0000159 Source.reset(Reader.take());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000160 Context.setExternalSource(Source);
161
Mike Stump1eb44332009-09-09 15:08:12 +0000162 return AST.take();
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000163}