Eugene Zelenko | db914a4 | 2018-03-27 00:01:49 +0000 | [diff] [blame] | 1 | //===- FixItRewriter.cpp - Fix-It Rewriter Diagnostic Client --------------===// |
Douglas Gregor | 578dae5 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 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 | // This is a diagnostic client adaptor that performs rewrites as |
| 11 | // suggested by code modification hints attached to diagnostics. It |
| 12 | // then forwards any diagnostics to the adapted diagnostic client. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 068913e | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 15 | |
Ted Kremenek | cdf8149 | 2012-09-01 05:09:24 +0000 | [diff] [blame] | 16 | #include "clang/Rewrite/Frontend/FixItRewriter.h" |
Eugene Zelenko | db914a4 | 2018-03-27 00:01:49 +0000 | [diff] [blame] | 17 | #include "clang/Basic/Diagnostic.h" |
Nick Lewycky | a1e20de | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 18 | #include "clang/Basic/FileManager.h" |
Eugene Zelenko | db914a4 | 2018-03-27 00:01:49 +0000 | [diff] [blame] | 19 | #include "clang/Basic/LLVM.h" |
Nick Lewycky | a1e20de | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceLocation.h" |
Douglas Gregor | 068913e | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceManager.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Edit/Commit.h" |
| 23 | #include "clang/Edit/EditsReceiver.h" |
Douglas Gregor | a42bd84 | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/FrontendDiagnostic.h" |
Eugene Zelenko | db914a4 | 2018-03-27 00:01:49 +0000 | [diff] [blame] | 25 | #include "clang/Rewrite/Core/RewriteBuffer.h" |
| 26 | #include "clang/Rewrite/Core/Rewriter.h" |
| 27 | #include "llvm/ADT/StringRef.h" |
| 28 | #include "llvm/Support/FileSystem.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Torok Edwin | db71492 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 30 | #include <cstdio> |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 31 | #include <memory> |
Eugene Zelenko | db914a4 | 2018-03-27 00:01:49 +0000 | [diff] [blame] | 32 | #include <string> |
| 33 | #include <system_error> |
| 34 | #include <utility> |
Torok Edwin | db71492 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 35 | |
Douglas Gregor | 578dae5 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 36 | using namespace clang; |
| 37 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 38 | FixItRewriter::FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr, |
Nick Lewycky | 784fad7 | 2010-04-24 01:30:46 +0000 | [diff] [blame] | 39 | const LangOptions &LangOpts, |
Nick Lewycky | 078a5e2 | 2010-08-13 17:31:00 +0000 | [diff] [blame] | 40 | FixItOptions *FixItOpts) |
Eugene Zelenko | db914a4 | 2018-03-27 00:01:49 +0000 | [diff] [blame] | 41 | : Diags(Diags), Editor(SourceMgr, LangOpts), Rewrite(SourceMgr, LangOpts), |
| 42 | FixItOpts(FixItOpts) { |
Alexander Kornienko | 41c247a | 2014-11-17 23:46:02 +0000 | [diff] [blame] | 43 | Owner = Diags.takeClient(); |
| 44 | Client = Diags.getClient(); |
| 45 | Diags.setClient(this, false); |
Douglas Gregor | 578dae5 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | FixItRewriter::~FixItRewriter() { |
Alexander Kornienko | 41c247a | 2014-11-17 23:46:02 +0000 | [diff] [blame] | 49 | Diags.setClient(Client, Owner.release() != nullptr); |
Douglas Gregor | 578dae5 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 52 | bool FixItRewriter::WriteFixedFile(FileID ID, raw_ostream &OS) { |
Nick Lewycky | a1e20de | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 53 | const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID); |
| 54 | if (!RewriteBuf) return true; |
Nick Lewycky | 40884c0 | 2010-04-16 18:49:45 +0000 | [diff] [blame] | 55 | RewriteBuf->write(OS); |
Nick Lewycky | a1e20de | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 56 | OS.flush(); |
| 57 | return false; |
| 58 | } |
| 59 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 60 | namespace { |
| 61 | |
| 62 | class RewritesReceiver : public edit::EditsReceiver { |
| 63 | Rewriter &Rewrite; |
| 64 | |
| 65 | public: |
Eugene Zelenko | db914a4 | 2018-03-27 00:01:49 +0000 | [diff] [blame] | 66 | RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) {} |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 67 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 68 | void insert(SourceLocation loc, StringRef text) override { |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 69 | Rewrite.InsertText(loc, text); |
| 70 | } |
Eugene Zelenko | db914a4 | 2018-03-27 00:01:49 +0000 | [diff] [blame] | 71 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 72 | void replace(CharSourceRange range, StringRef text) override { |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 73 | Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text); |
| 74 | } |
| 75 | }; |
| 76 | |
Eugene Zelenko | db914a4 | 2018-03-27 00:01:49 +0000 | [diff] [blame] | 77 | } // namespace |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 78 | |
Argyrios Kyrtzidis | 24e9aff | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 79 | bool FixItRewriter::WriteFixedFiles( |
Eugene Zelenko | db914a4 | 2018-03-27 00:01:49 +0000 | [diff] [blame] | 80 | std::vector<std::pair<std::string, std::string>> *RewrittenFiles) { |
Nick Lewycky | 53f1042 | 2010-08-15 16:47:39 +0000 | [diff] [blame] | 81 | if (NumFailures > 0 && !FixItOpts->FixWhatYouCan) { |
Douglas Gregor | a42bd84 | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 82 | Diag(FullSourceLoc(), diag::warn_fixit_no_changes); |
Douglas Gregor | 578dae5 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 83 | return true; |
| 84 | } |
| 85 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 86 | RewritesReceiver Rec(Rewrite); |
| 87 | Editor.applyRewrites(Rec); |
| 88 | |
Reid Kleckner | 3df5dd4 | 2015-06-17 17:47:30 +0000 | [diff] [blame] | 89 | if (FixItOpts->InPlace) { |
| 90 | // Overwriting open files on Windows is tricky, but the rewriter can do it |
| 91 | // for us. |
| 92 | Rewrite.overwriteChangedFiles(); |
| 93 | return false; |
| 94 | } |
| 95 | |
Nick Lewycky | a1e20de | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 96 | for (iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) { |
| 97 | const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first); |
Argyrios Kyrtzidis | 623e877 | 2012-01-26 04:19:04 +0000 | [diff] [blame] | 98 | int fd; |
| 99 | std::string Filename = FixItOpts->RewriteFilename(Entry->getName(), fd); |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 100 | std::error_code EC; |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 101 | std::unique_ptr<llvm::raw_fd_ostream> OS; |
Argyrios Kyrtzidis | 623e877 | 2012-01-26 04:19:04 +0000 | [diff] [blame] | 102 | if (fd != -1) { |
| 103 | OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true)); |
| 104 | } else { |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 105 | OS.reset(new llvm::raw_fd_ostream(Filename, EC, llvm::sys::fs::F_None)); |
Argyrios Kyrtzidis | 623e877 | 2012-01-26 04:19:04 +0000 | [diff] [blame] | 106 | } |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 107 | if (EC) { |
| 108 | Diags.Report(clang::diag::err_fe_unable_to_open_output) << Filename |
| 109 | << EC.message(); |
Nick Lewycky | a1e20de | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 110 | continue; |
| 111 | } |
| 112 | RewriteBuffer &RewriteBuf = I->second; |
Argyrios Kyrtzidis | 623e877 | 2012-01-26 04:19:04 +0000 | [diff] [blame] | 113 | RewriteBuf.write(*OS); |
| 114 | OS->flush(); |
Argyrios Kyrtzidis | 24e9aff | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 115 | |
| 116 | if (RewrittenFiles) |
| 117 | RewrittenFiles->push_back(std::make_pair(Entry->getName(), Filename)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | } |
Douglas Gregor | 578dae5 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 119 | |
Douglas Gregor | 578dae5 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 120 | return false; |
| 121 | } |
| 122 | |
| 123 | bool FixItRewriter::IncludeInDiagnosticCounts() const { |
Nick Lewycky | a1e20de | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 124 | return Client ? Client->IncludeInDiagnosticCounts() : true; |
Douglas Gregor | 578dae5 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 125 | } |
| 126 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 127 | void FixItRewriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
David Blaikie | b578432 | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 128 | const Diagnostic &Info) { |
Argyrios Kyrtzidis | 6d35b5a | 2010-11-18 21:13:54 +0000 | [diff] [blame] | 129 | // Default implementation (Warnings/errors count). |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 130 | DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info); |
Argyrios Kyrtzidis | 6d35b5a | 2010-11-18 21:13:54 +0000 | [diff] [blame] | 131 | |
Argyrios Kyrtzidis | 24e9aff | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 132 | if (!FixItOpts->Silent || |
| 133 | DiagLevel >= DiagnosticsEngine::Error || |
| 134 | (DiagLevel == DiagnosticsEngine::Note && !PrevDiagSilenced) || |
| 135 | (DiagLevel > DiagnosticsEngine::Note && Info.getNumFixItHints())) { |
| 136 | Client->HandleDiagnostic(DiagLevel, Info); |
| 137 | PrevDiagSilenced = false; |
| 138 | } else { |
| 139 | PrevDiagSilenced = true; |
| 140 | } |
Douglas Gregor | 9c0d38a | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 141 | |
Nick Lewycky | 784fad7 | 2010-04-24 01:30:46 +0000 | [diff] [blame] | 142 | // Skip over any diagnostics that are ignored or notes. |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 143 | if (DiagLevel <= DiagnosticsEngine::Note) |
Douglas Gregor | 9c0d38a | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 144 | return; |
Argyrios Kyrtzidis | 24e9aff | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 145 | // Skip over errors if we are only fixing warnings. |
| 146 | if (DiagLevel >= DiagnosticsEngine::Error && FixItOpts->FixOnlyWarnings) { |
| 147 | ++NumFailures; |
| 148 | return; |
| 149 | } |
Douglas Gregor | 9c0d38a | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 150 | |
Douglas Gregor | 578dae5 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 151 | // Make sure that we can perform all of the modifications we |
| 152 | // in this diagnostic. |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 153 | edit::Commit commit(Editor); |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 154 | for (unsigned Idx = 0, Last = Info.getNumFixItHints(); |
Douglas Gregor | 068913e | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 155 | Idx < Last; ++Idx) { |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 156 | const FixItHint &Hint = Info.getFixItHint(Idx); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 157 | |
| 158 | if (Hint.CodeToInsert.empty()) { |
| 159 | if (Hint.InsertFromRange.isValid()) |
| 160 | commit.insertFromRange(Hint.RemoveRange.getBegin(), |
| 161 | Hint.InsertFromRange, /*afterToken=*/false, |
| 162 | Hint.BeforePreviousInsertions); |
| 163 | else |
| 164 | commit.remove(Hint.RemoveRange); |
| 165 | } else { |
| 166 | if (Hint.RemoveRange.isTokenRange() || |
| 167 | Hint.RemoveRange.getBegin() != Hint.RemoveRange.getEnd()) |
| 168 | commit.replace(Hint.RemoveRange, Hint.CodeToInsert); |
| 169 | else |
| 170 | commit.insert(Hint.RemoveRange.getBegin(), Hint.CodeToInsert, |
| 171 | /*afterToken=*/false, Hint.BeforePreviousInsertions); |
Douglas Gregor | 578dae5 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 172 | } |
Douglas Gregor | 578dae5 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 173 | } |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 174 | bool CanRewrite = Info.getNumFixItHints() > 0 && commit.isCommitable(); |
Douglas Gregor | 578dae5 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 175 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | if (!CanRewrite) { |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 177 | if (Info.getNumFixItHints() > 0) |
Douglas Gregor | a42bd84 | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 178 | Diag(Info.getLocation(), diag::note_fixit_in_macro); |
Douglas Gregor | 068913e | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 179 | |
| 180 | // If this was an error, refuse to perform any rewriting. |
Argyrios Kyrtzidis | 24e9aff | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 181 | if (DiagLevel >= DiagnosticsEngine::Error) { |
Douglas Gregor | a42bd84 | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 182 | if (++NumFailures == 1) |
| 183 | Diag(Info.getLocation(), diag::note_fixit_unfixed_error); |
Douglas Gregor | 068913e | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 184 | } |
Douglas Gregor | 578dae5 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 185 | return; |
Douglas Gregor | 068913e | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 186 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 187 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 188 | if (!Editor.commit(commit)) { |
Douglas Gregor | 578dae5 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 189 | ++NumFailures; |
Douglas Gregor | a42bd84 | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 190 | Diag(Info.getLocation(), diag::note_fixit_failed); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | Diag(Info.getLocation(), diag::note_fixit_applied); |
| 195 | } |
| 196 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 197 | /// Emit a diagnostic via the adapted diagnostic client. |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 198 | void FixItRewriter::Diag(SourceLocation Loc, unsigned DiagID) { |
Douglas Gregor | a42bd84 | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 199 | // When producing this diagnostic, we temporarily bypass ourselves, |
| 200 | // clear out any current diagnostic, and let the downstream client |
| 201 | // format the diagnostic. |
Alexander Kornienko | 41c247a | 2014-11-17 23:46:02 +0000 | [diff] [blame] | 202 | Diags.setClient(Client, false); |
Douglas Gregor | a42bd84 | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 203 | Diags.Clear(); |
| 204 | Diags.Report(Loc, DiagID); |
Alexander Kornienko | 41c247a | 2014-11-17 23:46:02 +0000 | [diff] [blame] | 205 | Diags.setClient(this, false); |
Douglas Gregor | 578dae5 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 206 | } |
Nick Lewycky | 784fad7 | 2010-04-24 01:30:46 +0000 | [diff] [blame] | 207 | |
Eugene Zelenko | db914a4 | 2018-03-27 00:01:49 +0000 | [diff] [blame] | 208 | FixItOptions::~FixItOptions() = default; |