blob: 399ad1c2cd7b4f17e1d6b04d26c0baa95e3a8cb5 [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,
Dan Gohman92db2842009-07-15 17:32:18 +000050 /*Force=*/true,
Douglas Gregor558cb562009-04-02 01:08:08 +000051 Err);
52 OwnedStream.reset(OutFile);
53 } else if (InFileName == "-") {
54 OutFile = &llvm::outs();
55 } else {
56 llvm::sys::Path Path(InFileName);
Douglas Gregor26103482009-04-02 03:14:12 +000057 std::string Suffix = Path.getSuffix();
Douglas Gregor558cb562009-04-02 01:08:08 +000058 Path.eraseSuffix();
Douglas Gregor26103482009-04-02 03:14:12 +000059 Path.appendSuffix("fixit." + Suffix);
Douglas Gregor558cb562009-04-02 01:08:08 +000060 std::string Err;
61 OutFile = new llvm::raw_fd_ostream(Path.toString().c_str(),
62 // set binary mode (critical for Windoze)
63 true,
Dan Gohman92db2842009-07-15 17:32:18 +000064 /*Force=*/true,
Douglas Gregor558cb562009-04-02 01:08:08 +000065 Err);
66 OwnedStream.reset(OutFile);
67 }
68
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000069 FileID MainFileID = Rewrite.getSourceMgr().getMainFileID();
Douglas Gregor558cb562009-04-02 01:08:08 +000070 if (const RewriteBuffer *RewriteBuf =
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000071 Rewrite.getRewriteBufferFor(MainFileID)) {
Douglas Gregor558cb562009-04-02 01:08:08 +000072 *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
73 } else {
74 std::fprintf(stderr, "Main file is unchanged\n");
75 }
76 OutFile->flush();
77
78 return false;
79}
80
81bool FixItRewriter::IncludeInDiagnosticCounts() const {
Douglas Gregor837a4062009-04-02 16:34:42 +000082 return Client? Client->IncludeInDiagnosticCounts() : true;
Douglas Gregor558cb562009-04-02 01:08:08 +000083}
84
85void FixItRewriter::HandleDiagnostic(Diagnostic::Level DiagLevel,
86 const DiagnosticInfo &Info) {
Douglas Gregor26df2f02009-04-02 19:05:20 +000087 Client->HandleDiagnostic(DiagLevel, Info);
88
89 // Skip over any diagnostics that are ignored.
90 if (DiagLevel == Diagnostic::Ignored)
91 return;
92
93 if (!FixItLocations.empty()) {
94 // The user has specified the locations where we should perform
95 // the various fix-it modifications.
96
97 // If this diagnostic does not have any code modifications,
98 // completely ignore it, even if it's an error: fix-it locations
99 // are meant to perform specific fix-ups even in the presence of
100 // other errors.
101 if (Info.getNumCodeModificationHints() == 0)
102 return;
103
104 // See if the location of the error is one that matches what the
105 // user requested.
106 bool AcceptableLocation = false;
107 const FileEntry *File
108 = Rewrite.getSourceMgr().getFileEntryForID(
109 Info.getLocation().getFileID());
110 unsigned Line = Info.getLocation().getSpellingLineNumber();
111 unsigned Column = Info.getLocation().getSpellingColumnNumber();
112 for (llvm::SmallVector<RequestedSourceLocation, 4>::iterator
113 Loc = FixItLocations.begin(), LocEnd = FixItLocations.end();
114 Loc != LocEnd; ++Loc) {
115 if (Loc->File == File && Loc->Line == Line && Loc->Column == Column) {
116 AcceptableLocation = true;
117 break;
118 }
119 }
120
121 if (!AcceptableLocation)
122 return;
123 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000124
125 // Make sure that we can perform all of the modifications we
126 // in this diagnostic.
Douglas Gregor837a4062009-04-02 16:34:42 +0000127 bool CanRewrite = Info.getNumCodeModificationHints() > 0;
128 for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints();
129 Idx < Last; ++Idx) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000130 const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
131 if (Hint.RemoveRange.isValid() &&
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000132 Rewrite.getRangeSize(Hint.RemoveRange) == -1) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000133 CanRewrite = false;
134 break;
135 }
136
137 if (Hint.InsertionLoc.isValid() &&
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000138 !Rewrite.isRewritable(Hint.InsertionLoc)) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000139 CanRewrite = false;
140 break;
141 }
142 }
143
Douglas Gregor837a4062009-04-02 16:34:42 +0000144 if (!CanRewrite) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000145 if (Info.getNumCodeModificationHints() > 0)
146 Diag(Info.getLocation(), diag::note_fixit_in_macro);
Douglas Gregor837a4062009-04-02 16:34:42 +0000147
148 // If this was an error, refuse to perform any rewriting.
149 if (DiagLevel == Diagnostic::Error || DiagLevel == Diagnostic::Fatal) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000150 if (++NumFailures == 1)
151 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
Douglas Gregor837a4062009-04-02 16:34:42 +0000152 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000153 return;
Douglas Gregor837a4062009-04-02 16:34:42 +0000154 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000155
156 bool Failed = false;
Douglas Gregor837a4062009-04-02 16:34:42 +0000157 for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints();
158 Idx < Last; ++Idx) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000159 const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
Douglas Gregor837a4062009-04-02 16:34:42 +0000160 if (!Hint.RemoveRange.isValid()) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000161 // We're adding code.
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000162 if (Rewrite.InsertTextBefore(Hint.InsertionLoc, Hint.CodeToInsert))
Douglas Gregor558cb562009-04-02 01:08:08 +0000163 Failed = true;
Douglas Gregor837a4062009-04-02 16:34:42 +0000164 continue;
Douglas Gregor558cb562009-04-02 01:08:08 +0000165 }
Douglas Gregor837a4062009-04-02 16:34:42 +0000166
167 if (Hint.CodeToInsert.empty()) {
168 // We're removing code.
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000169 if (Rewrite.RemoveText(Hint.RemoveRange.getBegin(),
170 Rewrite.getRangeSize(Hint.RemoveRange)))
Douglas Gregor837a4062009-04-02 16:34:42 +0000171 Failed = true;
172 continue;
173 }
174
175 // We're replacing code.
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000176 if (Rewrite.ReplaceText(Hint.RemoveRange.getBegin(),
177 Rewrite.getRangeSize(Hint.RemoveRange),
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000178 Hint.CodeToInsert))
Douglas Gregor837a4062009-04-02 16:34:42 +0000179 Failed = true;
Douglas Gregor558cb562009-04-02 01:08:08 +0000180 }
181
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000182 if (Failed) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000183 ++NumFailures;
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000184 Diag(Info.getLocation(), diag::note_fixit_failed);
185 return;
186 }
187
188 Diag(Info.getLocation(), diag::note_fixit_applied);
189}
190
191/// \brief Emit a diagnostic via the adapted diagnostic client.
192void FixItRewriter::Diag(FullSourceLoc Loc, unsigned DiagID) {
193 // When producing this diagnostic, we temporarily bypass ourselves,
194 // clear out any current diagnostic, and let the downstream client
195 // format the diagnostic.
196 Diags.setClient(Client);
197 Diags.Clear();
198 Diags.Report(Loc, DiagID);
199 Diags.setClient(this);
Douglas Gregor558cb562009-04-02 01:08:08 +0000200}