FrontendAction.cpp revision 374a00bcc6e26b4fc3cd1d378a5d056c4c7d618e
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" 30using namespace clang; 31 32namespace { 33 34class DelegatingDeserializationListener : public ASTDeserializationListener { 35 ASTDeserializationListener *Previous; 36 37public: 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. 71class DeserializedDeclsDumper : public DelegatingDeserializationListener { 72public: 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. 88class DeserializedDeclsChecker : public DelegatingDeserializationListener { 89 ASTContext &Ctx; 90 std::set<std::string> NamesToCheck; 91 92public: 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 115FrontendAction::FrontendAction() : Instance(0) {} 116 117FrontendAction::~FrontendAction() {} 118 119void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput, 120 ASTUnit *AST) { 121 this->CurrentInput = CurrentInput; 122 CurrentASTUnit.reset(AST); 123} 124 125ASTConsumer* 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 158bool 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 318bool 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 false; 329 } 330 331 if (CI.hasFrontendTimer()) { 332 llvm::TimeRegion Timer(CI.getFrontendTimer()); 333 ExecuteAction(); 334 } 335 else ExecuteAction(); 336 337 return true; 338} 339 340void FrontendAction::EndSourceFile() { 341 CompilerInstance &CI = getCompilerInstance(); 342 343 // Inform the diagnostic client we are done with this source file. 344 CI.getDiagnosticClient().EndSourceFile(); 345 346 // Finalize the action. 347 EndSourceFileAction(); 348 349 // Release the consumer and the AST, in that order since the consumer may 350 // perform actions in its destructor which require the context. 351 // 352 // FIXME: There is more per-file stuff we could just drop here? 353 if (CI.getFrontendOpts().DisableFree) { 354 CI.takeASTConsumer(); 355 if (!isCurrentFileAST()) { 356 CI.takeSema(); 357 CI.resetAndLeakASTContext(); 358 } 359 } else { 360 if (!isCurrentFileAST()) { 361 CI.setSema(0); 362 CI.setASTContext(0); 363 } 364 CI.setASTConsumer(0); 365 } 366 367 // Inform the preprocessor we are done. 368 if (CI.hasPreprocessor()) 369 CI.getPreprocessor().EndSourceFile(); 370 371 if (CI.getFrontendOpts().ShowStats) { 372 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n"; 373 CI.getPreprocessor().PrintStats(); 374 CI.getPreprocessor().getIdentifierTable().PrintStats(); 375 CI.getPreprocessor().getHeaderSearchInfo().PrintStats(); 376 CI.getSourceManager().PrintStats(); 377 llvm::errs() << "\n"; 378 } 379 380 // Cleanup the output streams, and erase the output files if we encountered 381 // an error. 382 CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().hasErrorOccurred()); 383 384 if (isCurrentFileAST()) { 385 CI.takeSema(); 386 CI.resetAndLeakASTContext(); 387 CI.resetAndLeakPreprocessor(); 388 CI.resetAndLeakSourceManager(); 389 CI.resetAndLeakFileManager(); 390 } 391 392 setCompilerInstance(0); 393 setCurrentInput(FrontendInputFile()); 394} 395 396//===----------------------------------------------------------------------===// 397// Utility Actions 398//===----------------------------------------------------------------------===// 399 400void ASTFrontendAction::ExecuteAction() { 401 CompilerInstance &CI = getCompilerInstance(); 402 403 // FIXME: Move the truncation aspect of this into Sema, we delayed this till 404 // here so the source manager would be initialized. 405 if (hasCodeCompletionSupport() && 406 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty()) 407 CI.createCodeCompletionConsumer(); 408 409 // Use a code completion consumer? 410 CodeCompleteConsumer *CompletionConsumer = 0; 411 if (CI.hasCodeCompletionConsumer()) 412 CompletionConsumer = &CI.getCodeCompletionConsumer(); 413 414 if (!CI.hasSema()) 415 CI.createSema(getTranslationUnitKind(), CompletionConsumer); 416 417 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats, 418 CI.getFrontendOpts().SkipFunctionBodies); 419} 420 421void PluginASTAction::anchor() { } 422 423ASTConsumer * 424PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI, 425 StringRef InFile) { 426 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!"); 427} 428 429ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI, 430 StringRef InFile) { 431 return WrappedAction->CreateASTConsumer(CI, InFile); 432} 433bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) { 434 return WrappedAction->BeginInvocation(CI); 435} 436bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI, 437 StringRef Filename) { 438 WrappedAction->setCurrentInput(getCurrentInput()); 439 WrappedAction->setCompilerInstance(&CI); 440 return WrappedAction->BeginSourceFileAction(CI, Filename); 441} 442void WrapperFrontendAction::ExecuteAction() { 443 WrappedAction->ExecuteAction(); 444} 445void WrapperFrontendAction::EndSourceFileAction() { 446 WrappedAction->EndSourceFileAction(); 447} 448 449bool WrapperFrontendAction::usesPreprocessorOnly() const { 450 return WrappedAction->usesPreprocessorOnly(); 451} 452TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() { 453 return WrappedAction->getTranslationUnitKind(); 454} 455bool WrapperFrontendAction::hasPCHSupport() const { 456 return WrappedAction->hasPCHSupport(); 457} 458bool WrapperFrontendAction::hasASTFileSupport() const { 459 return WrappedAction->hasASTFileSupport(); 460} 461bool WrapperFrontendAction::hasIRSupport() const { 462 return WrappedAction->hasIRSupport(); 463} 464bool WrapperFrontendAction::hasCodeCompletionSupport() const { 465 return WrappedAction->hasCodeCompletionSupport(); 466} 467 468WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction) 469 : WrappedAction(WrappedAction) {} 470 471