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