FrontendAction.cpp revision 0e2c34f92f00628d48968dfea096d36381f494cb
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/Frontend/ASTUnit.h" 15#include "clang/Frontend/CompilerInstance.h" 16#include "clang/Frontend/FrontendDiagnostic.h" 17#include "clang/Frontend/FrontendPluginRegistry.h" 18#include "clang/Frontend/LayoutOverrideSource.h" 19#include "clang/Frontend/MultiplexConsumer.h" 20#include "clang/Frontend/Utils.h" 21#include "clang/Lex/HeaderSearch.h" 22#include "clang/Lex/Preprocessor.h" 23#include "clang/Parse/ParseAST.h" 24#include "clang/Serialization/ASTDeserializationListener.h" 25#include "clang/Serialization/ASTReader.h" 26#include "clang/Serialization/GlobalModuleIndex.h" 27#include "llvm/Support/ErrorHandling.h" 28#include "llvm/Support/FileSystem.h" 29#include "llvm/Support/MemoryBuffer.h" 30#include "llvm/Support/Timer.h" 31#include "llvm/Support/raw_ostream.h" 32#include <system_error> 33using namespace clang; 34 35template class llvm::Registry<clang::PluginASTAction>; 36 37namespace { 38 39class DelegatingDeserializationListener : public ASTDeserializationListener { 40 ASTDeserializationListener *Previous; 41 bool DeletePrevious; 42 43public: 44 explicit DelegatingDeserializationListener( 45 ASTDeserializationListener *Previous, bool DeletePrevious) 46 : Previous(Previous), DeletePrevious(DeletePrevious) {} 47 virtual ~DelegatingDeserializationListener() { 48 if (DeletePrevious) 49 delete Previous; 50 } 51 52 void ReaderInitialized(ASTReader *Reader) override { 53 if (Previous) 54 Previous->ReaderInitialized(Reader); 55 } 56 void IdentifierRead(serialization::IdentID ID, 57 IdentifierInfo *II) override { 58 if (Previous) 59 Previous->IdentifierRead(ID, II); 60 } 61 void TypeRead(serialization::TypeIdx Idx, QualType T) override { 62 if (Previous) 63 Previous->TypeRead(Idx, T); 64 } 65 void DeclRead(serialization::DeclID ID, const Decl *D) override { 66 if (Previous) 67 Previous->DeclRead(ID, D); 68 } 69 void SelectorRead(serialization::SelectorID ID, Selector Sel) override { 70 if (Previous) 71 Previous->SelectorRead(ID, Sel); 72 } 73 void MacroDefinitionRead(serialization::PreprocessedEntityID PPID, 74 MacroDefinition *MD) override { 75 if (Previous) 76 Previous->MacroDefinitionRead(PPID, MD); 77 } 78}; 79 80/// \brief Dumps deserialized declarations. 81class DeserializedDeclsDumper : public DelegatingDeserializationListener { 82public: 83 explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous, 84 bool DeletePrevious) 85 : DelegatingDeserializationListener(Previous, DeletePrevious) {} 86 87 void DeclRead(serialization::DeclID ID, const Decl *D) override { 88 llvm::outs() << "PCH DECL: " << D->getDeclKindName(); 89 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) 90 llvm::outs() << " - " << *ND; 91 llvm::outs() << "\n"; 92 93 DelegatingDeserializationListener::DeclRead(ID, D); 94 } 95}; 96 97/// \brief Checks deserialized declarations and emits error if a name 98/// matches one given in command-line using -error-on-deserialized-decl. 99class DeserializedDeclsChecker : public DelegatingDeserializationListener { 100 ASTContext &Ctx; 101 std::set<std::string> NamesToCheck; 102 103public: 104 DeserializedDeclsChecker(ASTContext &Ctx, 105 const std::set<std::string> &NamesToCheck, 106 ASTDeserializationListener *Previous, 107 bool DeletePrevious) 108 : DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx), 109 NamesToCheck(NamesToCheck) {} 110 111 void DeclRead(serialization::DeclID ID, const Decl *D) override { 112 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) 113 if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) { 114 unsigned DiagID 115 = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, 116 "%0 was deserialized"); 117 Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID) 118 << ND->getNameAsString(); 119 } 120 121 DelegatingDeserializationListener::DeclRead(ID, D); 122 } 123}; 124 125} // end anonymous namespace 126 127FrontendAction::FrontendAction() : Instance(nullptr) {} 128 129FrontendAction::~FrontendAction() {} 130 131void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput, 132 std::unique_ptr<ASTUnit> AST) { 133 this->CurrentInput = CurrentInput; 134 CurrentASTUnit = std::move(AST); 135} 136 137std::unique_ptr<ASTConsumer> 138FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI, 139 StringRef InFile) { 140 std::unique_ptr<ASTConsumer> Consumer = CreateASTConsumer(CI, InFile); 141 if (!Consumer) 142 return nullptr; 143 144 if (CI.getFrontendOpts().AddPluginActions.size() == 0) 145 return Consumer; 146 147 // Make sure the non-plugin consumer is first, so that plugins can't 148 // modifiy the AST. 149 std::vector<std::unique_ptr<ASTConsumer>> Consumers; 150 Consumers.push_back(std::move(Consumer)); 151 152 for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size(); 153 i != e; ++i) { 154 // This is O(|plugins| * |add_plugins|), but since both numbers are 155 // way below 50 in practice, that's ok. 156 for (FrontendPluginRegistry::iterator 157 it = FrontendPluginRegistry::begin(), 158 ie = FrontendPluginRegistry::end(); 159 it != ie; ++it) { 160 if (it->getName() != CI.getFrontendOpts().AddPluginActions[i]) 161 continue; 162 std::unique_ptr<PluginASTAction> P = it->instantiate(); 163 if (P->ParseArgs(CI, CI.getFrontendOpts().AddPluginArgs[i])) 164 Consumers.push_back(P->CreateASTConsumer(CI, InFile)); 165 } 166 } 167 168 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers)); 169} 170 171bool FrontendAction::BeginSourceFile(CompilerInstance &CI, 172 const FrontendInputFile &Input) { 173 assert(!Instance && "Already processing a source file!"); 174 assert(!Input.isEmpty() && "Unexpected empty filename!"); 175 setCurrentInput(Input); 176 setCompilerInstance(&CI); 177 178 StringRef InputFile = Input.getFile(); 179 bool HasBegunSourceFile = false; 180 if (!BeginInvocation(CI)) 181 goto failure; 182 183 // AST files follow a very different path, since they share objects via the 184 // AST unit. 185 if (Input.getKind() == IK_AST) { 186 assert(!usesPreprocessorOnly() && 187 "Attempt to pass AST file to preprocessor only action!"); 188 assert(hasASTFileSupport() && 189 "This action does not have AST file support!"); 190 191 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics()); 192 193 std::unique_ptr<ASTUnit> AST = 194 ASTUnit::LoadFromASTFile(InputFile, Diags, CI.getFileSystemOpts()); 195 196 if (!AST) 197 goto failure; 198 199 // Inform the diagnostic client we are processing a source file. 200 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr); 201 HasBegunSourceFile = true; 202 203 // Set the shared objects, these are reset when we finish processing the 204 // file, otherwise the CompilerInstance will happily destroy them. 205 CI.setFileManager(&AST->getFileManager()); 206 CI.setSourceManager(&AST->getSourceManager()); 207 CI.setPreprocessor(&AST->getPreprocessor()); 208 CI.setASTContext(&AST->getASTContext()); 209 210 setCurrentInput(Input, std::move(AST)); 211 212 // Initialize the action. 213 if (!BeginSourceFileAction(CI, InputFile)) 214 goto failure; 215 216 // Create the AST consumer. 217 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile)); 218 if (!CI.hasASTConsumer()) 219 goto failure; 220 221 return true; 222 } 223 224 if (!CI.hasVirtualFileSystem()) { 225 if (IntrusiveRefCntPtr<vfs::FileSystem> VFS = 226 createVFSFromCompilerInvocation(CI.getInvocation(), 227 CI.getDiagnostics())) 228 CI.setVirtualFileSystem(VFS); 229 else 230 goto failure; 231 } 232 233 // Set up the file and source managers, if needed. 234 if (!CI.hasFileManager()) 235 CI.createFileManager(); 236 if (!CI.hasSourceManager()) 237 CI.createSourceManager(CI.getFileManager()); 238 239 // IR files bypass the rest of initialization. 240 if (Input.getKind() == IK_LLVM_IR) { 241 assert(hasIRSupport() && 242 "This action does not have IR file support!"); 243 244 // Inform the diagnostic client we are processing a source file. 245 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr); 246 HasBegunSourceFile = true; 247 248 // Initialize the action. 249 if (!BeginSourceFileAction(CI, InputFile)) 250 goto failure; 251 252 // Initialize the main file entry. 253 if (!CI.InitializeSourceManager(CurrentInput)) 254 goto failure; 255 256 return true; 257 } 258 259 // If the implicit PCH include is actually a directory, rather than 260 // a single file, search for a suitable PCH file in that directory. 261 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) { 262 FileManager &FileMgr = CI.getFileManager(); 263 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts(); 264 StringRef PCHInclude = PPOpts.ImplicitPCHInclude; 265 std::string SpecificModuleCachePath = CI.getSpecificModuleCachePath(); 266 if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) { 267 std::error_code EC; 268 SmallString<128> DirNative; 269 llvm::sys::path::native(PCHDir->getName(), DirNative); 270 bool Found = false; 271 for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd; 272 Dir != DirEnd && !EC; Dir.increment(EC)) { 273 // Check whether this is an acceptable AST file. 274 if (ASTReader::isAcceptableASTFile(Dir->path(), FileMgr, 275 CI.getLangOpts(), 276 CI.getTargetOpts(), 277 CI.getPreprocessorOpts(), 278 SpecificModuleCachePath)) { 279 PPOpts.ImplicitPCHInclude = Dir->path(); 280 Found = true; 281 break; 282 } 283 } 284 285 if (!Found) { 286 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude; 287 return true; 288 } 289 } 290 } 291 292 // Set up the preprocessor if needed. When parsing model files the 293 // preprocessor of the original source is reused. 294 if (!isModelParsingAction()) 295 CI.createPreprocessor(getTranslationUnitKind()); 296 297 // Inform the diagnostic client we are processing a source file. 298 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 299 &CI.getPreprocessor()); 300 HasBegunSourceFile = true; 301 302 // Initialize the action. 303 if (!BeginSourceFileAction(CI, InputFile)) 304 goto failure; 305 306 // Initialize the main file entry. It is important that this occurs after 307 // BeginSourceFileAction, which may change CurrentInput during module builds. 308 if (!CI.InitializeSourceManager(CurrentInput)) 309 goto failure; 310 311 // Create the AST context and consumer unless this is a preprocessor only 312 // action. 313 if (!usesPreprocessorOnly()) { 314 // Parsing a model file should reuse the existing ASTContext. 315 if (!isModelParsingAction()) 316 CI.createASTContext(); 317 318 std::unique_ptr<ASTConsumer> Consumer = 319 CreateWrappedASTConsumer(CI, InputFile); 320 if (!Consumer) 321 goto failure; 322 323 // FIXME: should not overwrite ASTMutationListener when parsing model files? 324 if (!isModelParsingAction()) 325 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener()); 326 327 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) { 328 // Convert headers to PCH and chain them. 329 IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader; 330 source = createChainedIncludesSource(CI, FinalReader); 331 if (!source) 332 goto failure; 333 CI.setModuleManager(static_cast<ASTReader *>(FinalReader.get())); 334 CI.getASTContext().setExternalSource(source); 335 } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) { 336 // Use PCH. 337 assert(hasPCHSupport() && "This action does not have PCH support!"); 338 ASTDeserializationListener *DeserialListener = 339 Consumer->GetASTDeserializationListener(); 340 bool DeleteDeserialListener = false; 341 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) { 342 DeserialListener = new DeserializedDeclsDumper(DeserialListener, 343 DeleteDeserialListener); 344 DeleteDeserialListener = true; 345 } 346 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) { 347 DeserialListener = new DeserializedDeclsChecker( 348 CI.getASTContext(), 349 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn, 350 DeserialListener, DeleteDeserialListener); 351 DeleteDeserialListener = true; 352 } 353 CI.createPCHExternalASTSource( 354 CI.getPreprocessorOpts().ImplicitPCHInclude, 355 CI.getPreprocessorOpts().DisablePCHValidation, 356 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, DeserialListener, 357 DeleteDeserialListener); 358 if (!CI.getASTContext().getExternalSource()) 359 goto failure; 360 } 361 362 CI.setASTConsumer(std::move(Consumer)); 363 if (!CI.hasASTConsumer()) 364 goto failure; 365 } 366 367 // Initialize built-in info as long as we aren't using an external AST 368 // source. 369 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) { 370 Preprocessor &PP = CI.getPreprocessor(); 371 372 // If modules are enabled, create the module manager before creating 373 // any builtins, so that all declarations know that they might be 374 // extended by an external source. 375 if (CI.getLangOpts().Modules) 376 CI.createModuleManager(); 377 378 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(), 379 PP.getLangOpts()); 380 } else { 381 // FIXME: If this is a problem, recover from it by creating a multiplex 382 // source. 383 assert((!CI.getLangOpts().Modules || CI.getModuleManager()) && 384 "modules enabled but created an external source that " 385 "doesn't support modules"); 386 } 387 388 // If we were asked to load any module map files, do so now. 389 for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) { 390 if (auto *File = CI.getFileManager().getFile(Filename)) 391 CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile( 392 File, /*IsSystem*/false); 393 else 394 CI.getDiagnostics().Report(diag::err_module_map_not_found) << Filename; 395 } 396 397 // If we were asked to load any module files, do so now. 398 for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles) 399 if (!CI.loadModuleFile(ModuleFile)) 400 goto failure; 401 402 // If there is a layout overrides file, attach an external AST source that 403 // provides the layouts from that file. 404 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() && 405 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) { 406 IntrusiveRefCntPtr<ExternalASTSource> 407 Override(new LayoutOverrideSource( 408 CI.getFrontendOpts().OverrideRecordLayoutsFile)); 409 CI.getASTContext().setExternalSource(Override); 410 } 411 412 return true; 413 414 // If we failed, reset state since the client will not end up calling the 415 // matching EndSourceFile(). 416 failure: 417 if (isCurrentFileAST()) { 418 CI.setASTContext(nullptr); 419 CI.setPreprocessor(nullptr); 420 CI.setSourceManager(nullptr); 421 CI.setFileManager(nullptr); 422 } 423 424 if (HasBegunSourceFile) 425 CI.getDiagnosticClient().EndSourceFile(); 426 CI.clearOutputFiles(/*EraseFiles=*/true); 427 setCurrentInput(FrontendInputFile()); 428 setCompilerInstance(nullptr); 429 return false; 430} 431 432bool FrontendAction::Execute() { 433 CompilerInstance &CI = getCompilerInstance(); 434 435 if (CI.hasFrontendTimer()) { 436 llvm::TimeRegion Timer(CI.getFrontendTimer()); 437 ExecuteAction(); 438 } 439 else ExecuteAction(); 440 441 // If we are supposed to rebuild the global module index, do so now unless 442 // there were any module-build failures. 443 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() && 444 CI.hasPreprocessor()) { 445 GlobalModuleIndex::writeIndex( 446 CI.getFileManager(), 447 CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath()); 448 } 449 450 return true; 451} 452 453void FrontendAction::EndSourceFile() { 454 CompilerInstance &CI = getCompilerInstance(); 455 456 // Inform the diagnostic client we are done with this source file. 457 CI.getDiagnosticClient().EndSourceFile(); 458 459 // Inform the preprocessor we are done. 460 if (CI.hasPreprocessor()) 461 CI.getPreprocessor().EndSourceFile(); 462 463 // Finalize the action. 464 EndSourceFileAction(); 465 466 // Sema references the ast consumer, so reset sema first. 467 // 468 // FIXME: There is more per-file stuff we could just drop here? 469 bool DisableFree = CI.getFrontendOpts().DisableFree; 470 if (DisableFree) { 471 if (!isCurrentFileAST()) { 472 CI.resetAndLeakSema(); 473 CI.resetAndLeakASTContext(); 474 } 475 BuryPointer(CI.takeASTConsumer().get()); 476 } else { 477 if (!isCurrentFileAST()) { 478 CI.setSema(nullptr); 479 CI.setASTContext(nullptr); 480 } 481 CI.setASTConsumer(nullptr); 482 } 483 484 if (CI.getFrontendOpts().ShowStats) { 485 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n"; 486 CI.getPreprocessor().PrintStats(); 487 CI.getPreprocessor().getIdentifierTable().PrintStats(); 488 CI.getPreprocessor().getHeaderSearchInfo().PrintStats(); 489 CI.getSourceManager().PrintStats(); 490 llvm::errs() << "\n"; 491 } 492 493 // Cleanup the output streams, and erase the output files if instructed by the 494 // FrontendAction. 495 CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles()); 496 497 // FIXME: Only do this if DisableFree is set. 498 if (isCurrentFileAST()) { 499 CI.resetAndLeakSema(); 500 CI.resetAndLeakASTContext(); 501 CI.resetAndLeakPreprocessor(); 502 CI.resetAndLeakSourceManager(); 503 CI.resetAndLeakFileManager(); 504 } 505 506 setCompilerInstance(nullptr); 507 setCurrentInput(FrontendInputFile()); 508} 509 510bool FrontendAction::shouldEraseOutputFiles() { 511 return getCompilerInstance().getDiagnostics().hasErrorOccurred(); 512} 513 514//===----------------------------------------------------------------------===// 515// Utility Actions 516//===----------------------------------------------------------------------===// 517 518void ASTFrontendAction::ExecuteAction() { 519 CompilerInstance &CI = getCompilerInstance(); 520 if (!CI.hasPreprocessor()) 521 return; 522 523 // FIXME: Move the truncation aspect of this into Sema, we delayed this till 524 // here so the source manager would be initialized. 525 if (hasCodeCompletionSupport() && 526 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty()) 527 CI.createCodeCompletionConsumer(); 528 529 // Use a code completion consumer? 530 CodeCompleteConsumer *CompletionConsumer = nullptr; 531 if (CI.hasCodeCompletionConsumer()) 532 CompletionConsumer = &CI.getCodeCompletionConsumer(); 533 534 if (!CI.hasSema()) 535 CI.createSema(getTranslationUnitKind(), CompletionConsumer); 536 537 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats, 538 CI.getFrontendOpts().SkipFunctionBodies); 539} 540 541void PluginASTAction::anchor() { } 542 543std::unique_ptr<ASTConsumer> 544PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI, 545 StringRef InFile) { 546 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!"); 547} 548 549std::unique_ptr<ASTConsumer> 550WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI, 551 StringRef InFile) { 552 return WrappedAction->CreateASTConsumer(CI, InFile); 553} 554bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) { 555 return WrappedAction->BeginInvocation(CI); 556} 557bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI, 558 StringRef Filename) { 559 WrappedAction->setCurrentInput(getCurrentInput()); 560 WrappedAction->setCompilerInstance(&CI); 561 return WrappedAction->BeginSourceFileAction(CI, Filename); 562} 563void WrapperFrontendAction::ExecuteAction() { 564 WrappedAction->ExecuteAction(); 565} 566void WrapperFrontendAction::EndSourceFileAction() { 567 WrappedAction->EndSourceFileAction(); 568} 569 570bool WrapperFrontendAction::usesPreprocessorOnly() const { 571 return WrappedAction->usesPreprocessorOnly(); 572} 573TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() { 574 return WrappedAction->getTranslationUnitKind(); 575} 576bool WrapperFrontendAction::hasPCHSupport() const { 577 return WrappedAction->hasPCHSupport(); 578} 579bool WrapperFrontendAction::hasASTFileSupport() const { 580 return WrappedAction->hasASTFileSupport(); 581} 582bool WrapperFrontendAction::hasIRSupport() const { 583 return WrappedAction->hasIRSupport(); 584} 585bool WrapperFrontendAction::hasCodeCompletionSupport() const { 586 return WrappedAction->hasCodeCompletionSupport(); 587} 588 589WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction) 590 : WrappedAction(WrappedAction) {} 591 592