blob: a7a62fb93a899bc28b84aabef6b41ef23e1ed728 [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"
Benjamin Kramer4a630d32009-10-18 11:34:14 +000024#include "llvm/System/Path.h"
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000025
26using namespace clang;
27
Steve Naroff36c44642009-10-19 14:34:22 +000028ASTUnit::ASTUnit() : tempFile(false) {
29 Diags.setClient(&DiagClient);
30}
Steve Naroffe19944c2009-10-15 22:23:48 +000031ASTUnit::~ASTUnit() {
32 if (tempFile)
Benjamin Kramer4a630d32009-10-18 11:34:14 +000033 llvm::sys::Path(getPCHFileName()).eraseFromDisk();
Steve Naroffe19944c2009-10-15 22:23:48 +000034}
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000035
36namespace {
37
38/// \brief Gathers information from PCHReader that will be used to initialize
39/// a Preprocessor.
40class VISIBILITY_HIDDEN PCHInfoCollector : public PCHReaderListener {
41 LangOptions &LangOpt;
42 HeaderSearch &HSI;
43 std::string &TargetTriple;
44 std::string &Predefines;
45 unsigned &Counter;
Mike Stump1eb44332009-09-09 15:08:12 +000046
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000047 unsigned NumHeaderInfos;
Mike Stump1eb44332009-09-09 15:08:12 +000048
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000049public:
50 PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
51 std::string &TargetTriple, std::string &Predefines,
52 unsigned &Counter)
53 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
54 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +000055
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000056 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
57 LangOpt = LangOpts;
58 return false;
59 }
Mike Stump1eb44332009-09-09 15:08:12 +000060
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000061 virtual bool ReadTargetTriple(const std::string &Triple) {
62 TargetTriple = Triple;
63 return false;
64 }
Mike Stump1eb44332009-09-09 15:08:12 +000065
66 virtual bool ReadPredefinesBuffer(const char *PCHPredef,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000067 unsigned PCHPredefLen,
68 FileID PCHBufferID,
69 std::string &SuggestedPredefines) {
70 Predefines = PCHPredef;
71 return false;
72 }
Mike Stump1eb44332009-09-09 15:08:12 +000073
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000074 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) {
75 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
76 }
Mike Stump1eb44332009-09-09 15:08:12 +000077
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000078 virtual void ReadCounter(unsigned Value) {
79 Counter = Value;
80 }
81};
82
83} // anonymous namespace
84
Steve Naroff77accc12009-09-03 18:19:54 +000085const std::string &ASTUnit::getOriginalSourceFileName() {
86 return dyn_cast<PCHReader>(Ctx->getExternalSource())->getOriginalSourceFile();
87}
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000088
Steve Naroffe19944c2009-10-15 22:23:48 +000089const std::string &ASTUnit::getPCHFileName() {
90 return dyn_cast<PCHReader>(Ctx->getExternalSource())->getFileName();
91}
92
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000093ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000094 std::string *ErrMsg,
Ted Kremenek5cf48762009-10-17 00:34:24 +000095 bool OnlyLocalDecls,
96 bool UseBumpAllocator) {
Steve Naroff36c44642009-10-19 14:34:22 +000097 llvm::OwningPtr<ASTUnit> AST(new ASTUnit());
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000098 AST->OnlyLocalDecls = OnlyLocalDecls;
Steve Naroff36c44642009-10-19 14:34:22 +000099 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000100
101 // Gather Info for preprocessor construction later on.
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000103 LangOptions LangInfo;
104 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
105 std::string TargetTriple;
106 std::string Predefines;
107 unsigned Counter;
108
Daniel Dunbarbce6f622009-09-03 05:59:50 +0000109 llvm::OwningPtr<PCHReader> Reader;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000110 llvm::OwningPtr<ExternalASTSource> Source;
111
Steve Naroff36c44642009-10-19 14:34:22 +0000112 Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(), AST->Diags));
Daniel Dunbarcc318932009-09-03 05:59:35 +0000113 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
114 Predefines, Counter));
115
116 switch (Reader->ReadPCH(Filename)) {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000117 case PCHReader::Success:
118 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000120 case PCHReader::Failure:
Argyrios Kyrtzidis106c9982009-06-25 18:22:30 +0000121 case PCHReader::IgnorePCH:
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000122 if (ErrMsg)
123 *ErrMsg = "Could not load PCH file";
124 return NULL;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000125 }
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000127 // PCH loaded successfully. Now create the preprocessor.
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000129 // Get information about the target being compiled for.
130 AST->Target.reset(TargetInfo::CreateTargetInfo(TargetTriple));
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000131 AST->PP.reset(new Preprocessor(AST->Diags, LangInfo, *AST->Target.get(),
132 AST->getSourceManager(), HeaderInfo));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000133 Preprocessor &PP = *AST->PP.get();
134
Daniel Dunbard5b61262009-09-21 03:03:47 +0000135 PP.setPredefines(Reader->getSuggestedPredefines());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000136 PP.setCounterValue(Counter);
Daniel Dunbarcc318932009-09-03 05:59:35 +0000137 Reader->setPreprocessor(PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000139 // Create and initialize the ASTContext.
140
141 AST->Ctx.reset(new ASTContext(LangInfo,
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000142 AST->getSourceManager(),
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000143 *AST->Target.get(),
144 PP.getIdentifierTable(),
145 PP.getSelectorTable(),
146 PP.getBuiltinInfo(),
Ted Kremenek5cf48762009-10-17 00:34:24 +0000147 /* FreeMemory = */ !UseBumpAllocator,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000148 /* size_reserve = */0));
149 ASTContext &Context = *AST->Ctx.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Daniel Dunbarcc318932009-09-03 05:59:35 +0000151 Reader->InitializeContext(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000153 // Attach the PCH reader to the AST context as an external AST
154 // source, so that declarations will be deserialized from the
155 // PCH file as needed.
Daniel Dunbarcc318932009-09-03 05:59:35 +0000156 Source.reset(Reader.take());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000157 Context.setExternalSource(Source);
158
Mike Stump1eb44332009-09-09 15:08:12 +0000159 return AST.take();
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000160}