blob: 48f115fd3e3a214f6de62eb17b4726568752b05c [file] [log] [blame]
Daniel Dunbar4ee24092009-11-14 10:42:35 +00001//===--- FrontendAction.cpp -----------------------------------------------===//
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#include "clang/Frontend/FrontendAction.h"
Sebastian Redlffaab3e2010-07-30 00:29:29 +000011#include "clang/AST/ASTConsumer.h"
Daniel Dunbar4ee24092009-11-14 10:42:35 +000012#include "clang/AST/ASTContext.h"
13#include "clang/Lex/HeaderSearch.h"
14#include "clang/Lex/Preprocessor.h"
15#include "clang/Frontend/ASTUnit.h"
16#include "clang/Frontend/CompilerInstance.h"
17#include "clang/Frontend/FrontendDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000018#include "clang/Parse/ParseAST.h"
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000019#include "clang/Serialization/ASTDeserializationListener.h"
Daniel Dunbar4ee24092009-11-14 10:42:35 +000020#include "llvm/Support/MemoryBuffer.h"
21#include "llvm/Support/Timer.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/raw_ostream.h"
24using namespace clang;
25
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000026namespace {
27
28/// \brief Dumps deserialized declarations.
29class DeserializedDeclsDumper : public ASTDeserializationListener {
30 ASTDeserializationListener *Previous;
31
32public:
33 DeserializedDeclsDumper(ASTDeserializationListener *Previous)
34 : Previous(Previous) { }
35
36 virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
37 llvm::outs() << "PCH DECL: " << D->getDeclKindName();
38 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
39 llvm::outs() << " - " << ND->getNameAsString();
40 llvm::outs() << "\n";
41
42 if (Previous)
43 Previous->DeclRead(ID, D);
44 }
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000045};
46
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +000047 /// \brief Checks deserialized declarations and emits error if a name
48 /// matches one given in command-line using -error-on-deserialized-decl.
49 class DeserializedDeclsChecker : public ASTDeserializationListener {
50 ASTContext &Ctx;
51 std::set<std::string> NamesToCheck;
52 ASTDeserializationListener *Previous;
53
54 public:
55 DeserializedDeclsChecker(ASTContext &Ctx,
56 const std::set<std::string> &NamesToCheck,
57 ASTDeserializationListener *Previous)
58 : Ctx(Ctx), NamesToCheck(NamesToCheck), Previous(Previous) { }
59
60 virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
61 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
62 if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
63 unsigned DiagID
64 = Ctx.getDiagnostics().getCustomDiagID(Diagnostic::Error,
65 "%0 was deserialized");
66 Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
67 << ND->getNameAsString();
68 }
69
70 if (Previous)
71 Previous->DeclRead(ID, D);
72 }
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +000073};
74
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000075} // end anonymous namespace
76
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +000077FrontendAction::FrontendAction() : Instance(0) {}
Daniel Dunbar4ee24092009-11-14 10:42:35 +000078
79FrontendAction::~FrontendAction() {}
80
Daniel Dunbar685ac662010-06-07 23:25:49 +000081void FrontendAction::setCurrentFile(llvm::StringRef Value, InputKind Kind,
82 ASTUnit *AST) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +000083 CurrentFile = Value;
Daniel Dunbar685ac662010-06-07 23:25:49 +000084 CurrentFileKind = Kind;
Daniel Dunbar4ee24092009-11-14 10:42:35 +000085 CurrentASTUnit.reset(AST);
86}
87
88bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
89 llvm::StringRef Filename,
Daniel Dunbard3598a62010-06-07 23:23:06 +000090 InputKind InputKind) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +000091 assert(!Instance && "Already processing a source file!");
92 assert(!Filename.empty() && "Unexpected empty filename!");
Daniel Dunbar685ac662010-06-07 23:25:49 +000093 setCurrentFile(Filename, InputKind);
Daniel Dunbar4ee24092009-11-14 10:42:35 +000094 setCompilerInstance(&CI);
95
96 // AST files follow a very different path, since they share objects via the
97 // AST unit.
Daniel Dunbar20560482010-06-07 23:23:50 +000098 if (InputKind == IK_AST) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +000099 assert(!usesPreprocessorOnly() &&
100 "Attempt to pass AST file to preprocessor only action!");
Daniel Dunbareb58d832010-06-07 23:24:43 +0000101 assert(hasASTFileSupport() &&
102 "This action does not have AST file support!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000103
Douglas Gregor28019772010-04-05 23:52:57 +0000104 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(&CI.getDiagnostics());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000105 std::string Error;
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000106 ASTUnit *AST = ASTUnit::LoadFromASTFile(Filename, Diags);
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000107 if (!AST)
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000108 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000109
Daniel Dunbar685ac662010-06-07 23:25:49 +0000110 setCurrentFile(Filename, InputKind, AST);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000111
112 // Set the shared objects, these are reset when we finish processing the
113 // file, otherwise the CompilerInstance will happily destroy them.
114 CI.setFileManager(&AST->getFileManager());
115 CI.setSourceManager(&AST->getSourceManager());
116 CI.setPreprocessor(&AST->getPreprocessor());
117 CI.setASTContext(&AST->getASTContext());
118
119 // Initialize the action.
120 if (!BeginSourceFileAction(CI, Filename))
121 goto failure;
122
123 /// Create the AST consumer.
124 CI.setASTConsumer(CreateASTConsumer(CI, Filename));
125 if (!CI.hasASTConsumer())
126 goto failure;
127
128 return true;
129 }
130
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000131 // Set up the file and source managers, if needed.
Daniel Dunbar20560482010-06-07 23:23:50 +0000132 if (!CI.hasFileManager())
133 CI.createFileManager();
134 if (!CI.hasSourceManager())
135 CI.createSourceManager();
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000136
137 // IR files bypass the rest of initialization.
138 if (InputKind == IK_LLVM_IR) {
139 assert(hasIRSupport() &&
140 "This action does not have IR file support!");
141
142 // Inform the diagnostic client we are processing a source file.
143 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
144
145 // Initialize the action.
146 if (!BeginSourceFileAction(CI, Filename))
147 goto failure;
148
149 return true;
150 }
151
152 // Set up the preprocessor.
Daniel Dunbar20560482010-06-07 23:23:50 +0000153 CI.createPreprocessor();
154
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000155 // Inform the diagnostic client we are processing a source file.
156 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
157 &CI.getPreprocessor());
158
159 // Initialize the action.
160 if (!BeginSourceFileAction(CI, Filename))
161 goto failure;
162
163 /// Create the AST context and consumer unless this is a preprocessor only
164 /// action.
165 if (!usesPreprocessorOnly()) {
166 CI.createASTContext();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000167
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000168 llvm::OwningPtr<ASTConsumer> Consumer(CreateASTConsumer(CI, Filename));
Fariborz Jahaniand3057192010-10-29 19:49:13 +0000169 if (!Consumer)
170 goto failure;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000171
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000172 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
173
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000174 /// Use PCH?
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000175 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000176 assert(hasPCHSupport() && "This action does not have PCH support!");
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000177 ASTDeserializationListener *DeserialListener
178 = CI.getInvocation().getFrontendOpts().ChainedPCH ?
179 Consumer->GetASTDeserializationListener() : 0;
180 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls)
181 DeserialListener = new DeserializedDeclsDumper(DeserialListener);
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000182 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty())
183 DeserialListener = new DeserializedDeclsChecker(CI.getASTContext(),
184 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
185 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000186 CI.createPCHExternalASTSource(
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000187 CI.getPreprocessorOpts().ImplicitPCHInclude,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000188 CI.getPreprocessorOpts().DisablePCHValidation,
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000189 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000190 if (!CI.getASTContext().getExternalSource())
191 goto failure;
192 }
Sebastian Redl77f46032010-07-09 21:00:24 +0000193
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000194 CI.setASTConsumer(Consumer.take());
Sebastian Redl77f46032010-07-09 21:00:24 +0000195 if (!CI.hasASTConsumer())
196 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000197 }
198
199 // Initialize builtin info as long as we aren't using an external AST
200 // source.
201 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
202 Preprocessor &PP = CI.getPreprocessor();
203 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
204 PP.getLangOptions().NoBuiltin);
205 }
206
207 return true;
208
209 // If we failed, reset state since the client will not end up calling the
210 // matching EndSourceFile().
211 failure:
212 if (isCurrentFileAST()) {
213 CI.takeASTContext();
214 CI.takePreprocessor();
215 CI.takeSourceManager();
216 CI.takeFileManager();
217 }
218
219 CI.getDiagnosticClient().EndSourceFile();
Daniel Dunbar685ac662010-06-07 23:25:49 +0000220 setCurrentFile("", IK_None);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000221 setCompilerInstance(0);
222 return false;
223}
224
225void FrontendAction::Execute() {
226 CompilerInstance &CI = getCompilerInstance();
227
228 // Initialize the main file entry. This needs to be delayed until after PCH
229 // has loaded.
230 if (isCurrentFileAST()) {
231 // Set the main file ID to an empty file.
232 //
233 // FIXME: We probably shouldn't need this, but for now this is the
234 // simplest way to reuse the logic in ParseAST.
235 const char *EmptyStr = "";
236 llvm::MemoryBuffer *SB =
Chris Lattnera0a270c2010-04-05 22:42:27 +0000237 llvm::MemoryBuffer::getMemBuffer(EmptyStr, "<dummy input>");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000238 CI.getSourceManager().createMainFileIDForMemBuffer(SB);
239 } else {
240 if (!CI.InitializeSourceManager(getCurrentFile()))
241 return;
242 }
243
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000244 if (CI.hasFrontendTimer()) {
245 llvm::TimeRegion Timer(CI.getFrontendTimer());
246 ExecuteAction();
247 }
248 else ExecuteAction();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000249}
250
251void FrontendAction::EndSourceFile() {
252 CompilerInstance &CI = getCompilerInstance();
253
254 // Finalize the action.
255 EndSourceFileAction();
256
257 // Release the consumer and the AST, in that order since the consumer may
258 // perform actions in its destructor which require the context.
259 //
260 // FIXME: There is more per-file stuff we could just drop here?
261 if (CI.getFrontendOpts().DisableFree) {
262 CI.takeASTConsumer();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000263 if (!isCurrentFileAST()) {
264 CI.takeSema();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000265 CI.takeASTContext();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000266 }
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000267 } else {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000268 if (!isCurrentFileAST()) {
269 CI.setSema(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000270 CI.setASTContext(0);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000271 }
272 CI.setASTConsumer(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000273 }
274
Daniel Dunbardbd82092010-03-23 05:09:10 +0000275 // Inform the preprocessor we are done.
276 if (CI.hasPreprocessor())
277 CI.getPreprocessor().EndSourceFile();
278
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000279 if (CI.getFrontendOpts().ShowStats) {
280 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
281 CI.getPreprocessor().PrintStats();
282 CI.getPreprocessor().getIdentifierTable().PrintStats();
283 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
284 CI.getSourceManager().PrintStats();
285 llvm::errs() << "\n";
286 }
287
288 // Cleanup the output streams, and erase the output files if we encountered
289 // an error.
Kovarththanan Rajaratname51dd7b2010-03-06 12:07:48 +0000290 CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().getNumErrors());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000291
292 // Inform the diagnostic client we are done with this source file.
293 CI.getDiagnosticClient().EndSourceFile();
294
295 if (isCurrentFileAST()) {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000296 CI.takeSema();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000297 CI.takeASTContext();
298 CI.takePreprocessor();
299 CI.takeSourceManager();
300 CI.takeFileManager();
301 }
302
303 setCompilerInstance(0);
Daniel Dunbar685ac662010-06-07 23:25:49 +0000304 setCurrentFile("", IK_None);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000305}
306
307//===----------------------------------------------------------------------===//
308// Utility Actions
309//===----------------------------------------------------------------------===//
310
311void ASTFrontendAction::ExecuteAction() {
312 CompilerInstance &CI = getCompilerInstance();
313
314 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
315 // here so the source manager would be initialized.
316 if (hasCodeCompletionSupport() &&
317 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
318 CI.createCodeCompletionConsumer();
319
320 // Use a code completion consumer?
321 CodeCompleteConsumer *CompletionConsumer = 0;
322 if (CI.hasCodeCompletionConsumer())
323 CompletionConsumer = &CI.getCodeCompletionConsumer();
324
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000325 if (!CI.hasSema())
326 CI.createSema(usesCompleteTranslationUnit(), CompletionConsumer);
327
328 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000329}
330
331ASTConsumer *
332PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
333 llvm::StringRef InFile) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000334 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000335}