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/Basic/FileManager.h" |
| 13 | #include "clang/Frontend/CompilerInstance.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/FrontendActions.h" |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 16 | #include "clang/Frontend/Utils.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Preprocessor.h" |
| 18 | #include "clang/Parse/Parser.h" |
Ted Kremenek | cdf8149 | 2012-09-01 05:09:24 +0000 | [diff] [blame] | 19 | #include "clang/Rewrite/Frontend/ASTConsumers.h" |
| 20 | #include "clang/Rewrite/Frontend/FixItRewriter.h" |
| 21 | #include "clang/Rewrite/Frontend/Rewriters.h" |
Argyrios Kyrtzidis | 24e9aff | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 22 | #include "llvm/Support/FileSystem.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Path.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 25 | #include <memory> |
NAKAMURA Takumi | 69ee7d5 | 2012-01-26 03:47:18 +0000 | [diff] [blame] | 26 | |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | // AST Consumer Actions |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 33 | std::unique_ptr<ASTConsumer> |
| 34 | HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 35 | if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile)) |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 36 | return CreateHTMLPrinter(OS, CI.getPreprocessor()); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 37 | return nullptr; |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | FixItAction::FixItAction() {} |
| 41 | FixItAction::~FixItAction() {} |
| 42 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 43 | std::unique_ptr<ASTConsumer> |
| 44 | FixItAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
| 45 | return llvm::make_unique<ASTConsumer>(); |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Benjamin Kramer | fb5e584 | 2010-10-22 16:48:22 +0000 | [diff] [blame] | 48 | namespace { |
Nick Lewycky | 53f1042 | 2010-08-15 16:47:39 +0000 | [diff] [blame] | 49 | class FixItRewriteInPlace : public FixItOptions { |
| 50 | public: |
Reid Kleckner | 3df5dd4 | 2015-06-17 17:47:30 +0000 | [diff] [blame] | 51 | FixItRewriteInPlace() { InPlace = true; } |
| 52 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 53 | std::string RewriteFilename(const std::string &Filename, int &fd) override { |
Reid Kleckner | 3df5dd4 | 2015-06-17 17:47:30 +0000 | [diff] [blame] | 54 | llvm_unreachable("don't call RewriteFilename for inplace rewrites"); |
Argyrios Kyrtzidis | 623e877 | 2012-01-26 04:19:04 +0000 | [diff] [blame] | 55 | } |
Nick Lewycky | 53f1042 | 2010-08-15 16:47:39 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Nick Lewycky | 078a5e2 | 2010-08-13 17:31:00 +0000 | [diff] [blame] | 58 | class FixItActionSuffixInserter : public FixItOptions { |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 59 | std::string NewSuffix; |
| 60 | |
| 61 | public: |
Nick Lewycky | 078a5e2 | 2010-08-13 17:31:00 +0000 | [diff] [blame] | 62 | FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan) |
| 63 | : NewSuffix(NewSuffix) { |
| 64 | this->FixWhatYouCan = FixWhatYouCan; |
| 65 | } |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 66 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 67 | std::string RewriteFilename(const std::string &Filename, int &fd) override { |
Argyrios Kyrtzidis | 623e877 | 2012-01-26 04:19:04 +0000 | [diff] [blame] | 68 | fd = -1; |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 69 | SmallString<128> Path(Filename); |
Michael J. Spencer | e47230f | 2010-12-18 04:13:32 +0000 | [diff] [blame] | 70 | llvm::sys::path::replace_extension(Path, |
| 71 | NewSuffix + llvm::sys::path::extension(Path)); |
| 72 | return Path.str(); |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 73 | } |
| 74 | }; |
Argyrios Kyrtzidis | 24e9aff | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 75 | |
| 76 | class FixItRewriteToTemp : public FixItOptions { |
| 77 | public: |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 78 | std::string RewriteFilename(const std::string &Filename, int &fd) override { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 79 | SmallString<128> Path; |
Rafael Espindola | a36e78e | 2013-07-05 20:00:06 +0000 | [diff] [blame] | 80 | llvm::sys::fs::createTemporaryFile(llvm::sys::path::filename(Filename), |
Argyrios Kyrtzidis | 203e923 | 2015-09-09 16:48:47 +0000 | [diff] [blame^] | 81 | llvm::sys::path::extension(Filename).drop_front(), fd, |
Rafael Espindola | a36e78e | 2013-07-05 20:00:06 +0000 | [diff] [blame] | 82 | Path); |
| 83 | return Path.str(); |
Argyrios Kyrtzidis | 24e9aff | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 84 | } |
| 85 | }; |
Benjamin Kramer | fb5e584 | 2010-10-22 16:48:22 +0000 | [diff] [blame] | 86 | } // end anonymous namespace |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 87 | |
| 88 | bool FixItAction::BeginSourceFileAction(CompilerInstance &CI, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 89 | StringRef Filename) { |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 90 | const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts(); |
| 91 | if (!FEOpts.FixItSuffix.empty()) { |
Nick Lewycky | 078a5e2 | 2010-08-13 17:31:00 +0000 | [diff] [blame] | 92 | FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix, |
| 93 | FEOpts.FixWhatYouCan)); |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 94 | } else { |
Nick Lewycky | 53f1042 | 2010-08-15 16:47:39 +0000 | [diff] [blame] | 95 | FixItOpts.reset(new FixItRewriteInPlace); |
| 96 | FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan; |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 97 | } |
| 98 | Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(), |
Nick Lewycky | 078a5e2 | 2010-08-13 17:31:00 +0000 | [diff] [blame] | 99 | CI.getLangOpts(), FixItOpts.get())); |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 100 | return true; |
| 101 | } |
| 102 | |
| 103 | void FixItAction::EndSourceFileAction() { |
| 104 | // Otherwise rewrite all files. |
| 105 | Rewriter->WriteFixedFiles(); |
| 106 | } |
| 107 | |
Argyrios Kyrtzidis | 24e9aff | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 108 | bool FixItRecompile::BeginInvocation(CompilerInstance &CI) { |
| 109 | |
| 110 | std::vector<std::pair<std::string, std::string> > RewrittenFiles; |
| 111 | bool err = false; |
| 112 | { |
| 113 | const FrontendOptions &FEOpts = CI.getFrontendOpts(); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 114 | std::unique_ptr<FrontendAction> FixAction(new SyntaxOnlyAction()); |
Argyrios Kyrtzidis | af0bdfc | 2012-01-27 01:00:47 +0000 | [diff] [blame] | 115 | if (FixAction->BeginSourceFile(CI, FEOpts.Inputs[0])) { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 116 | std::unique_ptr<FixItOptions> FixItOpts; |
Argyrios Kyrtzidis | af0bdfc | 2012-01-27 01:00:47 +0000 | [diff] [blame] | 117 | if (FEOpts.FixToTemporaries) |
| 118 | FixItOpts.reset(new FixItRewriteToTemp()); |
| 119 | else |
| 120 | FixItOpts.reset(new FixItRewriteInPlace()); |
| 121 | FixItOpts->Silent = true; |
| 122 | FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan; |
| 123 | FixItOpts->FixOnlyWarnings = FEOpts.FixOnlyWarnings; |
| 124 | FixItRewriter Rewriter(CI.getDiagnostics(), CI.getSourceManager(), |
| 125 | CI.getLangOpts(), FixItOpts.get()); |
| 126 | FixAction->Execute(); |
Argyrios Kyrtzidis | 24e9aff | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 127 | |
Argyrios Kyrtzidis | af0bdfc | 2012-01-27 01:00:47 +0000 | [diff] [blame] | 128 | err = Rewriter.WriteFixedFiles(&RewrittenFiles); |
| 129 | |
| 130 | FixAction->EndSourceFile(); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 131 | CI.setSourceManager(nullptr); |
| 132 | CI.setFileManager(nullptr); |
Argyrios Kyrtzidis | af0bdfc | 2012-01-27 01:00:47 +0000 | [diff] [blame] | 133 | } else { |
| 134 | err = true; |
| 135 | } |
Argyrios Kyrtzidis | 24e9aff | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 136 | } |
| 137 | if (err) |
| 138 | return false; |
| 139 | CI.getDiagnosticClient().clear(); |
Argyrios Kyrtzidis | 0b5ec2d | 2012-01-27 06:15:37 +0000 | [diff] [blame] | 140 | CI.getDiagnostics().Reset(); |
Argyrios Kyrtzidis | 24e9aff | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 141 | |
| 142 | PreprocessorOptions &PPOpts = CI.getPreprocessorOpts(); |
| 143 | PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(), |
| 144 | RewrittenFiles.begin(), RewrittenFiles.end()); |
| 145 | PPOpts.RemappedFilesKeepOriginalName = false; |
| 146 | |
| 147 | return true; |
| 148 | } |
| 149 | |
Alp Toker | 0621cb2 | 2014-07-16 16:48:33 +0000 | [diff] [blame] | 150 | #ifdef CLANG_ENABLE_OBJC_REWRITER |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 151 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 152 | std::unique_ptr<ASTConsumer> |
| 153 | RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 154 | if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp")) { |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 155 | if (CI.getLangOpts().ObjCRuntime.isNonFragile()) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 156 | return CreateModernObjCRewriter(InFile, OS, |
| 157 | CI.getDiagnostics(), CI.getLangOpts(), |
Fariborz Jahanian | e4c7e85 | 2013-02-08 00:27:34 +0000 | [diff] [blame] | 158 | CI.getDiagnosticOpts().NoRewriteMacros, |
| 159 | (CI.getCodeGenOpts().getDebugInfo() != |
| 160 | CodeGenOptions::NoDebugInfo)); |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 161 | return CreateObjCRewriter(InFile, OS, |
| 162 | CI.getDiagnostics(), CI.getLangOpts(), |
| 163 | CI.getDiagnosticOpts().NoRewriteMacros); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 164 | } |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 165 | return nullptr; |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Alp Toker | 0621cb2 | 2014-07-16 16:48:33 +0000 | [diff] [blame] | 168 | #endif |
| 169 | |
| 170 | //===----------------------------------------------------------------------===// |
| 171 | // Preprocessor Actions |
| 172 | //===----------------------------------------------------------------------===// |
| 173 | |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 174 | void RewriteMacrosAction::ExecuteAction() { |
| 175 | CompilerInstance &CI = getCompilerInstance(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 176 | raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile()); |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 177 | if (!OS) return; |
| 178 | |
| 179 | RewriteMacrosInInput(CI.getPreprocessor(), OS); |
| 180 | } |
| 181 | |
| 182 | void RewriteTestAction::ExecuteAction() { |
| 183 | CompilerInstance &CI = getCompilerInstance(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 184 | raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile()); |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 185 | if (!OS) return; |
| 186 | |
| 187 | DoRewriteTest(CI.getPreprocessor(), OS); |
| 188 | } |
David Blaikie | d532124 | 2012-06-06 18:52:13 +0000 | [diff] [blame] | 189 | |
| 190 | void RewriteIncludesAction::ExecuteAction() { |
| 191 | CompilerInstance &CI = getCompilerInstance(); |
| 192 | raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile()); |
| 193 | if (!OS) return; |
| 194 | |
| 195 | RewriteIncludesInInput(CI.getPreprocessor(), OS, |
| 196 | CI.getPreprocessorOutputOpts()); |
| 197 | } |