blob: 97188919f9a8066f009ca0eb106ea7fe92d2c7a3 [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));
169
170 /// Use PCH?
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000171 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000172 assert(hasPCHSupport() && "This action does not have PCH support!");
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000173 ASTDeserializationListener *DeserialListener
174 = CI.getInvocation().getFrontendOpts().ChainedPCH ?
175 Consumer->GetASTDeserializationListener() : 0;
176 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls)
177 DeserialListener = new DeserializedDeclsDumper(DeserialListener);
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000178 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty())
179 DeserialListener = new DeserializedDeclsChecker(CI.getASTContext(),
180 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
181 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000182 CI.createPCHExternalASTSource(
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000183 CI.getPreprocessorOpts().ImplicitPCHInclude,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000184 CI.getPreprocessorOpts().DisablePCHValidation,
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000185 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000186 if (!CI.getASTContext().getExternalSource())
187 goto failure;
188 }
Sebastian Redl77f46032010-07-09 21:00:24 +0000189
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000190 CI.setASTConsumer(Consumer.take());
Sebastian Redl77f46032010-07-09 21:00:24 +0000191 if (!CI.hasASTConsumer())
192 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000193 }
194
195 // Initialize builtin info as long as we aren't using an external AST
196 // source.
197 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
198 Preprocessor &PP = CI.getPreprocessor();
199 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
200 PP.getLangOptions().NoBuiltin);
201 }
202
203 return true;
204
205 // If we failed, reset state since the client will not end up calling the
206 // matching EndSourceFile().
207 failure:
208 if (isCurrentFileAST()) {
209 CI.takeASTContext();
210 CI.takePreprocessor();
211 CI.takeSourceManager();
212 CI.takeFileManager();
213 }
214
215 CI.getDiagnosticClient().EndSourceFile();
Daniel Dunbar685ac662010-06-07 23:25:49 +0000216 setCurrentFile("", IK_None);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000217 setCompilerInstance(0);
218 return false;
219}
220
221void FrontendAction::Execute() {
222 CompilerInstance &CI = getCompilerInstance();
223
224 // Initialize the main file entry. This needs to be delayed until after PCH
225 // has loaded.
226 if (isCurrentFileAST()) {
227 // Set the main file ID to an empty file.
228 //
229 // FIXME: We probably shouldn't need this, but for now this is the
230 // simplest way to reuse the logic in ParseAST.
231 const char *EmptyStr = "";
232 llvm::MemoryBuffer *SB =
Chris Lattnera0a270c2010-04-05 22:42:27 +0000233 llvm::MemoryBuffer::getMemBuffer(EmptyStr, "<dummy input>");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000234 CI.getSourceManager().createMainFileIDForMemBuffer(SB);
235 } else {
236 if (!CI.InitializeSourceManager(getCurrentFile()))
237 return;
238 }
239
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000240 if (CI.hasFrontendTimer()) {
241 llvm::TimeRegion Timer(CI.getFrontendTimer());
242 ExecuteAction();
243 }
244 else ExecuteAction();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000245}
246
247void FrontendAction::EndSourceFile() {
248 CompilerInstance &CI = getCompilerInstance();
249
250 // Finalize the action.
251 EndSourceFileAction();
252
253 // Release the consumer and the AST, in that order since the consumer may
254 // perform actions in its destructor which require the context.
255 //
256 // FIXME: There is more per-file stuff we could just drop here?
257 if (CI.getFrontendOpts().DisableFree) {
258 CI.takeASTConsumer();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000259 if (!isCurrentFileAST()) {
260 CI.takeSema();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000261 CI.takeASTContext();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000262 }
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000263 } else {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000264 if (!isCurrentFileAST()) {
265 CI.setSema(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000266 CI.setASTContext(0);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000267 }
268 CI.setASTConsumer(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000269 }
270
Daniel Dunbardbd82092010-03-23 05:09:10 +0000271 // Inform the preprocessor we are done.
272 if (CI.hasPreprocessor())
273 CI.getPreprocessor().EndSourceFile();
274
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000275 if (CI.getFrontendOpts().ShowStats) {
276 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
277 CI.getPreprocessor().PrintStats();
278 CI.getPreprocessor().getIdentifierTable().PrintStats();
279 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
280 CI.getSourceManager().PrintStats();
281 llvm::errs() << "\n";
282 }
283
284 // Cleanup the output streams, and erase the output files if we encountered
285 // an error.
Kovarththanan Rajaratname51dd7b2010-03-06 12:07:48 +0000286 CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().getNumErrors());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000287
288 // Inform the diagnostic client we are done with this source file.
289 CI.getDiagnosticClient().EndSourceFile();
290
291 if (isCurrentFileAST()) {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000292 CI.takeSema();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000293 CI.takeASTContext();
294 CI.takePreprocessor();
295 CI.takeSourceManager();
296 CI.takeFileManager();
297 }
298
299 setCompilerInstance(0);
Daniel Dunbar685ac662010-06-07 23:25:49 +0000300 setCurrentFile("", IK_None);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000301}
302
303//===----------------------------------------------------------------------===//
304// Utility Actions
305//===----------------------------------------------------------------------===//
306
307void ASTFrontendAction::ExecuteAction() {
308 CompilerInstance &CI = getCompilerInstance();
309
310 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
311 // here so the source manager would be initialized.
312 if (hasCodeCompletionSupport() &&
313 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
314 CI.createCodeCompletionConsumer();
315
316 // Use a code completion consumer?
317 CodeCompleteConsumer *CompletionConsumer = 0;
318 if (CI.hasCodeCompletionConsumer())
319 CompletionConsumer = &CI.getCodeCompletionConsumer();
320
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000321 if (!CI.hasSema())
322 CI.createSema(usesCompleteTranslationUnit(), CompletionConsumer);
323
324 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000325}
326
327ASTConsumer *
328PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
329 llvm::StringRef InFile) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000330 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000331}