Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +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 | |
Ted Kremenek | cdf8149 | 2012-09-01 05:09:24 +0000 | [diff] [blame] | 10 | #include "clang/Rewrite/Frontend/FrontendActions.h" |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTConsumer.h" |
Richard Smith | 9565c75b | 2017-06-19 23:09:36 +0000 | [diff] [blame] | 12 | #include "clang/Basic/CharInfo.h" |
NAKAMURA Takumi | 7f633df | 2017-07-18 08:55:03 +0000 | [diff] [blame^] | 13 | #include "clang/Config/config.h" |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/CompilerInstance.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/FrontendActions.h" |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 17 | #include "clang/Frontend/Utils.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 19 | #include "clang/Lex/PreprocessorOptions.h" |
Ted Kremenek | cdf8149 | 2012-09-01 05:09:24 +0000 | [diff] [blame] | 20 | #include "clang/Rewrite/Frontend/ASTConsumers.h" |
| 21 | #include "clang/Rewrite/Frontend/FixItRewriter.h" |
| 22 | #include "clang/Rewrite/Frontend/Rewriters.h" |
Richard Smith | 86a3ef5 | 2017-06-09 21:24:02 +0000 | [diff] [blame] | 23 | #include "clang/Serialization/ASTReader.h" |
| 24 | #include "clang/Serialization/Module.h" |
| 25 | #include "clang/Serialization/ModuleManager.h" |
| 26 | #include "llvm/ADT/DenseSet.h" |
| 27 | #include "llvm/Support/CrashRecoveryContext.h" |
Argyrios Kyrtzidis | 24e9aff | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 28 | #include "llvm/Support/FileSystem.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Path.h" |
| 30 | #include "llvm/Support/raw_ostream.h" |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 31 | #include <memory> |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 32 | #include <utility> |
NAKAMURA Takumi | 69ee7d5 | 2012-01-26 03:47:18 +0000 | [diff] [blame] | 33 | |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | // AST Consumer Actions |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 40 | std::unique_ptr<ASTConsumer> |
| 41 | HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 42 | if (std::unique_ptr<raw_ostream> OS = |
| 43 | CI.createDefaultOutputFile(false, InFile)) |
| 44 | return CreateHTMLPrinter(std::move(OS), CI.getPreprocessor()); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 45 | return nullptr; |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | FixItAction::FixItAction() {} |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 49 | FixItAction::~FixItAction() {} |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 50 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 51 | std::unique_ptr<ASTConsumer> |
| 52 | FixItAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
| 53 | return llvm::make_unique<ASTConsumer>(); |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Benjamin Kramer | fb5e584 | 2010-10-22 16:48:22 +0000 | [diff] [blame] | 56 | namespace { |
Nick Lewycky | 53f1042 | 2010-08-15 16:47:39 +0000 | [diff] [blame] | 57 | class FixItRewriteInPlace : public FixItOptions { |
| 58 | public: |
Reid Kleckner | 3df5dd4 | 2015-06-17 17:47:30 +0000 | [diff] [blame] | 59 | FixItRewriteInPlace() { InPlace = true; } |
| 60 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 61 | std::string RewriteFilename(const std::string &Filename, int &fd) override { |
Reid Kleckner | 3df5dd4 | 2015-06-17 17:47:30 +0000 | [diff] [blame] | 62 | llvm_unreachable("don't call RewriteFilename for inplace rewrites"); |
Argyrios Kyrtzidis | 623e877 | 2012-01-26 04:19:04 +0000 | [diff] [blame] | 63 | } |
Nick Lewycky | 53f1042 | 2010-08-15 16:47:39 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
Nick Lewycky | 078a5e2 | 2010-08-13 17:31:00 +0000 | [diff] [blame] | 66 | class FixItActionSuffixInserter : public FixItOptions { |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 67 | std::string NewSuffix; |
| 68 | |
| 69 | public: |
Nick Lewycky | 078a5e2 | 2010-08-13 17:31:00 +0000 | [diff] [blame] | 70 | FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan) |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 71 | : NewSuffix(std::move(NewSuffix)) { |
| 72 | this->FixWhatYouCan = FixWhatYouCan; |
Nick Lewycky | 078a5e2 | 2010-08-13 17:31:00 +0000 | [diff] [blame] | 73 | } |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 74 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 75 | std::string RewriteFilename(const std::string &Filename, int &fd) override { |
Argyrios Kyrtzidis | 623e877 | 2012-01-26 04:19:04 +0000 | [diff] [blame] | 76 | fd = -1; |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 77 | SmallString<128> Path(Filename); |
Michael J. Spencer | e47230f | 2010-12-18 04:13:32 +0000 | [diff] [blame] | 78 | llvm::sys::path::replace_extension(Path, |
| 79 | NewSuffix + llvm::sys::path::extension(Path)); |
| 80 | return Path.str(); |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 81 | } |
| 82 | }; |
Argyrios Kyrtzidis | 24e9aff | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 83 | |
| 84 | class FixItRewriteToTemp : public FixItOptions { |
| 85 | public: |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 86 | std::string RewriteFilename(const std::string &Filename, int &fd) override { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 87 | SmallString<128> Path; |
Rafael Espindola | a36e78e | 2013-07-05 20:00:06 +0000 | [diff] [blame] | 88 | llvm::sys::fs::createTemporaryFile(llvm::sys::path::filename(Filename), |
Argyrios Kyrtzidis | 203e923 | 2015-09-09 16:48:47 +0000 | [diff] [blame] | 89 | llvm::sys::path::extension(Filename).drop_front(), fd, |
Rafael Espindola | a36e78e | 2013-07-05 20:00:06 +0000 | [diff] [blame] | 90 | Path); |
| 91 | return Path.str(); |
Argyrios Kyrtzidis | 24e9aff | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 92 | } |
| 93 | }; |
Benjamin Kramer | fb5e584 | 2010-10-22 16:48:22 +0000 | [diff] [blame] | 94 | } // end anonymous namespace |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 95 | |
Richard Smith | d9259c2 | 2017-06-09 01:36:10 +0000 | [diff] [blame] | 96 | bool FixItAction::BeginSourceFileAction(CompilerInstance &CI) { |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 97 | const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts(); |
| 98 | if (!FEOpts.FixItSuffix.empty()) { |
Nick Lewycky | 078a5e2 | 2010-08-13 17:31:00 +0000 | [diff] [blame] | 99 | FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix, |
| 100 | FEOpts.FixWhatYouCan)); |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 101 | } else { |
Nick Lewycky | 53f1042 | 2010-08-15 16:47:39 +0000 | [diff] [blame] | 102 | FixItOpts.reset(new FixItRewriteInPlace); |
| 103 | FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan; |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 104 | } |
| 105 | Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(), |
Nick Lewycky | 078a5e2 | 2010-08-13 17:31:00 +0000 | [diff] [blame] | 106 | CI.getLangOpts(), FixItOpts.get())); |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 107 | return true; |
| 108 | } |
| 109 | |
| 110 | void FixItAction::EndSourceFileAction() { |
| 111 | // Otherwise rewrite all files. |
| 112 | Rewriter->WriteFixedFiles(); |
| 113 | } |
| 114 | |
Argyrios Kyrtzidis | 24e9aff | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 115 | bool FixItRecompile::BeginInvocation(CompilerInstance &CI) { |
| 116 | |
| 117 | std::vector<std::pair<std::string, std::string> > RewrittenFiles; |
| 118 | bool err = false; |
| 119 | { |
| 120 | const FrontendOptions &FEOpts = CI.getFrontendOpts(); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 121 | std::unique_ptr<FrontendAction> FixAction(new SyntaxOnlyAction()); |
Argyrios Kyrtzidis | af0bdfc | 2012-01-27 01:00:47 +0000 | [diff] [blame] | 122 | if (FixAction->BeginSourceFile(CI, FEOpts.Inputs[0])) { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 123 | std::unique_ptr<FixItOptions> FixItOpts; |
Argyrios Kyrtzidis | af0bdfc | 2012-01-27 01:00:47 +0000 | [diff] [blame] | 124 | if (FEOpts.FixToTemporaries) |
| 125 | FixItOpts.reset(new FixItRewriteToTemp()); |
| 126 | else |
| 127 | FixItOpts.reset(new FixItRewriteInPlace()); |
| 128 | FixItOpts->Silent = true; |
| 129 | FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan; |
| 130 | FixItOpts->FixOnlyWarnings = FEOpts.FixOnlyWarnings; |
| 131 | FixItRewriter Rewriter(CI.getDiagnostics(), CI.getSourceManager(), |
| 132 | CI.getLangOpts(), FixItOpts.get()); |
| 133 | FixAction->Execute(); |
Argyrios Kyrtzidis | 24e9aff | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 134 | |
Argyrios Kyrtzidis | af0bdfc | 2012-01-27 01:00:47 +0000 | [diff] [blame] | 135 | err = Rewriter.WriteFixedFiles(&RewrittenFiles); |
| 136 | |
| 137 | FixAction->EndSourceFile(); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 138 | CI.setSourceManager(nullptr); |
| 139 | CI.setFileManager(nullptr); |
Argyrios Kyrtzidis | af0bdfc | 2012-01-27 01:00:47 +0000 | [diff] [blame] | 140 | } else { |
| 141 | err = true; |
| 142 | } |
Argyrios Kyrtzidis | 24e9aff | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 143 | } |
| 144 | if (err) |
| 145 | return false; |
| 146 | CI.getDiagnosticClient().clear(); |
Argyrios Kyrtzidis | 0b5ec2d | 2012-01-27 06:15:37 +0000 | [diff] [blame] | 147 | CI.getDiagnostics().Reset(); |
Argyrios Kyrtzidis | 24e9aff | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 148 | |
| 149 | PreprocessorOptions &PPOpts = CI.getPreprocessorOpts(); |
| 150 | PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(), |
| 151 | RewrittenFiles.begin(), RewrittenFiles.end()); |
| 152 | PPOpts.RemappedFilesKeepOriginalName = false; |
| 153 | |
| 154 | return true; |
| 155 | } |
| 156 | |
Alp Toker | 0621cb2 | 2014-07-16 16:48:33 +0000 | [diff] [blame] | 157 | #ifdef CLANG_ENABLE_OBJC_REWRITER |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 158 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 159 | std::unique_ptr<ASTConsumer> |
| 160 | RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 161 | if (std::unique_ptr<raw_ostream> OS = |
| 162 | CI.createDefaultOutputFile(false, InFile, "cpp")) { |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 163 | if (CI.getLangOpts().ObjCRuntime.isNonFragile()) |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 164 | return CreateModernObjCRewriter( |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 165 | InFile, std::move(OS), CI.getDiagnostics(), CI.getLangOpts(), |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 166 | CI.getDiagnosticOpts().NoRewriteMacros, |
| 167 | (CI.getCodeGenOpts().getDebugInfo() != codegenoptions::NoDebugInfo)); |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 168 | return CreateObjCRewriter(InFile, std::move(OS), CI.getDiagnostics(), |
| 169 | CI.getLangOpts(), |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 170 | CI.getDiagnosticOpts().NoRewriteMacros); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 171 | } |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 172 | return nullptr; |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Alp Toker | 0621cb2 | 2014-07-16 16:48:33 +0000 | [diff] [blame] | 175 | #endif |
| 176 | |
| 177 | //===----------------------------------------------------------------------===// |
| 178 | // Preprocessor Actions |
| 179 | //===----------------------------------------------------------------------===// |
| 180 | |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 181 | void RewriteMacrosAction::ExecuteAction() { |
| 182 | CompilerInstance &CI = getCompilerInstance(); |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 183 | std::unique_ptr<raw_ostream> OS = |
| 184 | CI.createDefaultOutputFile(true, getCurrentFile()); |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 185 | if (!OS) return; |
| 186 | |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 187 | RewriteMacrosInInput(CI.getPreprocessor(), OS.get()); |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void RewriteTestAction::ExecuteAction() { |
| 191 | CompilerInstance &CI = getCompilerInstance(); |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 192 | std::unique_ptr<raw_ostream> OS = |
| 193 | CI.createDefaultOutputFile(false, getCurrentFile()); |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 194 | if (!OS) return; |
| 195 | |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 196 | DoRewriteTest(CI.getPreprocessor(), OS.get()); |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 197 | } |
David Blaikie | d532124 | 2012-06-06 18:52:13 +0000 | [diff] [blame] | 198 | |
Richard Smith | 86a3ef5 | 2017-06-09 21:24:02 +0000 | [diff] [blame] | 199 | class RewriteIncludesAction::RewriteImportsListener : public ASTReaderListener { |
| 200 | CompilerInstance &CI; |
| 201 | std::weak_ptr<raw_ostream> Out; |
| 202 | |
| 203 | llvm::DenseSet<const FileEntry*> Rewritten; |
| 204 | |
| 205 | public: |
| 206 | RewriteImportsListener(CompilerInstance &CI, std::shared_ptr<raw_ostream> Out) |
| 207 | : CI(CI), Out(Out) {} |
| 208 | |
| 209 | void visitModuleFile(StringRef Filename, |
| 210 | serialization::ModuleKind Kind) override { |
| 211 | auto *File = CI.getFileManager().getFile(Filename); |
| 212 | assert(File && "missing file for loaded module?"); |
| 213 | |
| 214 | // Only rewrite each module file once. |
| 215 | if (!Rewritten.insert(File).second) |
| 216 | return; |
| 217 | |
| 218 | serialization::ModuleFile *MF = |
| 219 | CI.getModuleManager()->getModuleManager().lookup(File); |
| 220 | assert(File && "missing module file for loaded module?"); |
| 221 | |
| 222 | // Not interested in PCH / preambles. |
| 223 | if (!MF->isModule()) |
| 224 | return; |
| 225 | |
| 226 | auto OS = Out.lock(); |
| 227 | assert(OS && "loaded module file after finishing rewrite action?"); |
| 228 | |
Richard Smith | 9565c75b | 2017-06-19 23:09:36 +0000 | [diff] [blame] | 229 | (*OS) << "#pragma clang module build "; |
| 230 | if (isValidIdentifier(MF->ModuleName)) |
| 231 | (*OS) << MF->ModuleName; |
| 232 | else { |
| 233 | (*OS) << '"'; |
| 234 | OS->write_escaped(MF->ModuleName); |
| 235 | (*OS) << '"'; |
| 236 | } |
| 237 | (*OS) << '\n'; |
Richard Smith | 86a3ef5 | 2017-06-09 21:24:02 +0000 | [diff] [blame] | 238 | |
| 239 | // Rewrite the contents of the module in a separate compiler instance. |
| 240 | CompilerInstance Instance(CI.getPCHContainerOperations(), |
| 241 | &CI.getPreprocessor().getPCMCache()); |
| 242 | Instance.setInvocation( |
| 243 | std::make_shared<CompilerInvocation>(CI.getInvocation())); |
| 244 | Instance.createDiagnostics( |
| 245 | new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()), |
| 246 | /*ShouldOwnClient=*/true); |
Richard Smith | 4f05b36 | 2017-06-20 01:31:53 +0000 | [diff] [blame] | 247 | Instance.getFrontendOpts().DisableFree = false; |
Richard Smith | 86a3ef5 | 2017-06-09 21:24:02 +0000 | [diff] [blame] | 248 | Instance.getFrontendOpts().Inputs.clear(); |
| 249 | Instance.getFrontendOpts().Inputs.emplace_back( |
| 250 | Filename, InputKind(InputKind::Unknown, InputKind::Precompiled)); |
Richard Smith | a21c8e1 | 2017-06-26 19:39:25 +0000 | [diff] [blame] | 251 | Instance.getFrontendOpts().ModuleFiles.clear(); |
| 252 | Instance.getFrontendOpts().ModuleMapFiles.clear(); |
Richard Smith | 86a3ef5 | 2017-06-09 21:24:02 +0000 | [diff] [blame] | 253 | // Don't recursively rewrite imports. We handle them all at the top level. |
| 254 | Instance.getPreprocessorOutputOpts().RewriteImports = false; |
| 255 | |
| 256 | llvm::CrashRecoveryContext().RunSafelyOnThread([&]() { |
| 257 | RewriteIncludesAction Action; |
| 258 | Action.OutputStream = OS; |
| 259 | Instance.ExecuteAction(Action); |
| 260 | }); |
| 261 | |
| 262 | (*OS) << "#pragma clang module endbuild /*" << MF->ModuleName << "*/\n"; |
| 263 | } |
| 264 | }; |
| 265 | |
| 266 | bool RewriteIncludesAction::BeginSourceFileAction(CompilerInstance &CI) { |
| 267 | if (!OutputStream) { |
| 268 | OutputStream = CI.createDefaultOutputFile(true, getCurrentFile()); |
| 269 | if (!OutputStream) |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | auto &OS = *OutputStream; |
David Blaikie | d532124 | 2012-06-06 18:52:13 +0000 | [diff] [blame] | 274 | |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 275 | // If we're preprocessing a module map, start by dumping the contents of the |
| 276 | // module itself before switching to the input buffer. |
| 277 | auto &Input = getCurrentInput(); |
| 278 | if (Input.getKind().getFormat() == InputKind::ModuleMap) { |
Richard Smith | c784e96 | 2017-06-01 20:10:35 +0000 | [diff] [blame] | 279 | if (Input.isFile()) { |
Richard Smith | 86a3ef5 | 2017-06-09 21:24:02 +0000 | [diff] [blame] | 280 | OS << "# 1 \""; |
| 281 | OS.write_escaped(Input.getFile()); |
| 282 | OS << "\"\n"; |
Richard Smith | c784e96 | 2017-06-01 20:10:35 +0000 | [diff] [blame] | 283 | } |
Richard Smith | 86a3ef5 | 2017-06-09 21:24:02 +0000 | [diff] [blame] | 284 | getCurrentModule()->print(OS); |
| 285 | OS << "#pragma clang module contents\n"; |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Richard Smith | 86a3ef5 | 2017-06-09 21:24:02 +0000 | [diff] [blame] | 288 | // If we're rewriting imports, set up a listener to track when we import |
| 289 | // module files. |
| 290 | if (CI.getPreprocessorOutputOpts().RewriteImports) { |
| 291 | CI.createModuleManager(); |
| 292 | CI.getModuleManager()->addListener( |
| 293 | llvm::make_unique<RewriteImportsListener>(CI, OutputStream)); |
| 294 | } |
| 295 | |
| 296 | return true; |
| 297 | } |
| 298 | |
| 299 | void RewriteIncludesAction::ExecuteAction() { |
| 300 | CompilerInstance &CI = getCompilerInstance(); |
| 301 | |
| 302 | // If we're rewriting imports, emit the module build output first rather |
| 303 | // than switching back and forth (potentially in the middle of a line). |
| 304 | if (CI.getPreprocessorOutputOpts().RewriteImports) { |
| 305 | std::string Buffer; |
| 306 | llvm::raw_string_ostream OS(Buffer); |
| 307 | |
| 308 | RewriteIncludesInInput(CI.getPreprocessor(), &OS, |
| 309 | CI.getPreprocessorOutputOpts()); |
| 310 | |
| 311 | (*OutputStream) << OS.str(); |
| 312 | } else { |
| 313 | RewriteIncludesInInput(CI.getPreprocessor(), OutputStream.get(), |
| 314 | CI.getPreprocessorOutputOpts()); |
| 315 | } |
| 316 | |
| 317 | OutputStream.reset(); |
David Blaikie | d532124 | 2012-06-06 18:52:13 +0000 | [diff] [blame] | 318 | } |