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