blob: b5e65589e32334d6fa5d4e15c7c06c3de60a9d75 [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
Ted Kremenekfc062212009-10-19 21:44:57 +000028ASTUnit::ASTUnit(DiagnosticClient *diagClient) : tempFile(false) {
29 Diags.setClient(diagClient ? diagClient : new TextDiagnosticBuffer());
Steve Naroff36c44642009-10-19 14:34:22 +000030}
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();
Ted Kremenekfc062212009-10-19 21:44:57 +000034
35 // The ASTUnit object owns the DiagnosticClient.
36 delete Diags.getClient();
Steve Naroffe19944c2009-10-15 22:23:48 +000037}
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000038
39namespace {
40
41/// \brief Gathers information from PCHReader that will be used to initialize
42/// a Preprocessor.
43class VISIBILITY_HIDDEN PCHInfoCollector : public PCHReaderListener {
44 LangOptions &LangOpt;
45 HeaderSearch &HSI;
46 std::string &TargetTriple;
47 std::string &Predefines;
48 unsigned &Counter;
Mike Stump1eb44332009-09-09 15:08:12 +000049
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000050 unsigned NumHeaderInfos;
Mike Stump1eb44332009-09-09 15:08:12 +000051
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000052public:
53 PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
54 std::string &TargetTriple, std::string &Predefines,
55 unsigned &Counter)
56 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
57 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +000058
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000059 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
60 LangOpt = LangOpts;
61 return false;
62 }
Mike Stump1eb44332009-09-09 15:08:12 +000063
Daniel Dunbardc3c0d22009-11-11 00:52:11 +000064 virtual bool ReadTargetTriple(llvm::StringRef Triple) {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000065 TargetTriple = Triple;
66 return false;
67 }
Mike Stump1eb44332009-09-09 15:08:12 +000068
Daniel Dunbardc3c0d22009-11-11 00:52:11 +000069 virtual bool ReadPredefinesBuffer(llvm::StringRef PCHPredef,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000070 FileID PCHBufferID,
71 std::string &SuggestedPredefines) {
72 Predefines = PCHPredef;
73 return false;
74 }
Mike Stump1eb44332009-09-09 15:08:12 +000075
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000076 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) {
77 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
78 }
Mike Stump1eb44332009-09-09 15:08:12 +000079
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000080 virtual void ReadCounter(unsigned Value) {
81 Counter = Value;
82 }
83};
84
85} // anonymous namespace
86
Steve Naroff77accc12009-09-03 18:19:54 +000087const std::string &ASTUnit::getOriginalSourceFileName() {
88 return dyn_cast<PCHReader>(Ctx->getExternalSource())->getOriginalSourceFile();
89}
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000090
Steve Naroffe19944c2009-10-15 22:23:48 +000091const std::string &ASTUnit::getPCHFileName() {
92 return dyn_cast<PCHReader>(Ctx->getExternalSource())->getFileName();
93}
94
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000095ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000096 std::string *ErrMsg,
Ted Kremenekfc062212009-10-19 21:44:57 +000097 DiagnosticClient *diagClient,
Ted Kremenek5cf48762009-10-17 00:34:24 +000098 bool OnlyLocalDecls,
99 bool UseBumpAllocator) {
Ted Kremenekfc062212009-10-19 21:44:57 +0000100 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(diagClient));
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000101 AST->OnlyLocalDecls = OnlyLocalDecls;
Steve Naroff36c44642009-10-19 14:34:22 +0000102 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
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
Ted Kremenekfc062212009-10-19 21:44:57 +0000115 Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(),
116 AST->Diags));
Daniel Dunbarcc318932009-09-03 05:59:35 +0000117 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
118 Predefines, Counter));
119
120 switch (Reader->ReadPCH(Filename)) {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000121 case PCHReader::Success:
122 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000124 case PCHReader::Failure:
Argyrios Kyrtzidis106c9982009-06-25 18:22:30 +0000125 case PCHReader::IgnorePCH:
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000126 if (ErrMsg)
127 *ErrMsg = "Could not load PCH file";
128 return NULL;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000129 }
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000131 // PCH loaded successfully. Now create the preprocessor.
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000133 // Get information about the target being compiled for.
134 AST->Target.reset(TargetInfo::CreateTargetInfo(TargetTriple));
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000135 AST->PP.reset(new Preprocessor(AST->Diags, LangInfo, *AST->Target.get(),
136 AST->getSourceManager(), HeaderInfo));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000137 Preprocessor &PP = *AST->PP.get();
138
Daniel Dunbard5b61262009-09-21 03:03:47 +0000139 PP.setPredefines(Reader->getSuggestedPredefines());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000140 PP.setCounterValue(Counter);
Daniel Dunbarcc318932009-09-03 05:59:35 +0000141 Reader->setPreprocessor(PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000143 // Create and initialize the ASTContext.
144
145 AST->Ctx.reset(new ASTContext(LangInfo,
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000146 AST->getSourceManager(),
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000147 *AST->Target.get(),
148 PP.getIdentifierTable(),
149 PP.getSelectorTable(),
150 PP.getBuiltinInfo(),
Ted Kremenek5cf48762009-10-17 00:34:24 +0000151 /* FreeMemory = */ !UseBumpAllocator,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000152 /* size_reserve = */0));
153 ASTContext &Context = *AST->Ctx.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Daniel Dunbarcc318932009-09-03 05:59:35 +0000155 Reader->InitializeContext(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000157 // Attach the PCH reader to the AST context as an external AST
158 // source, so that declarations will be deserialized from the
159 // PCH file as needed.
Daniel Dunbarcc318932009-09-03 05:59:35 +0000160 Source.reset(Reader.take());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000161 Context.setExternalSource(Source);
162
Mike Stump1eb44332009-09-09 15:08:12 +0000163 return AST.take();
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000164}