blob: 084fa4d3454ef3e51b820fb1dae519f7e10aae3a [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 Naroffe19944c2009-10-15 22:23:48 +000028ASTUnit::ASTUnit(Diagnostic &_Diags) : Diags(_Diags), tempFile(false) { }
29ASTUnit::~ASTUnit() {
30 if (tempFile)
Benjamin Kramer4a630d32009-10-18 11:34:14 +000031 llvm::sys::Path(getPCHFileName()).eraseFromDisk();
Steve Naroffe19944c2009-10-15 22:23:48 +000032}
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000033
34namespace {
35
36/// \brief Gathers information from PCHReader that will be used to initialize
37/// a Preprocessor.
38class VISIBILITY_HIDDEN PCHInfoCollector : public PCHReaderListener {
39 LangOptions &LangOpt;
40 HeaderSearch &HSI;
41 std::string &TargetTriple;
42 std::string &Predefines;
43 unsigned &Counter;
Mike Stump1eb44332009-09-09 15:08:12 +000044
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000045 unsigned NumHeaderInfos;
Mike Stump1eb44332009-09-09 15:08:12 +000046
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000047public:
48 PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
49 std::string &TargetTriple, std::string &Predefines,
50 unsigned &Counter)
51 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
52 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +000053
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000054 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
55 LangOpt = LangOpts;
56 return false;
57 }
Mike Stump1eb44332009-09-09 15:08:12 +000058
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000059 virtual bool ReadTargetTriple(const std::string &Triple) {
60 TargetTriple = Triple;
61 return false;
62 }
Mike Stump1eb44332009-09-09 15:08:12 +000063
64 virtual bool ReadPredefinesBuffer(const char *PCHPredef,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000065 unsigned PCHPredefLen,
66 FileID PCHBufferID,
67 std::string &SuggestedPredefines) {
68 Predefines = PCHPredef;
69 return false;
70 }
Mike Stump1eb44332009-09-09 15:08:12 +000071
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000072 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) {
73 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
74 }
Mike Stump1eb44332009-09-09 15:08:12 +000075
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000076 virtual void ReadCounter(unsigned Value) {
77 Counter = Value;
78 }
79};
80
81} // anonymous namespace
82
Steve Naroff77accc12009-09-03 18:19:54 +000083const std::string &ASTUnit::getOriginalSourceFileName() {
84 return dyn_cast<PCHReader>(Ctx->getExternalSource())->getOriginalSourceFile();
85}
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000086
Steve Naroffe19944c2009-10-15 22:23:48 +000087const std::string &ASTUnit::getPCHFileName() {
88 return dyn_cast<PCHReader>(Ctx->getExternalSource())->getFileName();
89}
90
Steve Naroff9efa7672009-09-04 15:44:05 +000091FileManager &ASTUnit::getFileManager() {
92 return HeaderInfo->getFileMgr();
93}
94
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000095ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
Daniel Dunbar31b87d82009-09-21 03:03:39 +000096 Diagnostic &Diags,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000097 FileManager &FileMgr,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000098 std::string *ErrMsg,
Ted Kremenek5cf48762009-10-17 00:34:24 +000099 bool OnlyLocalDecls,
100 bool UseBumpAllocator) {
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000101 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(Diags));
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000102 AST->OnlyLocalDecls = OnlyLocalDecls;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000103 AST->HeaderInfo.reset(new HeaderSearch(FileMgr));
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
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000116 Reader.reset(new PCHReader(AST->getSourceManager(), FileMgr, 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}