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