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/CompilerInstance.h" 15 #include "clang/Frontend/FrontendDiagnostic.h" 16 #include "clang/Frontend/MultiplexConsumer.h" 17 #include "clang/Frontend/Utils.h" 18 #include "clang/Lex/HeaderSearch.h" 19 #include "clang/Lex/Preprocessor.h" 20 #include "clang/Lex/PreprocessorOptions.h" 21 #include "clang/Serialization/ASTReader.h" 22 #include "clang/Serialization/ASTWriter.h" 23 #include "llvm/Support/FileSystem.h" 24 #include "llvm/Support/MemoryBuffer.h" 25 #include "llvm/Support/Path.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 (std::unique_ptr<raw_ostream> OS = 51 CI.createDefaultOutputFile(false, InFile)) 52 return CreateASTPrinter(std::move(OS), CI.getFrontendOpts().ASTDumpFilter); 53 return nullptr; 54 } 55 56 std::unique_ptr<ASTConsumer> 57 ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 58 return CreateASTDumper(CI.getFrontendOpts().ASTDumpFilter, 59 CI.getFrontendOpts().ASTDumpDecls, 60 CI.getFrontendOpts().ASTDumpAll, 61 CI.getFrontendOpts().ASTDumpLookups); 62 } 63 64 std::unique_ptr<ASTConsumer> 65 ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 66 return CreateASTDeclNodeLister(); 67 } 68 69 std::unique_ptr<ASTConsumer> 70 ASTViewAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 71 return CreateASTViewer(); 72 } 73 74 std::unique_ptr<ASTConsumer> 75 DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI, 76 StringRef InFile) { 77 return CreateDeclContextPrinter(); 78 } 79 80 std::unique_ptr<ASTConsumer> 81 GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 82 std::string Sysroot; 83 std::string OutputFile; 84 std::unique_ptr<raw_pwrite_stream> OS = 85 ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile); 86 if (!OS) 87 return nullptr; 88 89 if (!CI.getFrontendOpts().RelocatablePCH) 90 Sysroot.clear(); 91 92 auto Buffer = std::make_shared<PCHBuffer>(); 93 std::vector<std::unique_ptr<ASTConsumer>> Consumers; 94 Consumers.push_back(llvm::make_unique<PCHGenerator>( 95 CI.getPreprocessor(), OutputFile, Sysroot, 96 Buffer, CI.getFrontendOpts().ModuleFileExtensions, 97 /*AllowASTWithErrors*/CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, 98 /*IncludeTimestamps*/ 99 +CI.getFrontendOpts().IncludeTimestamps)); 100 Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator( 101 CI, InFile, OutputFile, std::move(OS), Buffer)); 102 103 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers)); 104 } 105 106 std::unique_ptr<raw_pwrite_stream> 107 GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI, 108 StringRef InFile, 109 std::string &Sysroot, 110 std::string &OutputFile) { 111 Sysroot = CI.getHeaderSearchOpts().Sysroot; 112 if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) { 113 CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot); 114 return nullptr; 115 } 116 117 // We use createOutputFile here because this is exposed via libclang, and we 118 // must disable the RemoveFileOnSignal behavior. 119 // We use a temporary to avoid race conditions. 120 std::unique_ptr<raw_pwrite_stream> OS = 121 CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true, 122 /*RemoveFileOnSignal=*/false, InFile, 123 /*Extension=*/"", /*useTemporary=*/true); 124 if (!OS) 125 return nullptr; 126 127 OutputFile = CI.getFrontendOpts().OutputFile; 128 return OS; 129 } 130 131 bool GeneratePCHAction::shouldEraseOutputFiles() { 132 if (getCompilerInstance().getPreprocessorOpts().AllowPCHWithCompilerErrors) 133 return false; 134 return ASTFrontendAction::shouldEraseOutputFiles(); 135 } 136 137 bool GeneratePCHAction::BeginSourceFileAction(CompilerInstance &CI) { 138 CI.getLangOpts().CompilingPCH = true; 139 return true; 140 } 141 142 std::unique_ptr<ASTConsumer> 143 GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI, 144 StringRef InFile) { 145 std::unique_ptr<raw_pwrite_stream> OS = CreateOutputFile(CI, InFile); 146 if (!OS) 147 return nullptr; 148 149 std::string OutputFile = CI.getFrontendOpts().OutputFile; 150 std::string Sysroot; 151 152 auto Buffer = std::make_shared<PCHBuffer>(); 153 std::vector<std::unique_ptr<ASTConsumer>> Consumers; 154 155 Consumers.push_back(llvm::make_unique<PCHGenerator>( 156 CI.getPreprocessor(), OutputFile, Sysroot, 157 Buffer, CI.getFrontendOpts().ModuleFileExtensions, 158 /*AllowASTWithErrors=*/false, 159 /*IncludeTimestamps=*/ 160 +CI.getFrontendOpts().BuildingImplicitModule)); 161 Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator( 162 CI, InFile, OutputFile, std::move(OS), Buffer)); 163 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers)); 164 } 165 166 bool GenerateModuleFromModuleMapAction::BeginSourceFileAction( 167 CompilerInstance &CI) { 168 if (!CI.getLangOpts().Modules) { 169 CI.getDiagnostics().Report(diag::err_module_build_requires_fmodules); 170 return false; 171 } 172 173 return GenerateModuleAction::BeginSourceFileAction(CI); 174 } 175 176 std::unique_ptr<raw_pwrite_stream> 177 GenerateModuleFromModuleMapAction::CreateOutputFile(CompilerInstance &CI, 178 StringRef InFile) { 179 // If no output file was provided, figure out where this module would go 180 // in the module cache. 181 if (CI.getFrontendOpts().OutputFile.empty()) { 182 StringRef ModuleMapFile = CI.getFrontendOpts().OriginalModuleMap; 183 if (ModuleMapFile.empty()) 184 ModuleMapFile = InFile; 185 186 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); 187 CI.getFrontendOpts().OutputFile = 188 HS.getCachedModuleFileName(CI.getLangOpts().CurrentModule, 189 ModuleMapFile); 190 } 191 192 // We use createOutputFile here because this is exposed via libclang, and we 193 // must disable the RemoveFileOnSignal behavior. 194 // We use a temporary to avoid race conditions. 195 return CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true, 196 /*RemoveFileOnSignal=*/false, InFile, 197 /*Extension=*/"", /*useTemporary=*/true, 198 /*CreateMissingDirectories=*/true); 199 } 200 201 bool GenerateModuleInterfaceAction::BeginSourceFileAction( 202 CompilerInstance &CI) { 203 if (!CI.getLangOpts().ModulesTS) { 204 CI.getDiagnostics().Report(diag::err_module_interface_requires_modules_ts); 205 return false; 206 } 207 208 CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface); 209 210 return GenerateModuleAction::BeginSourceFileAction(CI); 211 } 212 213 std::unique_ptr<raw_pwrite_stream> 214 GenerateModuleInterfaceAction::CreateOutputFile(CompilerInstance &CI, 215 StringRef InFile) { 216 return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm"); 217 } 218 219 SyntaxOnlyAction::~SyntaxOnlyAction() { 220 } 221 222 std::unique_ptr<ASTConsumer> 223 SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 224 return llvm::make_unique<ASTConsumer>(); 225 } 226 227 std::unique_ptr<ASTConsumer> 228 DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI, 229 StringRef InFile) { 230 return llvm::make_unique<ASTConsumer>(); 231 } 232 233 std::unique_ptr<ASTConsumer> 234 VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 235 return llvm::make_unique<ASTConsumer>(); 236 } 237 238 void VerifyPCHAction::ExecuteAction() { 239 CompilerInstance &CI = getCompilerInstance(); 240 bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0; 241 const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot; 242 std::unique_ptr<ASTReader> Reader(new ASTReader( 243 CI.getPreprocessor(), &CI.getASTContext(), CI.getPCHContainerReader(), 244 CI.getFrontendOpts().ModuleFileExtensions, 245 Sysroot.empty() ? "" : Sysroot.c_str(), 246 /*DisableValidation*/ false, 247 /*AllowPCHWithCompilerErrors*/ false, 248 /*AllowConfigurationMismatch*/ true, 249 /*ValidateSystemInputs*/ true)); 250 251 Reader->ReadAST(getCurrentFile(), 252 Preamble ? serialization::MK_Preamble 253 : serialization::MK_PCH, 254 SourceLocation(), 255 ASTReader::ARR_ConfigurationMismatch); 256 } 257 258 namespace { 259 /// \brief AST reader listener that dumps module information for a module 260 /// file. 261 class DumpModuleInfoListener : public ASTReaderListener { 262 llvm::raw_ostream &Out; 263 264 public: 265 DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { } 266 267 #define DUMP_BOOLEAN(Value, Text) \ 268 Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n" 269 270 bool ReadFullVersionInformation(StringRef FullVersion) override { 271 Out.indent(2) 272 << "Generated by " 273 << (FullVersion == getClangFullRepositoryVersion()? "this" 274 : "a different") 275 << " Clang: " << FullVersion << "\n"; 276 return ASTReaderListener::ReadFullVersionInformation(FullVersion); 277 } 278 279 void ReadModuleName(StringRef ModuleName) override { 280 Out.indent(2) << "Module name: " << ModuleName << "\n"; 281 } 282 void ReadModuleMapFile(StringRef ModuleMapPath) override { 283 Out.indent(2) << "Module map file: " << ModuleMapPath << "\n"; 284 } 285 286 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 287 bool AllowCompatibleDifferences) override { 288 Out.indent(2) << "Language options:\n"; 289 #define LANGOPT(Name, Bits, Default, Description) \ 290 DUMP_BOOLEAN(LangOpts.Name, Description); 291 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 292 Out.indent(4) << Description << ": " \ 293 << static_cast<unsigned>(LangOpts.get##Name()) << "\n"; 294 #define VALUE_LANGOPT(Name, Bits, Default, Description) \ 295 Out.indent(4) << Description << ": " << LangOpts.Name << "\n"; 296 #define BENIGN_LANGOPT(Name, Bits, Default, Description) 297 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 298 #include "clang/Basic/LangOptions.def" 299 300 if (!LangOpts.ModuleFeatures.empty()) { 301 Out.indent(4) << "Module features:\n"; 302 for (StringRef Feature : LangOpts.ModuleFeatures) 303 Out.indent(6) << Feature << "\n"; 304 } 305 306 return false; 307 } 308 309 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 310 bool AllowCompatibleDifferences) override { 311 Out.indent(2) << "Target options:\n"; 312 Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n"; 313 Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n"; 314 Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n"; 315 316 if (!TargetOpts.FeaturesAsWritten.empty()) { 317 Out.indent(4) << "Target features:\n"; 318 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); 319 I != N; ++I) { 320 Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n"; 321 } 322 } 323 324 return false; 325 } 326 327 bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, 328 bool Complain) override { 329 Out.indent(2) << "Diagnostic options:\n"; 330 #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name); 331 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 332 Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n"; 333 #define VALUE_DIAGOPT(Name, Bits, Default) \ 334 Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n"; 335 #include "clang/Basic/DiagnosticOptions.def" 336 337 Out.indent(4) << "Diagnostic flags:\n"; 338 for (const std::string &Warning : DiagOpts->Warnings) 339 Out.indent(6) << "-W" << Warning << "\n"; 340 for (const std::string &Remark : DiagOpts->Remarks) 341 Out.indent(6) << "-R" << Remark << "\n"; 342 343 return false; 344 } 345 346 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 347 StringRef SpecificModuleCachePath, 348 bool Complain) override { 349 Out.indent(2) << "Header search options:\n"; 350 Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n"; 351 Out.indent(4) << "Resource dir [ -resource-dir=]: '" << HSOpts.ResourceDir << "'\n"; 352 Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n"; 353 DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes, 354 "Use builtin include directories [-nobuiltininc]"); 355 DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes, 356 "Use standard system include directories [-nostdinc]"); 357 DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes, 358 "Use standard C++ include directories [-nostdinc++]"); 359 DUMP_BOOLEAN(HSOpts.UseLibcxx, 360 "Use libc++ (rather than libstdc++) [-stdlib=]"); 361 return false; 362 } 363 364 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 365 bool Complain, 366 std::string &SuggestedPredefines) override { 367 Out.indent(2) << "Preprocessor options:\n"; 368 DUMP_BOOLEAN(PPOpts.UsePredefines, 369 "Uses compiler/target-specific predefines [-undef]"); 370 DUMP_BOOLEAN(PPOpts.DetailedRecord, 371 "Uses detailed preprocessing record (for indexing)"); 372 373 if (!PPOpts.Macros.empty()) { 374 Out.indent(4) << "Predefined macros:\n"; 375 } 376 377 for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator 378 I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end(); 379 I != IEnd; ++I) { 380 Out.indent(6); 381 if (I->second) 382 Out << "-U"; 383 else 384 Out << "-D"; 385 Out << I->first << "\n"; 386 } 387 return false; 388 } 389 390 /// Indicates that a particular module file extension has been read. 391 void readModuleFileExtension( 392 const ModuleFileExtensionMetadata &Metadata) override { 393 Out.indent(2) << "Module file extension '" 394 << Metadata.BlockName << "' " << Metadata.MajorVersion 395 << "." << Metadata.MinorVersion; 396 if (!Metadata.UserInfo.empty()) { 397 Out << ": "; 398 Out.write_escaped(Metadata.UserInfo); 399 } 400 401 Out << "\n"; 402 } 403 #undef DUMP_BOOLEAN 404 }; 405 } 406 407 bool DumpModuleInfoAction::BeginInvocation(CompilerInstance &CI) { 408 // The Object file reader also supports raw ast files and there is no point in 409 // being strict about the module file format in -module-file-info mode. 410 CI.getHeaderSearchOpts().ModuleFormat = "obj"; 411 return true; 412 } 413 414 void DumpModuleInfoAction::ExecuteAction() { 415 // Set up the output file. 416 std::unique_ptr<llvm::raw_fd_ostream> OutFile; 417 StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile; 418 if (!OutputFileName.empty() && OutputFileName != "-") { 419 std::error_code EC; 420 OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC, 421 llvm::sys::fs::F_Text)); 422 } 423 llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs(); 424 425 Out << "Information for module file '" << getCurrentFile() << "':\n"; 426 auto &FileMgr = getCompilerInstance().getFileManager(); 427 auto Buffer = FileMgr.getBufferForFile(getCurrentFile()); 428 StringRef Magic = (*Buffer)->getMemBufferRef().getBuffer(); 429 bool IsRaw = (Magic.size() >= 4 && Magic[0] == 'C' && Magic[1] == 'P' && 430 Magic[2] == 'C' && Magic[3] == 'H'); 431 Out << " Module format: " << (IsRaw ? "raw" : "obj") << "\n"; 432 433 Preprocessor &PP = getCompilerInstance().getPreprocessor(); 434 DumpModuleInfoListener Listener(Out); 435 HeaderSearchOptions &HSOpts = 436 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 437 ASTReader::readASTFileControlBlock( 438 getCurrentFile(), FileMgr, getCompilerInstance().getPCHContainerReader(), 439 /*FindModuleFileExtensions=*/true, Listener, 440 HSOpts.ModulesValidateDiagnosticOptions); 441 } 442 443 //===----------------------------------------------------------------------===// 444 // Preprocessor Actions 445 //===----------------------------------------------------------------------===// 446 447 void DumpRawTokensAction::ExecuteAction() { 448 Preprocessor &PP = getCompilerInstance().getPreprocessor(); 449 SourceManager &SM = PP.getSourceManager(); 450 451 // Start lexing the specified input file. 452 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID()); 453 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts()); 454 RawLex.SetKeepWhitespaceMode(true); 455 456 Token RawTok; 457 RawLex.LexFromRawLexer(RawTok); 458 while (RawTok.isNot(tok::eof)) { 459 PP.DumpToken(RawTok, true); 460 llvm::errs() << "\n"; 461 RawLex.LexFromRawLexer(RawTok); 462 } 463 } 464 465 void DumpTokensAction::ExecuteAction() { 466 Preprocessor &PP = getCompilerInstance().getPreprocessor(); 467 // Start preprocessing the specified input file. 468 Token Tok; 469 PP.EnterMainSourceFile(); 470 do { 471 PP.Lex(Tok); 472 PP.DumpToken(Tok, true); 473 llvm::errs() << "\n"; 474 } while (Tok.isNot(tok::eof)); 475 } 476 477 void GeneratePTHAction::ExecuteAction() { 478 CompilerInstance &CI = getCompilerInstance(); 479 std::unique_ptr<raw_pwrite_stream> OS = 480 CI.createDefaultOutputFile(true, getCurrentFile()); 481 if (!OS) 482 return; 483 484 CacheTokens(CI.getPreprocessor(), OS.get()); 485 } 486 487 void PreprocessOnlyAction::ExecuteAction() { 488 Preprocessor &PP = getCompilerInstance().getPreprocessor(); 489 490 // Ignore unknown pragmas. 491 PP.IgnorePragmas(); 492 493 Token Tok; 494 // Start parsing the specified input file. 495 PP.EnterMainSourceFile(); 496 do { 497 PP.Lex(Tok); 498 } while (Tok.isNot(tok::eof)); 499 } 500 501 void PrintPreprocessedAction::ExecuteAction() { 502 CompilerInstance &CI = getCompilerInstance(); 503 // Output file may need to be set to 'Binary', to avoid converting Unix style 504 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>). 505 // 506 // Look to see what type of line endings the file uses. If there's a 507 // CRLF, then we won't open the file up in binary mode. If there is 508 // just an LF or CR, then we will open the file up in binary mode. 509 // In this fashion, the output format should match the input format, unless 510 // the input format has inconsistent line endings. 511 // 512 // This should be a relatively fast operation since most files won't have 513 // all of their source code on a single line. However, that is still a 514 // concern, so if we scan for too long, we'll just assume the file should 515 // be opened in binary mode. 516 bool BinaryMode = true; 517 bool InvalidFile = false; 518 const SourceManager& SM = CI.getSourceManager(); 519 const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(), 520 &InvalidFile); 521 if (!InvalidFile) { 522 const char *cur = Buffer->getBufferStart(); 523 const char *end = Buffer->getBufferEnd(); 524 const char *next = (cur != end) ? cur + 1 : end; 525 526 // Limit ourselves to only scanning 256 characters into the source 527 // file. This is mostly a sanity check in case the file has no 528 // newlines whatsoever. 529 if (end - cur > 256) end = cur + 256; 530 531 while (next < end) { 532 if (*cur == 0x0D) { // CR 533 if (*next == 0x0A) // CRLF 534 BinaryMode = false; 535 536 break; 537 } else if (*cur == 0x0A) // LF 538 break; 539 540 ++cur; 541 ++next; 542 } 543 } 544 545 std::unique_ptr<raw_ostream> OS = 546 CI.createDefaultOutputFile(BinaryMode, getCurrentFile()); 547 if (!OS) return; 548 549 // If we're preprocessing a module map, start by dumping the contents of the 550 // module itself before switching to the input buffer. 551 auto &Input = getCurrentInput(); 552 if (Input.getKind().getFormat() == InputKind::ModuleMap) { 553 if (Input.isFile()) { 554 (*OS) << "# 1 \""; 555 OS->write_escaped(Input.getFile()); 556 (*OS) << "\"\n"; 557 } 558 // FIXME: Include additional information here so that we don't need the 559 // original source files to exist on disk. 560 getCurrentModule()->print(*OS); 561 (*OS) << "#pragma clang module contents\n"; 562 } 563 564 DoPrintPreprocessedInput(CI.getPreprocessor(), OS.get(), 565 CI.getPreprocessorOutputOpts()); 566 } 567 568 void PrintPreambleAction::ExecuteAction() { 569 switch (getCurrentFileKind().getLanguage()) { 570 case InputKind::C: 571 case InputKind::CXX: 572 case InputKind::ObjC: 573 case InputKind::ObjCXX: 574 case InputKind::OpenCL: 575 case InputKind::CUDA: 576 break; 577 578 case InputKind::Unknown: 579 case InputKind::Asm: 580 case InputKind::LLVM_IR: 581 case InputKind::RenderScript: 582 // We can't do anything with these. 583 return; 584 } 585 586 // We don't expect to find any #include directives in a preprocessed input. 587 if (getCurrentFileKind().isPreprocessed()) 588 return; 589 590 CompilerInstance &CI = getCompilerInstance(); 591 auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile()); 592 if (Buffer) { 593 unsigned Preamble = 594 Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).Size; 595 llvm::outs().write((*Buffer)->getBufferStart(), Preamble); 596 } 597 } 598