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