blob: 1ed89d75a9c953de828d850ced58d1706ae50b23 [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 Gregorde4bf6a2009-04-02 17:13:00 +000018#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregor558cb562009-04-02 01:08:08 +000019#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 Gregor558cb562009-04-02 01:08:08 +000023using namespace clang;
24
Chris Lattner2c78b872009-04-14 23:22:57 +000025FixItRewriter::FixItRewriter(Diagnostic &Diags, SourceManager &SourceMgr,
26 const LangOptions &LangOpts)
27 : Diags(Diags), Rewrite(SourceMgr, LangOpts), NumFailures(0) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000028 Client = Diags.getClient();
29 Diags.setClient(this);
Douglas Gregor558cb562009-04-02 01:08:08 +000030}
31
32FixItRewriter::~FixItRewriter() {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000033 Diags.setClient(Client);
Douglas Gregor558cb562009-04-02 01:08:08 +000034}
35
36bool FixItRewriter::WriteFixedFile(const std::string &InFileName,
37 const std::string &OutFileName) {
38 if (NumFailures > 0) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000039 Diag(FullSourceLoc(), diag::warn_fixit_no_changes);
Douglas Gregor558cb562009-04-02 01:08:08 +000040 return true;
41 }
42
43 llvm::OwningPtr<llvm::raw_ostream> OwnedStream;
44 llvm::raw_ostream *OutFile;
Douglas Gregor837a4062009-04-02 16:34:42 +000045 if (!OutFileName.empty()) {
Douglas Gregor558cb562009-04-02 01:08:08 +000046 std::string Err;
47 OutFile = new llvm::raw_fd_ostream(OutFileName.c_str(),
48 // set binary mode (critical for Windoze)
49 true,
50 Err);
51 OwnedStream.reset(OutFile);
52 } else if (InFileName == "-") {
53 OutFile = &llvm::outs();
54 } else {
55 llvm::sys::Path Path(InFileName);
Douglas Gregor26103482009-04-02 03:14:12 +000056 std::string Suffix = Path.getSuffix();
Douglas Gregor558cb562009-04-02 01:08:08 +000057 Path.eraseSuffix();
Douglas Gregor26103482009-04-02 03:14:12 +000058 Path.appendSuffix("fixit." + Suffix);
Douglas Gregor558cb562009-04-02 01:08:08 +000059 std::string Err;
60 OutFile = new llvm::raw_fd_ostream(Path.toString().c_str(),
61 // set binary mode (critical for Windoze)
62 true,
63 Err);
64 OwnedStream.reset(OutFile);
65 }
66
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000067 FileID MainFileID = Rewrite.getSourceMgr().getMainFileID();
Douglas Gregor558cb562009-04-02 01:08:08 +000068 if (const RewriteBuffer *RewriteBuf =
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000069 Rewrite.getRewriteBufferFor(MainFileID)) {
Douglas Gregor558cb562009-04-02 01:08:08 +000070 *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
71 } else {
72 std::fprintf(stderr, "Main file is unchanged\n");
73 }
74 OutFile->flush();
75
76 return false;
77}
78
79bool FixItRewriter::IncludeInDiagnosticCounts() const {
Douglas Gregor837a4062009-04-02 16:34:42 +000080 return Client? Client->IncludeInDiagnosticCounts() : true;
Douglas Gregor558cb562009-04-02 01:08:08 +000081}
82
83void FixItRewriter::HandleDiagnostic(Diagnostic::Level DiagLevel,
84 const DiagnosticInfo &Info) {
Douglas Gregor26df2f02009-04-02 19:05:20 +000085 Client->HandleDiagnostic(DiagLevel, Info);
86
87 // Skip over any diagnostics that are ignored.
88 if (DiagLevel == Diagnostic::Ignored)
89 return;
90
91 if (!FixItLocations.empty()) {
92 // The user has specified the locations where we should perform
93 // the various fix-it modifications.
94
95 // If this diagnostic does not have any code modifications,
96 // completely ignore it, even if it's an error: fix-it locations
97 // are meant to perform specific fix-ups even in the presence of
98 // other errors.
99 if (Info.getNumCodeModificationHints() == 0)
100 return;
101
102 // See if the location of the error is one that matches what the
103 // user requested.
104 bool AcceptableLocation = false;
105 const FileEntry *File
106 = Rewrite.getSourceMgr().getFileEntryForID(
107 Info.getLocation().getFileID());
108 unsigned Line = Info.getLocation().getSpellingLineNumber();
109 unsigned Column = Info.getLocation().getSpellingColumnNumber();
110 for (llvm::SmallVector<RequestedSourceLocation, 4>::iterator
111 Loc = FixItLocations.begin(), LocEnd = FixItLocations.end();
112 Loc != LocEnd; ++Loc) {
113 if (Loc->File == File && Loc->Line == Line && Loc->Column == Column) {
114 AcceptableLocation = true;
115 break;
116 }
117 }
118
119 if (!AcceptableLocation)
120 return;
121 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000122
123 // Make sure that we can perform all of the modifications we
124 // in this diagnostic.
Douglas Gregor837a4062009-04-02 16:34:42 +0000125 bool CanRewrite = Info.getNumCodeModificationHints() > 0;
126 for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints();
127 Idx < Last; ++Idx) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000128 const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
129 if (Hint.RemoveRange.isValid() &&
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000130 Rewrite.getRangeSize(Hint.RemoveRange) == -1) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000131 CanRewrite = false;
132 break;
133 }
134
135 if (Hint.InsertionLoc.isValid() &&
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000136 !Rewrite.isRewritable(Hint.InsertionLoc)) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000137 CanRewrite = false;
138 break;
139 }
140 }
141
Douglas Gregor837a4062009-04-02 16:34:42 +0000142 if (!CanRewrite) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000143 if (Info.getNumCodeModificationHints() > 0)
144 Diag(Info.getLocation(), diag::note_fixit_in_macro);
Douglas Gregor837a4062009-04-02 16:34:42 +0000145
146 // If this was an error, refuse to perform any rewriting.
147 if (DiagLevel == Diagnostic::Error || DiagLevel == Diagnostic::Fatal) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000148 if (++NumFailures == 1)
149 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
Douglas Gregor837a4062009-04-02 16:34:42 +0000150 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000151 return;
Douglas Gregor837a4062009-04-02 16:34:42 +0000152 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000153
154 bool Failed = false;
Douglas Gregor837a4062009-04-02 16:34:42 +0000155 for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints();
156 Idx < Last; ++Idx) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000157 const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
Douglas Gregor837a4062009-04-02 16:34:42 +0000158 if (!Hint.RemoveRange.isValid()) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000159 // We're adding code.
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000160 if (Rewrite.InsertStrBefore(Hint.InsertionLoc, Hint.CodeToInsert))
Douglas Gregor558cb562009-04-02 01:08:08 +0000161 Failed = true;
Douglas Gregor837a4062009-04-02 16:34:42 +0000162 continue;
Douglas Gregor558cb562009-04-02 01:08:08 +0000163 }
Douglas Gregor837a4062009-04-02 16:34:42 +0000164
165 if (Hint.CodeToInsert.empty()) {
166 // We're removing code.
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000167 if (Rewrite.RemoveText(Hint.RemoveRange.getBegin(),
168 Rewrite.getRangeSize(Hint.RemoveRange)))
Douglas Gregor837a4062009-04-02 16:34:42 +0000169 Failed = true;
170 continue;
171 }
172
173 // We're replacing code.
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000174 if (Rewrite.ReplaceText(Hint.RemoveRange.getBegin(),
175 Rewrite.getRangeSize(Hint.RemoveRange),
176 Hint.CodeToInsert.c_str(),
177 Hint.CodeToInsert.size()))
Douglas Gregor837a4062009-04-02 16:34:42 +0000178 Failed = true;
Douglas Gregor558cb562009-04-02 01:08:08 +0000179 }
180
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000181 if (Failed) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000182 ++NumFailures;
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000183 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.
191void 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);
198 Diags.setClient(this);
Douglas Gregor558cb562009-04-02 01:08:08 +0000199}