Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 1 | //===--- FixItRewriter.cpp - Fix-It Rewriter Diagnostic Client --*- C++ -*-===// |
| 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 | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 15 | |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 16 | #include "clang/Rewrite/FixItRewriter.h" |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame^] | 17 | #include "clang/Edit/Commit.h" |
| 18 | #include "clang/Edit/EditsReceiver.h" |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 19 | #include "clang/Basic/FileManager.h" |
| 20 | #include "clang/Basic/SourceLocation.h" |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/FrontendDiagnostic.h" |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 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" |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/OwningPtr.h" |
Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 26 | #include <cstdio> |
| 27 | |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 30 | FixItRewriter::FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr, |
Nick Lewycky | ba5f6ec | 2010-04-24 01:30:46 +0000 | [diff] [blame] | 31 | const LangOptions &LangOpts, |
Nick Lewycky | 1450f26 | 2010-08-13 17:31:00 +0000 | [diff] [blame] | 32 | FixItOptions *FixItOpts) |
Nick Lewycky | ba5f6ec | 2010-04-24 01:30:46 +0000 | [diff] [blame] | 33 | : Diags(Diags), |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame^] | 34 | Editor(SourceMgr, LangOpts), |
Nick Lewycky | ba5f6ec | 2010-04-24 01:30:46 +0000 | [diff] [blame] | 35 | Rewrite(SourceMgr, LangOpts), |
Nick Lewycky | 1450f26 | 2010-08-13 17:31:00 +0000 | [diff] [blame] | 36 | FixItOpts(FixItOpts), |
Argyrios Kyrtzidis | 61d679a | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 37 | NumFailures(0), |
| 38 | PrevDiagSilenced(false) { |
| 39 | OwnsClient = Diags.ownsClient(); |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 40 | Client = Diags.takeClient(); |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 41 | Diags.setClient(this); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | FixItRewriter::~FixItRewriter() { |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 45 | Diags.takeClient(); |
Argyrios Kyrtzidis | 61d679a | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 46 | Diags.setClient(Client, OwnsClient); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 49 | bool FixItRewriter::WriteFixedFile(FileID ID, raw_ostream &OS) { |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 50 | const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID); |
| 51 | if (!RewriteBuf) return true; |
Nick Lewycky | 0ade808 | 2010-04-16 18:49:45 +0000 | [diff] [blame] | 52 | RewriteBuf->write(OS); |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 53 | OS.flush(); |
| 54 | return false; |
| 55 | } |
| 56 | |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame^] | 57 | namespace { |
| 58 | |
| 59 | class RewritesReceiver : public edit::EditsReceiver { |
| 60 | Rewriter &Rewrite; |
| 61 | |
| 62 | public: |
| 63 | RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) { } |
| 64 | |
| 65 | virtual void insert(SourceLocation loc, StringRef text) { |
| 66 | Rewrite.InsertText(loc, text); |
| 67 | } |
| 68 | virtual void replace(CharSourceRange range, StringRef text) { |
| 69 | Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text); |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | } |
| 74 | |
Argyrios Kyrtzidis | 61d679a | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 75 | bool FixItRewriter::WriteFixedFiles( |
| 76 | std::vector<std::pair<std::string, std::string> > *RewrittenFiles) { |
Nick Lewycky | 96872c4 | 2010-08-15 16:47:39 +0000 | [diff] [blame] | 77 | if (NumFailures > 0 && !FixItOpts->FixWhatYouCan) { |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 78 | Diag(FullSourceLoc(), diag::warn_fixit_no_changes); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame^] | 82 | RewritesReceiver Rec(Rewrite); |
| 83 | Editor.applyRewrites(Rec); |
| 84 | |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 85 | for (iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) { |
| 86 | const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first); |
Argyrios Kyrtzidis | c8af910 | 2012-01-26 04:19:04 +0000 | [diff] [blame] | 87 | int fd; |
| 88 | std::string Filename = FixItOpts->RewriteFilename(Entry->getName(), fd); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 89 | std::string Err; |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 90 | OwningPtr<llvm::raw_fd_ostream> OS; |
Argyrios Kyrtzidis | c8af910 | 2012-01-26 04:19:04 +0000 | [diff] [blame] | 91 | if (fd != -1) { |
| 92 | OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true)); |
| 93 | } else { |
| 94 | OS.reset(new llvm::raw_fd_ostream(Filename.c_str(), Err, |
| 95 | llvm::raw_fd_ostream::F_Binary)); |
| 96 | } |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 97 | if (!Err.empty()) { |
| 98 | Diags.Report(clang::diag::err_fe_unable_to_open_output) |
Nick Lewycky | ba5f6ec | 2010-04-24 01:30:46 +0000 | [diff] [blame] | 99 | << Filename << Err; |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 100 | continue; |
| 101 | } |
| 102 | RewriteBuffer &RewriteBuf = I->second; |
Argyrios Kyrtzidis | c8af910 | 2012-01-26 04:19:04 +0000 | [diff] [blame] | 103 | RewriteBuf.write(*OS); |
| 104 | OS->flush(); |
Argyrios Kyrtzidis | 61d679a | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 105 | |
| 106 | if (RewrittenFiles) |
| 107 | RewrittenFiles->push_back(std::make_pair(Entry->getName(), Filename)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | } |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 109 | |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 110 | return false; |
| 111 | } |
| 112 | |
| 113 | bool FixItRewriter::IncludeInDiagnosticCounts() const { |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 114 | return Client ? Client->IncludeInDiagnosticCounts() : true; |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 115 | } |
| 116 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 117 | void FixItRewriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
David Blaikie | 40847cf | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 118 | const Diagnostic &Info) { |
Argyrios Kyrtzidis | dea22a3 | 2010-11-18 21:13:54 +0000 | [diff] [blame] | 119 | // Default implementation (Warnings/errors count). |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 120 | DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info); |
Argyrios Kyrtzidis | dea22a3 | 2010-11-18 21:13:54 +0000 | [diff] [blame] | 121 | |
Argyrios Kyrtzidis | 61d679a | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 122 | if (!FixItOpts->Silent || |
| 123 | DiagLevel >= DiagnosticsEngine::Error || |
| 124 | (DiagLevel == DiagnosticsEngine::Note && !PrevDiagSilenced) || |
| 125 | (DiagLevel > DiagnosticsEngine::Note && Info.getNumFixItHints())) { |
| 126 | Client->HandleDiagnostic(DiagLevel, Info); |
| 127 | PrevDiagSilenced = false; |
| 128 | } else { |
| 129 | PrevDiagSilenced = true; |
| 130 | } |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 131 | |
Nick Lewycky | ba5f6ec | 2010-04-24 01:30:46 +0000 | [diff] [blame] | 132 | // Skip over any diagnostics that are ignored or notes. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 133 | if (DiagLevel <= DiagnosticsEngine::Note) |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 134 | return; |
Argyrios Kyrtzidis | 61d679a | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 135 | // Skip over errors if we are only fixing warnings. |
| 136 | if (DiagLevel >= DiagnosticsEngine::Error && FixItOpts->FixOnlyWarnings) { |
| 137 | ++NumFailures; |
| 138 | return; |
| 139 | } |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 140 | |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 141 | // Make sure that we can perform all of the modifications we |
| 142 | // in this diagnostic. |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame^] | 143 | edit::Commit commit(Editor); |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 144 | for (unsigned Idx = 0, Last = Info.getNumFixItHints(); |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 145 | Idx < Last; ++Idx) { |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 146 | const FixItHint &Hint = Info.getFixItHint(Idx); |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame^] | 147 | |
| 148 | if (Hint.CodeToInsert.empty()) { |
| 149 | if (Hint.InsertFromRange.isValid()) |
| 150 | commit.insertFromRange(Hint.RemoveRange.getBegin(), |
| 151 | Hint.InsertFromRange, /*afterToken=*/false, |
| 152 | Hint.BeforePreviousInsertions); |
| 153 | else |
| 154 | commit.remove(Hint.RemoveRange); |
| 155 | } else { |
| 156 | if (Hint.RemoveRange.isTokenRange() || |
| 157 | Hint.RemoveRange.getBegin() != Hint.RemoveRange.getEnd()) |
| 158 | commit.replace(Hint.RemoveRange, Hint.CodeToInsert); |
| 159 | else |
| 160 | commit.insert(Hint.RemoveRange.getBegin(), Hint.CodeToInsert, |
| 161 | /*afterToken=*/false, Hint.BeforePreviousInsertions); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 162 | } |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 163 | } |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame^] | 164 | bool CanRewrite = Info.getNumFixItHints() > 0 && commit.isCommitable(); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 165 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | if (!CanRewrite) { |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 167 | if (Info.getNumFixItHints() > 0) |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 168 | Diag(Info.getLocation(), diag::note_fixit_in_macro); |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 169 | |
| 170 | // If this was an error, refuse to perform any rewriting. |
Argyrios Kyrtzidis | 61d679a | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 171 | if (DiagLevel >= DiagnosticsEngine::Error) { |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 172 | if (++NumFailures == 1) |
| 173 | Diag(Info.getLocation(), diag::note_fixit_unfixed_error); |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 174 | } |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 175 | return; |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 176 | } |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame^] | 177 | |
| 178 | if (!Editor.commit(commit)) { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 179 | ++NumFailures; |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 180 | Diag(Info.getLocation(), diag::note_fixit_failed); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | Diag(Info.getLocation(), diag::note_fixit_applied); |
| 185 | } |
| 186 | |
| 187 | /// \brief Emit a diagnostic via the adapted diagnostic client. |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 188 | void FixItRewriter::Diag(SourceLocation Loc, unsigned DiagID) { |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 189 | // When producing this diagnostic, we temporarily bypass ourselves, |
| 190 | // clear out any current diagnostic, and let the downstream client |
| 191 | // format the diagnostic. |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 192 | Diags.takeClient(); |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 193 | Diags.setClient(Client); |
| 194 | Diags.Clear(); |
| 195 | Diags.Report(Loc, DiagID); |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 196 | Diags.takeClient(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | Diags.setClient(this); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 198 | } |
Nick Lewycky | ba5f6ec | 2010-04-24 01:30:46 +0000 | [diff] [blame] | 199 | |
Douglas Gregor | aee526e | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 200 | DiagnosticConsumer *FixItRewriter::clone(DiagnosticsEngine &Diags) const { |
| 201 | return new FixItRewriter(Diags, Diags.getSourceManager(), |
| 202 | Rewrite.getLangOpts(), FixItOpts); |
| 203 | } |
| 204 | |
Nick Lewycky | 1450f26 | 2010-08-13 17:31:00 +0000 | [diff] [blame] | 205 | FixItOptions::~FixItOptions() {} |