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