1 //===--- FrontendActions.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/FrontendActions.h" 11 #include "clang/AST/ASTConsumer.h" 12 #include "clang/Basic/FileManager.h" 13 #include "clang/Frontend/ASTConsumers.h" 14 #include "clang/Frontend/ASTUnit.h" 15 #include "clang/Frontend/CompilerInstance.h" 16 #include "clang/Frontend/FrontendDiagnostic.h" 17 #include "clang/Frontend/Utils.h" 18 #include "clang/Lex/HeaderSearch.h" 19 #include "clang/Lex/Pragma.h" 20 #include "clang/Lex/Preprocessor.h" 21 #include "clang/Parse/Parser.h" 22 #include "clang/Serialization/ASTReader.h" 23 #include "clang/Serialization/ASTWriter.h" 24 #include "llvm/Support/FileSystem.h" 25 #include "llvm/Support/MemoryBuffer.h" 26 #include "llvm/Support/raw_ostream.h" 27 #include <memory> 28 #include <system_error> 29 30 using namespace clang; 31 32 //===----------------------------------------------------------------------===// 33 // Custom Actions 34 //===----------------------------------------------------------------------===// 35 36 std::unique_ptr<ASTConsumer> 37 InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 38 return llvm::make_unique<ASTConsumer>(); 39 } 40 41 void InitOnlyAction::ExecuteAction() { 42 } 43 44 //===----------------------------------------------------------------------===// 45 // AST Consumer Actions 46 //===----------------------------------------------------------------------===// 47 48 std::unique_ptr<ASTConsumer> 49 ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 50 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile)) 51 return CreateASTPrinter(OS, CI.getFrontendOpts().ASTDumpFilter); 52 return nullptr; 53 } 54 55 std::unique_ptr<ASTConsumer> 56 ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 57 return CreateASTDumper(CI.getFrontendOpts().ASTDumpFilter, 58 CI.getFrontendOpts().ASTDumpDecls, 59 CI.getFrontendOpts().ASTDumpLookups); 60 } 61 62 std::unique_ptr<ASTConsumer> 63 ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 64 return CreateASTDeclNodeLister(); 65 } 66 67 std::unique_ptr<ASTConsumer> 68 ASTViewAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 69 return CreateASTViewer(); 70 } 71 72 std::unique_ptr<ASTConsumer> 73 DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI, 74 StringRef InFile) { 75 return CreateDeclContextPrinter(); 76 } 77 78 std::unique_ptr<ASTConsumer> 79 GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 80 std::string Sysroot; 81 std::string OutputFile; 82 raw_ostream *OS = nullptr; 83 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS)) 84 return nullptr; 85 86 if (!CI.getFrontendOpts().RelocatablePCH) 87 Sysroot.clear(); 88 return llvm::make_unique<PCHGenerator>(CI.getPreprocessor(), OutputFile, 89 nullptr, Sysroot, OS); 90 } 91 92 bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI, 93 StringRef InFile, 94 std::string &Sysroot, 95 std::string &OutputFile, 96 raw_ostream *&OS) { 97 Sysroot = CI.getHeaderSearchOpts().Sysroot; 98 if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) { 99 CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot); 100 return true; 101 } 102 103 // We use createOutputFile here because this is exposed via libclang, and we 104 // must disable the RemoveFileOnSignal behavior. 105 // We use a temporary to avoid race conditions. 106 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true, 107 /*RemoveFileOnSignal=*/false, InFile, 108 /*Extension=*/"", /*useTemporary=*/true); 109 if (!OS) 110 return true; 111 112 OutputFile = CI.getFrontendOpts().OutputFile; 113 return false; 114 } 115 116 std::unique_ptr<ASTConsumer> 117 GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI, 118 StringRef InFile) { 119 std::string Sysroot; 120 std::string OutputFile; 121 raw_ostream *OS = nullptr; 122 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS)) 123 return nullptr; 124 125 return llvm::make_unique<PCHGenerator>(CI.getPreprocessor(), OutputFile, 126 Module, Sysroot, OS); 127 } 128 129 static SmallVectorImpl<char> & 130 operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) { 131 Includes.append(RHS.begin(), RHS.end()); 132 return Includes; 133 } 134 135 static std::error_code addHeaderInclude(StringRef HeaderName, 136 SmallVectorImpl<char> &Includes, 137 const LangOptions &LangOpts, 138 bool IsExternC) { 139 if (IsExternC && LangOpts.CPlusPlus) 140 Includes += "extern \"C\" {\n"; 141 if (LangOpts.ObjC1) 142 Includes += "#import \""; 143 else 144 Includes += "#include \""; 145 146 Includes += HeaderName; 147 148 Includes += "\"\n"; 149 if (IsExternC && LangOpts.CPlusPlus) 150 Includes += "}\n"; 151 return std::error_code(); 152 } 153 154 static std::error_code addHeaderInclude(const FileEntry *Header, 155 SmallVectorImpl<char> &Includes, 156 const LangOptions &LangOpts, 157 bool IsExternC) { 158 // Use an absolute path if we don't have a filename as written in the module 159 // map file; this ensures that we will identify the right file independent of 160 // header search paths. 161 if (llvm::sys::path::is_absolute(Header->getName())) 162 return addHeaderInclude(Header->getName(), Includes, LangOpts, IsExternC); 163 164 SmallString<256> AbsName(Header->getName()); 165 if (std::error_code Err = llvm::sys::fs::make_absolute(AbsName)) 166 return Err; 167 return addHeaderInclude(AbsName, Includes, LangOpts, IsExternC); 168 } 169 170 /// \brief Collect the set of header includes needed to construct the given 171 /// module and update the TopHeaders file set of the module. 172 /// 173 /// \param Module The module we're collecting includes from. 174 /// 175 /// \param Includes Will be augmented with the set of \#includes or \#imports 176 /// needed to load all of the named headers. 177 static std::error_code 178 collectModuleHeaderIncludes(const LangOptions &LangOpts, FileManager &FileMgr, 179 ModuleMap &ModMap, clang::Module *Module, 180 SmallVectorImpl<char> &Includes) { 181 // Don't collect any headers for unavailable modules. 182 if (!Module->isAvailable()) 183 return std::error_code(); 184 185 // Add includes for each of these headers. 186 for (Module::Header &H : Module->Headers[Module::HK_Normal]) { 187 Module->addTopHeader(H.Entry); 188 // Use the path as specified in the module map file. We'll look for this 189 // file relative to the module build directory (the directory containing 190 // the module map file) so this will find the same file that we found 191 // while parsing the module map. 192 if (std::error_code Err = addHeaderInclude(H.NameAsWritten, Includes, 193 LangOpts, Module->IsExternC)) 194 return Err; 195 } 196 // Note that Module->PrivateHeaders will not be a TopHeader. 197 198 if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader()) { 199 // FIXME: Track the name as written here. 200 Module->addTopHeader(UmbrellaHeader); 201 if (Module->Parent) { 202 // Include the umbrella header for submodules. 203 if (std::error_code Err = addHeaderInclude(UmbrellaHeader, Includes, 204 LangOpts, Module->IsExternC)) 205 return Err; 206 } 207 } else if (const DirectoryEntry *UmbrellaDir = Module->getUmbrellaDir()) { 208 // Add all of the headers we find in this subdirectory. 209 std::error_code EC; 210 SmallString<128> DirNative; 211 llvm::sys::path::native(UmbrellaDir->getName(), DirNative); 212 for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative.str(), EC), 213 DirEnd; 214 Dir != DirEnd && !EC; Dir.increment(EC)) { 215 // Check whether this entry has an extension typically associated with 216 // headers. 217 if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path())) 218 .Cases(".h", ".H", ".hh", ".hpp", true) 219 .Default(false)) 220 continue; 221 222 const FileEntry *Header = FileMgr.getFile(Dir->path()); 223 // FIXME: This shouldn't happen unless there is a file system race. Is 224 // that worth diagnosing? 225 if (!Header) 226 continue; 227 228 // If this header is marked 'unavailable' in this module, don't include 229 // it. 230 if (ModMap.isHeaderUnavailableInModule(Header, Module)) 231 continue; 232 233 // Include this header as part of the umbrella directory. 234 // FIXME: Track the name as written through to here. 235 Module->addTopHeader(Header); 236 if (std::error_code Err = 237 addHeaderInclude(Header, Includes, LangOpts, Module->IsExternC)) 238 return Err; 239 } 240 241 if (EC) 242 return EC; 243 } 244 245 // Recurse into submodules. 246 for (clang::Module::submodule_iterator Sub = Module->submodule_begin(), 247 SubEnd = Module->submodule_end(); 248 Sub != SubEnd; ++Sub) 249 if (std::error_code Err = collectModuleHeaderIncludes( 250 LangOpts, FileMgr, ModMap, *Sub, Includes)) 251 return Err; 252 253 return std::error_code(); 254 } 255 256 bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI, 257 StringRef Filename) { 258 // Find the module map file. 259 const FileEntry *ModuleMap = CI.getFileManager().getFile(Filename); 260 if (!ModuleMap) { 261 CI.getDiagnostics().Report(diag::err_module_map_not_found) 262 << Filename; 263 return false; 264 } 265 266 // Parse the module map file. 267 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); 268 if (HS.loadModuleMapFile(ModuleMap, IsSystem)) 269 return false; 270 271 if (CI.getLangOpts().CurrentModule.empty()) { 272 CI.getDiagnostics().Report(diag::err_missing_module_name); 273 274 // FIXME: Eventually, we could consider asking whether there was just 275 // a single module described in the module map, and use that as a 276 // default. Then it would be fairly trivial to just "compile" a module 277 // map with a single module (the common case). 278 return false; 279 } 280 281 // If we're being run from the command-line, the module build stack will not 282 // have been filled in yet, so complete it now in order to allow us to detect 283 // module cycles. 284 SourceManager &SourceMgr = CI.getSourceManager(); 285 if (SourceMgr.getModuleBuildStack().empty()) 286 SourceMgr.pushModuleBuildStack(CI.getLangOpts().CurrentModule, 287 FullSourceLoc(SourceLocation(), SourceMgr)); 288 289 // Dig out the module definition. 290 Module = HS.lookupModule(CI.getLangOpts().CurrentModule, 291 /*AllowSearch=*/false); 292 if (!Module) { 293 CI.getDiagnostics().Report(diag::err_missing_module) 294 << CI.getLangOpts().CurrentModule << Filename; 295 296 return false; 297 } 298 299 // Check whether we can build this module at all. 300 clang::Module::Requirement Requirement; 301 clang::Module::UnresolvedHeaderDirective MissingHeader; 302 if (!Module->isAvailable(CI.getLangOpts(), CI.getTarget(), Requirement, 303 MissingHeader)) { 304 if (MissingHeader.FileNameLoc.isValid()) { 305 CI.getDiagnostics().Report(MissingHeader.FileNameLoc, 306 diag::err_module_header_missing) 307 << MissingHeader.IsUmbrella << MissingHeader.FileName; 308 } else { 309 CI.getDiagnostics().Report(diag::err_module_unavailable) 310 << Module->getFullModuleName() 311 << Requirement.second << Requirement.first; 312 } 313 314 return false; 315 } 316 317 if (ModuleMapForUniquing && ModuleMapForUniquing != ModuleMap) { 318 Module->IsInferred = true; 319 HS.getModuleMap().setInferredModuleAllowedBy(Module, ModuleMapForUniquing); 320 } else { 321 ModuleMapForUniquing = ModuleMap; 322 } 323 324 FileManager &FileMgr = CI.getFileManager(); 325 326 // Collect the set of #includes we need to build the module. 327 SmallString<256> HeaderContents; 328 std::error_code Err = std::error_code(); 329 if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader()) 330 // FIXME: Track the file name as written. 331 Err = addHeaderInclude(UmbrellaHeader, HeaderContents, CI.getLangOpts(), 332 Module->IsExternC); 333 if (!Err) 334 Err = collectModuleHeaderIncludes( 335 CI.getLangOpts(), FileMgr, 336 CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), Module, 337 HeaderContents); 338 339 if (Err) { 340 CI.getDiagnostics().Report(diag::err_module_cannot_create_includes) 341 << Module->getFullModuleName() << Err.message(); 342 return false; 343 } 344 345 // Inform the preprocessor that includes from within the input buffer should 346 // be resolved relative to the build directory of the module map file. 347 CI.getPreprocessor().setMainFileDir(Module->Directory); 348 349 std::unique_ptr<llvm::MemoryBuffer> InputBuffer = 350 llvm::MemoryBuffer::getMemBufferCopy(HeaderContents, 351 Module::getModuleInputBufferName()); 352 // Ownership of InputBuffer will be transferred to the SourceManager. 353 setCurrentInput(FrontendInputFile(InputBuffer.release(), getCurrentFileKind(), 354 Module->IsSystem)); 355 return true; 356 } 357 358 bool GenerateModuleAction::ComputeASTConsumerArguments(CompilerInstance &CI, 359 StringRef InFile, 360 std::string &Sysroot, 361 std::string &OutputFile, 362 raw_ostream *&OS) { 363 // If no output file was provided, figure out where this module would go 364 // in the module cache. 365 if (CI.getFrontendOpts().OutputFile.empty()) { 366 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); 367 CI.getFrontendOpts().OutputFile = 368 HS.getModuleFileName(CI.getLangOpts().CurrentModule, 369 ModuleMapForUniquing->getName()); 370 } 371 372 // We use createOutputFile here because this is exposed via libclang, and we 373 // must disable the RemoveFileOnSignal behavior. 374 // We use a temporary to avoid race conditions. 375 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true, 376 /*RemoveFileOnSignal=*/false, InFile, 377 /*Extension=*/"", /*useTemporary=*/true, 378 /*CreateMissingDirectories=*/true); 379 if (!OS) 380 return true; 381 382 OutputFile = CI.getFrontendOpts().OutputFile; 383 return false; 384 } 385 386 std::unique_ptr<ASTConsumer> 387 SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 388 return llvm::make_unique<ASTConsumer>(); 389 } 390 391 std::unique_ptr<ASTConsumer> 392 DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI, 393 StringRef InFile) { 394 return llvm::make_unique<ASTConsumer>(); 395 } 396 397 std::unique_ptr<ASTConsumer> 398 VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 399 return llvm::make_unique<ASTConsumer>(); 400 } 401 402 void VerifyPCHAction::ExecuteAction() { 403 CompilerInstance &CI = getCompilerInstance(); 404 bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0; 405 const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot; 406 std::unique_ptr<ASTReader> Reader( 407 new ASTReader(CI.getPreprocessor(), CI.getASTContext(), 408 Sysroot.empty() ? "" : Sysroot.c_str(), 409 /*DisableValidation*/ false, 410 /*AllowPCHWithCompilerErrors*/ false, 411 /*AllowConfigurationMismatch*/ true, 412 /*ValidateSystemInputs*/ true)); 413 414 Reader->ReadAST(getCurrentFile(), 415 Preamble ? serialization::MK_Preamble 416 : serialization::MK_PCH, 417 SourceLocation(), 418 ASTReader::ARR_ConfigurationMismatch); 419 } 420 421 namespace { 422 /// \brief AST reader listener that dumps module information for a module 423 /// file. 424 class DumpModuleInfoListener : public ASTReaderListener { 425 llvm::raw_ostream &Out; 426 427 public: 428 DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { } 429 430 #define DUMP_BOOLEAN(Value, Text) \ 431 Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n" 432 433 bool ReadFullVersionInformation(StringRef FullVersion) override { 434 Out.indent(2) 435 << "Generated by " 436 << (FullVersion == getClangFullRepositoryVersion()? "this" 437 : "a different") 438 << " Clang: " << FullVersion << "\n"; 439 return ASTReaderListener::ReadFullVersionInformation(FullVersion); 440 } 441 442 void ReadModuleName(StringRef ModuleName) override { 443 Out.indent(2) << "Module name: " << ModuleName << "\n"; 444 } 445 void ReadModuleMapFile(StringRef ModuleMapPath) override { 446 Out.indent(2) << "Module map file: " << ModuleMapPath << "\n"; 447 } 448 449 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 450 bool AllowCompatibleDifferences) override { 451 Out.indent(2) << "Language options:\n"; 452 #define LANGOPT(Name, Bits, Default, Description) \ 453 DUMP_BOOLEAN(LangOpts.Name, Description); 454 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 455 Out.indent(4) << Description << ": " \ 456 << static_cast<unsigned>(LangOpts.get##Name()) << "\n"; 457 #define VALUE_LANGOPT(Name, Bits, Default, Description) \ 458 Out.indent(4) << Description << ": " << LangOpts.Name << "\n"; 459 #define BENIGN_LANGOPT(Name, Bits, Default, Description) 460 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 461 #include "clang/Basic/LangOptions.def" 462 return false; 463 } 464 465 bool ReadTargetOptions(const TargetOptions &TargetOpts, 466 bool Complain) override { 467 Out.indent(2) << "Target options:\n"; 468 Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n"; 469 Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n"; 470 Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n"; 471 472 if (!TargetOpts.FeaturesAsWritten.empty()) { 473 Out.indent(4) << "Target features:\n"; 474 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); 475 I != N; ++I) { 476 Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n"; 477 } 478 } 479 480 return false; 481 } 482 483 virtual bool 484 ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, 485 bool Complain) override { 486 Out.indent(2) << "Diagnostic options:\n"; 487 #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name); 488 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 489 Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n"; 490 #define VALUE_DIAGOPT(Name, Bits, Default) \ 491 Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n"; 492 #include "clang/Basic/DiagnosticOptions.def" 493 494 Out.indent(4) << "Diagnostic flags:\n"; 495 for (const std::string &Warning : DiagOpts->Warnings) 496 Out.indent(6) << "-W" << Warning << "\n"; 497 for (const std::string &Remark : DiagOpts->Remarks) 498 Out.indent(6) << "-R" << Remark << "\n"; 499 500 return false; 501 } 502 503 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 504 bool Complain) override { 505 Out.indent(2) << "Header search options:\n"; 506 Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n"; 507 DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes, 508 "Use builtin include directories [-nobuiltininc]"); 509 DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes, 510 "Use standard system include directories [-nostdinc]"); 511 DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes, 512 "Use standard C++ include directories [-nostdinc++]"); 513 DUMP_BOOLEAN(HSOpts.UseLibcxx, 514 "Use libc++ (rather than libstdc++) [-stdlib=]"); 515 return false; 516 } 517 518 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 519 bool Complain, 520 std::string &SuggestedPredefines) override { 521 Out.indent(2) << "Preprocessor options:\n"; 522 DUMP_BOOLEAN(PPOpts.UsePredefines, 523 "Uses compiler/target-specific predefines [-undef]"); 524 DUMP_BOOLEAN(PPOpts.DetailedRecord, 525 "Uses detailed preprocessing record (for indexing)"); 526 527 if (!PPOpts.Macros.empty()) { 528 Out.indent(4) << "Predefined macros:\n"; 529 } 530 531 for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator 532 I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end(); 533 I != IEnd; ++I) { 534 Out.indent(6); 535 if (I->second) 536 Out << "-U"; 537 else 538 Out << "-D"; 539 Out << I->first << "\n"; 540 } 541 return false; 542 } 543 #undef DUMP_BOOLEAN 544 }; 545 } 546 547 void DumpModuleInfoAction::ExecuteAction() { 548 // Set up the output file. 549 std::unique_ptr<llvm::raw_fd_ostream> OutFile; 550 StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile; 551 if (!OutputFileName.empty() && OutputFileName != "-") { 552 std::error_code EC; 553 OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC, 554 llvm::sys::fs::F_Text)); 555 } 556 llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs(); 557 558 Out << "Information for module file '" << getCurrentFile() << "':\n"; 559 DumpModuleInfoListener Listener(Out); 560 ASTReader::readASTFileControlBlock(getCurrentFile(), 561 getCompilerInstance().getFileManager(), 562 Listener); 563 } 564 565 //===----------------------------------------------------------------------===// 566 // Preprocessor Actions 567 //===----------------------------------------------------------------------===// 568 569 void DumpRawTokensAction::ExecuteAction() { 570 Preprocessor &PP = getCompilerInstance().getPreprocessor(); 571 SourceManager &SM = PP.getSourceManager(); 572 573 // Start lexing the specified input file. 574 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID()); 575 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts()); 576 RawLex.SetKeepWhitespaceMode(true); 577 578 Token RawTok; 579 RawLex.LexFromRawLexer(RawTok); 580 while (RawTok.isNot(tok::eof)) { 581 PP.DumpToken(RawTok, true); 582 llvm::errs() << "\n"; 583 RawLex.LexFromRawLexer(RawTok); 584 } 585 } 586 587 void DumpTokensAction::ExecuteAction() { 588 Preprocessor &PP = getCompilerInstance().getPreprocessor(); 589 // Start preprocessing the specified input file. 590 Token Tok; 591 PP.EnterMainSourceFile(); 592 do { 593 PP.Lex(Tok); 594 PP.DumpToken(Tok, true); 595 llvm::errs() << "\n"; 596 } while (Tok.isNot(tok::eof)); 597 } 598 599 void GeneratePTHAction::ExecuteAction() { 600 CompilerInstance &CI = getCompilerInstance(); 601 if (CI.getFrontendOpts().OutputFile.empty() || 602 CI.getFrontendOpts().OutputFile == "-") { 603 // FIXME: Don't fail this way. 604 // FIXME: Verify that we can actually seek in the given file. 605 llvm::report_fatal_error("PTH requires a seekable file for output!"); 606 } 607 llvm::raw_fd_ostream *OS = 608 CI.createDefaultOutputFile(true, getCurrentFile()); 609 if (!OS) return; 610 611 CacheTokens(CI.getPreprocessor(), OS); 612 } 613 614 void PreprocessOnlyAction::ExecuteAction() { 615 Preprocessor &PP = getCompilerInstance().getPreprocessor(); 616 617 // Ignore unknown pragmas. 618 PP.IgnorePragmas(); 619 620 Token Tok; 621 // Start parsing the specified input file. 622 PP.EnterMainSourceFile(); 623 do { 624 PP.Lex(Tok); 625 } while (Tok.isNot(tok::eof)); 626 } 627 628 void PrintPreprocessedAction::ExecuteAction() { 629 CompilerInstance &CI = getCompilerInstance(); 630 // Output file may need to be set to 'Binary', to avoid converting Unix style 631 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>). 632 // 633 // Look to see what type of line endings the file uses. If there's a 634 // CRLF, then we won't open the file up in binary mode. If there is 635 // just an LF or CR, then we will open the file up in binary mode. 636 // In this fashion, the output format should match the input format, unless 637 // the input format has inconsistent line endings. 638 // 639 // This should be a relatively fast operation since most files won't have 640 // all of their source code on a single line. However, that is still a 641 // concern, so if we scan for too long, we'll just assume the file should 642 // be opened in binary mode. 643 bool BinaryMode = true; 644 bool InvalidFile = false; 645 const SourceManager& SM = CI.getSourceManager(); 646 const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(), 647 &InvalidFile); 648 if (!InvalidFile) { 649 const char *cur = Buffer->getBufferStart(); 650 const char *end = Buffer->getBufferEnd(); 651 const char *next = (cur != end) ? cur + 1 : end; 652 653 // Limit ourselves to only scanning 256 characters into the source 654 // file. This is mostly a sanity check in case the file has no 655 // newlines whatsoever. 656 if (end - cur > 256) end = cur + 256; 657 658 while (next < end) { 659 if (*cur == 0x0D) { // CR 660 if (*next == 0x0A) // CRLF 661 BinaryMode = false; 662 663 break; 664 } else if (*cur == 0x0A) // LF 665 break; 666 667 ++cur, ++next; 668 } 669 } 670 671 raw_ostream *OS = CI.createDefaultOutputFile(BinaryMode, getCurrentFile()); 672 if (!OS) return; 673 674 DoPrintPreprocessedInput(CI.getPreprocessor(), OS, 675 CI.getPreprocessorOutputOpts()); 676 } 677 678 void PrintPreambleAction::ExecuteAction() { 679 switch (getCurrentFileKind()) { 680 case IK_C: 681 case IK_CXX: 682 case IK_ObjC: 683 case IK_ObjCXX: 684 case IK_OpenCL: 685 case IK_CUDA: 686 break; 687 688 case IK_None: 689 case IK_Asm: 690 case IK_PreprocessedC: 691 case IK_PreprocessedCXX: 692 case IK_PreprocessedObjC: 693 case IK_PreprocessedObjCXX: 694 case IK_AST: 695 case IK_LLVM_IR: 696 // We can't do anything with these. 697 return; 698 } 699 700 CompilerInstance &CI = getCompilerInstance(); 701 auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile()); 702 if (Buffer) { 703 unsigned Preamble = 704 Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).first; 705 llvm::outs().write((*Buffer)->getBufferStart(), Preamble); 706 } 707 } 708