clang API Documentation
00001 //===--- FrontendActions.cpp ----------------------------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "clang/Frontend/FrontendActions.h" 00011 #include "clang/AST/ASTConsumer.h" 00012 #include "clang/Basic/FileManager.h" 00013 #include "clang/Frontend/ASTConsumers.h" 00014 #include "clang/Frontend/ASTUnit.h" 00015 #include "clang/Frontend/CompilerInstance.h" 00016 #include "clang/Frontend/FrontendDiagnostic.h" 00017 #include "clang/Frontend/Utils.h" 00018 #include "clang/Lex/HeaderSearch.h" 00019 #include "clang/Lex/Pragma.h" 00020 #include "clang/Lex/Preprocessor.h" 00021 #include "clang/Parse/Parser.h" 00022 #include "clang/Serialization/ASTReader.h" 00023 #include "clang/Serialization/ASTWriter.h" 00024 #include "llvm/Support/FileSystem.h" 00025 #include "llvm/Support/MemoryBuffer.h" 00026 #include "llvm/Support/raw_ostream.h" 00027 #include <memory> 00028 #include <system_error> 00029 00030 using namespace clang; 00031 00032 //===----------------------------------------------------------------------===// 00033 // Custom Actions 00034 //===----------------------------------------------------------------------===// 00035 00036 std::unique_ptr<ASTConsumer> 00037 InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 00038 return llvm::make_unique<ASTConsumer>(); 00039 } 00040 00041 void InitOnlyAction::ExecuteAction() { 00042 } 00043 00044 //===----------------------------------------------------------------------===// 00045 // AST Consumer Actions 00046 //===----------------------------------------------------------------------===// 00047 00048 std::unique_ptr<ASTConsumer> 00049 ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 00050 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile)) 00051 return CreateASTPrinter(OS, CI.getFrontendOpts().ASTDumpFilter); 00052 return nullptr; 00053 } 00054 00055 std::unique_ptr<ASTConsumer> 00056 ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 00057 return CreateASTDumper(CI.getFrontendOpts().ASTDumpFilter, 00058 CI.getFrontendOpts().ASTDumpDecls, 00059 CI.getFrontendOpts().ASTDumpLookups); 00060 } 00061 00062 std::unique_ptr<ASTConsumer> 00063 ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 00064 return CreateASTDeclNodeLister(); 00065 } 00066 00067 std::unique_ptr<ASTConsumer> 00068 ASTViewAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 00069 return CreateASTViewer(); 00070 } 00071 00072 std::unique_ptr<ASTConsumer> 00073 DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI, 00074 StringRef InFile) { 00075 return CreateDeclContextPrinter(); 00076 } 00077 00078 std::unique_ptr<ASTConsumer> 00079 GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 00080 std::string Sysroot; 00081 std::string OutputFile; 00082 raw_ostream *OS = nullptr; 00083 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS)) 00084 return nullptr; 00085 00086 if (!CI.getFrontendOpts().RelocatablePCH) 00087 Sysroot.clear(); 00088 return llvm::make_unique<PCHGenerator>(CI.getPreprocessor(), OutputFile, 00089 nullptr, Sysroot, OS); 00090 } 00091 00092 bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI, 00093 StringRef InFile, 00094 std::string &Sysroot, 00095 std::string &OutputFile, 00096 raw_ostream *&OS) { 00097 Sysroot = CI.getHeaderSearchOpts().Sysroot; 00098 if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) { 00099 CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot); 00100 return true; 00101 } 00102 00103 // We use createOutputFile here because this is exposed via libclang, and we 00104 // must disable the RemoveFileOnSignal behavior. 00105 // We use a temporary to avoid race conditions. 00106 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true, 00107 /*RemoveFileOnSignal=*/false, InFile, 00108 /*Extension=*/"", /*useTemporary=*/true); 00109 if (!OS) 00110 return true; 00111 00112 OutputFile = CI.getFrontendOpts().OutputFile; 00113 return false; 00114 } 00115 00116 std::unique_ptr<ASTConsumer> 00117 GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI, 00118 StringRef InFile) { 00119 std::string Sysroot; 00120 std::string OutputFile; 00121 raw_ostream *OS = nullptr; 00122 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS)) 00123 return nullptr; 00124 00125 return llvm::make_unique<PCHGenerator>(CI.getPreprocessor(), OutputFile, 00126 Module, Sysroot, OS); 00127 } 00128 00129 static SmallVectorImpl<char> & 00130 operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) { 00131 Includes.append(RHS.begin(), RHS.end()); 00132 return Includes; 00133 } 00134 00135 static std::error_code addHeaderInclude(StringRef HeaderName, 00136 SmallVectorImpl<char> &Includes, 00137 const LangOptions &LangOpts, 00138 bool IsExternC) { 00139 if (IsExternC && LangOpts.CPlusPlus) 00140 Includes += "extern \"C\" {\n"; 00141 if (LangOpts.ObjC1) 00142 Includes += "#import \""; 00143 else 00144 Includes += "#include \""; 00145 // Use an absolute path for the include; there's no reason to think that 00146 // a relative path will work (. might not be on our include path) or that 00147 // it will find the same file. 00148 if (llvm::sys::path::is_absolute(HeaderName)) { 00149 Includes += HeaderName; 00150 } else { 00151 SmallString<256> Header = HeaderName; 00152 if (std::error_code Err = llvm::sys::fs::make_absolute(Header)) 00153 return Err; 00154 Includes += Header; 00155 } 00156 Includes += "\"\n"; 00157 if (IsExternC && LangOpts.CPlusPlus) 00158 Includes += "}\n"; 00159 return std::error_code(); 00160 } 00161 00162 static std::error_code addHeaderInclude(const FileEntry *Header, 00163 SmallVectorImpl<char> &Includes, 00164 const LangOptions &LangOpts, 00165 bool IsExternC) { 00166 return addHeaderInclude(Header->getName(), Includes, LangOpts, IsExternC); 00167 } 00168 00169 /// \brief Collect the set of header includes needed to construct the given 00170 /// module and update the TopHeaders file set of the module. 00171 /// 00172 /// \param Module The module we're collecting includes from. 00173 /// 00174 /// \param Includes Will be augmented with the set of \#includes or \#imports 00175 /// needed to load all of the named headers. 00176 static std::error_code 00177 collectModuleHeaderIncludes(const LangOptions &LangOpts, FileManager &FileMgr, 00178 ModuleMap &ModMap, clang::Module *Module, 00179 SmallVectorImpl<char> &Includes) { 00180 // Don't collect any headers for unavailable modules. 00181 if (!Module->isAvailable()) 00182 return std::error_code(); 00183 00184 // Add includes for each of these headers. 00185 for (unsigned I = 0, N = Module->NormalHeaders.size(); I != N; ++I) { 00186 const FileEntry *Header = Module->NormalHeaders[I]; 00187 Module->addTopHeader(Header); 00188 if (std::error_code Err = 00189 addHeaderInclude(Header, Includes, LangOpts, Module->IsExternC)) 00190 return Err; 00191 } 00192 // Note that Module->PrivateHeaders will not be a TopHeader. 00193 00194 if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader()) { 00195 Module->addTopHeader(UmbrellaHeader); 00196 if (Module->Parent) { 00197 // Include the umbrella header for submodules. 00198 if (std::error_code Err = addHeaderInclude(UmbrellaHeader, Includes, 00199 LangOpts, Module->IsExternC)) 00200 return Err; 00201 } 00202 } else if (const DirectoryEntry *UmbrellaDir = Module->getUmbrellaDir()) { 00203 // Add all of the headers we find in this subdirectory. 00204 std::error_code EC; 00205 SmallString<128> DirNative; 00206 llvm::sys::path::native(UmbrellaDir->getName(), DirNative); 00207 for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative.str(), EC), 00208 DirEnd; 00209 Dir != DirEnd && !EC; Dir.increment(EC)) { 00210 // Check whether this entry has an extension typically associated with 00211 // headers. 00212 if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path())) 00213 .Cases(".h", ".H", ".hh", ".hpp", true) 00214 .Default(false)) 00215 continue; 00216 00217 // If this header is marked 'unavailable' in this module, don't include 00218 // it. 00219 if (const FileEntry *Header = FileMgr.getFile(Dir->path())) { 00220 if (ModMap.isHeaderUnavailableInModule(Header, Module)) 00221 continue; 00222 Module->addTopHeader(Header); 00223 } 00224 00225 // Include this header as part of the umbrella directory. 00226 if (std::error_code Err = addHeaderInclude(Dir->path(), Includes, 00227 LangOpts, Module->IsExternC)) 00228 return Err; 00229 } 00230 00231 if (EC) 00232 return EC; 00233 } 00234 00235 // Recurse into submodules. 00236 for (clang::Module::submodule_iterator Sub = Module->submodule_begin(), 00237 SubEnd = Module->submodule_end(); 00238 Sub != SubEnd; ++Sub) 00239 if (std::error_code Err = collectModuleHeaderIncludes( 00240 LangOpts, FileMgr, ModMap, *Sub, Includes)) 00241 return Err; 00242 00243 return std::error_code(); 00244 } 00245 00246 bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI, 00247 StringRef Filename) { 00248 // Find the module map file. 00249 const FileEntry *ModuleMap = CI.getFileManager().getFile(Filename); 00250 if (!ModuleMap) { 00251 CI.getDiagnostics().Report(diag::err_module_map_not_found) 00252 << Filename; 00253 return false; 00254 } 00255 00256 // Parse the module map file. 00257 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); 00258 if (HS.loadModuleMapFile(ModuleMap, IsSystem)) 00259 return false; 00260 00261 if (CI.getLangOpts().CurrentModule.empty()) { 00262 CI.getDiagnostics().Report(diag::err_missing_module_name); 00263 00264 // FIXME: Eventually, we could consider asking whether there was just 00265 // a single module described in the module map, and use that as a 00266 // default. Then it would be fairly trivial to just "compile" a module 00267 // map with a single module (the common case). 00268 return false; 00269 } 00270 00271 // If we're being run from the command-line, the module build stack will not 00272 // have been filled in yet, so complete it now in order to allow us to detect 00273 // module cycles. 00274 SourceManager &SourceMgr = CI.getSourceManager(); 00275 if (SourceMgr.getModuleBuildStack().empty()) 00276 SourceMgr.pushModuleBuildStack(CI.getLangOpts().CurrentModule, 00277 FullSourceLoc(SourceLocation(), SourceMgr)); 00278 00279 // Dig out the module definition. 00280 Module = HS.lookupModule(CI.getLangOpts().CurrentModule, 00281 /*AllowSearch=*/false); 00282 if (!Module) { 00283 CI.getDiagnostics().Report(diag::err_missing_module) 00284 << CI.getLangOpts().CurrentModule << Filename; 00285 00286 return false; 00287 } 00288 00289 // Check whether we can build this module at all. 00290 clang::Module::Requirement Requirement; 00291 clang::Module::HeaderDirective MissingHeader; 00292 if (!Module->isAvailable(CI.getLangOpts(), CI.getTarget(), Requirement, 00293 MissingHeader)) { 00294 if (MissingHeader.FileNameLoc.isValid()) { 00295 CI.getDiagnostics().Report(MissingHeader.FileNameLoc, 00296 diag::err_module_header_missing) 00297 << MissingHeader.IsUmbrella << MissingHeader.FileName; 00298 } else { 00299 CI.getDiagnostics().Report(diag::err_module_unavailable) 00300 << Module->getFullModuleName() 00301 << Requirement.second << Requirement.first; 00302 } 00303 00304 return false; 00305 } 00306 00307 if (ModuleMapForUniquing && ModuleMapForUniquing != ModuleMap) { 00308 Module->IsInferred = true; 00309 HS.getModuleMap().setInferredModuleAllowedBy(Module, ModuleMapForUniquing); 00310 } else { 00311 ModuleMapForUniquing = ModuleMap; 00312 } 00313 00314 FileManager &FileMgr = CI.getFileManager(); 00315 00316 // Collect the set of #includes we need to build the module. 00317 SmallString<256> HeaderContents; 00318 std::error_code Err = std::error_code(); 00319 if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader()) 00320 Err = addHeaderInclude(UmbrellaHeader, HeaderContents, CI.getLangOpts(), 00321 Module->IsExternC); 00322 if (!Err) 00323 Err = collectModuleHeaderIncludes( 00324 CI.getLangOpts(), FileMgr, 00325 CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), Module, 00326 HeaderContents); 00327 00328 if (Err) { 00329 CI.getDiagnostics().Report(diag::err_module_cannot_create_includes) 00330 << Module->getFullModuleName() << Err.message(); 00331 return false; 00332 } 00333 00334 std::unique_ptr<llvm::MemoryBuffer> InputBuffer = 00335 llvm::MemoryBuffer::getMemBufferCopy(HeaderContents, 00336 Module::getModuleInputBufferName()); 00337 // Ownership of InputBuffer will be transferred to the SourceManager. 00338 setCurrentInput(FrontendInputFile(InputBuffer.release(), getCurrentFileKind(), 00339 Module->IsSystem)); 00340 return true; 00341 } 00342 00343 bool GenerateModuleAction::ComputeASTConsumerArguments(CompilerInstance &CI, 00344 StringRef InFile, 00345 std::string &Sysroot, 00346 std::string &OutputFile, 00347 raw_ostream *&OS) { 00348 // If no output file was provided, figure out where this module would go 00349 // in the module cache. 00350 if (CI.getFrontendOpts().OutputFile.empty()) { 00351 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); 00352 CI.getFrontendOpts().OutputFile = 00353 HS.getModuleFileName(CI.getLangOpts().CurrentModule, 00354 ModuleMapForUniquing->getName()); 00355 } 00356 00357 // We use createOutputFile here because this is exposed via libclang, and we 00358 // must disable the RemoveFileOnSignal behavior. 00359 // We use a temporary to avoid race conditions. 00360 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true, 00361 /*RemoveFileOnSignal=*/false, InFile, 00362 /*Extension=*/"", /*useTemporary=*/true, 00363 /*CreateMissingDirectories=*/true); 00364 if (!OS) 00365 return true; 00366 00367 OutputFile = CI.getFrontendOpts().OutputFile; 00368 return false; 00369 } 00370 00371 std::unique_ptr<ASTConsumer> 00372 SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 00373 return llvm::make_unique<ASTConsumer>(); 00374 } 00375 00376 std::unique_ptr<ASTConsumer> 00377 DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI, 00378 StringRef InFile) { 00379 return llvm::make_unique<ASTConsumer>(); 00380 } 00381 00382 std::unique_ptr<ASTConsumer> 00383 VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 00384 return llvm::make_unique<ASTConsumer>(); 00385 } 00386 00387 void VerifyPCHAction::ExecuteAction() { 00388 CompilerInstance &CI = getCompilerInstance(); 00389 bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0; 00390 const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot; 00391 std::unique_ptr<ASTReader> Reader( 00392 new ASTReader(CI.getPreprocessor(), CI.getASTContext(), 00393 Sysroot.empty() ? "" : Sysroot.c_str(), 00394 /*DisableValidation*/ false, 00395 /*AllowPCHWithCompilerErrors*/ false, 00396 /*AllowConfigurationMismatch*/ true, 00397 /*ValidateSystemInputs*/ true)); 00398 00399 Reader->ReadAST(getCurrentFile(), 00400 Preamble ? serialization::MK_Preamble 00401 : serialization::MK_PCH, 00402 SourceLocation(), 00403 ASTReader::ARR_ConfigurationMismatch); 00404 } 00405 00406 namespace { 00407 /// \brief AST reader listener that dumps module information for a module 00408 /// file. 00409 class DumpModuleInfoListener : public ASTReaderListener { 00410 llvm::raw_ostream &Out; 00411 00412 public: 00413 DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { } 00414 00415 #define DUMP_BOOLEAN(Value, Text) \ 00416 Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n" 00417 00418 bool ReadFullVersionInformation(StringRef FullVersion) override { 00419 Out.indent(2) 00420 << "Generated by " 00421 << (FullVersion == getClangFullRepositoryVersion()? "this" 00422 : "a different") 00423 << " Clang: " << FullVersion << "\n"; 00424 return ASTReaderListener::ReadFullVersionInformation(FullVersion); 00425 } 00426 00427 void ReadModuleName(StringRef ModuleName) override { 00428 Out.indent(2) << "Module name: " << ModuleName << "\n"; 00429 } 00430 void ReadModuleMapFile(StringRef ModuleMapPath) override { 00431 Out.indent(2) << "Module map file: " << ModuleMapPath << "\n"; 00432 } 00433 00434 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 00435 bool AllowCompatibleDifferences) override { 00436 Out.indent(2) << "Language options:\n"; 00437 #define LANGOPT(Name, Bits, Default, Description) \ 00438 DUMP_BOOLEAN(LangOpts.Name, Description); 00439 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 00440 Out.indent(4) << Description << ": " \ 00441 << static_cast<unsigned>(LangOpts.get##Name()) << "\n"; 00442 #define VALUE_LANGOPT(Name, Bits, Default, Description) \ 00443 Out.indent(4) << Description << ": " << LangOpts.Name << "\n"; 00444 #define BENIGN_LANGOPT(Name, Bits, Default, Description) 00445 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 00446 #include "clang/Basic/LangOptions.def" 00447 return false; 00448 } 00449 00450 bool ReadTargetOptions(const TargetOptions &TargetOpts, 00451 bool Complain) override { 00452 Out.indent(2) << "Target options:\n"; 00453 Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n"; 00454 Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n"; 00455 Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n"; 00456 00457 if (!TargetOpts.FeaturesAsWritten.empty()) { 00458 Out.indent(4) << "Target features:\n"; 00459 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); 00460 I != N; ++I) { 00461 Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n"; 00462 } 00463 } 00464 00465 return false; 00466 } 00467 00468 virtual bool 00469 ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, 00470 bool Complain) override { 00471 Out.indent(2) << "Diagnostic options:\n"; 00472 #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name); 00473 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 00474 Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n"; 00475 #define VALUE_DIAGOPT(Name, Bits, Default) \ 00476 Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n"; 00477 #include "clang/Basic/DiagnosticOptions.def" 00478 00479 Out.indent(4) << "Diagnostic flags:\n"; 00480 for (const std::string &Warning : DiagOpts->Warnings) 00481 Out.indent(6) << "-W" << Warning << "\n"; 00482 for (const std::string &Remark : DiagOpts->Remarks) 00483 Out.indent(6) << "-R" << Remark << "\n"; 00484 00485 return false; 00486 } 00487 00488 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 00489 bool Complain) override { 00490 Out.indent(2) << "Header search options:\n"; 00491 Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n"; 00492 DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes, 00493 "Use builtin include directories [-nobuiltininc]"); 00494 DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes, 00495 "Use standard system include directories [-nostdinc]"); 00496 DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes, 00497 "Use standard C++ include directories [-nostdinc++]"); 00498 DUMP_BOOLEAN(HSOpts.UseLibcxx, 00499 "Use libc++ (rather than libstdc++) [-stdlib=]"); 00500 return false; 00501 } 00502 00503 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 00504 bool Complain, 00505 std::string &SuggestedPredefines) override { 00506 Out.indent(2) << "Preprocessor options:\n"; 00507 DUMP_BOOLEAN(PPOpts.UsePredefines, 00508 "Uses compiler/target-specific predefines [-undef]"); 00509 DUMP_BOOLEAN(PPOpts.DetailedRecord, 00510 "Uses detailed preprocessing record (for indexing)"); 00511 00512 if (!PPOpts.Macros.empty()) { 00513 Out.indent(4) << "Predefined macros:\n"; 00514 } 00515 00516 for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator 00517 I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end(); 00518 I != IEnd; ++I) { 00519 Out.indent(6); 00520 if (I->second) 00521 Out << "-U"; 00522 else 00523 Out << "-D"; 00524 Out << I->first << "\n"; 00525 } 00526 return false; 00527 } 00528 #undef DUMP_BOOLEAN 00529 }; 00530 } 00531 00532 void DumpModuleInfoAction::ExecuteAction() { 00533 // Set up the output file. 00534 std::unique_ptr<llvm::raw_fd_ostream> OutFile; 00535 StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile; 00536 if (!OutputFileName.empty() && OutputFileName != "-") { 00537 std::error_code EC; 00538 OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC, 00539 llvm::sys::fs::F_Text)); 00540 } 00541 llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs(); 00542 00543 Out << "Information for module file '" << getCurrentFile() << "':\n"; 00544 DumpModuleInfoListener Listener(Out); 00545 ASTReader::readASTFileControlBlock(getCurrentFile(), 00546 getCompilerInstance().getFileManager(), 00547 Listener); 00548 } 00549 00550 //===----------------------------------------------------------------------===// 00551 // Preprocessor Actions 00552 //===----------------------------------------------------------------------===// 00553 00554 void DumpRawTokensAction::ExecuteAction() { 00555 Preprocessor &PP = getCompilerInstance().getPreprocessor(); 00556 SourceManager &SM = PP.getSourceManager(); 00557 00558 // Start lexing the specified input file. 00559 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID()); 00560 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts()); 00561 RawLex.SetKeepWhitespaceMode(true); 00562 00563 Token RawTok; 00564 RawLex.LexFromRawLexer(RawTok); 00565 while (RawTok.isNot(tok::eof)) { 00566 PP.DumpToken(RawTok, true); 00567 llvm::errs() << "\n"; 00568 RawLex.LexFromRawLexer(RawTok); 00569 } 00570 } 00571 00572 void DumpTokensAction::ExecuteAction() { 00573 Preprocessor &PP = getCompilerInstance().getPreprocessor(); 00574 // Start preprocessing the specified input file. 00575 Token Tok; 00576 PP.EnterMainSourceFile(); 00577 do { 00578 PP.Lex(Tok); 00579 PP.DumpToken(Tok, true); 00580 llvm::errs() << "\n"; 00581 } while (Tok.isNot(tok::eof)); 00582 } 00583 00584 void GeneratePTHAction::ExecuteAction() { 00585 CompilerInstance &CI = getCompilerInstance(); 00586 if (CI.getFrontendOpts().OutputFile.empty() || 00587 CI.getFrontendOpts().OutputFile == "-") { 00588 // FIXME: Don't fail this way. 00589 // FIXME: Verify that we can actually seek in the given file. 00590 llvm::report_fatal_error("PTH requires a seekable file for output!"); 00591 } 00592 llvm::raw_fd_ostream *OS = 00593 CI.createDefaultOutputFile(true, getCurrentFile()); 00594 if (!OS) return; 00595 00596 CacheTokens(CI.getPreprocessor(), OS); 00597 } 00598 00599 void PreprocessOnlyAction::ExecuteAction() { 00600 Preprocessor &PP = getCompilerInstance().getPreprocessor(); 00601 00602 // Ignore unknown pragmas. 00603 PP.IgnorePragmas(); 00604 00605 Token Tok; 00606 // Start parsing the specified input file. 00607 PP.EnterMainSourceFile(); 00608 do { 00609 PP.Lex(Tok); 00610 } while (Tok.isNot(tok::eof)); 00611 } 00612 00613 void PrintPreprocessedAction::ExecuteAction() { 00614 CompilerInstance &CI = getCompilerInstance(); 00615 // Output file may need to be set to 'Binary', to avoid converting Unix style 00616 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>). 00617 // 00618 // Look to see what type of line endings the file uses. If there's a 00619 // CRLF, then we won't open the file up in binary mode. If there is 00620 // just an LF or CR, then we will open the file up in binary mode. 00621 // In this fashion, the output format should match the input format, unless 00622 // the input format has inconsistent line endings. 00623 // 00624 // This should be a relatively fast operation since most files won't have 00625 // all of their source code on a single line. However, that is still a 00626 // concern, so if we scan for too long, we'll just assume the file should 00627 // be opened in binary mode. 00628 bool BinaryMode = true; 00629 bool InvalidFile = false; 00630 const SourceManager& SM = CI.getSourceManager(); 00631 const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(), 00632 &InvalidFile); 00633 if (!InvalidFile) { 00634 const char *cur = Buffer->getBufferStart(); 00635 const char *end = Buffer->getBufferEnd(); 00636 const char *next = (cur != end) ? cur + 1 : end; 00637 00638 // Limit ourselves to only scanning 256 characters into the source 00639 // file. This is mostly a sanity check in case the file has no 00640 // newlines whatsoever. 00641 if (end - cur > 256) end = cur + 256; 00642 00643 while (next < end) { 00644 if (*cur == 0x0D) { // CR 00645 if (*next == 0x0A) // CRLF 00646 BinaryMode = false; 00647 00648 break; 00649 } else if (*cur == 0x0A) // LF 00650 break; 00651 00652 ++cur, ++next; 00653 } 00654 } 00655 00656 raw_ostream *OS = CI.createDefaultOutputFile(BinaryMode, getCurrentFile()); 00657 if (!OS) return; 00658 00659 DoPrintPreprocessedInput(CI.getPreprocessor(), OS, 00660 CI.getPreprocessorOutputOpts()); 00661 } 00662 00663 void PrintPreambleAction::ExecuteAction() { 00664 switch (getCurrentFileKind()) { 00665 case IK_C: 00666 case IK_CXX: 00667 case IK_ObjC: 00668 case IK_ObjCXX: 00669 case IK_OpenCL: 00670 case IK_CUDA: 00671 break; 00672 00673 case IK_None: 00674 case IK_Asm: 00675 case IK_PreprocessedC: 00676 case IK_PreprocessedCXX: 00677 case IK_PreprocessedObjC: 00678 case IK_PreprocessedObjCXX: 00679 case IK_AST: 00680 case IK_LLVM_IR: 00681 // We can't do anything with these. 00682 return; 00683 } 00684 00685 CompilerInstance &CI = getCompilerInstance(); 00686 auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile()); 00687 if (Buffer) { 00688 unsigned Preamble = 00689 Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).first; 00690 llvm::outs().write((*Buffer)->getBufferStart(), Preamble); 00691 } 00692 }