FrontendActions.cpp 21 KB

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