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 | |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/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, |
| 29 | const LangOptions &LangOpts) |
| 30 | : Diags(Diags), Rewrite(SourceMgr, LangOpts), NumFailures(0) { |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 31 | Client = Diags.getClient(); |
| 32 | Diags.setClient(this); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | FixItRewriter::~FixItRewriter() { |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 36 | Diags.setClient(Client); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 39 | bool FixItRewriter::WriteFixedFile(FileID ID, llvm::raw_ostream &OS) { |
| 40 | const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID); |
| 41 | if (!RewriteBuf) return true; |
Nick Lewycky | 0ade808 | 2010-04-16 18:49:45 +0000 | [diff] [blame] | 42 | RewriteBuf->write(OS); |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 43 | OS.flush(); |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | bool FixItRewriter::WriteFixedFiles() { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 48 | if (NumFailures > 0) { |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 49 | Diag(FullSourceLoc(), diag::warn_fixit_no_changes); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 50 | return true; |
| 51 | } |
| 52 | |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 53 | for (iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) { |
| 54 | const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first); |
| 55 | llvm::sys::Path Path(Entry->getName()); |
Douglas Gregor | 2610348 | 2009-04-02 03:14:12 +0000 | [diff] [blame] | 56 | std::string Suffix = Path.getSuffix(); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 57 | Path.eraseSuffix(); |
Douglas Gregor | 2610348 | 2009-04-02 03:14:12 +0000 | [diff] [blame] | 58 | Path.appendSuffix("fixit." + Suffix); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 59 | std::string Err; |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 60 | llvm::raw_fd_ostream OS(Path.c_str(), Err, llvm::raw_fd_ostream::F_Binary); |
| 61 | if (!Err.empty()) { |
| 62 | Diags.Report(clang::diag::err_fe_unable_to_open_output) |
| 63 | << Path.c_str() << Err; |
| 64 | continue; |
| 65 | } |
| 66 | RewriteBuffer &RewriteBuf = I->second; |
| 67 | OS << std::string(RewriteBuf.begin(), RewriteBuf.end()); |
| 68 | OS.flush(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | } |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 70 | |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 71 | return false; |
| 72 | } |
| 73 | |
| 74 | bool FixItRewriter::IncludeInDiagnosticCounts() const { |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 75 | return Client ? Client->IncludeInDiagnosticCounts() : true; |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void FixItRewriter::HandleDiagnostic(Diagnostic::Level DiagLevel, |
| 79 | const DiagnosticInfo &Info) { |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 80 | Client->HandleDiagnostic(DiagLevel, Info); |
| 81 | |
| 82 | // Skip over any diagnostics that are ignored. |
| 83 | if (DiagLevel == Diagnostic::Ignored) |
| 84 | return; |
| 85 | |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 86 | const SourceManager &SM = Rewrite.getSourceMgr(); |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 87 | if (!FixItLocations.empty()) { |
| 88 | // The user has specified the locations where we should perform |
| 89 | // the various fix-it modifications. |
| 90 | |
| 91 | // If this diagnostic does not have any code modifications, |
| 92 | // completely ignore it, even if it's an error: fix-it locations |
| 93 | // are meant to perform specific fix-ups even in the presence of |
| 94 | // other errors. |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 95 | if (Info.getNumFixItHints() == 0) |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 96 | return; |
| 97 | |
| 98 | // See if the location of the error is one that matches what the |
| 99 | // user requested. |
| 100 | bool AcceptableLocation = false; |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 101 | const FileEntry *File = SM.getFileEntryForID( |
| 102 | Info.getLocation().getFileID()); |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 103 | unsigned Line = Info.getLocation().getSpellingLineNumber(); |
| 104 | unsigned Column = Info.getLocation().getSpellingColumnNumber(); |
| 105 | for (llvm::SmallVector<RequestedSourceLocation, 4>::iterator |
| 106 | Loc = FixItLocations.begin(), LocEnd = FixItLocations.end(); |
| 107 | Loc != LocEnd; ++Loc) { |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 108 | if (Loc->File == File && |
| 109 | ((Loc->Line == 0 && Loc->Column == 0 && |
| 110 | DiagLevel > Diagnostic::Note) || |
| 111 | (Loc->Line == Line && Loc->Column == Column))) { |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 112 | AcceptableLocation = true; |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if (!AcceptableLocation) |
| 118 | return; |
Douglas Gregor | a6f2638 | 2010-01-06 23:44:25 +0000 | [diff] [blame] | 119 | } else if (DiagLevel == Diagnostic::Note) { |
| 120 | // Don't apply fix-it modifications in notes. |
| 121 | return; |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 122 | } |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 123 | |
| 124 | // Make sure that we can perform all of the modifications we |
| 125 | // in this diagnostic. |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 126 | bool CanRewrite = Info.getNumFixItHints() > 0; |
| 127 | for (unsigned Idx = 0, Last = Info.getNumFixItHints(); |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 128 | Idx < Last; ++Idx) { |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 129 | const FixItHint &Hint = Info.getFixItHint(Idx); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 130 | if (Hint.RemoveRange.isValid() && |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 131 | Rewrite.getRangeSize(Hint.RemoveRange) == -1) { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 132 | CanRewrite = false; |
| 133 | break; |
| 134 | } |
| 135 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | if (Hint.InsertionLoc.isValid() && |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 137 | !Rewrite.isRewritable(Hint.InsertionLoc)) { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 138 | CanRewrite = false; |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | if (!CanRewrite) { |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 144 | if (Info.getNumFixItHints() > 0) |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 145 | Diag(Info.getLocation(), diag::note_fixit_in_macro); |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 146 | |
| 147 | // If this was an error, refuse to perform any rewriting. |
| 148 | if (DiagLevel == Diagnostic::Error || DiagLevel == Diagnostic::Fatal) { |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 149 | if (++NumFailures == 1) |
| 150 | Diag(Info.getLocation(), diag::note_fixit_unfixed_error); |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 151 | } |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 152 | return; |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 153 | } |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 154 | |
| 155 | bool Failed = false; |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 156 | for (unsigned Idx = 0, Last = Info.getNumFixItHints(); |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 157 | Idx < Last; ++Idx) { |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 158 | const FixItHint &Hint = Info.getFixItHint(Idx); |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 159 | if (!Hint.RemoveRange.isValid()) { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 160 | // We're adding code. |
Daniel Dunbar | 44ba7bf | 2009-08-19 20:32:38 +0000 | [diff] [blame] | 161 | if (Rewrite.InsertTextBefore(Hint.InsertionLoc, Hint.CodeToInsert)) |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 162 | Failed = true; |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 163 | continue; |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 164 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 166 | if (Hint.CodeToInsert.empty()) { |
| 167 | // We're removing code. |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 168 | if (Rewrite.RemoveText(Hint.RemoveRange.getBegin(), |
| 169 | Rewrite.getRangeSize(Hint.RemoveRange))) |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 170 | Failed = true; |
| 171 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 174 | // We're replacing code. |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 175 | if (Rewrite.ReplaceText(Hint.RemoveRange.getBegin(), |
| 176 | Rewrite.getRangeSize(Hint.RemoveRange), |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 177 | Hint.CodeToInsert)) |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 178 | Failed = true; |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 181 | if (Failed) { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 182 | ++NumFailures; |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 183 | Diag(Info.getLocation(), diag::note_fixit_failed); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | Diag(Info.getLocation(), diag::note_fixit_applied); |
| 188 | } |
| 189 | |
| 190 | /// \brief Emit a diagnostic via the adapted diagnostic client. |
| 191 | void FixItRewriter::Diag(FullSourceLoc Loc, unsigned DiagID) { |
| 192 | // When producing this diagnostic, we temporarily bypass ourselves, |
| 193 | // clear out any current diagnostic, and let the downstream client |
| 194 | // format the diagnostic. |
| 195 | Diags.setClient(Client); |
| 196 | Diags.Clear(); |
| 197 | Diags.Report(Loc, DiagID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | Diags.setClient(this); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 199 | } |