1 //===--- 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"
11 #include "clang/AST/ASTConsumer.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/AST/DeclGroup.h"
14 #include "clang/Lex/HeaderSearch.h"
15 #include "clang/Lex/Preprocessor.h"
16 #include "clang/Frontend/ASTUnit.h"
17 #include "clang/Frontend/ChainedIncludesSource.h"
18 #include "clang/Frontend/CompilerInstance.h"
19 #include "clang/Frontend/FrontendDiagnostic.h"
20 #include "clang/Frontend/FrontendPluginRegistry.h"
21 #include "clang/Frontend/LayoutOverrideSource.h"
22 #include "clang/Frontend/MultiplexConsumer.h"
23 #include "clang/Parse/ParseAST.h"
24 #include "clang/Serialization/ASTDeserializationListener.h"
25 #include "clang/Serialization/ASTReader.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/Timer.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/raw_ostream.h"
30 using namespace clang;
31 
32 namespace {
33 
34 class DelegatingDeserializationListener : public ASTDeserializationListener {
35   ASTDeserializationListener *Previous;
36 
37 public:
38   explicit DelegatingDeserializationListener(
39                                            ASTDeserializationListener *Previous)
40     : Previous(Previous) { }
41 
42   virtual void ReaderInitialized(ASTReader *Reader) {
43     if (Previous)
44       Previous->ReaderInitialized(Reader);
45   }
46   virtual void IdentifierRead(serialization::IdentID ID,
47                               IdentifierInfo *II) {
48     if (Previous)
49       Previous->IdentifierRead(ID, II);
50   }
51   virtual void TypeRead(serialization::TypeIdx Idx, QualType T) {
52     if (Previous)
53       Previous->TypeRead(Idx, T);
54   }
55   virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
56     if (Previous)
57       Previous->DeclRead(ID, D);
58   }
59   virtual void SelectorRead(serialization::SelectorID ID, Selector Sel) {
60     if (Previous)
61       Previous->SelectorRead(ID, Sel);
62   }
63   virtual void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
64                                    MacroDefinition *MD) {
65     if (Previous)
66       Previous->MacroDefinitionRead(PPID, MD);
67   }
68 };
69 
70 /// \brief Dumps deserialized declarations.
71 class DeserializedDeclsDumper : public DelegatingDeserializationListener {
72 public:
73   explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous)
74     : DelegatingDeserializationListener(Previous) { }
75 
76   virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
77     llvm::outs() << "PCH DECL: " << D->getDeclKindName();
78     if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
79       llvm::outs() << " - " << ND->getNameAsString();
80     llvm::outs() << "\n";
81 
82     DelegatingDeserializationListener::DeclRead(ID, D);
83   }
84 };
85 
86   /// \brief Checks deserialized declarations and emits error if a name
87   /// matches one given in command-line using -error-on-deserialized-decl.
88   class DeserializedDeclsChecker : public DelegatingDeserializationListener {
89     ASTContext &Ctx;
90     std::set<std::string> NamesToCheck;
91 
92   public:
93     DeserializedDeclsChecker(ASTContext &Ctx,
94                              const std::set<std::string> &NamesToCheck,
95                              ASTDeserializationListener *Previous)
96       : DelegatingDeserializationListener(Previous),
97         Ctx(Ctx), NamesToCheck(NamesToCheck) { }
98 
99     virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
100       if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
101         if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
102           unsigned DiagID
103             = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
104                                                    "%0 was deserialized");
105           Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
106               << ND->getNameAsString();
107         }
108 
109       DelegatingDeserializationListener::DeclRead(ID, D);
110     }
111 };
112 
113 } // end anonymous namespace
114 
115 FrontendAction::FrontendAction() : Instance(0) {}
116 
117 FrontendAction::~FrontendAction() {}
118 
119 void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
120                                      ASTUnit *AST) {
121   this->CurrentInput = CurrentInput;
122   CurrentASTUnit.reset(AST);
123 }
124 
125 ASTConsumer* FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
126                                                       StringRef InFile) {
127   ASTConsumer* Consumer = CreateASTConsumer(CI, InFile);
128   if (!Consumer)
129     return 0;
130 
131   if (CI.getFrontendOpts().AddPluginActions.size() == 0)
132     return Consumer;
133 
134   // Make sure the non-plugin consumer is first, so that plugins can't
135   // modifiy the AST.
136   std::vector<ASTConsumer*> Consumers(1, Consumer);
137 
138   for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size();
139        i != e; ++i) {
140     // This is O(|plugins| * |add_plugins|), but since both numbers are
141     // way below 50 in practice, that's ok.
142     for (FrontendPluginRegistry::iterator
143         it = FrontendPluginRegistry::begin(),
144         ie = FrontendPluginRegistry::end();
145         it != ie; ++it) {
146       if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) {
147         llvm::OwningPtr<PluginASTAction> P(it->instantiate());
148         FrontendAction* c = P.get();
149         if (P->ParseArgs(CI, CI.getFrontendOpts().AddPluginArgs[i]))
150           Consumers.push_back(c->CreateASTConsumer(CI, InFile));
151       }
152     }
153   }
154 
155   return new MultiplexConsumer(Consumers);
156 }
157 
158 bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
159                                      const FrontendInputFile &Input) {
160   assert(!Instance && "Already processing a source file!");
161   assert(!Input.File.empty() && "Unexpected empty filename!");
162   setCurrentInput(Input);
163   setCompilerInstance(&CI);
164 
165   if (!BeginInvocation(CI))
166     goto failure;
167 
168   // AST files follow a very different path, since they share objects via the
169   // AST unit.
170   if (Input.Kind == IK_AST) {
171     assert(!usesPreprocessorOnly() &&
172            "Attempt to pass AST file to preprocessor only action!");
173     assert(hasASTFileSupport() &&
174            "This action does not have AST file support!");
175 
176     llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
177     std::string Error;
178     ASTUnit *AST = ASTUnit::LoadFromASTFile(Input.File, Diags,
179                                             CI.getFileSystemOpts());
180     if (!AST)
181       goto failure;
182 
183     setCurrentInput(Input, AST);
184 
185     // Set the shared objects, these are reset when we finish processing the
186     // file, otherwise the CompilerInstance will happily destroy them.
187     CI.setFileManager(&AST->getFileManager());
188     CI.setSourceManager(&AST->getSourceManager());
189     CI.setPreprocessor(&AST->getPreprocessor());
190     CI.setASTContext(&AST->getASTContext());
191 
192     // Initialize the action.
193     if (!BeginSourceFileAction(CI, Input.File))
194       goto failure;
195 
196     /// Create the AST consumer.
197     CI.setASTConsumer(CreateWrappedASTConsumer(CI, Input.File));
198     if (!CI.hasASTConsumer())
199       goto failure;
200 
201     return true;
202   }
203 
204   // Set up the file and source managers, if needed.
205   if (!CI.hasFileManager())
206     CI.createFileManager();
207   if (!CI.hasSourceManager())
208     CI.createSourceManager(CI.getFileManager());
209 
210   // IR files bypass the rest of initialization.
211   if (Input.Kind == IK_LLVM_IR) {
212     assert(hasIRSupport() &&
213            "This action does not have IR file support!");
214 
215     // Inform the diagnostic client we are processing a source file.
216     CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
217 
218     // Initialize the action.
219     if (!BeginSourceFileAction(CI, Input.File))
220       goto failure;
221 
222     return true;
223   }
224 
225   // Set up the preprocessor.
226   CI.createPreprocessor();
227 
228   // Inform the diagnostic client we are processing a source file.
229   CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
230                                            &CI.getPreprocessor());
231 
232   // Initialize the action.
233   if (!BeginSourceFileAction(CI, Input.File))
234     goto failure;
235 
236   /// Create the AST context and consumer unless this is a preprocessor only
237   /// action.
238   if (!usesPreprocessorOnly()) {
239     CI.createASTContext();
240 
241     llvm::OwningPtr<ASTConsumer> Consumer(
242                                    CreateWrappedASTConsumer(CI, Input.File));
243     if (!Consumer)
244       goto failure;
245 
246     CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
247 
248     if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
249       // Convert headers to PCH and chain them.
250       llvm::OwningPtr<ExternalASTSource> source;
251       source.reset(ChainedIncludesSource::create(CI));
252       if (!source)
253         goto failure;
254       CI.getASTContext().setExternalSource(source);
255 
256     } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
257       // Use PCH.
258       assert(hasPCHSupport() && "This action does not have PCH support!");
259       ASTDeserializationListener *DeserialListener =
260           Consumer->GetASTDeserializationListener();
261       if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls)
262         DeserialListener = new DeserializedDeclsDumper(DeserialListener);
263       if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty())
264         DeserialListener = new DeserializedDeclsChecker(CI.getASTContext(),
265                          CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
266                                                         DeserialListener);
267       CI.createPCHExternalASTSource(
268                                 CI.getPreprocessorOpts().ImplicitPCHInclude,
269                                 CI.getPreprocessorOpts().DisablePCHValidation,
270                                 CI.getPreprocessorOpts().DisableStatCache,
271                                 DeserialListener);
272       if (!CI.getASTContext().getExternalSource())
273         goto failure;
274     }
275 
276     CI.setASTConsumer(Consumer.take());
277     if (!CI.hasASTConsumer())
278       goto failure;
279   }
280 
281   // Initialize built-in info as long as we aren't using an external AST
282   // source.
283   if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
284     Preprocessor &PP = CI.getPreprocessor();
285     PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
286                                            PP.getLangOptions());
287   }
288 
289   // If there is a layout overrides file, attach an external AST source that
290   // provides the layouts from that file.
291   if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
292       CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
293     llvm::OwningPtr<ExternalASTSource>
294       Override(new LayoutOverrideSource(
295                      CI.getFrontendOpts().OverrideRecordLayoutsFile));
296     CI.getASTContext().setExternalSource(Override);
297   }
298 
299   return true;
300 
301   // If we failed, reset state since the client will not end up calling the
302   // matching EndSourceFile().
303   failure:
304   if (isCurrentFileAST()) {
305     CI.setASTContext(0);
306     CI.setPreprocessor(0);
307     CI.setSourceManager(0);
308     CI.setFileManager(0);
309   }
310 
311   CI.getDiagnosticClient().EndSourceFile();
312   setCurrentInput(FrontendInputFile());
313   setCompilerInstance(0);
314   return false;
315 }
316 
317 void FrontendAction::Execute() {
318   CompilerInstance &CI = getCompilerInstance();
319 
320   // Initialize the main file entry. This needs to be delayed until after PCH
321   // has loaded.
322   if (!isCurrentFileAST()) {
323     if (!CI.InitializeSourceManager(getCurrentFile(),
324                                     getCurrentInput().IsSystem
325                                       ? SrcMgr::C_System
326                                       : SrcMgr::C_User))
327       return;
328   }
329 
330   if (CI.hasFrontendTimer()) {
331     llvm::TimeRegion Timer(CI.getFrontendTimer());
332     ExecuteAction();
333   }
334   else ExecuteAction();
335 }
336 
337 void FrontendAction::EndSourceFile() {
338   CompilerInstance &CI = getCompilerInstance();
339 
340   // Inform the diagnostic client we are done with this source file.
341   CI.getDiagnosticClient().EndSourceFile();
342 
343   // Finalize the action.
344   EndSourceFileAction();
345 
346   // Release the consumer and the AST, in that order since the consumer may
347   // perform actions in its destructor which require the context.
348   //
349   // FIXME: There is more per-file stuff we could just drop here?
350   if (CI.getFrontendOpts().DisableFree) {
351     CI.takeASTConsumer();
352     if (!isCurrentFileAST()) {
353       CI.takeSema();
354       CI.resetAndLeakASTContext();
355     }
356   } else {
357     if (!isCurrentFileAST()) {
358       CI.setSema(0);
359       CI.setASTContext(0);
360     }
361     CI.setASTConsumer(0);
362   }
363 
364   // Inform the preprocessor we are done.
365   if (CI.hasPreprocessor())
366     CI.getPreprocessor().EndSourceFile();
367 
368   if (CI.getFrontendOpts().ShowStats) {
369     llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
370     CI.getPreprocessor().PrintStats();
371     CI.getPreprocessor().getIdentifierTable().PrintStats();
372     CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
373     CI.getSourceManager().PrintStats();
374     llvm::errs() << "\n";
375   }
376 
377   // Cleanup the output streams, and erase the output files if we encountered
378   // an error.
379   CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().hasErrorOccurred());
380 
381   if (isCurrentFileAST()) {
382     CI.takeSema();
383     CI.resetAndLeakASTContext();
384     CI.resetAndLeakPreprocessor();
385     CI.resetAndLeakSourceManager();
386     CI.resetAndLeakFileManager();
387   }
388 
389   setCompilerInstance(0);
390   setCurrentInput(FrontendInputFile());
391 }
392 
393 //===----------------------------------------------------------------------===//
394 // Utility Actions
395 //===----------------------------------------------------------------------===//
396 
397 void ASTFrontendAction::ExecuteAction() {
398   CompilerInstance &CI = getCompilerInstance();
399 
400   // FIXME: Move the truncation aspect of this into Sema, we delayed this till
401   // here so the source manager would be initialized.
402   if (hasCodeCompletionSupport() &&
403       !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
404     CI.createCodeCompletionConsumer();
405 
406   // Use a code completion consumer?
407   CodeCompleteConsumer *CompletionConsumer = 0;
408   if (CI.hasCodeCompletionConsumer())
409     CompletionConsumer = &CI.getCodeCompletionConsumer();
410 
411   if (!CI.hasSema())
412     CI.createSema(getTranslationUnitKind(), CompletionConsumer);
413 
414   ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats);
415 }
416 
417 void PluginASTAction::anchor() { }
418 
419 ASTConsumer *
420 PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
421                                               StringRef InFile) {
422   llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
423 }
424 
425 ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
426                                                       StringRef InFile) {
427   return WrappedAction->CreateASTConsumer(CI, InFile);
428 }
429 bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
430   return WrappedAction->BeginInvocation(CI);
431 }
432 bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
433                                                   StringRef Filename) {
434   WrappedAction->setCurrentInput(getCurrentInput());
435   WrappedAction->setCompilerInstance(&CI);
436   return WrappedAction->BeginSourceFileAction(CI, Filename);
437 }
438 void WrapperFrontendAction::ExecuteAction() {
439   WrappedAction->ExecuteAction();
440 }
441 void WrapperFrontendAction::EndSourceFileAction() {
442   WrappedAction->EndSourceFileAction();
443 }
444 
445 bool WrapperFrontendAction::usesPreprocessorOnly() const {
446   return WrappedAction->usesPreprocessorOnly();
447 }
448 TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
449   return WrappedAction->getTranslationUnitKind();
450 }
451 bool WrapperFrontendAction::hasPCHSupport() const {
452   return WrappedAction->hasPCHSupport();
453 }
454 bool WrapperFrontendAction::hasASTFileSupport() const {
455   return WrappedAction->hasASTFileSupport();
456 }
457 bool WrapperFrontendAction::hasIRSupport() const {
458   return WrappedAction->hasIRSupport();
459 }
460 bool WrapperFrontendAction::hasCodeCompletionSupport() const {
461   return WrappedAction->hasCodeCompletionSupport();
462 }
463 
464 WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
465   : WrappedAction(WrappedAction) {}
466 
467