blob: a3db339b83f4028c6f04b2d908b8313ad76a7edf [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"
Daniel Dunbar764c0822009-12-01 09:51:01 +000017#include "clang/AST/ASTConsumer.h"
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000018#include "clang/AST/DeclVisitor.h"
19#include "clang/AST/StmtVisitor.h"
Daniel Dunbar55a17b62009-12-02 03:23:45 +000020#include "clang/Driver/Compilation.h"
21#include "clang/Driver/Driver.h"
22#include "clang/Driver/Job.h"
23#include "clang/Driver/Tool.h"
Daniel Dunbar764c0822009-12-01 09:51:01 +000024#include "clang/Frontend/CompilerInstance.h"
25#include "clang/Frontend/FrontendActions.h"
Daniel Dunbar55a17b62009-12-02 03:23:45 +000026#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar764c0822009-12-01 09:51:01 +000027#include "clang/Frontend/FrontendOptions.h"
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000028#include "clang/Lex/HeaderSearch.h"
29#include "clang/Lex/Preprocessor.h"
Daniel Dunbarb9bbd542009-11-15 06:48:46 +000030#include "clang/Basic/TargetOptions.h"
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000031#include "clang/Basic/TargetInfo.h"
32#include "clang/Basic/Diagnostic.h"
Daniel Dunbar55a17b62009-12-02 03:23:45 +000033#include "llvm/System/Host.h"
Benjamin Kramer6c839f82009-10-18 11:34:14 +000034#include "llvm/System/Path.h"
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000035using namespace clang;
36
Daniel Dunbara18f9582009-12-02 21:47:43 +000037ASTUnit::ASTUnit(bool _MainFileIsAST,
38 DiagnosticClient *diagClient)
39 : tempFile(false), MainFileIsAST(_MainFileIsAST)
40{
Ted Kremenek428c6372009-10-19 21:44:57 +000041 Diags.setClient(diagClient ? diagClient : new TextDiagnosticBuffer());
Steve Naroff505fb842009-10-19 14:34:22 +000042}
Daniel Dunbar764c0822009-12-01 09:51:01 +000043ASTUnit::~ASTUnit() {
Steve Naroff44cd60e2009-10-15 22:23:48 +000044 if (tempFile)
Benjamin Kramer6c839f82009-10-18 11:34:14 +000045 llvm::sys::Path(getPCHFileName()).eraseFromDisk();
Daniel Dunbar764c0822009-12-01 09:51:01 +000046
Ted Kremenek428c6372009-10-19 21:44:57 +000047 // The ASTUnit object owns the DiagnosticClient.
48 delete Diags.getClient();
Steve Naroff44cd60e2009-10-15 22:23:48 +000049}
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000050
51namespace {
52
53/// \brief Gathers information from PCHReader that will be used to initialize
54/// a Preprocessor.
Benjamin Kramer16634c22009-11-28 10:07:24 +000055class PCHInfoCollector : public PCHReaderListener {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000056 LangOptions &LangOpt;
57 HeaderSearch &HSI;
58 std::string &TargetTriple;
59 std::string &Predefines;
60 unsigned &Counter;
Mike Stump11289f42009-09-09 15:08:12 +000061
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000062 unsigned NumHeaderInfos;
Mike Stump11289f42009-09-09 15:08:12 +000063
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000064public:
65 PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
66 std::string &TargetTriple, std::string &Predefines,
67 unsigned &Counter)
68 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
69 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
Mike Stump11289f42009-09-09 15:08:12 +000070
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000071 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
72 LangOpt = LangOpts;
73 return false;
74 }
Mike Stump11289f42009-09-09 15:08:12 +000075
Daniel Dunbar20a682d2009-11-11 00:52:11 +000076 virtual bool ReadTargetTriple(llvm::StringRef Triple) {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000077 TargetTriple = Triple;
78 return false;
79 }
Mike Stump11289f42009-09-09 15:08:12 +000080
Daniel Dunbar20a682d2009-11-11 00:52:11 +000081 virtual bool ReadPredefinesBuffer(llvm::StringRef PCHPredef,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000082 FileID PCHBufferID,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +000083 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000084 std::string &SuggestedPredefines) {
85 Predefines = PCHPredef;
86 return false;
87 }
Mike Stump11289f42009-09-09 15:08:12 +000088
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000089 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) {
90 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
91 }
Mike Stump11289f42009-09-09 15:08:12 +000092
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000093 virtual void ReadCounter(unsigned Value) {
94 Counter = Value;
95 }
96};
97
98} // anonymous namespace
99
Steve Naroffc0683b92009-09-03 18:19:54 +0000100const std::string &ASTUnit::getOriginalSourceFileName() {
Daniel Dunbara8a50932009-12-02 08:44:16 +0000101 return OriginalSourceFile;
Steve Naroffc0683b92009-09-03 18:19:54 +0000102}
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000103
Steve Naroff44cd60e2009-10-15 22:23:48 +0000104const std::string &ASTUnit::getPCHFileName() {
Daniel Dunbara18f9582009-12-02 21:47:43 +0000105 assert(isMainFileAST() && "Not an ASTUnit from a PCH file!");
Steve Naroff44cd60e2009-10-15 22:23:48 +0000106 return dyn_cast<PCHReader>(Ctx->getExternalSource())->getFileName();
107}
108
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000109ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
Douglas Gregor16bef852009-10-16 20:01:17 +0000110 std::string *ErrMsg,
Ted Kremenek428c6372009-10-19 21:44:57 +0000111 DiagnosticClient *diagClient,
Ted Kremenek8bcb1c62009-10-17 00:34:24 +0000112 bool OnlyLocalDecls,
113 bool UseBumpAllocator) {
Daniel Dunbara18f9582009-12-02 21:47:43 +0000114 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true, diagClient));
Douglas Gregor16bef852009-10-16 20:01:17 +0000115 AST->OnlyLocalDecls = OnlyLocalDecls;
Steve Naroff505fb842009-10-19 14:34:22 +0000116 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000117
118 // Gather Info for preprocessor construction later on.
Mike Stump11289f42009-09-09 15:08:12 +0000119
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000120 LangOptions LangInfo;
121 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
122 std::string TargetTriple;
123 std::string Predefines;
124 unsigned Counter;
125
Daniel Dunbar3a0637b2009-09-03 05:59:50 +0000126 llvm::OwningPtr<PCHReader> Reader;
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000127 llvm::OwningPtr<ExternalASTSource> Source;
128
Ted Kremenek428c6372009-10-19 21:44:57 +0000129 Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(),
130 AST->Diags));
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000131 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
132 Predefines, Counter));
133
134 switch (Reader->ReadPCH(Filename)) {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000135 case PCHReader::Success:
136 break;
Mike Stump11289f42009-09-09 15:08:12 +0000137
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000138 case PCHReader::Failure:
Argyrios Kyrtzidis55c34112009-06-25 18:22:30 +0000139 case PCHReader::IgnorePCH:
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000140 if (ErrMsg)
141 *ErrMsg = "Could not load PCH file";
142 return NULL;
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000143 }
Mike Stump11289f42009-09-09 15:08:12 +0000144
Daniel Dunbara8a50932009-12-02 08:44:16 +0000145 AST->OriginalSourceFile = Reader->getOriginalSourceFile();
146
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000147 // PCH loaded successfully. Now create the preprocessor.
Mike Stump11289f42009-09-09 15:08:12 +0000148
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000149 // Get information about the target being compiled for.
Daniel Dunbarb9bbd542009-11-15 06:48:46 +0000150 //
151 // FIXME: This is broken, we should store the TargetOptions in the PCH.
152 TargetOptions TargetOpts;
153 TargetOpts.ABI = "";
154 TargetOpts.CPU = "";
155 TargetOpts.Features.clear();
156 TargetOpts.Triple = TargetTriple;
157 AST->Target.reset(TargetInfo::CreateTargetInfo(AST->Diags, TargetOpts));
Daniel Dunbar7cd285f2009-09-21 03:03:39 +0000158 AST->PP.reset(new Preprocessor(AST->Diags, LangInfo, *AST->Target.get(),
159 AST->getSourceManager(), HeaderInfo));
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000160 Preprocessor &PP = *AST->PP.get();
161
Daniel Dunbarb7bbfdd2009-09-21 03:03:47 +0000162 PP.setPredefines(Reader->getSuggestedPredefines());
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000163 PP.setCounterValue(Counter);
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000164 Reader->setPreprocessor(PP);
Mike Stump11289f42009-09-09 15:08:12 +0000165
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000166 // Create and initialize the ASTContext.
167
168 AST->Ctx.reset(new ASTContext(LangInfo,
Daniel Dunbar7cd285f2009-09-21 03:03:39 +0000169 AST->getSourceManager(),
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000170 *AST->Target.get(),
171 PP.getIdentifierTable(),
172 PP.getSelectorTable(),
173 PP.getBuiltinInfo(),
Ted Kremenek8bcb1c62009-10-17 00:34:24 +0000174 /* FreeMemory = */ !UseBumpAllocator,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000175 /* size_reserve = */0));
176 ASTContext &Context = *AST->Ctx.get();
Mike Stump11289f42009-09-09 15:08:12 +0000177
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000178 Reader->InitializeContext(Context);
Mike Stump11289f42009-09-09 15:08:12 +0000179
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000180 // Attach the PCH reader to the AST context as an external AST
181 // source, so that declarations will be deserialized from the
182 // PCH file as needed.
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000183 Source.reset(Reader.take());
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000184 Context.setExternalSource(Source);
185
Mike Stump11289f42009-09-09 15:08:12 +0000186 return AST.take();
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000187}
Daniel Dunbar764c0822009-12-01 09:51:01 +0000188
189namespace {
190
191class NullAction : public ASTFrontendAction {
192 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
193 llvm::StringRef InFile) {
194 return new ASTConsumer();
195 }
196
197public:
198 virtual bool hasCodeCompletionSupport() const { return false; }
199};
200
201}
202
203ASTUnit *ASTUnit::LoadFromCompilerInvocation(const CompilerInvocation &CI,
204 Diagnostic &Diags,
Daniel Dunbar48973492009-12-02 21:47:32 +0000205 bool OnlyLocalDecls) {
Daniel Dunbar764c0822009-12-01 09:51:01 +0000206 // Create the compiler instance to use for building the AST.
Daniel Dunbar7afbb8c2009-12-02 08:43:56 +0000207 CompilerInstance Clang;
Daniel Dunbar764c0822009-12-01 09:51:01 +0000208 llvm::OwningPtr<ASTUnit> AST;
209 NullAction Act;
210
211 Clang.getInvocation() = CI;
212
213 Clang.setDiagnostics(&Diags);
214 Clang.setDiagnosticClient(Diags.getClient());
215
216 // Create the target instance.
217 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
218 Clang.getTargetOpts()));
219 if (!Clang.hasTarget())
220 goto error;
221
222 // Inform the target of the language options.
223 //
224 // FIXME: We shouldn't need to do this, the target should be immutable once
225 // created. This complexity should be lifted elsewhere.
226 Clang.getTarget().setForcedLangOptions(Clang.getLangOpts());
227
228 assert(Clang.getFrontendOpts().Inputs.size() == 1 &&
229 "Invocation must have exactly one source file!");
230 assert(Clang.getFrontendOpts().Inputs[0].first != FrontendOptions::IK_AST &&
231 "FIXME: AST inputs not yet supported here!");
232
233 // Create the AST unit.
234 //
235 // FIXME: Use the provided diagnostic client.
Daniel Dunbara18f9582009-12-02 21:47:43 +0000236 AST.reset(new ASTUnit(false));
Daniel Dunbar764c0822009-12-01 09:51:01 +0000237
Daniel Dunbar48973492009-12-02 21:47:32 +0000238 AST->OnlyLocalDecls = OnlyLocalDecls;
Daniel Dunbara8a50932009-12-02 08:44:16 +0000239 AST->OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second;
240
Daniel Dunbar764c0822009-12-01 09:51:01 +0000241 // Create a file manager object to provide access to and cache the filesystem.
242 Clang.setFileManager(&AST->getFileManager());
243
244 // Create the source manager.
245 Clang.setSourceManager(&AST->getSourceManager());
246
247 // Create the preprocessor.
248 Clang.createPreprocessor();
249
250 if (!Act.BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second,
251 /*IsAST=*/false))
252 goto error;
253
254 Act.Execute();
255
Daniel Dunbard2f8be32009-12-01 21:57:33 +0000256 // Steal the created target, context, and preprocessor, and take back the
257 // source and file managers.
Daniel Dunbar764c0822009-12-01 09:51:01 +0000258 AST->Ctx.reset(Clang.takeASTContext());
259 AST->PP.reset(Clang.takePreprocessor());
260 Clang.takeSourceManager();
261 Clang.takeFileManager();
Daniel Dunbard2f8be32009-12-01 21:57:33 +0000262 AST->Target.reset(Clang.takeTarget());
Daniel Dunbar764c0822009-12-01 09:51:01 +0000263
264 Act.EndSourceFile();
265
266 Clang.takeDiagnosticClient();
267 Clang.takeDiagnostics();
268
269 return AST.take();
270
271error:
272 Clang.takeSourceManager();
273 Clang.takeFileManager();
274 Clang.takeDiagnosticClient();
275 Clang.takeDiagnostics();
276 return 0;
277}
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000278
279ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
280 const char **ArgEnd,
281 Diagnostic &Diags,
282 const char *Argv0,
283 void *MainAddr,
284 bool OnlyLocalDecls,
285 bool UseBumpAllocator) {
286 llvm::SmallVector<const char *, 16> Args;
287 Args.push_back("<clang>"); // FIXME: Remove dummy argument.
288 Args.insert(Args.end(), ArgBegin, ArgEnd);
289
290 // FIXME: Find a cleaner way to force the driver into restricted modes. We
291 // also want to force it to use clang.
292 Args.push_back("-fsyntax-only");
293
294 llvm::sys::Path Path = llvm::sys::Path::GetMainExecutable(Argv0, MainAddr);
295 driver::Driver TheDriver(Path.getBasename().c_str(),Path.getDirname().c_str(),
296 llvm::sys::getHostTriple().c_str(),
297 "a.out", false, Diags);
298 llvm::OwningPtr<driver::Compilation> C(
299 TheDriver.BuildCompilation(Args.size(), Args.data()));
300
301 // We expect to get back exactly one command job, if we didn't something
302 // failed.
303 const driver::JobList &Jobs = C->getJobs();
304 if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) {
305 llvm::SmallString<256> Msg;
306 llvm::raw_svector_ostream OS(Msg);
307 C->PrintJob(OS, C->getJobs(), "; ", true);
308 Diags.Report(diag::err_fe_expected_compiler_job) << OS.str();
309 return 0;
310 }
311
312 const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
313 if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") {
314 Diags.Report(diag::err_fe_expected_clang_command);
315 return 0;
316 }
317
318 const driver::ArgStringList &CCArgs = Cmd->getArguments();
319 CompilerInvocation CI;
320 CompilerInvocation::CreateFromArgs(CI, (const char**) CCArgs.data(),
321 (const char**) CCArgs.data()+CCArgs.size(),
322 Argv0, MainAddr, Diags);
323
Daniel Dunbar48973492009-12-02 21:47:32 +0000324 CI.getFrontendOpts().DisableFree = UseBumpAllocator;
325 return LoadFromCompilerInvocation(CI, Diags, OnlyLocalDecls);
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000326}