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" |
| 22 | #include "llvm/System/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 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 28 | FixItRewriter::FixItRewriter(Diagnostic &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), |
Nick Lewycky | ba5f6ec | 2010-04-24 01:30:46 +0000 | [diff] [blame] | 34 | NumFailures(0) { |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 35 | Client = Diags.getClient(); |
| 36 | Diags.setClient(this); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | FixItRewriter::~FixItRewriter() { |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 40 | Diags.setClient(Client); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 43 | bool FixItRewriter::WriteFixedFile(FileID ID, llvm::raw_ostream &OS) { |
| 44 | const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID); |
| 45 | if (!RewriteBuf) return true; |
Nick Lewycky | 0ade808 | 2010-04-16 18:49:45 +0000 | [diff] [blame] | 46 | RewriteBuf->write(OS); |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 47 | OS.flush(); |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | bool FixItRewriter::WriteFixedFiles() { |
Nick Lewycky | 1450f26 | 2010-08-13 17:31:00 +0000 | [diff] [blame^] | 52 | if (NumFailures > 0 && !FixItOpts->FixWhatYouCan) { |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 53 | Diag(FullSourceLoc(), diag::warn_fixit_no_changes); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 54 | return true; |
| 55 | } |
| 56 | |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 57 | for (iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) { |
| 58 | const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first); |
Nick Lewycky | ba5f6ec | 2010-04-24 01:30:46 +0000 | [diff] [blame] | 59 | std::string Filename = Entry->getName(); |
Nick Lewycky | 1450f26 | 2010-08-13 17:31:00 +0000 | [diff] [blame^] | 60 | if (FixItOpts) |
| 61 | Filename = FixItOpts->RewriteFilename(Filename); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 62 | std::string Err; |
Nick Lewycky | ba5f6ec | 2010-04-24 01:30:46 +0000 | [diff] [blame] | 63 | llvm::raw_fd_ostream OS(Filename.c_str(), Err, |
| 64 | llvm::raw_fd_ostream::F_Binary); |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 65 | if (!Err.empty()) { |
| 66 | Diags.Report(clang::diag::err_fe_unable_to_open_output) |
Nick Lewycky | ba5f6ec | 2010-04-24 01:30:46 +0000 | [diff] [blame] | 67 | << Filename << Err; |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 68 | continue; |
| 69 | } |
| 70 | RewriteBuffer &RewriteBuf = I->second; |
Nick Lewycky | 89fe019 | 2010-04-24 22:31:36 +0000 | [diff] [blame] | 71 | RewriteBuf.write(OS); |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 72 | OS.flush(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | } |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 74 | |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 75 | return false; |
| 76 | } |
| 77 | |
| 78 | bool FixItRewriter::IncludeInDiagnosticCounts() const { |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 79 | return Client ? Client->IncludeInDiagnosticCounts() : true; |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void FixItRewriter::HandleDiagnostic(Diagnostic::Level DiagLevel, |
| 83 | const DiagnosticInfo &Info) { |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 84 | Client->HandleDiagnostic(DiagLevel, Info); |
| 85 | |
Nick Lewycky | ba5f6ec | 2010-04-24 01:30:46 +0000 | [diff] [blame] | 86 | // Skip over any diagnostics that are ignored or notes. |
| 87 | if (DiagLevel <= Diagnostic::Note) |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 88 | return; |
| 89 | |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 90 | // Make sure that we can perform all of the modifications we |
| 91 | // in this diagnostic. |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 92 | bool CanRewrite = Info.getNumFixItHints() > 0; |
| 93 | for (unsigned Idx = 0, Last = Info.getNumFixItHints(); |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 94 | Idx < Last; ++Idx) { |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 95 | const FixItHint &Hint = Info.getFixItHint(Idx); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 96 | if (Hint.RemoveRange.isValid() && |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 97 | Rewrite.getRangeSize(Hint.RemoveRange) == -1) { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 98 | CanRewrite = false; |
| 99 | break; |
| 100 | } |
| 101 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | if (Hint.InsertionLoc.isValid() && |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 103 | !Rewrite.isRewritable(Hint.InsertionLoc)) { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 104 | CanRewrite = false; |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | if (!CanRewrite) { |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 110 | if (Info.getNumFixItHints() > 0) |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 111 | Diag(Info.getLocation(), diag::note_fixit_in_macro); |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 112 | |
| 113 | // If this was an error, refuse to perform any rewriting. |
| 114 | if (DiagLevel == Diagnostic::Error || DiagLevel == Diagnostic::Fatal) { |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 115 | if (++NumFailures == 1) |
| 116 | Diag(Info.getLocation(), diag::note_fixit_unfixed_error); |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 117 | } |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 118 | return; |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 119 | } |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 120 | |
| 121 | bool Failed = false; |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 122 | for (unsigned Idx = 0, Last = Info.getNumFixItHints(); |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 123 | Idx < Last; ++Idx) { |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 124 | const FixItHint &Hint = Info.getFixItHint(Idx); |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 125 | if (!Hint.RemoveRange.isValid()) { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 126 | // We're adding code. |
Daniel Dunbar | 44ba7bf | 2009-08-19 20:32:38 +0000 | [diff] [blame] | 127 | if (Rewrite.InsertTextBefore(Hint.InsertionLoc, Hint.CodeToInsert)) |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 128 | Failed = true; |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 129 | continue; |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 130 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 132 | if (Hint.CodeToInsert.empty()) { |
| 133 | // We're removing code. |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 134 | if (Rewrite.RemoveText(Hint.RemoveRange.getBegin(), |
| 135 | Rewrite.getRangeSize(Hint.RemoveRange))) |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 136 | Failed = true; |
| 137 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 140 | // We're replacing code. |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 141 | if (Rewrite.ReplaceText(Hint.RemoveRange.getBegin(), |
| 142 | Rewrite.getRangeSize(Hint.RemoveRange), |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 143 | Hint.CodeToInsert)) |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 144 | Failed = true; |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 147 | if (Failed) { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 148 | ++NumFailures; |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 149 | Diag(Info.getLocation(), diag::note_fixit_failed); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | Diag(Info.getLocation(), diag::note_fixit_applied); |
| 154 | } |
| 155 | |
| 156 | /// \brief Emit a diagnostic via the adapted diagnostic client. |
| 157 | void FixItRewriter::Diag(FullSourceLoc Loc, unsigned DiagID) { |
| 158 | // When producing this diagnostic, we temporarily bypass ourselves, |
| 159 | // clear out any current diagnostic, and let the downstream client |
| 160 | // format the diagnostic. |
| 161 | Diags.setClient(Client); |
| 162 | Diags.Clear(); |
| 163 | Diags.Report(Loc, DiagID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | Diags.setClient(this); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 165 | } |
Nick Lewycky | ba5f6ec | 2010-04-24 01:30:46 +0000 | [diff] [blame] | 166 | |
Nick Lewycky | 1450f26 | 2010-08-13 17:31:00 +0000 | [diff] [blame^] | 167 | FixItOptions::~FixItOptions() {} |