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 | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 18 | #include "clang/Rewrite/Rewriter.h" |
| 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" |
| 23 | #include <cstdio> |
| 24 | using namespace clang; |
| 25 | |
| 26 | FixItRewriter::FixItRewriter(DiagnosticClient *Client, |
| 27 | SourceManager &SourceMgr) |
| 28 | : Client(Client), NumFailures(0) { |
| 29 | Rewrite = new Rewriter(SourceMgr); |
| 30 | } |
| 31 | |
| 32 | FixItRewriter::~FixItRewriter() { |
| 33 | delete Rewrite; |
| 34 | } |
| 35 | |
| 36 | bool FixItRewriter::WriteFixedFile(const std::string &InFileName, |
| 37 | const std::string &OutFileName) { |
| 38 | if (NumFailures > 0) { |
| 39 | // FIXME: Use diagnostic machinery! |
| 40 | std::fprintf(stderr, |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame^] | 41 | "%d fix-it failures detected; code will not be modified\n", |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 42 | NumFailures); |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | llvm::OwningPtr<llvm::raw_ostream> OwnedStream; |
| 47 | llvm::raw_ostream *OutFile; |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame^] | 48 | if (!OutFileName.empty()) { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 49 | std::string Err; |
| 50 | OutFile = new llvm::raw_fd_ostream(OutFileName.c_str(), |
| 51 | // set binary mode (critical for Windoze) |
| 52 | true, |
| 53 | Err); |
| 54 | OwnedStream.reset(OutFile); |
| 55 | } else if (InFileName == "-") { |
| 56 | OutFile = &llvm::outs(); |
| 57 | } else { |
| 58 | llvm::sys::Path Path(InFileName); |
Douglas Gregor | 2610348 | 2009-04-02 03:14:12 +0000 | [diff] [blame] | 59 | std::string Suffix = Path.getSuffix(); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 60 | Path.eraseSuffix(); |
Douglas Gregor | 2610348 | 2009-04-02 03:14:12 +0000 | [diff] [blame] | 61 | Path.appendSuffix("fixit." + Suffix); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 62 | std::string Err; |
| 63 | OutFile = new llvm::raw_fd_ostream(Path.toString().c_str(), |
| 64 | // set binary mode (critical for Windoze) |
| 65 | true, |
| 66 | Err); |
| 67 | OwnedStream.reset(OutFile); |
| 68 | } |
| 69 | |
| 70 | FileID MainFileID = Rewrite->getSourceMgr().getMainFileID(); |
| 71 | if (const RewriteBuffer *RewriteBuf = |
| 72 | Rewrite->getRewriteBufferFor(MainFileID)) { |
| 73 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 74 | } else { |
| 75 | std::fprintf(stderr, "Main file is unchanged\n"); |
| 76 | } |
| 77 | OutFile->flush(); |
| 78 | |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | bool FixItRewriter::IncludeInDiagnosticCounts() const { |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame^] | 83 | return Client? Client->IncludeInDiagnosticCounts() : true; |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void FixItRewriter::HandleDiagnostic(Diagnostic::Level DiagLevel, |
| 87 | const DiagnosticInfo &Info) { |
| 88 | if (Client) |
| 89 | Client->HandleDiagnostic(DiagLevel, Info); |
| 90 | |
| 91 | // Make sure that we can perform all of the modifications we |
| 92 | // in this diagnostic. |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame^] | 93 | bool CanRewrite = Info.getNumCodeModificationHints() > 0; |
| 94 | for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints(); |
| 95 | Idx < Last; ++Idx) { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 96 | const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx); |
| 97 | if (Hint.RemoveRange.isValid() && |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame^] | 98 | Rewrite->getRangeSize(Hint.RemoveRange) == -1) { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 99 | CanRewrite = false; |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | if (Hint.InsertionLoc.isValid() && |
| 104 | !Rewrite->isRewritable(Hint.InsertionLoc)) { |
| 105 | CanRewrite = false; |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame^] | 110 | if (!CanRewrite) { |
| 111 | if (Info.getNumCodeModificationHints() > 0) { |
| 112 | // FIXME: warn the user that this rewrite couldn't be done |
| 113 | } |
| 114 | |
| 115 | // If this was an error, refuse to perform any rewriting. |
| 116 | if (DiagLevel == Diagnostic::Error || DiagLevel == Diagnostic::Fatal) { |
| 117 | if (++NumFailures == 1) { |
| 118 | // FIXME: use diagnostic machinery to print this. |
| 119 | std::fprintf(stderr, "error without fix-it advice detected; " |
| 120 | "fix-it will produce no output\n"); |
| 121 | } |
| 122 | } |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 123 | return; |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame^] | 124 | } |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 125 | |
| 126 | bool Failed = false; |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame^] | 127 | for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints(); |
| 128 | Idx < Last; ++Idx) { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 129 | const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx); |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame^] | 130 | if (!Hint.RemoveRange.isValid()) { |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 131 | // We're adding code. |
| 132 | if (Rewrite->InsertStrBefore(Hint.InsertionLoc, Hint.CodeToInsert)) |
| 133 | Failed = true; |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame^] | 134 | continue; |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 135 | } |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame^] | 136 | |
| 137 | if (Hint.CodeToInsert.empty()) { |
| 138 | // We're removing code. |
| 139 | if (Rewrite->RemoveText(Hint.RemoveRange.getBegin(), |
| 140 | Rewrite->getRangeSize(Hint.RemoveRange))) |
| 141 | Failed = true; |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | // We're replacing code. |
| 146 | if (Rewrite->ReplaceText(Hint.RemoveRange.getBegin(), |
| 147 | Rewrite->getRangeSize(Hint.RemoveRange), |
| 148 | Hint.CodeToInsert.c_str(), |
| 149 | Hint.CodeToInsert.size())) |
| 150 | Failed = true; |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Douglas Gregor | 837a406 | 2009-04-02 16:34:42 +0000 | [diff] [blame^] | 153 | if (Failed) // FIXME: notify the user that the rewrite failed. |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 154 | ++NumFailures; |
| 155 | } |