Daniel Dunbar | 9b414d3 | 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 | |
| 10 | #include "clang/Rewrite/FrontendActions.h" |
| 11 | #include "clang/AST/ASTConsumer.h" |
| 12 | #include "clang/Lex/Preprocessor.h" |
| 13 | #include "clang/Parse/Parser.h" |
| 14 | #include "clang/Basic/FileManager.h" |
Argyrios Kyrtzidis | 61d679a | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/FrontendActions.h" |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/CompilerInstance.h" |
| 17 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 18 | #include "clang/Frontend/Utils.h" |
| 19 | #include "clang/Rewrite/ASTConsumers.h" |
| 20 | #include "clang/Rewrite/FixItRewriter.h" |
| 21 | #include "clang/Rewrite/Rewriters.h" |
| 22 | #include "llvm/ADT/OwningPtr.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Path.h" |
Argyrios Kyrtzidis | 61d679a | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 25 | #include "llvm/Support/FileSystem.h" |
NAKAMURA Takumi | 0277039 | 2012-01-26 03:47:18 +0000 | [diff] [blame] | 26 | |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | // AST Consumer Actions |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
| 33 | ASTConsumer *HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 34 | StringRef InFile) { |
| 35 | if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile)) |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 36 | return CreateHTMLPrinter(OS, CI.getPreprocessor()); |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | FixItAction::FixItAction() {} |
| 41 | FixItAction::~FixItAction() {} |
| 42 | |
| 43 | ASTConsumer *FixItAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 44 | StringRef InFile) { |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 45 | return new ASTConsumer(); |
| 46 | } |
| 47 | |
Benjamin Kramer | 79ba2a6 | 2010-10-22 16:48:22 +0000 | [diff] [blame] | 48 | namespace { |
Nick Lewycky | 96872c4 | 2010-08-15 16:47:39 +0000 | [diff] [blame] | 49 | class FixItRewriteInPlace : public FixItOptions { |
| 50 | public: |
Argyrios Kyrtzidis | c8af910 | 2012-01-26 04:19:04 +0000 | [diff] [blame] | 51 | std::string RewriteFilename(const std::string &Filename, int &fd) { |
| 52 | fd = -1; |
| 53 | return Filename; |
| 54 | } |
Nick Lewycky | 96872c4 | 2010-08-15 16:47:39 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
Nick Lewycky | 1450f26 | 2010-08-13 17:31:00 +0000 | [diff] [blame] | 57 | class FixItActionSuffixInserter : public FixItOptions { |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 58 | std::string NewSuffix; |
| 59 | |
| 60 | public: |
Nick Lewycky | 1450f26 | 2010-08-13 17:31:00 +0000 | [diff] [blame] | 61 | FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan) |
| 62 | : NewSuffix(NewSuffix) { |
| 63 | this->FixWhatYouCan = FixWhatYouCan; |
| 64 | } |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 65 | |
Argyrios Kyrtzidis | c8af910 | 2012-01-26 04:19:04 +0000 | [diff] [blame] | 66 | std::string RewriteFilename(const std::string &Filename, int &fd) { |
| 67 | fd = -1; |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 68 | SmallString<128> Path(Filename); |
Michael J. Spencer | d5b08be | 2010-12-18 04:13:32 +0000 | [diff] [blame] | 69 | llvm::sys::path::replace_extension(Path, |
| 70 | NewSuffix + llvm::sys::path::extension(Path)); |
| 71 | return Path.str(); |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 72 | } |
| 73 | }; |
Argyrios Kyrtzidis | 61d679a | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 74 | |
| 75 | class FixItRewriteToTemp : public FixItOptions { |
| 76 | public: |
Argyrios Kyrtzidis | c8af910 | 2012-01-26 04:19:04 +0000 | [diff] [blame] | 77 | std::string RewriteFilename(const std::string &Filename, int &fd) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 78 | SmallString<128> Path; |
Argyrios Kyrtzidis | 61d679a | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 79 | Path = llvm::sys::path::filename(Filename); |
| 80 | Path += "-%%%%%%%%"; |
| 81 | Path += llvm::sys::path::extension(Filename); |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 82 | SmallString<128> NewPath; |
Argyrios Kyrtzidis | c8af910 | 2012-01-26 04:19:04 +0000 | [diff] [blame] | 83 | llvm::sys::fs::unique_file(Path.str(), fd, NewPath); |
Argyrios Kyrtzidis | 61d679a | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 84 | return NewPath.str(); |
| 85 | } |
| 86 | }; |
Benjamin Kramer | 79ba2a6 | 2010-10-22 16:48:22 +0000 | [diff] [blame] | 87 | } // end anonymous namespace |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 88 | |
| 89 | bool FixItAction::BeginSourceFileAction(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 90 | StringRef Filename) { |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 91 | const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts(); |
| 92 | if (!FEOpts.FixItSuffix.empty()) { |
Nick Lewycky | 1450f26 | 2010-08-13 17:31:00 +0000 | [diff] [blame] | 93 | FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix, |
| 94 | FEOpts.FixWhatYouCan)); |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 95 | } else { |
Nick Lewycky | 96872c4 | 2010-08-15 16:47:39 +0000 | [diff] [blame] | 96 | FixItOpts.reset(new FixItRewriteInPlace); |
| 97 | FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan; |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 98 | } |
| 99 | Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(), |
Nick Lewycky | 1450f26 | 2010-08-13 17:31:00 +0000 | [diff] [blame] | 100 | CI.getLangOpts(), FixItOpts.get())); |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 101 | return true; |
| 102 | } |
| 103 | |
| 104 | void FixItAction::EndSourceFileAction() { |
| 105 | // Otherwise rewrite all files. |
| 106 | Rewriter->WriteFixedFiles(); |
| 107 | } |
| 108 | |
Argyrios Kyrtzidis | 61d679a | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 109 | bool FixItRecompile::BeginInvocation(CompilerInstance &CI) { |
| 110 | |
| 111 | std::vector<std::pair<std::string, std::string> > RewrittenFiles; |
| 112 | bool err = false; |
| 113 | { |
| 114 | const FrontendOptions &FEOpts = CI.getFrontendOpts(); |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 115 | OwningPtr<FrontendAction> FixAction(new SyntaxOnlyAction()); |
Argyrios Kyrtzidis | 30ae84c | 2012-01-27 01:00:47 +0000 | [diff] [blame] | 116 | if (FixAction->BeginSourceFile(CI, FEOpts.Inputs[0])) { |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 117 | OwningPtr<FixItOptions> FixItOpts; |
Argyrios Kyrtzidis | 30ae84c | 2012-01-27 01:00:47 +0000 | [diff] [blame] | 118 | if (FEOpts.FixToTemporaries) |
| 119 | FixItOpts.reset(new FixItRewriteToTemp()); |
| 120 | else |
| 121 | FixItOpts.reset(new FixItRewriteInPlace()); |
| 122 | FixItOpts->Silent = true; |
| 123 | FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan; |
| 124 | FixItOpts->FixOnlyWarnings = FEOpts.FixOnlyWarnings; |
| 125 | FixItRewriter Rewriter(CI.getDiagnostics(), CI.getSourceManager(), |
| 126 | CI.getLangOpts(), FixItOpts.get()); |
| 127 | FixAction->Execute(); |
Argyrios Kyrtzidis | 61d679a | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 128 | |
Argyrios Kyrtzidis | 30ae84c | 2012-01-27 01:00:47 +0000 | [diff] [blame] | 129 | err = Rewriter.WriteFixedFiles(&RewrittenFiles); |
| 130 | |
| 131 | FixAction->EndSourceFile(); |
| 132 | CI.setSourceManager(0); |
| 133 | CI.setFileManager(0); |
| 134 | } else { |
| 135 | err = true; |
| 136 | } |
Argyrios Kyrtzidis | 61d679a | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 137 | } |
| 138 | if (err) |
| 139 | return false; |
| 140 | CI.getDiagnosticClient().clear(); |
Argyrios Kyrtzidis | 97a9cf3 | 2012-01-27 06:15:37 +0000 | [diff] [blame] | 141 | CI.getDiagnostics().Reset(); |
Argyrios Kyrtzidis | 61d679a | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 142 | |
| 143 | PreprocessorOptions &PPOpts = CI.getPreprocessorOpts(); |
| 144 | PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(), |
| 145 | RewrittenFiles.begin(), RewrittenFiles.end()); |
| 146 | PPOpts.RemappedFilesKeepOriginalName = false; |
| 147 | |
| 148 | return true; |
| 149 | } |
| 150 | |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 151 | //===----------------------------------------------------------------------===// |
| 152 | // Preprocessor Actions |
| 153 | //===----------------------------------------------------------------------===// |
| 154 | |
| 155 | ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 156 | StringRef InFile) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 157 | if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp")) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame^] | 158 | if (CI.getLangOpts().ObjCRuntime.isNonFragile()) |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 159 | return CreateModernObjCRewriter(InFile, OS, |
| 160 | CI.getDiagnostics(), CI.getLangOpts(), |
| 161 | CI.getDiagnosticOpts().NoRewriteMacros); |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 162 | return CreateObjCRewriter(InFile, OS, |
| 163 | CI.getDiagnostics(), CI.getLangOpts(), |
| 164 | CI.getDiagnosticOpts().NoRewriteMacros); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 165 | } |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | void RewriteMacrosAction::ExecuteAction() { |
| 170 | CompilerInstance &CI = getCompilerInstance(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 171 | raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile()); |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 172 | if (!OS) return; |
| 173 | |
| 174 | RewriteMacrosInInput(CI.getPreprocessor(), OS); |
| 175 | } |
| 176 | |
| 177 | void RewriteTestAction::ExecuteAction() { |
| 178 | CompilerInstance &CI = getCompilerInstance(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 179 | raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile()); |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 180 | if (!OS) return; |
| 181 | |
| 182 | DoRewriteTest(CI.getPreprocessor(), OS); |
| 183 | } |
David Blaikie | 8c0b378 | 2012-06-06 18:52:13 +0000 | [diff] [blame] | 184 | |
| 185 | void RewriteIncludesAction::ExecuteAction() { |
| 186 | CompilerInstance &CI = getCompilerInstance(); |
| 187 | raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile()); |
| 188 | if (!OS) return; |
| 189 | |
| 190 | RewriteIncludesInInput(CI.getPreprocessor(), OS, |
| 191 | CI.getPreprocessorOutputOpts()); |
| 192 | } |