blob: ac213380588a925aba4ee9f15dee26bd39f64112 [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"
Daniel Dunbar521bf9c2009-12-01 09:51:01 +000017#include "clang/AST/ASTConsumer.h"
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000018#include "clang/AST/DeclVisitor.h"
19#include "clang/AST/StmtVisitor.h"
Daniel Dunbar7b556682009-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 Dunbar521bf9c2009-12-01 09:51:01 +000024#include "clang/Frontend/CompilerInstance.h"
25#include "clang/Frontend/FrontendActions.h"
Daniel Dunbar7b556682009-12-02 03:23:45 +000026#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar521bf9c2009-12-01 09:51:01 +000027#include "clang/Frontend/FrontendOptions.h"
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000028#include "clang/Lex/HeaderSearch.h"
29#include "clang/Lex/Preprocessor.h"
Daniel Dunbard58c03f2009-11-15 06:48:46 +000030#include "clang/Basic/TargetOptions.h"
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000031#include "clang/Basic/TargetInfo.h"
32#include "clang/Basic/Diagnostic.h"
Daniel Dunbar7b556682009-12-02 03:23:45 +000033#include "llvm/System/Host.h"
Benjamin Kramer4a630d32009-10-18 11:34:14 +000034#include "llvm/System/Path.h"
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000035using namespace clang;
36
Daniel Dunbar521bf9c2009-12-01 09:51:01 +000037ASTUnit::ASTUnit(DiagnosticClient *diagClient) : tempFile(false) {
Ted Kremenekfc062212009-10-19 21:44:57 +000038 Diags.setClient(diagClient ? diagClient : new TextDiagnosticBuffer());
Steve Naroff36c44642009-10-19 14:34:22 +000039}
Daniel Dunbar521bf9c2009-12-01 09:51:01 +000040ASTUnit::~ASTUnit() {
Steve Naroffe19944c2009-10-15 22:23:48 +000041 if (tempFile)
Benjamin Kramer4a630d32009-10-18 11:34:14 +000042 llvm::sys::Path(getPCHFileName()).eraseFromDisk();
Daniel Dunbar521bf9c2009-12-01 09:51:01 +000043
Ted Kremenekfc062212009-10-19 21:44:57 +000044 // The ASTUnit object owns the DiagnosticClient.
45 delete Diags.getClient();
Steve Naroffe19944c2009-10-15 22:23:48 +000046}
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000047
48namespace {
49
50/// \brief Gathers information from PCHReader that will be used to initialize
51/// a Preprocessor.
Benjamin Kramerbd218282009-11-28 10:07:24 +000052class PCHInfoCollector : public PCHReaderListener {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000053 LangOptions &LangOpt;
54 HeaderSearch &HSI;
55 std::string &TargetTriple;
56 std::string &Predefines;
57 unsigned &Counter;
Mike Stump1eb44332009-09-09 15:08:12 +000058
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000059 unsigned NumHeaderInfos;
Mike Stump1eb44332009-09-09 15:08:12 +000060
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000061public:
62 PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
63 std::string &TargetTriple, std::string &Predefines,
64 unsigned &Counter)
65 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
66 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +000067
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000068 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
69 LangOpt = LangOpts;
70 return false;
71 }
Mike Stump1eb44332009-09-09 15:08:12 +000072
Daniel Dunbardc3c0d22009-11-11 00:52:11 +000073 virtual bool ReadTargetTriple(llvm::StringRef Triple) {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000074 TargetTriple = Triple;
75 return false;
76 }
Mike Stump1eb44332009-09-09 15:08:12 +000077
Daniel Dunbardc3c0d22009-11-11 00:52:11 +000078 virtual bool ReadPredefinesBuffer(llvm::StringRef PCHPredef,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000079 FileID PCHBufferID,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +000080 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000081 std::string &SuggestedPredefines) {
82 Predefines = PCHPredef;
83 return false;
84 }
Mike Stump1eb44332009-09-09 15:08:12 +000085
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000086 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) {
87 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
88 }
Mike Stump1eb44332009-09-09 15:08:12 +000089
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000090 virtual void ReadCounter(unsigned Value) {
91 Counter = Value;
92 }
93};
94
95} // anonymous namespace
96
Steve Naroff77accc12009-09-03 18:19:54 +000097const std::string &ASTUnit::getOriginalSourceFileName() {
98 return dyn_cast<PCHReader>(Ctx->getExternalSource())->getOriginalSourceFile();
99}
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000100
Steve Naroffe19944c2009-10-15 22:23:48 +0000101const std::string &ASTUnit::getPCHFileName() {
102 return dyn_cast<PCHReader>(Ctx->getExternalSource())->getFileName();
103}
104
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000105ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000106 std::string *ErrMsg,
Ted Kremenekfc062212009-10-19 21:44:57 +0000107 DiagnosticClient *diagClient,
Ted Kremenek5cf48762009-10-17 00:34:24 +0000108 bool OnlyLocalDecls,
109 bool UseBumpAllocator) {
Ted Kremenekfc062212009-10-19 21:44:57 +0000110 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(diagClient));
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000111 AST->OnlyLocalDecls = OnlyLocalDecls;
Steve Naroff36c44642009-10-19 14:34:22 +0000112 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000113
114 // Gather Info for preprocessor construction later on.
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000116 LangOptions LangInfo;
117 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
118 std::string TargetTriple;
119 std::string Predefines;
120 unsigned Counter;
121
Daniel Dunbarbce6f622009-09-03 05:59:50 +0000122 llvm::OwningPtr<PCHReader> Reader;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000123 llvm::OwningPtr<ExternalASTSource> Source;
124
Ted Kremenekfc062212009-10-19 21:44:57 +0000125 Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(),
126 AST->Diags));
Daniel Dunbarcc318932009-09-03 05:59:35 +0000127 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
128 Predefines, Counter));
129
130 switch (Reader->ReadPCH(Filename)) {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000131 case PCHReader::Success:
132 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000134 case PCHReader::Failure:
Argyrios Kyrtzidis106c9982009-06-25 18:22:30 +0000135 case PCHReader::IgnorePCH:
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000136 if (ErrMsg)
137 *ErrMsg = "Could not load PCH file";
138 return NULL;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000139 }
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000141 // PCH loaded successfully. Now create the preprocessor.
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000143 // Get information about the target being compiled for.
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000144 //
145 // FIXME: This is broken, we should store the TargetOptions in the PCH.
146 TargetOptions TargetOpts;
147 TargetOpts.ABI = "";
148 TargetOpts.CPU = "";
149 TargetOpts.Features.clear();
150 TargetOpts.Triple = TargetTriple;
151 AST->Target.reset(TargetInfo::CreateTargetInfo(AST->Diags, TargetOpts));
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000152 AST->PP.reset(new Preprocessor(AST->Diags, LangInfo, *AST->Target.get(),
153 AST->getSourceManager(), HeaderInfo));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000154 Preprocessor &PP = *AST->PP.get();
155
Daniel Dunbard5b61262009-09-21 03:03:47 +0000156 PP.setPredefines(Reader->getSuggestedPredefines());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000157 PP.setCounterValue(Counter);
Daniel Dunbarcc318932009-09-03 05:59:35 +0000158 Reader->setPreprocessor(PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000160 // Create and initialize the ASTContext.
161
162 AST->Ctx.reset(new ASTContext(LangInfo,
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000163 AST->getSourceManager(),
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000164 *AST->Target.get(),
165 PP.getIdentifierTable(),
166 PP.getSelectorTable(),
167 PP.getBuiltinInfo(),
Ted Kremenek5cf48762009-10-17 00:34:24 +0000168 /* FreeMemory = */ !UseBumpAllocator,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000169 /* size_reserve = */0));
170 ASTContext &Context = *AST->Ctx.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Daniel Dunbarcc318932009-09-03 05:59:35 +0000172 Reader->InitializeContext(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000174 // Attach the PCH reader to the AST context as an external AST
175 // source, so that declarations will be deserialized from the
176 // PCH file as needed.
Daniel Dunbarcc318932009-09-03 05:59:35 +0000177 Source.reset(Reader.take());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000178 Context.setExternalSource(Source);
179
Mike Stump1eb44332009-09-09 15:08:12 +0000180 return AST.take();
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000181}
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000182
183namespace {
184
185class NullAction : public ASTFrontendAction {
186 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
187 llvm::StringRef InFile) {
188 return new ASTConsumer();
189 }
190
191public:
192 virtual bool hasCodeCompletionSupport() const { return false; }
193};
194
195}
196
197ASTUnit *ASTUnit::LoadFromCompilerInvocation(const CompilerInvocation &CI,
198 Diagnostic &Diags,
199 bool OnlyLocalDecls,
200 bool UseBumpAllocator) {
201 // Create the compiler instance to use for building the AST.
Daniel Dunbarcb6dda12009-12-02 08:43:56 +0000202 CompilerInstance Clang;
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000203 llvm::OwningPtr<ASTUnit> AST;
204 NullAction Act;
205
206 Clang.getInvocation() = CI;
207
208 Clang.setDiagnostics(&Diags);
209 Clang.setDiagnosticClient(Diags.getClient());
210
211 // Create the target instance.
212 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
213 Clang.getTargetOpts()));
214 if (!Clang.hasTarget())
215 goto error;
216
217 // Inform the target of the language options.
218 //
219 // FIXME: We shouldn't need to do this, the target should be immutable once
220 // created. This complexity should be lifted elsewhere.
221 Clang.getTarget().setForcedLangOptions(Clang.getLangOpts());
222
223 assert(Clang.getFrontendOpts().Inputs.size() == 1 &&
224 "Invocation must have exactly one source file!");
225 assert(Clang.getFrontendOpts().Inputs[0].first != FrontendOptions::IK_AST &&
226 "FIXME: AST inputs not yet supported here!");
227
228 // Create the AST unit.
229 //
230 // FIXME: Use the provided diagnostic client.
231 AST.reset(new ASTUnit());
232
233 // Create a file manager object to provide access to and cache the filesystem.
234 Clang.setFileManager(&AST->getFileManager());
235
236 // Create the source manager.
237 Clang.setSourceManager(&AST->getSourceManager());
238
239 // Create the preprocessor.
240 Clang.createPreprocessor();
241
242 if (!Act.BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second,
243 /*IsAST=*/false))
244 goto error;
245
246 Act.Execute();
247
Daniel Dunbar64a32ba2009-12-01 21:57:33 +0000248 // Steal the created target, context, and preprocessor, and take back the
249 // source and file managers.
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000250 AST->Ctx.reset(Clang.takeASTContext());
251 AST->PP.reset(Clang.takePreprocessor());
252 Clang.takeSourceManager();
253 Clang.takeFileManager();
Daniel Dunbar64a32ba2009-12-01 21:57:33 +0000254 AST->Target.reset(Clang.takeTarget());
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000255
256 Act.EndSourceFile();
257
258 Clang.takeDiagnosticClient();
259 Clang.takeDiagnostics();
260
261 return AST.take();
262
263error:
264 Clang.takeSourceManager();
265 Clang.takeFileManager();
266 Clang.takeDiagnosticClient();
267 Clang.takeDiagnostics();
268 return 0;
269}
Daniel Dunbar7b556682009-12-02 03:23:45 +0000270
271ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
272 const char **ArgEnd,
273 Diagnostic &Diags,
274 const char *Argv0,
275 void *MainAddr,
276 bool OnlyLocalDecls,
277 bool UseBumpAllocator) {
278 llvm::SmallVector<const char *, 16> Args;
279 Args.push_back("<clang>"); // FIXME: Remove dummy argument.
280 Args.insert(Args.end(), ArgBegin, ArgEnd);
281
282 // FIXME: Find a cleaner way to force the driver into restricted modes. We
283 // also want to force it to use clang.
284 Args.push_back("-fsyntax-only");
285
286 llvm::sys::Path Path = llvm::sys::Path::GetMainExecutable(Argv0, MainAddr);
287 driver::Driver TheDriver(Path.getBasename().c_str(),Path.getDirname().c_str(),
288 llvm::sys::getHostTriple().c_str(),
289 "a.out", false, Diags);
290 llvm::OwningPtr<driver::Compilation> C(
291 TheDriver.BuildCompilation(Args.size(), Args.data()));
292
293 // We expect to get back exactly one command job, if we didn't something
294 // failed.
295 const driver::JobList &Jobs = C->getJobs();
296 if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) {
297 llvm::SmallString<256> Msg;
298 llvm::raw_svector_ostream OS(Msg);
299 C->PrintJob(OS, C->getJobs(), "; ", true);
300 Diags.Report(diag::err_fe_expected_compiler_job) << OS.str();
301 return 0;
302 }
303
304 const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
305 if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") {
306 Diags.Report(diag::err_fe_expected_clang_command);
307 return 0;
308 }
309
310 const driver::ArgStringList &CCArgs = Cmd->getArguments();
311 CompilerInvocation CI;
312 CompilerInvocation::CreateFromArgs(CI, (const char**) CCArgs.data(),
313 (const char**) CCArgs.data()+CCArgs.size(),
314 Argv0, MainAddr, Diags);
315
316 return LoadFromCompilerInvocation(CI, Diags, OnlyLocalDecls,
317 UseBumpAllocator);
318}