blob: 7aff92340eaed42f7f0112cc1ecd121c317d0e54 [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"
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000017#include "clang/Basic/FileManager.h"
18#include "clang/Basic/SourceLocation.h"
Douglas Gregor837a4062009-04-02 16:34:42 +000019#include "clang/Basic/SourceManager.h"
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000020#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregor558cb562009-04-02 01:08:08 +000021#include "llvm/Support/raw_ostream.h"
22#include "llvm/System/Path.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000023#include "llvm/ADT/OwningPtr.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000024#include <cstdio>
25
Douglas Gregor558cb562009-04-02 01:08:08 +000026using namespace clang;
27
Chris Lattner2c78b872009-04-14 23:22:57 +000028FixItRewriter::FixItRewriter(Diagnostic &Diags, SourceManager &SourceMgr,
29 const LangOptions &LangOpts)
30 : Diags(Diags), Rewrite(SourceMgr, LangOpts), NumFailures(0) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000031 Client = Diags.getClient();
32 Diags.setClient(this);
Douglas Gregor558cb562009-04-02 01:08:08 +000033}
34
35FixItRewriter::~FixItRewriter() {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000036 Diags.setClient(Client);
Douglas Gregor558cb562009-04-02 01:08:08 +000037}
38
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000039bool FixItRewriter::WriteFixedFile(FileID ID, llvm::raw_ostream &OS) {
40 const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID);
41 if (!RewriteBuf) return true;
Nick Lewycky0ade8082010-04-16 18:49:45 +000042 RewriteBuf->write(OS);
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000043 OS.flush();
44 return false;
45}
46
47bool FixItRewriter::WriteFixedFiles() {
Douglas Gregor558cb562009-04-02 01:08:08 +000048 if (NumFailures > 0) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000049 Diag(FullSourceLoc(), diag::warn_fixit_no_changes);
Douglas Gregor558cb562009-04-02 01:08:08 +000050 return true;
51 }
52
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000053 for (iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
54 const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first);
55 llvm::sys::Path Path(Entry->getName());
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;
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000060 llvm::raw_fd_ostream OS(Path.c_str(), Err, llvm::raw_fd_ostream::F_Binary);
61 if (!Err.empty()) {
62 Diags.Report(clang::diag::err_fe_unable_to_open_output)
63 << Path.c_str() << Err;
64 continue;
65 }
66 RewriteBuffer &RewriteBuf = I->second;
67 OS << std::string(RewriteBuf.begin(), RewriteBuf.end());
68 OS.flush();
Mike Stump1eb44332009-09-09 15:08:12 +000069 }
Douglas Gregor558cb562009-04-02 01:08:08 +000070
Douglas Gregor558cb562009-04-02 01:08:08 +000071 return false;
72}
73
74bool FixItRewriter::IncludeInDiagnosticCounts() const {
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000075 return Client ? Client->IncludeInDiagnosticCounts() : true;
Douglas Gregor558cb562009-04-02 01:08:08 +000076}
77
78void FixItRewriter::HandleDiagnostic(Diagnostic::Level DiagLevel,
79 const DiagnosticInfo &Info) {
Douglas Gregor26df2f02009-04-02 19:05:20 +000080 Client->HandleDiagnostic(DiagLevel, Info);
81
82 // Skip over any diagnostics that are ignored.
83 if (DiagLevel == Diagnostic::Ignored)
84 return;
85
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000086 const SourceManager &SM = Rewrite.getSourceMgr();
Douglas Gregor26df2f02009-04-02 19:05:20 +000087 if (!FixItLocations.empty()) {
88 // The user has specified the locations where we should perform
89 // the various fix-it modifications.
90
91 // If this diagnostic does not have any code modifications,
92 // completely ignore it, even if it's an error: fix-it locations
93 // are meant to perform specific fix-ups even in the presence of
94 // other errors.
Douglas Gregor849b2432010-03-31 17:46:05 +000095 if (Info.getNumFixItHints() == 0)
Douglas Gregor26df2f02009-04-02 19:05:20 +000096 return;
97
98 // See if the location of the error is one that matches what the
99 // user requested.
100 bool AcceptableLocation = false;
Nick Lewyckyd4a97a12010-04-15 06:46:58 +0000101 const FileEntry *File = SM.getFileEntryForID(
102 Info.getLocation().getFileID());
Douglas Gregor26df2f02009-04-02 19:05:20 +0000103 unsigned Line = Info.getLocation().getSpellingLineNumber();
104 unsigned Column = Info.getLocation().getSpellingColumnNumber();
105 for (llvm::SmallVector<RequestedSourceLocation, 4>::iterator
106 Loc = FixItLocations.begin(), LocEnd = FixItLocations.end();
107 Loc != LocEnd; ++Loc) {
Nick Lewyckyd4a97a12010-04-15 06:46:58 +0000108 if (Loc->File == File &&
109 ((Loc->Line == 0 && Loc->Column == 0 &&
110 DiagLevel > Diagnostic::Note) ||
111 (Loc->Line == Line && Loc->Column == Column))) {
Douglas Gregor26df2f02009-04-02 19:05:20 +0000112 AcceptableLocation = true;
113 break;
114 }
115 }
116
117 if (!AcceptableLocation)
118 return;
Douglas Gregora6f26382010-01-06 23:44:25 +0000119 } else if (DiagLevel == Diagnostic::Note) {
120 // Don't apply fix-it modifications in notes.
121 return;
Douglas Gregor26df2f02009-04-02 19:05:20 +0000122 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000123
124 // Make sure that we can perform all of the modifications we
125 // in this diagnostic.
Douglas Gregor849b2432010-03-31 17:46:05 +0000126 bool CanRewrite = Info.getNumFixItHints() > 0;
127 for (unsigned Idx = 0, Last = Info.getNumFixItHints();
Douglas Gregor837a4062009-04-02 16:34:42 +0000128 Idx < Last; ++Idx) {
Douglas Gregor849b2432010-03-31 17:46:05 +0000129 const FixItHint &Hint = Info.getFixItHint(Idx);
Douglas Gregor558cb562009-04-02 01:08:08 +0000130 if (Hint.RemoveRange.isValid() &&
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000131 Rewrite.getRangeSize(Hint.RemoveRange) == -1) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000132 CanRewrite = false;
133 break;
134 }
135
Mike Stump1eb44332009-09-09 15:08:12 +0000136 if (Hint.InsertionLoc.isValid() &&
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000137 !Rewrite.isRewritable(Hint.InsertionLoc)) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000138 CanRewrite = false;
139 break;
140 }
141 }
142
Mike Stump1eb44332009-09-09 15:08:12 +0000143 if (!CanRewrite) {
Douglas Gregor849b2432010-03-31 17:46:05 +0000144 if (Info.getNumFixItHints() > 0)
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000145 Diag(Info.getLocation(), diag::note_fixit_in_macro);
Douglas Gregor837a4062009-04-02 16:34:42 +0000146
147 // If this was an error, refuse to perform any rewriting.
148 if (DiagLevel == Diagnostic::Error || DiagLevel == Diagnostic::Fatal) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000149 if (++NumFailures == 1)
150 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
Douglas Gregor837a4062009-04-02 16:34:42 +0000151 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000152 return;
Douglas Gregor837a4062009-04-02 16:34:42 +0000153 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000154
155 bool Failed = false;
Douglas Gregor849b2432010-03-31 17:46:05 +0000156 for (unsigned Idx = 0, Last = Info.getNumFixItHints();
Douglas Gregor837a4062009-04-02 16:34:42 +0000157 Idx < Last; ++Idx) {
Douglas Gregor849b2432010-03-31 17:46:05 +0000158 const FixItHint &Hint = Info.getFixItHint(Idx);
Douglas Gregor837a4062009-04-02 16:34:42 +0000159 if (!Hint.RemoveRange.isValid()) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000160 // We're adding code.
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000161 if (Rewrite.InsertTextBefore(Hint.InsertionLoc, Hint.CodeToInsert))
Douglas Gregor558cb562009-04-02 01:08:08 +0000162 Failed = true;
Douglas Gregor837a4062009-04-02 16:34:42 +0000163 continue;
Douglas Gregor558cb562009-04-02 01:08:08 +0000164 }
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Douglas Gregor837a4062009-04-02 16:34:42 +0000166 if (Hint.CodeToInsert.empty()) {
167 // We're removing code.
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000168 if (Rewrite.RemoveText(Hint.RemoveRange.getBegin(),
169 Rewrite.getRangeSize(Hint.RemoveRange)))
Douglas Gregor837a4062009-04-02 16:34:42 +0000170 Failed = true;
171 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000172 }
173
Douglas Gregor837a4062009-04-02 16:34:42 +0000174 // We're replacing code.
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000175 if (Rewrite.ReplaceText(Hint.RemoveRange.getBegin(),
176 Rewrite.getRangeSize(Hint.RemoveRange),
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000177 Hint.CodeToInsert))
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);
Mike Stump1eb44332009-09-09 15:08:12 +0000198 Diags.setClient(this);
Douglas Gregor558cb562009-04-02 01:08:08 +0000199}