| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | #include "clang/Frontend/FrontendAction.h" |
| 10 | #include "clang/AST/ASTConsumer.h" |
| 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "clang/AST/DeclGroup.h" |
| 13 | #include "clang/Frontend/ASTUnit.h" |
| 14 | #include "clang/Frontend/CompilerInstance.h" |
| 15 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 16 | #include "clang/Frontend/FrontendPluginRegistry.h" |
| 17 | #include "clang/Frontend/LayoutOverrideSource.h" |
| 18 | #include "clang/Frontend/MultiplexConsumer.h" |
| 19 | #include "clang/Frontend/Utils.h" |
| 20 | #include "clang/Lex/HeaderSearch.h" |
| 21 | #include "clang/Lex/LiteralSupport.h" |
| 22 | #include "clang/Lex/Preprocessor.h" |
| 23 | #include "clang/Lex/PreprocessorOptions.h" |
| 24 | #include "clang/Parse/ParseAST.h" |
| 25 | #include "clang/Serialization/ASTDeserializationListener.h" |
| 26 | #include "clang/Serialization/ASTReader.h" |
| 27 | #include "clang/Serialization/GlobalModuleIndex.h" |
| 28 | #include "llvm/Support/BuryPointer.h" |
| 29 | #include "llvm/Support/ErrorHandling.h" |
| 30 | #include "llvm/Support/FileSystem.h" |
| 31 | #include "llvm/Support/Path.h" |
| 32 | #include "llvm/Support/Timer.h" |
| 33 | #include "llvm/Support/raw_ostream.h" |
| 34 | #include <system_error> |
| 35 | using namespace clang; |
| 36 | |
| 37 | LLVM_INSTANTIATE_REGISTRY(FrontendPluginRegistry) |
| 38 | |
| 39 | namespace { |
| 40 | |
| 41 | class DelegatingDeserializationListener : public ASTDeserializationListener { |
| 42 | ASTDeserializationListener *Previous; |
| 43 | bool DeletePrevious; |
| 44 | |
| 45 | public: |
| 46 | explicit DelegatingDeserializationListener( |
| 47 | ASTDeserializationListener *Previous, bool DeletePrevious) |
| 48 | : Previous(Previous), DeletePrevious(DeletePrevious) {} |
| 49 | ~DelegatingDeserializationListener() override { |
| 50 | if (DeletePrevious) |
| 51 | delete Previous; |
| 52 | } |
| 53 | |
| 54 | void ReaderInitialized(ASTReader *Reader) override { |
| 55 | if (Previous) |
| 56 | Previous->ReaderInitialized(Reader); |
| 57 | } |
| 58 | void IdentifierRead(serialization::IdentID ID, |
| 59 | IdentifierInfo *II) override { |
| 60 | if (Previous) |
| 61 | Previous->IdentifierRead(ID, II); |
| 62 | } |
| 63 | void TypeRead(serialization::TypeIdx Idx, QualType T) override { |
| 64 | if (Previous) |
| 65 | Previous->TypeRead(Idx, T); |
| 66 | } |
| 67 | void DeclRead(serialization::DeclID ID, const Decl *D) override { |
| 68 | if (Previous) |
| 69 | Previous->DeclRead(ID, D); |
| 70 | } |
| 71 | void SelectorRead(serialization::SelectorID ID, Selector Sel) override { |
| 72 | if (Previous) |
| 73 | Previous->SelectorRead(ID, Sel); |
| 74 | } |
| 75 | void MacroDefinitionRead(serialization::PreprocessedEntityID PPID, |
| 76 | MacroDefinitionRecord *MD) override { |
| 77 | if (Previous) |
| 78 | Previous->MacroDefinitionRead(PPID, MD); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | |
| 83 | class DeserializedDeclsDumper : public DelegatingDeserializationListener { |
| 84 | public: |
| 85 | explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous, |
| 86 | bool DeletePrevious) |
| 87 | : DelegatingDeserializationListener(Previous, DeletePrevious) {} |
| 88 | |
| 89 | void DeclRead(serialization::DeclID ID, const Decl *D) override { |
| 90 | llvm::outs() << "PCH DECL: " << D->getDeclKindName(); |
| 91 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
| 92 | llvm::outs() << " - "; |
| 93 | ND->printQualifiedName(llvm::outs()); |
| 94 | } |
| 95 | llvm::outs() << "\n"; |
| 96 | |
| 97 | DelegatingDeserializationListener::DeclRead(ID, D); |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | |
| 102 | |
| 103 | class DeserializedDeclsChecker : public DelegatingDeserializationListener { |
| 104 | ASTContext &Ctx; |
| 105 | std::set<std::string> NamesToCheck; |
| 106 | |
| 107 | public: |
| 108 | DeserializedDeclsChecker(ASTContext &Ctx, |
| 109 | const std::set<std::string> &NamesToCheck, |
| 110 | ASTDeserializationListener *Previous, |
| 111 | bool DeletePrevious) |
| 112 | : DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx), |
| 113 | NamesToCheck(NamesToCheck) {} |
| 114 | |
| 115 | void DeclRead(serialization::DeclID ID, const Decl *D) override { |
| 116 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 117 | if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) { |
| 118 | unsigned DiagID |
| 119 | = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, |
| 120 | "%0 was deserialized"); |
| 121 | Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID) |
| 122 | << ND->getNameAsString(); |
| 123 | } |
| 124 | |
| 125 | DelegatingDeserializationListener::DeclRead(ID, D); |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | } |
| 130 | |
| 131 | FrontendAction::FrontendAction() : Instance(nullptr) {} |
| 132 | |
| 133 | FrontendAction::~FrontendAction() {} |
| 134 | |
| 135 | void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput, |
| 136 | std::unique_ptr<ASTUnit> AST) { |
| 137 | this->CurrentInput = CurrentInput; |
| 138 | CurrentASTUnit = std::move(AST); |
| 139 | } |
| 140 | |
| 141 | Module *FrontendAction::getCurrentModule() const { |
| 142 | CompilerInstance &CI = getCompilerInstance(); |
| 143 | return CI.getPreprocessor().getHeaderSearchInfo().lookupModule( |
| 144 | CI.getLangOpts().CurrentModule, ); |
| 145 | } |
| 146 | |
| 147 | std::unique_ptr<ASTConsumer> |
| 148 | FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI, |
| 149 | StringRef InFile) { |
| 150 | std::unique_ptr<ASTConsumer> Consumer = CreateASTConsumer(CI, InFile); |
| 151 | if (!Consumer) |
| 152 | return nullptr; |
| 153 | |
| 154 | |
| 155 | bool FoundAllPlugins = true; |
| 156 | for (const std::string &Arg : CI.getFrontendOpts().AddPluginActions) { |
| 157 | bool Found = false; |
| 158 | for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(), |
| 159 | ie = FrontendPluginRegistry::end(); |
| 160 | it != ie; ++it) { |
| 161 | if (it->getName() == Arg) |
| 162 | Found = true; |
| 163 | } |
| 164 | if (!Found) { |
| 165 | CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) << Arg; |
| 166 | FoundAllPlugins = false; |
| 167 | } |
| 168 | } |
| 169 | if (!FoundAllPlugins) |
| 170 | return nullptr; |
| 171 | |
| 172 | |
| 173 | if (FrontendPluginRegistry::begin() == FrontendPluginRegistry::end()) |
| 174 | return Consumer; |
| 175 | |
| 176 | |
| 177 | if (CI.hasCodeCompletionConsumer()) |
| 178 | return Consumer; |
| 179 | |
| 180 | |
| 181 | |
| 182 | std::vector<std::unique_ptr<ASTConsumer>> Consumers; |
| 183 | std::vector<std::unique_ptr<ASTConsumer>> AfterConsumers; |
| 184 | for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(), |
| 185 | ie = FrontendPluginRegistry::end(); |
| 186 | it != ie; ++it) { |
| 187 | std::unique_ptr<PluginASTAction> P = it->instantiate(); |
| 188 | PluginASTAction::ActionType ActionType = P->getActionType(); |
| 189 | if (ActionType == PluginASTAction::Cmdline) { |
| 190 | |
| 191 | |
| 192 | for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size(); |
| 193 | i != e; ++i) { |
| 194 | if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) { |
| 195 | ActionType = PluginASTAction::AddAfterMainAction; |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | if ((ActionType == PluginASTAction::AddBeforeMainAction || |
| 201 | ActionType == PluginASTAction::AddAfterMainAction) && |
| 202 | P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName()])) { |
| 203 | std::unique_ptr<ASTConsumer> PluginConsumer = P->CreateASTConsumer(CI, InFile); |
| 204 | if (ActionType == PluginASTAction::AddBeforeMainAction) { |
| 205 | Consumers.push_back(std::move(PluginConsumer)); |
| 206 | } else { |
| 207 | AfterConsumers.push_back(std::move(PluginConsumer)); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | |
| 213 | Consumers.push_back(std::move(Consumer)); |
| 214 | for (auto &C : AfterConsumers) { |
| 215 | Consumers.push_back(std::move(C)); |
| 216 | } |
| 217 | |
| 218 | return llvm::make_unique<MultiplexConsumer>(std::move(Consumers)); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | static SourceLocation ReadOriginalFileName(CompilerInstance &CI, |
| 231 | std::string &InputFile, |
| 232 | bool IsModuleMap = false) { |
| 233 | auto &SourceMgr = CI.getSourceManager(); |
| 234 | auto MainFileID = SourceMgr.getMainFileID(); |
| 235 | |
| 236 | bool Invalid = false; |
| 237 | const auto *MainFileBuf = SourceMgr.getBuffer(MainFileID, &Invalid); |
| 238 | if (Invalid) |
| 239 | return SourceLocation(); |
| 240 | |
| 241 | std::unique_ptr<Lexer> RawLexer( |
| 242 | new Lexer(MainFileID, MainFileBuf, SourceMgr, CI.getLangOpts())); |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | Token T; |
| 250 | if (RawLexer->LexFromRawLexer(T) || T.getKind() != tok::hash) |
| 251 | return SourceLocation(); |
| 252 | if (RawLexer->LexFromRawLexer(T) || T.isAtStartOfLine() || |
| 253 | T.getKind() != tok::numeric_constant) |
| 254 | return SourceLocation(); |
| 255 | |
| 256 | unsigned LineNo; |
| 257 | SourceLocation LineNoLoc = T.getLocation(); |
| 258 | if (IsModuleMap) { |
| 259 | llvm::SmallString<16> Buffer; |
| 260 | if (Lexer::getSpelling(LineNoLoc, Buffer, SourceMgr, CI.getLangOpts()) |
| 261 | .getAsInteger(10, LineNo)) |
| 262 | return SourceLocation(); |
| 263 | } |
| 264 | |
| 265 | RawLexer->LexFromRawLexer(T); |
| 266 | if (T.isAtStartOfLine() || T.getKind() != tok::string_literal) |
| 267 | return SourceLocation(); |
| 268 | |
| 269 | StringLiteralParser Literal(T, CI.getPreprocessor()); |
| 270 | if (Literal.hadError) |
| 271 | return SourceLocation(); |
| 272 | RawLexer->LexFromRawLexer(T); |
| 273 | if (T.isNot(tok::eof) && !T.isAtStartOfLine()) |
| 274 | return SourceLocation(); |
| 275 | InputFile = Literal.GetString().str(); |
| 276 | |
| 277 | if (IsModuleMap) |
| 278 | CI.getSourceManager().AddLineNote( |
| 279 | LineNoLoc, LineNo, SourceMgr.getLineTableFilenameID(InputFile), false, |
| 280 | false, SrcMgr::C_User_ModuleMap); |
| 281 | |
| 282 | return T.getLocation(); |
| 283 | } |
| 284 | |
| 285 | static SmallVectorImpl<char> & |
| 286 | operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) { |
| 287 | Includes.append(RHS.begin(), RHS.end()); |
| 288 | return Includes; |
| 289 | } |
| 290 | |
| 291 | static void addHeaderInclude(StringRef HeaderName, |
| 292 | SmallVectorImpl<char> &Includes, |
| 293 | const LangOptions &LangOpts, |
| 294 | bool IsExternC) { |
| 295 | if (IsExternC && LangOpts.CPlusPlus) |
| 296 | Includes += "extern \"C\" {\n"; |
| 297 | if (LangOpts.ObjC) |
| 298 | Includes += "#import \""; |
| 299 | else |
| 300 | Includes += "#include \""; |
| 301 | |
| 302 | Includes += HeaderName; |
| 303 | |
| 304 | Includes += "\"\n"; |
| 305 | if (IsExternC && LangOpts.CPlusPlus) |
| 306 | Includes += "}\n"; |
| 307 | } |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | static std::error_code collectModuleHeaderIncludes( |
| 317 | const LangOptions &LangOpts, FileManager &FileMgr, DiagnosticsEngine &Diag, |
| 318 | ModuleMap &ModMap, clang::Module *Module, SmallVectorImpl<char> &Includes) { |
| 319 | |
| 320 | if (!Module->isAvailable()) |
| 321 | return std::error_code(); |
| 322 | |
| 323 | |
| 324 | ModMap.resolveHeaderDirectives(Module); |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | if (!Module->MissingHeaders.empty()) { |
| 332 | auto &MissingHeader = Module->MissingHeaders.front(); |
| 333 | Diag.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing) |
| 334 | << MissingHeader.IsUmbrella << MissingHeader.FileName; |
| 335 | return std::error_code(); |
| 336 | } |
| 337 | |
| 338 | |
| 339 | for (auto HK : {Module::HK_Normal, Module::HK_Private}) { |
| 340 | for (Module::Header &H : Module->Headers[HK]) { |
| 341 | Module->addTopHeader(H.Entry); |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | addHeaderInclude(H.NameAsWritten, Includes, LangOpts, Module->IsExternC); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | |
| 351 | if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader()) { |
| 352 | Module->addTopHeader(UmbrellaHeader.Entry); |
| 353 | if (Module->Parent) |
| 354 | |
| 355 | addHeaderInclude(UmbrellaHeader.NameAsWritten, Includes, LangOpts, |
| 356 | Module->IsExternC); |
| 357 | } else if (Module::DirectoryName UmbrellaDir = Module->getUmbrellaDir()) { |
| 358 | |
| 359 | std::error_code EC; |
| 360 | SmallString<128> DirNative; |
| 361 | llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative); |
| 362 | |
| 363 | llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem(); |
| 364 | for (llvm::vfs::recursive_directory_iterator Dir(FS, DirNative, EC), End; |
| 365 | Dir != End && !EC; Dir.increment(EC)) { |
| 366 | |
| 367 | |
| 368 | if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path())) |
| 369 | .Cases(".h", ".H", ".hh", ".hpp", true) |
| 370 | .Default(false)) |
| 371 | continue; |
| 372 | |
| 373 | const FileEntry *Header = FileMgr.getFile(Dir->path()); |
| 374 | |
| 375 | |
| 376 | if (!Header) |
| 377 | continue; |
| 378 | |
| 379 | |
| 380 | |
| 381 | if (ModMap.isHeaderUnavailableInModule(Header, Module)) |
| 382 | continue; |
| 383 | |
| 384 | |
| 385 | SmallVector<StringRef, 16> Components; |
| 386 | auto PathIt = llvm::sys::path::rbegin(Dir->path()); |
| 387 | for (int I = 0; I != Dir.level() + 1; ++I, ++PathIt) |
| 388 | Components.push_back(*PathIt); |
| 389 | SmallString<128> RelativeHeader(UmbrellaDir.NameAsWritten); |
| 390 | for (auto It = Components.rbegin(), End = Components.rend(); It != End; |
| 391 | ++It) |
| 392 | llvm::sys::path::append(RelativeHeader, *It); |
| 393 | |
| 394 | |
| 395 | Module->addTopHeader(Header); |
| 396 | addHeaderInclude(RelativeHeader, Includes, LangOpts, Module->IsExternC); |
| 397 | } |
| 398 | |
| 399 | if (EC) |
| 400 | return EC; |
| 401 | } |
| 402 | |
| 403 | |
| 404 | for (clang::Module::submodule_iterator Sub = Module->submodule_begin(), |
| 405 | SubEnd = Module->submodule_end(); |
| 406 | Sub != SubEnd; ++Sub) |
| 407 | if (std::error_code Err = collectModuleHeaderIncludes( |
| 408 | LangOpts, FileMgr, Diag, ModMap, *Sub, Includes)) |
| 409 | return Err; |
| 410 | |
| 411 | return std::error_code(); |
| 412 | } |
| 413 | |
| 414 | static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem, |
| 415 | bool IsPreprocessed, |
| 416 | std::string &PresumedModuleMapFile, |
| 417 | unsigned &Offset) { |
| 418 | auto &SrcMgr = CI.getSourceManager(); |
| 419 | HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); |
| 420 | |
| 421 | |
| 422 | FileID ModuleMapID = SrcMgr.getMainFileID(); |
| 423 | const FileEntry *ModuleMap = SrcMgr.getFileEntryForID(ModuleMapID); |
| 424 | |
| 425 | |
| 426 | |
| 427 | Offset = 0; |
| 428 | if (IsPreprocessed) { |
| 429 | SourceLocation EndOfLineMarker = |
| 430 | ReadOriginalFileName(CI, PresumedModuleMapFile, true); |
| 431 | if (EndOfLineMarker.isValid()) |
| 432 | Offset = CI.getSourceManager().getDecomposedLoc(EndOfLineMarker).second; |
| 433 | } |
| 434 | |
| 435 | |
| 436 | if (HS.loadModuleMapFile(ModuleMap, IsSystem, ModuleMapID, &Offset, |
| 437 | PresumedModuleMapFile)) |
| 438 | return true; |
| 439 | |
| 440 | if (SrcMgr.getBuffer(ModuleMapID)->getBufferSize() == Offset) |
| 441 | Offset = 0; |
| 442 | |
| 443 | return false; |
| 444 | } |
| 445 | |
| 446 | static Module *prepareToBuildModule(CompilerInstance &CI, |
| 447 | StringRef ModuleMapFilename) { |
| 448 | if (CI.getLangOpts().CurrentModule.empty()) { |
| 449 | CI.getDiagnostics().Report(diag::err_missing_module_name); |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | return nullptr; |
| 456 | } |
| 457 | |
| 458 | |
| 459 | HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); |
| 460 | Module *M = HS.lookupModule(CI.getLangOpts().CurrentModule, |
| 461 | ); |
| 462 | if (!M) { |
| 463 | CI.getDiagnostics().Report(diag::err_missing_module) |
| 464 | << CI.getLangOpts().CurrentModule << ModuleMapFilename; |
| 465 | |
| 466 | return nullptr; |
| 467 | } |
| 468 | |
| 469 | |
| 470 | if (Preprocessor::checkModuleIsAvailable(CI.getLangOpts(), CI.getTarget(), |
| 471 | CI.getDiagnostics(), M)) |
| 472 | return nullptr; |
| 473 | |
| 474 | |
| 475 | |
| 476 | CI.getPreprocessor().setMainFileDir(M->Directory); |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | StringRef OriginalModuleMapName = CI.getFrontendOpts().OriginalModuleMap; |
| 483 | if (!OriginalModuleMapName.empty()) { |
| 484 | auto *OriginalModuleMap = |
| 485 | CI.getFileManager().getFile(OriginalModuleMapName, |
| 486 | true); |
| 487 | if (!OriginalModuleMap) { |
| 488 | CI.getDiagnostics().Report(diag::err_module_map_not_found) |
| 489 | << OriginalModuleMapName; |
| 490 | return nullptr; |
| 491 | } |
| 492 | if (OriginalModuleMap != CI.getSourceManager().getFileEntryForID( |
| 493 | CI.getSourceManager().getMainFileID())) { |
| 494 | M->IsInferred = true; |
| 495 | CI.getPreprocessor().getHeaderSearchInfo().getModuleMap() |
| 496 | .setInferredModuleAllowedBy(M, OriginalModuleMap); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | SourceManager &SourceMgr = CI.getSourceManager(); |
| 504 | if (SourceMgr.getModuleBuildStack().empty()) |
| 505 | SourceMgr.pushModuleBuildStack(CI.getLangOpts().CurrentModule, |
| 506 | FullSourceLoc(SourceLocation(), SourceMgr)); |
| 507 | return M; |
| 508 | } |
| 509 | |
| 510 | |
| 511 | static std::unique_ptr<llvm::MemoryBuffer> |
| 512 | getInputBufferForModule(CompilerInstance &CI, Module *M) { |
| 513 | FileManager &FileMgr = CI.getFileManager(); |
| 514 | |
| 515 | |
| 516 | SmallString<256> HeaderContents; |
| 517 | std::error_code Err = std::error_code(); |
| 518 | if (Module::Header UmbrellaHeader = M->getUmbrellaHeader()) |
| 519 | addHeaderInclude(UmbrellaHeader.NameAsWritten, HeaderContents, |
| 520 | CI.getLangOpts(), M->IsExternC); |
| 521 | Err = collectModuleHeaderIncludes( |
| 522 | CI.getLangOpts(), FileMgr, CI.getDiagnostics(), |
| 523 | CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), M, |
| 524 | HeaderContents); |
| 525 | |
| 526 | if (Err) { |
| 527 | CI.getDiagnostics().Report(diag::err_module_cannot_create_includes) |
| 528 | << M->getFullModuleName() << Err.message(); |
| 529 | return nullptr; |
| 530 | } |
| 531 | |
| 532 | return llvm::MemoryBuffer::getMemBufferCopy( |
| 533 | HeaderContents, Module::getModuleInputBufferName()); |
| 534 | } |
| 535 | |
| 536 | bool FrontendAction::BeginSourceFile(CompilerInstance &CI, |
| 537 | const FrontendInputFile &RealInput) { |
| 538 | FrontendInputFile Input(RealInput); |
| 539 | (0) . __assert_fail ("!Instance && \"Already processing a source file!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/FrontendAction.cpp", 539, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!Instance && "Already processing a source file!"); |
| 540 | (0) . __assert_fail ("!Input.isEmpty() && \"Unexpected empty filename!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/FrontendAction.cpp", 540, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!Input.isEmpty() && "Unexpected empty filename!"); |
| 541 | setCurrentInput(Input); |
| 542 | setCompilerInstance(&CI); |
| 543 | |
| 544 | bool HasBegunSourceFile = false; |
| 545 | bool ReplayASTFile = Input.getKind().getFormat() == InputKind::Precompiled && |
| 546 | usesPreprocessorOnly(); |
| 547 | if (!BeginInvocation(CI)) |
| 548 | goto failure; |
| 549 | |
| 550 | |
| 551 | |
| 552 | if (ReplayASTFile) { |
| 553 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics()); |
| 554 | |
| 555 | |
| 556 | IntrusiveRefCntPtr<DiagnosticsEngine> ASTDiags( |
| 557 | new DiagnosticsEngine(Diags->getDiagnosticIDs(), |
| 558 | &Diags->getDiagnosticOptions())); |
| 559 | ASTDiags->setClient(Diags->getClient(), ); |
| 560 | |
| 561 | |
| 562 | StringRef InputFile = Input.getFile(); |
| 563 | |
| 564 | std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile( |
| 565 | InputFile, CI.getPCHContainerReader(), ASTUnit::LoadPreprocessorOnly, |
| 566 | ASTDiags, CI.getFileSystemOpts(), CI.getCodeGenOpts().DebugTypeExtRefs); |
| 567 | if (!AST) |
| 568 | goto failure; |
| 569 | |
| 570 | |
| 571 | |
| 572 | CI.getHeaderSearchOpts() = AST->getHeaderSearchOpts(); |
| 573 | CI.getPreprocessorOpts() = AST->getPreprocessorOpts(); |
| 574 | CI.getLangOpts() = AST->getLangOpts(); |
| 575 | |
| 576 | |
| 577 | |
| 578 | CI.setFileManager(&AST->getFileManager()); |
| 579 | CI.createSourceManager(CI.getFileManager()); |
| 580 | CI.getSourceManager().initializeForReplay(AST->getSourceManager()); |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | if (auto ASTReader = AST->getASTReader()) { |
| 586 | auto &MM = ASTReader->getModuleManager(); |
| 587 | auto &PrimaryModule = MM.getPrimaryModule(); |
| 588 | |
| 589 | for (serialization::ModuleFile &MF : MM) |
| 590 | if (&MF != &PrimaryModule) |
| 591 | CI.getFrontendOpts().ModuleFiles.push_back(MF.FileName); |
| 592 | |
| 593 | ASTReader->visitTopLevelModuleMaps(PrimaryModule, |
| 594 | [&](const FileEntry *FE) { |
| 595 | CI.getFrontendOpts().ModuleMapFiles.push_back(FE->getName()); |
| 596 | }); |
| 597 | } |
| 598 | |
| 599 | |
| 600 | auto Kind = AST->getInputKind(); |
| 601 | if (Kind.getFormat() == InputKind::ModuleMap) { |
| 602 | Module *ASTModule = |
| 603 | AST->getPreprocessor().getHeaderSearchInfo().lookupModule( |
| 604 | AST->getLangOpts().CurrentModule, false); |
| 605 | (0) . __assert_fail ("ASTModule && \"module file does not define its own module\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/FrontendAction.cpp", 605, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ASTModule && "module file does not define its own module"); |
| 606 | Input = FrontendInputFile(ASTModule->PresumedModuleMapFile, Kind); |
| 607 | } else { |
| 608 | auto &OldSM = AST->getSourceManager(); |
| 609 | FileID ID = OldSM.getMainFileID(); |
| 610 | if (auto *File = OldSM.getFileEntryForID(ID)) |
| 611 | Input = FrontendInputFile(File->getName(), Kind); |
| 612 | else |
| 613 | Input = FrontendInputFile(OldSM.getBuffer(ID), Kind); |
| 614 | } |
| 615 | setCurrentInput(Input, std::move(AST)); |
| 616 | } |
| 617 | |
| 618 | |
| 619 | |
| 620 | if (Input.getKind().getFormat() == InputKind::Precompiled) { |
| 621 | (0) . __assert_fail ("!usesPreprocessorOnly() && \"this case was handled above\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/FrontendAction.cpp", 621, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!usesPreprocessorOnly() && "this case was handled above"); |
| 622 | (0) . __assert_fail ("hasASTFileSupport() && \"This action does not have AST file support!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/FrontendAction.cpp", 623, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(hasASTFileSupport() && |
| 623 | (0) . __assert_fail ("hasASTFileSupport() && \"This action does not have AST file support!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/FrontendAction.cpp", 623, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "This action does not have AST file support!"); |
| 624 | |
| 625 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics()); |
| 626 | |
| 627 | |
| 628 | StringRef InputFile = Input.getFile(); |
| 629 | |
| 630 | std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile( |
| 631 | InputFile, CI.getPCHContainerReader(), ASTUnit::LoadEverything, Diags, |
| 632 | CI.getFileSystemOpts(), CI.getCodeGenOpts().DebugTypeExtRefs); |
| 633 | |
| 634 | if (!AST) |
| 635 | goto failure; |
| 636 | |
| 637 | |
| 638 | CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr); |
| 639 | HasBegunSourceFile = true; |
| 640 | |
| 641 | |
| 642 | |
| 643 | CI.setFileManager(&AST->getFileManager()); |
| 644 | CI.setSourceManager(&AST->getSourceManager()); |
| 645 | CI.setPreprocessor(AST->getPreprocessorPtr()); |
| 646 | Preprocessor &PP = CI.getPreprocessor(); |
| 647 | PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(), |
| 648 | PP.getLangOpts()); |
| 649 | CI.setASTContext(&AST->getASTContext()); |
| 650 | |
| 651 | setCurrentInput(Input, std::move(AST)); |
| 652 | |
| 653 | |
| 654 | if (!BeginSourceFileAction(CI)) |
| 655 | goto failure; |
| 656 | |
| 657 | |
| 658 | CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile)); |
| 659 | if (!CI.hasASTConsumer()) |
| 660 | goto failure; |
| 661 | |
| 662 | return true; |
| 663 | } |
| 664 | |
| 665 | |
| 666 | if (!CI.hasFileManager()) { |
| 667 | if (!CI.createFileManager()) { |
| 668 | goto failure; |
| 669 | } |
| 670 | } |
| 671 | if (!CI.hasSourceManager()) |
| 672 | CI.createSourceManager(CI.getFileManager()); |
| 673 | |
| 674 | |
| 675 | |
| 676 | for (const auto &F : CI.getFrontendOpts().ModulesEmbedFiles) { |
| 677 | if (const auto *FE = CI.getFileManager().getFile(F, )) |
| 678 | CI.getSourceManager().setFileIsTransient(FE); |
| 679 | else |
| 680 | CI.getDiagnostics().Report(diag::err_modules_embed_file_not_found) << F; |
| 681 | } |
| 682 | if (CI.getFrontendOpts().ModulesEmbedAllFiles) |
| 683 | CI.getSourceManager().setAllFilesAreTransient(true); |
| 684 | |
| 685 | |
| 686 | if (Input.getKind().getLanguage() == InputKind::LLVM_IR) { |
| 687 | (0) . __assert_fail ("hasIRSupport() && \"This action does not have IR file support!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/FrontendAction.cpp", 688, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(hasIRSupport() && |
| 688 | (0) . __assert_fail ("hasIRSupport() && \"This action does not have IR file support!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/FrontendAction.cpp", 688, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "This action does not have IR file support!"); |
| 689 | |
| 690 | |
| 691 | CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr); |
| 692 | HasBegunSourceFile = true; |
| 693 | |
| 694 | |
| 695 | if (!BeginSourceFileAction(CI)) |
| 696 | goto failure; |
| 697 | |
| 698 | |
| 699 | if (!CI.InitializeSourceManager(CurrentInput)) |
| 700 | goto failure; |
| 701 | |
| 702 | return true; |
| 703 | } |
| 704 | |
| 705 | |
| 706 | |
| 707 | if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) { |
| 708 | FileManager &FileMgr = CI.getFileManager(); |
| 709 | PreprocessorOptions &PPOpts = CI.getPreprocessorOpts(); |
| 710 | StringRef PCHInclude = PPOpts.ImplicitPCHInclude; |
| 711 | std::string SpecificModuleCachePath = CI.getSpecificModuleCachePath(); |
| 712 | if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) { |
| 713 | std::error_code EC; |
| 714 | SmallString<128> DirNative; |
| 715 | llvm::sys::path::native(PCHDir->getName(), DirNative); |
| 716 | bool Found = false; |
| 717 | llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem(); |
| 718 | for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), |
| 719 | DirEnd; |
| 720 | Dir != DirEnd && !EC; Dir.increment(EC)) { |
| 721 | |
| 722 | if (ASTReader::isAcceptableASTFile( |
| 723 | Dir->path(), FileMgr, CI.getPCHContainerReader(), |
| 724 | CI.getLangOpts(), CI.getTargetOpts(), CI.getPreprocessorOpts(), |
| 725 | SpecificModuleCachePath)) { |
| 726 | PPOpts.ImplicitPCHInclude = Dir->path(); |
| 727 | Found = true; |
| 728 | break; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | if (!Found) { |
| 733 | CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude; |
| 734 | goto failure; |
| 735 | } |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | |
| 740 | |
| 741 | if (!isModelParsingAction()) |
| 742 | CI.createPreprocessor(getTranslationUnitKind()); |
| 743 | |
| 744 | |
| 745 | CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), |
| 746 | &CI.getPreprocessor()); |
| 747 | HasBegunSourceFile = true; |
| 748 | |
| 749 | |
| 750 | if (!CI.InitializeSourceManager(Input)) |
| 751 | goto failure; |
| 752 | |
| 753 | |
| 754 | |
| 755 | if (Input.getKind().getFormat() == InputKind::ModuleMap) { |
| 756 | CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleMap); |
| 757 | |
| 758 | std::string PresumedModuleMapFile; |
| 759 | unsigned OffsetToContents; |
| 760 | if (loadModuleMapForModuleBuild(CI, Input.isSystem(), |
| 761 | Input.isPreprocessed(), |
| 762 | PresumedModuleMapFile, OffsetToContents)) |
| 763 | goto failure; |
| 764 | |
| 765 | auto *CurrentModule = prepareToBuildModule(CI, Input.getFile()); |
| 766 | if (!CurrentModule) |
| 767 | goto failure; |
| 768 | |
| 769 | CurrentModule->PresumedModuleMapFile = PresumedModuleMapFile; |
| 770 | |
| 771 | if (OffsetToContents) |
| 772 | |
| 773 | CI.getPreprocessor().setSkipMainFilePreamble(OffsetToContents, true); |
| 774 | else { |
| 775 | |
| 776 | auto Buffer = getInputBufferForModule(CI, CurrentModule); |
| 777 | if (!Buffer) |
| 778 | goto failure; |
| 779 | |
| 780 | |
| 781 | auto Kind = CurrentModule->IsSystem ? SrcMgr::C_System : SrcMgr::C_User; |
| 782 | auto &SourceMgr = CI.getSourceManager(); |
| 783 | auto BufferID = SourceMgr.createFileID(std::move(Buffer), Kind); |
| 784 | (0) . __assert_fail ("BufferID.isValid() && \"couldn't creaate module buffer ID\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/FrontendAction.cpp", 784, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(BufferID.isValid() && "couldn't creaate module buffer ID"); |
| 785 | SourceMgr.setMainFileID(BufferID); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | |
| 790 | if (!BeginSourceFileAction(CI)) |
| 791 | goto failure; |
| 792 | |
| 793 | |
| 794 | for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) { |
| 795 | if (auto *File = CI.getFileManager().getFile(Filename)) |
| 796 | CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile( |
| 797 | File, ); |
| 798 | else |
| 799 | CI.getDiagnostics().Report(diag::err_module_map_not_found) << Filename; |
| 800 | } |
| 801 | |
| 802 | |
| 803 | |
| 804 | CI.getPreprocessor() |
| 805 | .getHeaderSearchInfo() |
| 806 | .getModuleMap() |
| 807 | .finishModuleDeclarationScope(); |
| 808 | |
| 809 | |
| 810 | |
| 811 | if (!usesPreprocessorOnly()) { |
| 812 | |
| 813 | if (!isModelParsingAction()) |
| 814 | CI.createASTContext(); |
| 815 | |
| 816 | |
| 817 | |
| 818 | std::string PresumedInputFile = getCurrentFileOrBufferName(); |
| 819 | if (Input.isPreprocessed()) |
| 820 | ReadOriginalFileName(CI, PresumedInputFile); |
| 821 | |
| 822 | std::unique_ptr<ASTConsumer> Consumer = |
| 823 | CreateWrappedASTConsumer(CI, PresumedInputFile); |
| 824 | if (!Consumer) |
| 825 | goto failure; |
| 826 | |
| 827 | |
| 828 | if (!isModelParsingAction()) |
| 829 | CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener()); |
| 830 | |
| 831 | if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) { |
| 832 | |
| 833 | IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader; |
| 834 | source = createChainedIncludesSource(CI, FinalReader); |
| 835 | if (!source) |
| 836 | goto failure; |
| 837 | CI.setModuleManager(static_cast<ASTReader *>(FinalReader.get())); |
| 838 | CI.getASTContext().setExternalSource(source); |
| 839 | } else if (CI.getLangOpts().Modules || |
| 840 | !CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) { |
| 841 | |
| 842 | (0) . __assert_fail ("hasPCHSupport() && \"This action does not have PCH support!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/FrontendAction.cpp", 842, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(hasPCHSupport() && "This action does not have PCH support!"); |
| 843 | ASTDeserializationListener *DeserialListener = |
| 844 | Consumer->GetASTDeserializationListener(); |
| 845 | bool DeleteDeserialListener = false; |
| 846 | if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) { |
| 847 | DeserialListener = new DeserializedDeclsDumper(DeserialListener, |
| 848 | DeleteDeserialListener); |
| 849 | DeleteDeserialListener = true; |
| 850 | } |
| 851 | if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) { |
| 852 | DeserialListener = new DeserializedDeclsChecker( |
| 853 | CI.getASTContext(), |
| 854 | CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn, |
| 855 | DeserialListener, DeleteDeserialListener); |
| 856 | DeleteDeserialListener = true; |
| 857 | } |
| 858 | if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) { |
| 859 | CI.createPCHExternalASTSource( |
| 860 | CI.getPreprocessorOpts().ImplicitPCHInclude, |
| 861 | CI.getPreprocessorOpts().DisablePCHValidation, |
| 862 | CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, DeserialListener, |
| 863 | DeleteDeserialListener); |
| 864 | if (!CI.getASTContext().getExternalSource()) |
| 865 | goto failure; |
| 866 | } |
| 867 | |
| 868 | |
| 869 | |
| 870 | if (CI.getLangOpts().Modules || !CI.hasASTContext() || |
| 871 | !CI.getASTContext().getExternalSource()) { |
| 872 | CI.createModuleManager(); |
| 873 | CI.getModuleManager()->setDeserializationListener(DeserialListener, |
| 874 | DeleteDeserialListener); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | CI.setASTConsumer(std::move(Consumer)); |
| 879 | if (!CI.hasASTConsumer()) |
| 880 | goto failure; |
| 881 | } |
| 882 | |
| 883 | |
| 884 | |
| 885 | if (CI.getLangOpts().Modules || !CI.hasASTContext() || |
| 886 | !CI.getASTContext().getExternalSource()) { |
| 887 | Preprocessor &PP = CI.getPreprocessor(); |
| 888 | PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(), |
| 889 | PP.getLangOpts()); |
| 890 | } else { |
| 891 | |
| 892 | |
| 893 | (0) . __assert_fail ("(!CI.getLangOpts().Modules || CI.getModuleManager()) && \"modules enabled but created an external source that \" \"doesn't support modules\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/FrontendAction.cpp", 895, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((!CI.getLangOpts().Modules || CI.getModuleManager()) && |
| 894 | (0) . __assert_fail ("(!CI.getLangOpts().Modules || CI.getModuleManager()) && \"modules enabled but created an external source that \" \"doesn't support modules\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/FrontendAction.cpp", 895, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "modules enabled but created an external source that " |
| 895 | (0) . __assert_fail ("(!CI.getLangOpts().Modules || CI.getModuleManager()) && \"modules enabled but created an external source that \" \"doesn't support modules\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/FrontendAction.cpp", 895, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "doesn't support modules"); |
| 896 | } |
| 897 | |
| 898 | |
| 899 | for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles) |
| 900 | if (!CI.loadModuleFile(ModuleFile)) |
| 901 | goto failure; |
| 902 | |
| 903 | |
| 904 | |
| 905 | if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() && |
| 906 | CI.hasASTContext() && !CI.getASTContext().getExternalSource()) { |
| 907 | IntrusiveRefCntPtr<ExternalASTSource> |
| 908 | Override(new LayoutOverrideSource( |
| 909 | CI.getFrontendOpts().OverrideRecordLayoutsFile)); |
| 910 | CI.getASTContext().setExternalSource(Override); |
| 911 | } |
| 912 | |
| 913 | return true; |
| 914 | |
| 915 | |
| 916 | |
| 917 | failure: |
| 918 | if (HasBegunSourceFile) |
| 919 | CI.getDiagnosticClient().EndSourceFile(); |
| 920 | CI.clearOutputFiles(); |
| 921 | CI.getLangOpts().setCompilingModule(LangOptions::CMK_None); |
| 922 | setCurrentInput(FrontendInputFile()); |
| 923 | setCompilerInstance(nullptr); |
| 924 | return false; |
| 925 | } |
| 926 | |
| 927 | bool FrontendAction::Execute() { |
| 928 | CompilerInstance &CI = getCompilerInstance(); |
| 929 | |
| 930 | if (CI.hasFrontendTimer()) { |
| 931 | llvm::TimeRegion Timer(CI.getFrontendTimer()); |
| 932 | ExecuteAction(); |
| 933 | } |
| 934 | else ExecuteAction(); |
| 935 | |
| 936 | |
| 937 | |
| 938 | if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() && |
| 939 | CI.hasPreprocessor()) { |
| 940 | StringRef Cache = |
| 941 | CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); |
| 942 | if (!Cache.empty()) |
| 943 | GlobalModuleIndex::writeIndex(CI.getFileManager(), |
| 944 | CI.getPCHContainerReader(), Cache); |
| 945 | } |
| 946 | |
| 947 | return true; |
| 948 | } |
| 949 | |
| 950 | void FrontendAction::EndSourceFile() { |
| 951 | CompilerInstance &CI = getCompilerInstance(); |
| 952 | |
| 953 | |
| 954 | CI.getDiagnosticClient().EndSourceFile(); |
| 955 | |
| 956 | |
| 957 | if (CI.hasPreprocessor()) |
| 958 | CI.getPreprocessor().EndSourceFile(); |
| 959 | |
| 960 | |
| 961 | EndSourceFileAction(); |
| 962 | |
| 963 | |
| 964 | |
| 965 | |
| 966 | bool DisableFree = CI.getFrontendOpts().DisableFree; |
| 967 | if (DisableFree) { |
| 968 | CI.resetAndLeakSema(); |
| 969 | CI.resetAndLeakASTContext(); |
| 970 | llvm::BuryPointer(CI.takeASTConsumer().get()); |
| 971 | } else { |
| 972 | CI.setSema(nullptr); |
| 973 | CI.setASTContext(nullptr); |
| 974 | CI.setASTConsumer(nullptr); |
| 975 | } |
| 976 | |
| 977 | if (CI.getFrontendOpts().ShowStats) { |
| 978 | llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n"; |
| 979 | CI.getPreprocessor().PrintStats(); |
| 980 | CI.getPreprocessor().getIdentifierTable().PrintStats(); |
| 981 | CI.getPreprocessor().getHeaderSearchInfo().PrintStats(); |
| 982 | CI.getSourceManager().PrintStats(); |
| 983 | llvm::errs() << "\n"; |
| 984 | } |
| 985 | |
| 986 | |
| 987 | |
| 988 | CI.clearOutputFiles(shouldEraseOutputFiles()); |
| 989 | |
| 990 | if (isCurrentFileAST()) { |
| 991 | if (DisableFree) { |
| 992 | CI.resetAndLeakPreprocessor(); |
| 993 | CI.resetAndLeakSourceManager(); |
| 994 | CI.resetAndLeakFileManager(); |
| 995 | llvm::BuryPointer(std::move(CurrentASTUnit)); |
| 996 | } else { |
| 997 | CI.setPreprocessor(nullptr); |
| 998 | CI.setSourceManager(nullptr); |
| 999 | CI.setFileManager(nullptr); |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | setCompilerInstance(nullptr); |
| 1004 | setCurrentInput(FrontendInputFile()); |
| 1005 | CI.getLangOpts().setCompilingModule(LangOptions::CMK_None); |
| 1006 | } |
| 1007 | |
| 1008 | bool FrontendAction::shouldEraseOutputFiles() { |
| 1009 | return getCompilerInstance().getDiagnostics().hasErrorOccurred(); |
| 1010 | } |
| 1011 | |
| 1012 | |
| 1013 | |
| 1014 | |
| 1015 | |
| 1016 | void ASTFrontendAction::ExecuteAction() { |
| 1017 | CompilerInstance &CI = getCompilerInstance(); |
| 1018 | if (!CI.hasPreprocessor()) |
| 1019 | return; |
| 1020 | |
| 1021 | |
| 1022 | |
| 1023 | if (hasCodeCompletionSupport() && |
| 1024 | !CI.getFrontendOpts().CodeCompletionAt.FileName.empty()) |
| 1025 | CI.createCodeCompletionConsumer(); |
| 1026 | |
| 1027 | |
| 1028 | CodeCompleteConsumer *CompletionConsumer = nullptr; |
| 1029 | if (CI.hasCodeCompletionConsumer()) |
| 1030 | CompletionConsumer = &CI.getCodeCompletionConsumer(); |
| 1031 | |
| 1032 | if (!CI.hasSema()) |
| 1033 | CI.createSema(getTranslationUnitKind(), CompletionConsumer); |
| 1034 | |
| 1035 | ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats, |
| 1036 | CI.getFrontendOpts().SkipFunctionBodies); |
| 1037 | } |
| 1038 | |
| 1039 | void PluginASTAction::anchor() { } |
| 1040 | |
| 1041 | std::unique_ptr<ASTConsumer> |
| 1042 | PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI, |
| 1043 | StringRef InFile) { |
| 1044 | llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!"); |
| 1045 | } |
| 1046 | |
| 1047 | bool WrapperFrontendAction::PrepareToExecuteAction(CompilerInstance &CI) { |
| 1048 | return WrappedAction->PrepareToExecuteAction(CI); |
| 1049 | } |
| 1050 | std::unique_ptr<ASTConsumer> |
| 1051 | WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI, |
| 1052 | StringRef InFile) { |
| 1053 | return WrappedAction->CreateASTConsumer(CI, InFile); |
| 1054 | } |
| 1055 | bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) { |
| 1056 | return WrappedAction->BeginInvocation(CI); |
| 1057 | } |
| 1058 | bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI) { |
| 1059 | WrappedAction->setCurrentInput(getCurrentInput()); |
| 1060 | WrappedAction->setCompilerInstance(&CI); |
| 1061 | auto Ret = WrappedAction->BeginSourceFileAction(CI); |
| 1062 | |
| 1063 | setCurrentInput(WrappedAction->getCurrentInput()); |
| 1064 | return Ret; |
| 1065 | } |
| 1066 | void WrapperFrontendAction::ExecuteAction() { |
| 1067 | WrappedAction->ExecuteAction(); |
| 1068 | } |
| 1069 | void WrapperFrontendAction::EndSourceFileAction() { |
| 1070 | WrappedAction->EndSourceFileAction(); |
| 1071 | } |
| 1072 | |
| 1073 | bool WrapperFrontendAction::usesPreprocessorOnly() const { |
| 1074 | return WrappedAction->usesPreprocessorOnly(); |
| 1075 | } |
| 1076 | TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() { |
| 1077 | return WrappedAction->getTranslationUnitKind(); |
| 1078 | } |
| 1079 | bool WrapperFrontendAction::hasPCHSupport() const { |
| 1080 | return WrappedAction->hasPCHSupport(); |
| 1081 | } |
| 1082 | bool WrapperFrontendAction::hasASTFileSupport() const { |
| 1083 | return WrappedAction->hasASTFileSupport(); |
| 1084 | } |
| 1085 | bool WrapperFrontendAction::hasIRSupport() const { |
| 1086 | return WrappedAction->hasIRSupport(); |
| 1087 | } |
| 1088 | bool WrapperFrontendAction::hasCodeCompletionSupport() const { |
| 1089 | return WrappedAction->hasCodeCompletionSupport(); |
| 1090 | } |
| 1091 | |
| 1092 | WrapperFrontendAction::WrapperFrontendAction( |
| 1093 | std::unique_ptr<FrontendAction> WrappedAction) |
| 1094 | : WrappedAction(std::move(WrappedAction)) {} |
| 1095 | |
| 1096 | |