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/ADT/OwningPtr.h" |
| 20 | #include "llvm/Support/Streams.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | #include "llvm/System/Path.h" |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 25 | FixItRewriter::FixItRewriter(Diagnostic &Diags, SourceManager &SourceMgr) |
| 26 | : Diags(Diags), Rewrite(SourceMgr), NumFailures(0) { |
| 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; |
| 46 | OutFile = new llvm::raw_fd_ostream(OutFileName.c_str(), |
| 47 | // set binary mode (critical for Windoze) |
| 48 | true, |
| 49 | Err); |
| 50 | OwnedStream.reset(OutFile); |
| 51 | } else if (InFileName == "-") { |
| 52 | OutFile = &llvm::outs(); |
| 53 | } else { |
| 54 | llvm::sys::Path Path(InFileName); |
Douglas Gregor | 2610348 | 2009-04-02 03:14:12 +0000 | [diff] [blame] | 55 | std::string Suffix = Path.getSuffix(); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 56 | Path.eraseSuffix(); |
Douglas Gregor | 2610348 | 2009-04-02 03:14:12 +0000 | [diff] [blame] | 57 | Path.appendSuffix("fixit." + Suffix); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 58 | std::string Err; |
| 59 | OutFile = new llvm::raw_fd_ostream(Path.toString().c_str(), |
| 60 | // set binary mode (critical for Windoze) |
| 61 | true, |
| 62 | Err); |
| 63 | OwnedStream.reset(OutFile); |
| 64 | } |
| 65 | |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 66 | FileID MainFileID = Rewrite.getSourceMgr().getMainFileID(); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 67 | if (const RewriteBuffer *RewriteBuf = |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 68 | Rewrite.getRewriteBufferFor(MainFileID)) { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 69 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 70 | } else { |
| 71 | std::fprintf(stderr, "Main file is unchanged\n"); |
| 72 | } |
| 73 | OutFile->flush(); |
| 74 | |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | bool FixItRewriter::IncludeInDiagnosticCounts() const { |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +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) { |
| 84 | if (Client) |
| 85 | Client->HandleDiagnostic(DiagLevel, Info); |
| 86 | |
| 87 | // Make sure that we can perform all of the modifications we |
| 88 | // in this diagnostic. |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 89 | bool CanRewrite = Info.getNumCodeModificationHints() > 0; |
| 90 | for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints(); |
| 91 | Idx < Last; ++Idx) { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 92 | const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx); |
| 93 | if (Hint.RemoveRange.isValid() && |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 94 | Rewrite.getRangeSize(Hint.RemoveRange) == -1) { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 95 | CanRewrite = false; |
| 96 | break; |
| 97 | } |
| 98 | |
| 99 | if (Hint.InsertionLoc.isValid() && |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 100 | !Rewrite.isRewritable(Hint.InsertionLoc)) { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 101 | CanRewrite = false; |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 106 | if (!CanRewrite) { |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 107 | if (Info.getNumCodeModificationHints() > 0) |
| 108 | Diag(Info.getLocation(), diag::note_fixit_in_macro); |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 109 | |
| 110 | // If this was an error, refuse to perform any rewriting. |
| 111 | if (DiagLevel == Diagnostic::Error || DiagLevel == Diagnostic::Fatal) { |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 112 | if (++NumFailures == 1) |
| 113 | Diag(Info.getLocation(), diag::note_fixit_unfixed_error); |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 114 | } |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 115 | return; |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 116 | } |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 117 | |
| 118 | bool Failed = false; |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 119 | for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints(); |
| 120 | Idx < Last; ++Idx) { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 121 | const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx); |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 122 | if (!Hint.RemoveRange.isValid()) { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 123 | // We're adding code. |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 124 | if (Rewrite.InsertStrBefore(Hint.InsertionLoc, Hint.CodeToInsert)) |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 125 | Failed = true; |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 126 | continue; |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 127 | } |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 128 | |
| 129 | if (Hint.CodeToInsert.empty()) { |
| 130 | // We're removing code. |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 131 | if (Rewrite.RemoveText(Hint.RemoveRange.getBegin(), |
| 132 | Rewrite.getRangeSize(Hint.RemoveRange))) |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 133 | Failed = true; |
| 134 | continue; |
| 135 | } |
| 136 | |
| 137 | // We're replacing code. |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 138 | if (Rewrite.ReplaceText(Hint.RemoveRange.getBegin(), |
| 139 | Rewrite.getRangeSize(Hint.RemoveRange), |
| 140 | Hint.CodeToInsert.c_str(), |
| 141 | Hint.CodeToInsert.size())) |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame] | 142 | Failed = true; |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 145 | if (Failed) { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 146 | ++NumFailures; |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 147 | Diag(Info.getLocation(), diag::note_fixit_failed); |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | Diag(Info.getLocation(), diag::note_fixit_applied); |
| 152 | } |
| 153 | |
| 154 | /// \brief Emit a diagnostic via the adapted diagnostic client. |
| 155 | void FixItRewriter::Diag(FullSourceLoc Loc, unsigned DiagID) { |
| 156 | // When producing this diagnostic, we temporarily bypass ourselves, |
| 157 | // clear out any current diagnostic, and let the downstream client |
| 158 | // format the diagnostic. |
| 159 | Diags.setClient(Client); |
| 160 | Diags.Clear(); |
| 161 | Diags.Report(Loc, DiagID); |
| 162 | Diags.setClient(this); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 163 | } |