blob: 084fa4d3454ef3e51b820fb1dae519f7e10aae3a [file] [log] [blame]
Argyrios Kyrtzidis3a08ec12009-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 Kyrtzidisce379752009-06-20 08:08:23 +000014#include "clang/Frontend/ASTUnit.h"
15#include "clang/Frontend/PCHReader.h"
Argyrios Kyrtzidisce379752009-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 Kramer6c839f82009-10-18 11:34:14 +000024#include "llvm/System/Path.h"
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000025
26using namespace clang;
27
Steve Naroff44cd60e2009-10-15 22:23:48 +000028ASTUnit::ASTUnit(Diagnostic &_Diags) : Diags(_Diags), tempFile(false) { }
29ASTUnit::~ASTUnit() {
30 if (tempFile)
Benjamin Kramer6c839f82009-10-18 11:34:14 +000031 llvm::sys::Path(getPCHFileName()).eraseFromDisk();
Steve Naroff44cd60e2009-10-15 22:23:48 +000032}
Argyrios Kyrtzidisce379752009-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 Stump11289f42009-09-09 15:08:12 +000044
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000045 unsigned NumHeaderInfos;
Mike Stump11289f42009-09-09 15:08:12 +000046
Argyrios Kyrtzidisce379752009-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 Stump11289f42009-09-09 15:08:12 +000053
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000054 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
55 LangOpt = LangOpts;
56 return false;
57 }
Mike Stump11289f42009-09-09 15:08:12 +000058
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000059 virtual bool ReadTargetTriple(const std::string &Triple) {
60 TargetTriple = Triple;
61 return false;
62 }
Mike Stump11289f42009-09-09 15:08:12 +000063
64 virtual bool ReadPredefinesBuffer(const char *PCHPredef,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000065 unsigned PCHPredefLen,
66 FileID PCHBufferID,
67 std::string &SuggestedPredefines) {
68 Predefines = PCHPredef;
69 return false;
70 }
Mike Stump11289f42009-09-09 15:08:12 +000071
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000072 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) {
73 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
74 }
Mike Stump11289f42009-09-09 15:08:12 +000075
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000076 virtual void ReadCounter(unsigned Value) {
77 Counter = Value;
78 }
79};
80
81} // anonymous namespace
82
Steve Naroffc0683b92009-09-03 18:19:54 +000083const std::string &ASTUnit::getOriginalSourceFileName() {
84 return dyn_cast<PCHReader>(Ctx->getExternalSource())->getOriginalSourceFile();
85}
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000086
Steve Naroff44cd60e2009-10-15 22:23:48 +000087const std::string &ASTUnit::getPCHFileName() {
88 return dyn_cast<PCHReader>(Ctx->getExternalSource())->getFileName();
89}
90
Steve Naroffef9618b2009-09-04 15:44:05 +000091FileManager &ASTUnit::getFileManager() {
92 return HeaderInfo->getFileMgr();
93}
94
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000095ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
Daniel Dunbar7cd285f2009-09-21 03:03:39 +000096 Diagnostic &Diags,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000097 FileManager &FileMgr,
Douglas Gregor16bef852009-10-16 20:01:17 +000098 std::string *ErrMsg,
Ted Kremenek8bcb1c62009-10-17 00:34:24 +000099 bool OnlyLocalDecls,
100 bool UseBumpAllocator) {
Daniel Dunbar7cd285f2009-09-21 03:03:39 +0000101 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(Diags));
Douglas Gregor16bef852009-10-16 20:01:17 +0000102 AST->OnlyLocalDecls = OnlyLocalDecls;
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000103 AST->HeaderInfo.reset(new HeaderSearch(FileMgr));
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000104
105 // Gather Info for preprocessor construction later on.
Mike Stump11289f42009-09-09 15:08:12 +0000106
Argyrios Kyrtzidisce379752009-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 Dunbar3a0637b2009-09-03 05:59:50 +0000113 llvm::OwningPtr<PCHReader> Reader;
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000114 llvm::OwningPtr<ExternalASTSource> Source;
115
Daniel Dunbar7cd285f2009-09-21 03:03:39 +0000116 Reader.reset(new PCHReader(AST->getSourceManager(), FileMgr, AST->Diags));
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000117 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
118 Predefines, Counter));
119
120 switch (Reader->ReadPCH(Filename)) {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000121 case PCHReader::Success:
122 break;
Mike Stump11289f42009-09-09 15:08:12 +0000123
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000124 case PCHReader::Failure:
Argyrios Kyrtzidis55c34112009-06-25 18:22:30 +0000125 case PCHReader::IgnorePCH:
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000126 if (ErrMsg)
127 *ErrMsg = "Could not load PCH file";
128 return NULL;
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000129 }
Mike Stump11289f42009-09-09 15:08:12 +0000130
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000131 // PCH loaded successfully. Now create the preprocessor.
Mike Stump11289f42009-09-09 15:08:12 +0000132
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000133 // Get information about the target being compiled for.
134 AST->Target.reset(TargetInfo::CreateTargetInfo(TargetTriple));
Daniel Dunbar7cd285f2009-09-21 03:03:39 +0000135 AST->PP.reset(new Preprocessor(AST->Diags, LangInfo, *AST->Target.get(),
136 AST->getSourceManager(), HeaderInfo));
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000137 Preprocessor &PP = *AST->PP.get();
138
Daniel Dunbarb7bbfdd2009-09-21 03:03:47 +0000139 PP.setPredefines(Reader->getSuggestedPredefines());
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000140 PP.setCounterValue(Counter);
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000141 Reader->setPreprocessor(PP);
Mike Stump11289f42009-09-09 15:08:12 +0000142
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000143 // Create and initialize the ASTContext.
144
145 AST->Ctx.reset(new ASTContext(LangInfo,
Daniel Dunbar7cd285f2009-09-21 03:03:39 +0000146 AST->getSourceManager(),
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000147 *AST->Target.get(),
148 PP.getIdentifierTable(),
149 PP.getSelectorTable(),
150 PP.getBuiltinInfo(),
Ted Kremenek8bcb1c62009-10-17 00:34:24 +0000151 /* FreeMemory = */ !UseBumpAllocator,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000152 /* size_reserve = */0));
153 ASTContext &Context = *AST->Ctx.get();
Mike Stump11289f42009-09-09 15:08:12 +0000154
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000155 Reader->InitializeContext(Context);
Mike Stump11289f42009-09-09 15:08:12 +0000156
Argyrios Kyrtzidisce379752009-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 Dunbar2d9c7402009-09-03 05:59:35 +0000160 Source.reset(Reader.take());
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000161 Context.setExternalSource(Source);
162
Mike Stump11289f42009-09-09 15:08:12 +0000163 return AST.take();
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000164}