blob: a462657727a5ceefdef7a7f32e315cde3734ebb6 [file] [log] [blame]
Douglas Gregor558cb562009-04-02 01:08:08 +00001//===--- 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 Gregor837a4062009-04-02 16:34:42 +000015
Douglas Gregor558cb562009-04-02 01:08:08 +000016#include "clang/Frontend/FixItRewriter.h"
Douglas Gregor837a4062009-04-02 16:34:42 +000017#include "clang/Basic/SourceManager.h"
Douglas Gregor558cb562009-04-02 01:08:08 +000018#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>
24using namespace clang;
25
26FixItRewriter::FixItRewriter(DiagnosticClient *Client,
27 SourceManager &SourceMgr)
28 : Client(Client), NumFailures(0) {
29 Rewrite = new Rewriter(SourceMgr);
30}
31
32FixItRewriter::~FixItRewriter() {
33 delete Rewrite;
34}
35
36bool 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 Gregor837a4062009-04-02 16:34:42 +000041 "%d fix-it failures detected; code will not be modified\n",
Douglas Gregor558cb562009-04-02 01:08:08 +000042 NumFailures);
43 return true;
44 }
45
46 llvm::OwningPtr<llvm::raw_ostream> OwnedStream;
47 llvm::raw_ostream *OutFile;
Douglas Gregor837a4062009-04-02 16:34:42 +000048 if (!OutFileName.empty()) {
Douglas Gregor558cb562009-04-02 01:08:08 +000049 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 Gregor26103482009-04-02 03:14:12 +000059 std::string Suffix = Path.getSuffix();
Douglas Gregor558cb562009-04-02 01:08:08 +000060 Path.eraseSuffix();
Douglas Gregor26103482009-04-02 03:14:12 +000061 Path.appendSuffix("fixit." + Suffix);
Douglas Gregor558cb562009-04-02 01:08:08 +000062 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
82bool FixItRewriter::IncludeInDiagnosticCounts() const {
Douglas Gregor837a4062009-04-02 16:34:42 +000083 return Client? Client->IncludeInDiagnosticCounts() : true;
Douglas Gregor558cb562009-04-02 01:08:08 +000084}
85
86void 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 Gregor837a4062009-04-02 16:34:42 +000093 bool CanRewrite = Info.getNumCodeModificationHints() > 0;
94 for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints();
95 Idx < Last; ++Idx) {
Douglas Gregor558cb562009-04-02 01:08:08 +000096 const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
97 if (Hint.RemoveRange.isValid() &&
Douglas Gregor837a4062009-04-02 16:34:42 +000098 Rewrite->getRangeSize(Hint.RemoveRange) == -1) {
Douglas Gregor558cb562009-04-02 01:08:08 +000099 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 Gregor837a4062009-04-02 16:34:42 +0000110 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 Gregor558cb562009-04-02 01:08:08 +0000123 return;
Douglas Gregor837a4062009-04-02 16:34:42 +0000124 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000125
126 bool Failed = false;
Douglas Gregor837a4062009-04-02 16:34:42 +0000127 for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints();
128 Idx < Last; ++Idx) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000129 const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
Douglas Gregor837a4062009-04-02 16:34:42 +0000130 if (!Hint.RemoveRange.isValid()) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000131 // We're adding code.
132 if (Rewrite->InsertStrBefore(Hint.InsertionLoc, Hint.CodeToInsert))
133 Failed = true;
Douglas Gregor837a4062009-04-02 16:34:42 +0000134 continue;
Douglas Gregor558cb562009-04-02 01:08:08 +0000135 }
Douglas Gregor837a4062009-04-02 16:34:42 +0000136
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 Gregor558cb562009-04-02 01:08:08 +0000151 }
152
Douglas Gregor837a4062009-04-02 16:34:42 +0000153 if (Failed) // FIXME: notify the user that the rewrite failed.
Douglas Gregor558cb562009-04-02 01:08:08 +0000154 ++NumFailures;
155}