blob: e3cd6ddd08d67d0ba7210f684abf6c09a3cc1625 [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"
Daniel Dunbard58c03f2009-11-15 06:48:46 +000021#include "clang/Basic/TargetOptions.h"
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000022#include "clang/Basic/TargetInfo.h"
23#include "clang/Basic/Diagnostic.h"
24#include "llvm/Support/Compiler.h"
Benjamin Kramer4a630d32009-10-18 11:34:14 +000025#include "llvm/System/Path.h"
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000026
27using namespace clang;
28
Ted Kremenekfc062212009-10-19 21:44:57 +000029ASTUnit::ASTUnit(DiagnosticClient *diagClient) : tempFile(false) {
30 Diags.setClient(diagClient ? diagClient : new TextDiagnosticBuffer());
Steve Naroff36c44642009-10-19 14:34:22 +000031}
Steve Naroffe19944c2009-10-15 22:23:48 +000032ASTUnit::~ASTUnit() {
33 if (tempFile)
Benjamin Kramer4a630d32009-10-18 11:34:14 +000034 llvm::sys::Path(getPCHFileName()).eraseFromDisk();
Ted Kremenekfc062212009-10-19 21:44:57 +000035
36 // The ASTUnit object owns the DiagnosticClient.
37 delete Diags.getClient();
Steve Naroffe19944c2009-10-15 22:23:48 +000038}
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000039
40namespace {
41
42/// \brief Gathers information from PCHReader that will be used to initialize
43/// a Preprocessor.
44class VISIBILITY_HIDDEN PCHInfoCollector : public PCHReaderListener {
45 LangOptions &LangOpt;
46 HeaderSearch &HSI;
47 std::string &TargetTriple;
48 std::string &Predefines;
49 unsigned &Counter;
Mike Stump1eb44332009-09-09 15:08:12 +000050
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000051 unsigned NumHeaderInfos;
Mike Stump1eb44332009-09-09 15:08:12 +000052
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000053public:
54 PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
55 std::string &TargetTriple, std::string &Predefines,
56 unsigned &Counter)
57 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
58 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +000059
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000060 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
61 LangOpt = LangOpts;
62 return false;
63 }
Mike Stump1eb44332009-09-09 15:08:12 +000064
Daniel Dunbardc3c0d22009-11-11 00:52:11 +000065 virtual bool ReadTargetTriple(llvm::StringRef Triple) {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000066 TargetTriple = Triple;
67 return false;
68 }
Mike Stump1eb44332009-09-09 15:08:12 +000069
Daniel Dunbardc3c0d22009-11-11 00:52:11 +000070 virtual bool ReadPredefinesBuffer(llvm::StringRef PCHPredef,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000071 FileID PCHBufferID,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +000072 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000073 std::string &SuggestedPredefines) {
74 Predefines = PCHPredef;
75 return false;
76 }
Mike Stump1eb44332009-09-09 15:08:12 +000077
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000078 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) {
79 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
80 }
Mike Stump1eb44332009-09-09 15:08:12 +000081
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000082 virtual void ReadCounter(unsigned Value) {
83 Counter = Value;
84 }
85};
86
87} // anonymous namespace
88
Steve Naroff77accc12009-09-03 18:19:54 +000089const std::string &ASTUnit::getOriginalSourceFileName() {
90 return dyn_cast<PCHReader>(Ctx->getExternalSource())->getOriginalSourceFile();
91}
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000092
Steve Naroffe19944c2009-10-15 22:23:48 +000093const std::string &ASTUnit::getPCHFileName() {
94 return dyn_cast<PCHReader>(Ctx->getExternalSource())->getFileName();
95}
96
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000097ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000098 std::string *ErrMsg,
Ted Kremenekfc062212009-10-19 21:44:57 +000099 DiagnosticClient *diagClient,
Ted Kremenek5cf48762009-10-17 00:34:24 +0000100 bool OnlyLocalDecls,
101 bool UseBumpAllocator) {
Ted Kremenekfc062212009-10-19 21:44:57 +0000102 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(diagClient));
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000103 AST->OnlyLocalDecls = OnlyLocalDecls;
Steve Naroff36c44642009-10-19 14:34:22 +0000104 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000105
106 // Gather Info for preprocessor construction later on.
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000108 LangOptions LangInfo;
109 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
110 std::string TargetTriple;
111 std::string Predefines;
112 unsigned Counter;
113
Daniel Dunbarbce6f622009-09-03 05:59:50 +0000114 llvm::OwningPtr<PCHReader> Reader;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000115 llvm::OwningPtr<ExternalASTSource> Source;
116
Ted Kremenekfc062212009-10-19 21:44:57 +0000117 Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(),
118 AST->Diags));
Daniel Dunbarcc318932009-09-03 05:59:35 +0000119 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
120 Predefines, Counter));
121
122 switch (Reader->ReadPCH(Filename)) {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000123 case PCHReader::Success:
124 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000126 case PCHReader::Failure:
Argyrios Kyrtzidis106c9982009-06-25 18:22:30 +0000127 case PCHReader::IgnorePCH:
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000128 if (ErrMsg)
129 *ErrMsg = "Could not load PCH file";
130 return NULL;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000131 }
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000133 // PCH loaded successfully. Now create the preprocessor.
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000135 // Get information about the target being compiled for.
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000136 //
137 // FIXME: This is broken, we should store the TargetOptions in the PCH.
138 TargetOptions TargetOpts;
139 TargetOpts.ABI = "";
140 TargetOpts.CPU = "";
141 TargetOpts.Features.clear();
142 TargetOpts.Triple = TargetTriple;
143 AST->Target.reset(TargetInfo::CreateTargetInfo(AST->Diags, TargetOpts));
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000144 AST->PP.reset(new Preprocessor(AST->Diags, LangInfo, *AST->Target.get(),
145 AST->getSourceManager(), HeaderInfo));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000146 Preprocessor &PP = *AST->PP.get();
147
Daniel Dunbard5b61262009-09-21 03:03:47 +0000148 PP.setPredefines(Reader->getSuggestedPredefines());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000149 PP.setCounterValue(Counter);
Daniel Dunbarcc318932009-09-03 05:59:35 +0000150 Reader->setPreprocessor(PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000152 // Create and initialize the ASTContext.
153
154 AST->Ctx.reset(new ASTContext(LangInfo,
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000155 AST->getSourceManager(),
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000156 *AST->Target.get(),
157 PP.getIdentifierTable(),
158 PP.getSelectorTable(),
159 PP.getBuiltinInfo(),
Ted Kremenek5cf48762009-10-17 00:34:24 +0000160 /* FreeMemory = */ !UseBumpAllocator,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000161 /* size_reserve = */0));
162 ASTContext &Context = *AST->Ctx.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Daniel Dunbarcc318932009-09-03 05:59:35 +0000164 Reader->InitializeContext(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000166 // Attach the PCH reader to the AST context as an external AST
167 // source, so that declarations will be deserialized from the
168 // PCH file as needed.
Daniel Dunbarcc318932009-09-03 05:59:35 +0000169 Source.reset(Reader.take());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000170 Context.setExternalSource(Source);
171
Mike Stump1eb44332009-09-09 15:08:12 +0000172 return AST.take();
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000173}