blob: 3863adb4f1625ad8b3ce10fc6a1be20139fc2881 [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
Daniel Dunbar9b414d32010-06-15 17:48:49 +000016#include "clang/Rewrite/FixItRewriter.h"
Ted Kremenek30660a82012-03-06 20:06:33 +000017#include "clang/Edit/Commit.h"
18#include "clang/Edit/EditsReceiver.h"
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000019#include "clang/Basic/FileManager.h"
20#include "clang/Basic/SourceLocation.h"
Douglas Gregor837a4062009-04-02 16:34:42 +000021#include "clang/Basic/SourceManager.h"
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000022#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregor558cb562009-04-02 01:08:08 +000023#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000024#include "llvm/Support/Path.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000025#include "llvm/ADT/OwningPtr.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000026#include <cstdio>
27
Douglas Gregor558cb562009-04-02 01:08:08 +000028using namespace clang;
29
David Blaikied6471f72011-09-25 23:23:43 +000030FixItRewriter::FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000031 const LangOptions &LangOpts,
Nick Lewycky1450f262010-08-13 17:31:00 +000032 FixItOptions *FixItOpts)
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000033 : Diags(Diags),
Ted Kremenek30660a82012-03-06 20:06:33 +000034 Editor(SourceMgr, LangOpts),
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000035 Rewrite(SourceMgr, LangOpts),
Nick Lewycky1450f262010-08-13 17:31:00 +000036 FixItOpts(FixItOpts),
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +000037 NumFailures(0),
38 PrevDiagSilenced(false) {
39 OwnsClient = Diags.ownsClient();
Douglas Gregorbdbb0042010-08-18 22:29:43 +000040 Client = Diags.takeClient();
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000041 Diags.setClient(this);
Douglas Gregor558cb562009-04-02 01:08:08 +000042}
43
44FixItRewriter::~FixItRewriter() {
Douglas Gregorbdbb0042010-08-18 22:29:43 +000045 Diags.takeClient();
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +000046 Diags.setClient(Client, OwnsClient);
Douglas Gregor558cb562009-04-02 01:08:08 +000047}
48
Chris Lattner5f9e2722011-07-23 10:55:15 +000049bool FixItRewriter::WriteFixedFile(FileID ID, raw_ostream &OS) {
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000050 const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID);
51 if (!RewriteBuf) return true;
Nick Lewycky0ade8082010-04-16 18:49:45 +000052 RewriteBuf->write(OS);
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000053 OS.flush();
54 return false;
55}
56
Ted Kremenek30660a82012-03-06 20:06:33 +000057namespace {
58
59class RewritesReceiver : public edit::EditsReceiver {
60 Rewriter &Rewrite;
61
62public:
63 RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) { }
64
65 virtual void insert(SourceLocation loc, StringRef text) {
66 Rewrite.InsertText(loc, text);
67 }
68 virtual void replace(CharSourceRange range, StringRef text) {
69 Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text);
70 }
71};
72
73}
74
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +000075bool FixItRewriter::WriteFixedFiles(
76 std::vector<std::pair<std::string, std::string> > *RewrittenFiles) {
Nick Lewycky96872c42010-08-15 16:47:39 +000077 if (NumFailures > 0 && !FixItOpts->FixWhatYouCan) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000078 Diag(FullSourceLoc(), diag::warn_fixit_no_changes);
Douglas Gregor558cb562009-04-02 01:08:08 +000079 return true;
80 }
81
Ted Kremenek30660a82012-03-06 20:06:33 +000082 RewritesReceiver Rec(Rewrite);
83 Editor.applyRewrites(Rec);
84
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000085 for (iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
86 const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first);
Argyrios Kyrtzidisc8af9102012-01-26 04:19:04 +000087 int fd;
88 std::string Filename = FixItOpts->RewriteFilename(Entry->getName(), fd);
Douglas Gregor558cb562009-04-02 01:08:08 +000089 std::string Err;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000090 OwningPtr<llvm::raw_fd_ostream> OS;
Argyrios Kyrtzidisc8af9102012-01-26 04:19:04 +000091 if (fd != -1) {
92 OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
93 } else {
94 OS.reset(new llvm::raw_fd_ostream(Filename.c_str(), Err,
95 llvm::raw_fd_ostream::F_Binary));
96 }
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000097 if (!Err.empty()) {
98 Diags.Report(clang::diag::err_fe_unable_to_open_output)
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000099 << Filename << Err;
Nick Lewyckyd4a97a12010-04-15 06:46:58 +0000100 continue;
101 }
102 RewriteBuffer &RewriteBuf = I->second;
Argyrios Kyrtzidisc8af9102012-01-26 04:19:04 +0000103 RewriteBuf.write(*OS);
104 OS->flush();
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +0000105
106 if (RewrittenFiles)
107 RewrittenFiles->push_back(std::make_pair(Entry->getName(), Filename));
Mike Stump1eb44332009-09-09 15:08:12 +0000108 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000109
Douglas Gregor558cb562009-04-02 01:08:08 +0000110 return false;
111}
112
113bool FixItRewriter::IncludeInDiagnosticCounts() const {
Nick Lewyckyd4a97a12010-04-15 06:46:58 +0000114 return Client ? Client->IncludeInDiagnosticCounts() : true;
Douglas Gregor558cb562009-04-02 01:08:08 +0000115}
116
David Blaikied6471f72011-09-25 23:23:43 +0000117void FixItRewriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikie40847cf2011-09-26 01:18:08 +0000118 const Diagnostic &Info) {
Argyrios Kyrtzidisdea22a32010-11-18 21:13:54 +0000119 // Default implementation (Warnings/errors count).
David Blaikie78ad0b92011-09-25 23:39:51 +0000120 DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
Argyrios Kyrtzidisdea22a32010-11-18 21:13:54 +0000121
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +0000122 if (!FixItOpts->Silent ||
123 DiagLevel >= DiagnosticsEngine::Error ||
124 (DiagLevel == DiagnosticsEngine::Note && !PrevDiagSilenced) ||
125 (DiagLevel > DiagnosticsEngine::Note && Info.getNumFixItHints())) {
126 Client->HandleDiagnostic(DiagLevel, Info);
127 PrevDiagSilenced = false;
128 } else {
129 PrevDiagSilenced = true;
130 }
Douglas Gregor26df2f02009-04-02 19:05:20 +0000131
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +0000132 // Skip over any diagnostics that are ignored or notes.
David Blaikied6471f72011-09-25 23:23:43 +0000133 if (DiagLevel <= DiagnosticsEngine::Note)
Douglas Gregor26df2f02009-04-02 19:05:20 +0000134 return;
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +0000135 // Skip over errors if we are only fixing warnings.
136 if (DiagLevel >= DiagnosticsEngine::Error && FixItOpts->FixOnlyWarnings) {
137 ++NumFailures;
138 return;
139 }
Douglas Gregor26df2f02009-04-02 19:05:20 +0000140
Douglas Gregor558cb562009-04-02 01:08:08 +0000141 // Make sure that we can perform all of the modifications we
142 // in this diagnostic.
Ted Kremenek30660a82012-03-06 20:06:33 +0000143 edit::Commit commit(Editor);
Douglas Gregor849b2432010-03-31 17:46:05 +0000144 for (unsigned Idx = 0, Last = Info.getNumFixItHints();
Douglas Gregor837a4062009-04-02 16:34:42 +0000145 Idx < Last; ++Idx) {
Douglas Gregor849b2432010-03-31 17:46:05 +0000146 const FixItHint &Hint = Info.getFixItHint(Idx);
Ted Kremenek30660a82012-03-06 20:06:33 +0000147
148 if (Hint.CodeToInsert.empty()) {
149 if (Hint.InsertFromRange.isValid())
150 commit.insertFromRange(Hint.RemoveRange.getBegin(),
151 Hint.InsertFromRange, /*afterToken=*/false,
152 Hint.BeforePreviousInsertions);
153 else
154 commit.remove(Hint.RemoveRange);
155 } else {
156 if (Hint.RemoveRange.isTokenRange() ||
157 Hint.RemoveRange.getBegin() != Hint.RemoveRange.getEnd())
158 commit.replace(Hint.RemoveRange, Hint.CodeToInsert);
159 else
160 commit.insert(Hint.RemoveRange.getBegin(), Hint.CodeToInsert,
161 /*afterToken=*/false, Hint.BeforePreviousInsertions);
Douglas Gregor558cb562009-04-02 01:08:08 +0000162 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000163 }
Ted Kremenek30660a82012-03-06 20:06:33 +0000164 bool CanRewrite = Info.getNumFixItHints() > 0 && commit.isCommitable();
Douglas Gregor558cb562009-04-02 01:08:08 +0000165
Mike Stump1eb44332009-09-09 15:08:12 +0000166 if (!CanRewrite) {
Douglas Gregor849b2432010-03-31 17:46:05 +0000167 if (Info.getNumFixItHints() > 0)
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000168 Diag(Info.getLocation(), diag::note_fixit_in_macro);
Douglas Gregor837a4062009-04-02 16:34:42 +0000169
170 // If this was an error, refuse to perform any rewriting.
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +0000171 if (DiagLevel >= DiagnosticsEngine::Error) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000172 if (++NumFailures == 1)
173 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
Douglas Gregor837a4062009-04-02 16:34:42 +0000174 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000175 return;
Douglas Gregor837a4062009-04-02 16:34:42 +0000176 }
Ted Kremenek30660a82012-03-06 20:06:33 +0000177
178 if (!Editor.commit(commit)) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000179 ++NumFailures;
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000180 Diag(Info.getLocation(), diag::note_fixit_failed);
181 return;
182 }
183
184 Diag(Info.getLocation(), diag::note_fixit_applied);
185}
186
187/// \brief Emit a diagnostic via the adapted diagnostic client.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000188void FixItRewriter::Diag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000189 // When producing this diagnostic, we temporarily bypass ourselves,
190 // clear out any current diagnostic, and let the downstream client
191 // format the diagnostic.
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000192 Diags.takeClient();
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000193 Diags.setClient(Client);
194 Diags.Clear();
195 Diags.Report(Loc, DiagID);
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000196 Diags.takeClient();
Mike Stump1eb44332009-09-09 15:08:12 +0000197 Diags.setClient(this);
Douglas Gregor558cb562009-04-02 01:08:08 +0000198}
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +0000199
Douglas Gregoraee526e2011-09-29 00:38:00 +0000200DiagnosticConsumer *FixItRewriter::clone(DiagnosticsEngine &Diags) const {
201 return new FixItRewriter(Diags, Diags.getSourceManager(),
202 Rewrite.getLangOpts(), FixItOpts);
203}
204
Nick Lewycky1450f262010-08-13 17:31:00 +0000205FixItOptions::~FixItOptions() {}