blob: c0415bf550f886b0809ffca500c6223071233a35 [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
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000064 virtual bool ReadTargetTriple(const std::string &Triple) {
65 TargetTriple = Triple;
66 return false;
67 }
Mike Stump1eb44332009-09-09 15:08:12 +000068
69 virtual bool ReadPredefinesBuffer(const char *PCHPredef,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000070 unsigned PCHPredefLen,
71 FileID PCHBufferID,
72 std::string &SuggestedPredefines) {
73 Predefines = PCHPredef;
74 return false;
75 }
Mike Stump1eb44332009-09-09 15:08:12 +000076
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000077 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) {
78 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
79 }
Mike Stump1eb44332009-09-09 15:08:12 +000080
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000081 virtual void ReadCounter(unsigned Value) {
82 Counter = Value;
83 }
84};
85
86} // anonymous namespace
87
Steve Naroff77accc12009-09-03 18:19:54 +000088const std::string &ASTUnit::getOriginalSourceFileName() {
89 return dyn_cast<PCHReader>(Ctx->getExternalSource())->getOriginalSourceFile();
90}
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000091
Steve Naroffe19944c2009-10-15 22:23:48 +000092const std::string &ASTUnit::getPCHFileName() {
93 return dyn_cast<PCHReader>(Ctx->getExternalSource())->getFileName();
94}
95
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000096ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000097 std::string *ErrMsg,
Ted Kremenekfc062212009-10-19 21:44:57 +000098 DiagnosticClient *diagClient,
Ted Kremenek5cf48762009-10-17 00:34:24 +000099 bool OnlyLocalDecls,
100 bool UseBumpAllocator) {
Ted Kremenekfc062212009-10-19 21:44:57 +0000101 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(diagClient));
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000102 AST->OnlyLocalDecls = OnlyLocalDecls;
Steve Naroff36c44642009-10-19 14:34:22 +0000103 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000104
105 // Gather Info for preprocessor construction later on.
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000107 LangOptions LangInfo;
108 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
109 std::string TargetTriple;
110 std::string Predefines;
111 unsigned Counter;
112
Daniel Dunbarbce6f622009-09-03 05:59:50 +0000113 llvm::OwningPtr<PCHReader> Reader;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000114 llvm::OwningPtr<ExternalASTSource> Source;
115
Ted Kremenekfc062212009-10-19 21:44:57 +0000116 Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(),
117 AST->Diags));
Daniel Dunbarcc318932009-09-03 05:59:35 +0000118 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
119 Predefines, Counter));
120
121 switch (Reader->ReadPCH(Filename)) {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000122 case PCHReader::Success:
123 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000125 case PCHReader::Failure:
Argyrios Kyrtzidis106c9982009-06-25 18:22:30 +0000126 case PCHReader::IgnorePCH:
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000127 if (ErrMsg)
128 *ErrMsg = "Could not load PCH file";
129 return NULL;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000130 }
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000132 // PCH loaded successfully. Now create the preprocessor.
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000134 // Get information about the target being compiled for.
135 AST->Target.reset(TargetInfo::CreateTargetInfo(TargetTriple));
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000136 AST->PP.reset(new Preprocessor(AST->Diags, LangInfo, *AST->Target.get(),
137 AST->getSourceManager(), HeaderInfo));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000138 Preprocessor &PP = *AST->PP.get();
139
Daniel Dunbard5b61262009-09-21 03:03:47 +0000140 PP.setPredefines(Reader->getSuggestedPredefines());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000141 PP.setCounterValue(Counter);
Daniel Dunbarcc318932009-09-03 05:59:35 +0000142 Reader->setPreprocessor(PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000144 // Create and initialize the ASTContext.
145
146 AST->Ctx.reset(new ASTContext(LangInfo,
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000147 AST->getSourceManager(),
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000148 *AST->Target.get(),
149 PP.getIdentifierTable(),
150 PP.getSelectorTable(),
151 PP.getBuiltinInfo(),
Ted Kremenek5cf48762009-10-17 00:34:24 +0000152 /* FreeMemory = */ !UseBumpAllocator,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000153 /* size_reserve = */0));
154 ASTContext &Context = *AST->Ctx.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Daniel Dunbarcc318932009-09-03 05:59:35 +0000156 Reader->InitializeContext(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000158 // Attach the PCH reader to the AST context as an external AST
159 // source, so that declarations will be deserialized from the
160 // PCH file as needed.
Daniel Dunbarcc318932009-09-03 05:59:35 +0000161 Source.reset(Reader.take());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000162 Context.setExternalSource(Source);
163
Mike Stump1eb44332009-09-09 15:08:12 +0000164 return AST.take();
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000165}