]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Frontend/FrontendAction.cpp
Merge ACPICA 20120620.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Frontend / FrontendAction.cpp
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;
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         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     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     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       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                             CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
272                                 DeserialListener);
273       if (!CI.getASTContext().getExternalSource())
274         goto failure;
275     }
276
277     CI.setASTConsumer(Consumer.take());
278     if (!CI.hasASTConsumer())
279       goto failure;
280   }
281
282   // Initialize built-in info as long as we aren't using an external AST
283   // source.
284   if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
285     Preprocessor &PP = CI.getPreprocessor();
286     PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
287                                            PP.getLangOpts());
288   }
289
290   // If there is a layout overrides file, attach an external AST source that
291   // provides the layouts from that file.
292   if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() && 
293       CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
294     OwningPtr<ExternalASTSource> 
295       Override(new LayoutOverrideSource(
296                      CI.getFrontendOpts().OverrideRecordLayoutsFile));
297     CI.getASTContext().setExternalSource(Override);
298   }
299   
300   return true;
301
302   // If we failed, reset state since the client will not end up calling the
303   // matching EndSourceFile().
304   failure:
305   if (isCurrentFileAST()) {
306     CI.setASTContext(0);
307     CI.setPreprocessor(0);
308     CI.setSourceManager(0);
309     CI.setFileManager(0);
310   }
311
312   CI.getDiagnosticClient().EndSourceFile();
313   setCurrentInput(FrontendInputFile());
314   setCompilerInstance(0);
315   return false;
316 }
317
318 void FrontendAction::Execute() {
319   CompilerInstance &CI = getCompilerInstance();
320
321   // Initialize the main file entry. This needs to be delayed until after PCH
322   // has loaded.
323   if (!isCurrentFileAST()) {
324     if (!CI.InitializeSourceManager(getCurrentFile(),
325                                     getCurrentInput().IsSystem
326                                       ? SrcMgr::C_System
327                                       : SrcMgr::C_User))
328       return;
329   }
330
331   if (CI.hasFrontendTimer()) {
332     llvm::TimeRegion Timer(CI.getFrontendTimer());
333     ExecuteAction();
334   }
335   else ExecuteAction();
336 }
337
338 void FrontendAction::EndSourceFile() {
339   CompilerInstance &CI = getCompilerInstance();
340
341   // Inform the diagnostic client we are done with this source file.
342   CI.getDiagnosticClient().EndSourceFile();
343
344   // Finalize the action.
345   EndSourceFileAction();
346
347   // Release the consumer and the AST, in that order since the consumer may
348   // perform actions in its destructor which require the context.
349   //
350   // FIXME: There is more per-file stuff we could just drop here?
351   if (CI.getFrontendOpts().DisableFree) {
352     CI.takeASTConsumer();
353     if (!isCurrentFileAST()) {
354       CI.takeSema();
355       CI.resetAndLeakASTContext();
356     }
357   } else {
358     if (!isCurrentFileAST()) {
359       CI.setSema(0);
360       CI.setASTContext(0);
361     }
362     CI.setASTConsumer(0);
363   }
364
365   // Inform the preprocessor we are done.
366   if (CI.hasPreprocessor())
367     CI.getPreprocessor().EndSourceFile();
368
369   if (CI.getFrontendOpts().ShowStats) {
370     llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
371     CI.getPreprocessor().PrintStats();
372     CI.getPreprocessor().getIdentifierTable().PrintStats();
373     CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
374     CI.getSourceManager().PrintStats();
375     llvm::errs() << "\n";
376   }
377
378   // Cleanup the output streams, and erase the output files if we encountered
379   // an error.
380   CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().hasErrorOccurred());
381
382   if (isCurrentFileAST()) {
383     CI.takeSema();
384     CI.resetAndLeakASTContext();
385     CI.resetAndLeakPreprocessor();
386     CI.resetAndLeakSourceManager();
387     CI.resetAndLeakFileManager();
388   }
389
390   setCompilerInstance(0);
391   setCurrentInput(FrontendInputFile());
392 }
393
394 //===----------------------------------------------------------------------===//
395 // Utility Actions
396 //===----------------------------------------------------------------------===//
397
398 void ASTFrontendAction::ExecuteAction() {
399   CompilerInstance &CI = getCompilerInstance();
400
401   // FIXME: Move the truncation aspect of this into Sema, we delayed this till
402   // here so the source manager would be initialized.
403   if (hasCodeCompletionSupport() &&
404       !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
405     CI.createCodeCompletionConsumer();
406
407   // Use a code completion consumer?
408   CodeCompleteConsumer *CompletionConsumer = 0;
409   if (CI.hasCodeCompletionConsumer())
410     CompletionConsumer = &CI.getCodeCompletionConsumer();
411
412   if (!CI.hasSema())
413     CI.createSema(getTranslationUnitKind(), CompletionConsumer);
414
415   ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
416            CI.getFrontendOpts().SkipFunctionBodies);
417 }
418
419 void PluginASTAction::anchor() { }
420
421 ASTConsumer *
422 PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
423                                               StringRef InFile) {
424   llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
425 }
426
427 ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
428                                                       StringRef InFile) {
429   return WrappedAction->CreateASTConsumer(CI, InFile);
430 }
431 bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
432   return WrappedAction->BeginInvocation(CI);
433 }
434 bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
435                                                   StringRef Filename) {
436   WrappedAction->setCurrentInput(getCurrentInput());
437   WrappedAction->setCompilerInstance(&CI);
438   return WrappedAction->BeginSourceFileAction(CI, Filename);
439 }
440 void WrapperFrontendAction::ExecuteAction() {
441   WrappedAction->ExecuteAction();
442 }
443 void WrapperFrontendAction::EndSourceFileAction() {
444   WrappedAction->EndSourceFileAction();
445 }
446
447 bool WrapperFrontendAction::usesPreprocessorOnly() const {
448   return WrappedAction->usesPreprocessorOnly();
449 }
450 TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
451   return WrappedAction->getTranslationUnitKind();
452 }
453 bool WrapperFrontendAction::hasPCHSupport() const {
454   return WrappedAction->hasPCHSupport();
455 }
456 bool WrapperFrontendAction::hasASTFileSupport() const {
457   return WrappedAction->hasASTFileSupport();
458 }
459 bool WrapperFrontendAction::hasIRSupport() const {
460   return WrappedAction->hasIRSupport();
461 }
462 bool WrapperFrontendAction::hasCodeCompletionSupport() const {
463   return WrappedAction->hasCodeCompletionSupport();
464 }
465
466 WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
467   : WrappedAction(WrappedAction) {}
468