blob: f1776239d5351c29781f005c4e418570a8ab66f6 [file] [log] [blame]
Douglas Gregor578dae52009-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 Gregor068913e2009-04-02 16:34:42 +000015
Ted Kremenekcdf81492012-09-01 05:09:24 +000016#include "clang/Rewrite/Frontend/FixItRewriter.h"
Nick Lewyckya1e20de2010-04-15 06:46:58 +000017#include "clang/Basic/FileManager.h"
18#include "clang/Basic/SourceLocation.h"
Douglas Gregor068913e2009-04-02 16:34:42 +000019#include "clang/Basic/SourceManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/Edit/Commit.h"
21#include "clang/Edit/EditsReceiver.h"
Douglas Gregora42bd842009-04-02 17:13:00 +000022#include "clang/Frontend/FrontendDiagnostic.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "llvm/Support/Path.h"
24#include "llvm/Support/raw_ostream.h"
Torok Edwindb714922009-08-24 13:25:12 +000025#include <cstdio>
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000026#include <memory>
Torok Edwindb714922009-08-24 13:25:12 +000027
Douglas Gregor578dae52009-04-02 01:08:08 +000028using namespace clang;
29
David Blaikie9c902b52011-09-25 23:23:43 +000030FixItRewriter::FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Nick Lewycky784fad72010-04-24 01:30:46 +000031 const LangOptions &LangOpts,
Nick Lewycky078a5e22010-08-13 17:31:00 +000032 FixItOptions *FixItOpts)
Nick Lewycky784fad72010-04-24 01:30:46 +000033 : Diags(Diags),
Ted Kremenekf7639e12012-03-06 20:06:33 +000034 Editor(SourceMgr, LangOpts),
Nick Lewycky784fad72010-04-24 01:30:46 +000035 Rewrite(SourceMgr, LangOpts),
Nick Lewycky078a5e22010-08-13 17:31:00 +000036 FixItOpts(FixItOpts),
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +000037 NumFailures(0),
38 PrevDiagSilenced(false) {
39 OwnsClient = Diags.ownsClient();
Douglas Gregor2dd19f12010-08-18 22:29:43 +000040 Client = Diags.takeClient();
Douglas Gregora42bd842009-04-02 17:13:00 +000041 Diags.setClient(this);
Douglas Gregor578dae52009-04-02 01:08:08 +000042}
43
44FixItRewriter::~FixItRewriter() {
Douglas Gregor2dd19f12010-08-18 22:29:43 +000045 Diags.takeClient();
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +000046 Diags.setClient(Client, OwnsClient);
Douglas Gregor578dae52009-04-02 01:08:08 +000047}
48
Chris Lattner0e62c1c2011-07-23 10:55:15 +000049bool FixItRewriter::WriteFixedFile(FileID ID, raw_ostream &OS) {
Nick Lewyckya1e20de2010-04-15 06:46:58 +000050 const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID);
51 if (!RewriteBuf) return true;
Nick Lewycky40884c02010-04-16 18:49:45 +000052 RewriteBuf->write(OS);
Nick Lewyckya1e20de2010-04-15 06:46:58 +000053 OS.flush();
54 return false;
55}
56
Ted Kremenekf7639e12012-03-06 20:06:33 +000057namespace {
58
59class RewritesReceiver : public edit::EditsReceiver {
60 Rewriter &Rewrite;
61
62public:
63 RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) { }
64
Craig Topperfb6b25b2014-03-15 04:29:04 +000065 void insert(SourceLocation loc, StringRef text) override {
Ted Kremenekf7639e12012-03-06 20:06:33 +000066 Rewrite.InsertText(loc, text);
67 }
Craig Topperfb6b25b2014-03-15 04:29:04 +000068 void replace(CharSourceRange range, StringRef text) override {
Ted Kremenekf7639e12012-03-06 20:06:33 +000069 Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text);
70 }
71};
72
73}
74
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +000075bool FixItRewriter::WriteFixedFiles(
76 std::vector<std::pair<std::string, std::string> > *RewrittenFiles) {
Nick Lewycky53f10422010-08-15 16:47:39 +000077 if (NumFailures > 0 && !FixItOpts->FixWhatYouCan) {
Douglas Gregora42bd842009-04-02 17:13:00 +000078 Diag(FullSourceLoc(), diag::warn_fixit_no_changes);
Douglas Gregor578dae52009-04-02 01:08:08 +000079 return true;
80 }
81
Ted Kremenekf7639e12012-03-06 20:06:33 +000082 RewritesReceiver Rec(Rewrite);
83 Editor.applyRewrites(Rec);
84
Nick Lewyckya1e20de2010-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 Kyrtzidis623e8772012-01-26 04:19:04 +000087 int fd;
88 std::string Filename = FixItOpts->RewriteFilename(Entry->getName(), fd);
Rafael Espindoladae941a2014-08-25 18:17:04 +000089 std::error_code EC;
Ahmed Charlesb8984322014-03-07 20:03:18 +000090 std::unique_ptr<llvm::raw_fd_ostream> OS;
Argyrios Kyrtzidis623e8772012-01-26 04:19:04 +000091 if (fd != -1) {
92 OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
93 } else {
Rafael Espindoladae941a2014-08-25 18:17:04 +000094 OS.reset(new llvm::raw_fd_ostream(Filename, EC, llvm::sys::fs::F_None));
Argyrios Kyrtzidis623e8772012-01-26 04:19:04 +000095 }
Rafael Espindoladae941a2014-08-25 18:17:04 +000096 if (EC) {
97 Diags.Report(clang::diag::err_fe_unable_to_open_output) << Filename
98 << EC.message();
Nick Lewyckya1e20de2010-04-15 06:46:58 +000099 continue;
100 }
101 RewriteBuffer &RewriteBuf = I->second;
Argyrios Kyrtzidis623e8772012-01-26 04:19:04 +0000102 RewriteBuf.write(*OS);
103 OS->flush();
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000104
105 if (RewrittenFiles)
106 RewrittenFiles->push_back(std::make_pair(Entry->getName(), Filename));
Mike Stump11289f42009-09-09 15:08:12 +0000107 }
Douglas Gregor578dae52009-04-02 01:08:08 +0000108
Douglas Gregor578dae52009-04-02 01:08:08 +0000109 return false;
110}
111
112bool FixItRewriter::IncludeInDiagnosticCounts() const {
Nick Lewyckya1e20de2010-04-15 06:46:58 +0000113 return Client ? Client->IncludeInDiagnosticCounts() : true;
Douglas Gregor578dae52009-04-02 01:08:08 +0000114}
115
David Blaikie9c902b52011-09-25 23:23:43 +0000116void FixItRewriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikieb5784322011-09-26 01:18:08 +0000117 const Diagnostic &Info) {
Argyrios Kyrtzidis6d35b5a2010-11-18 21:13:54 +0000118 // Default implementation (Warnings/errors count).
David Blaikiee2eefae2011-09-25 23:39:51 +0000119 DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
Argyrios Kyrtzidis6d35b5a2010-11-18 21:13:54 +0000120
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000121 if (!FixItOpts->Silent ||
122 DiagLevel >= DiagnosticsEngine::Error ||
123 (DiagLevel == DiagnosticsEngine::Note && !PrevDiagSilenced) ||
124 (DiagLevel > DiagnosticsEngine::Note && Info.getNumFixItHints())) {
125 Client->HandleDiagnostic(DiagLevel, Info);
126 PrevDiagSilenced = false;
127 } else {
128 PrevDiagSilenced = true;
129 }
Douglas Gregor9c0d38a2009-04-02 19:05:20 +0000130
Nick Lewycky784fad72010-04-24 01:30:46 +0000131 // Skip over any diagnostics that are ignored or notes.
David Blaikie9c902b52011-09-25 23:23:43 +0000132 if (DiagLevel <= DiagnosticsEngine::Note)
Douglas Gregor9c0d38a2009-04-02 19:05:20 +0000133 return;
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000134 // Skip over errors if we are only fixing warnings.
135 if (DiagLevel >= DiagnosticsEngine::Error && FixItOpts->FixOnlyWarnings) {
136 ++NumFailures;
137 return;
138 }
Douglas Gregor9c0d38a2009-04-02 19:05:20 +0000139
Douglas Gregor578dae52009-04-02 01:08:08 +0000140 // Make sure that we can perform all of the modifications we
141 // in this diagnostic.
Ted Kremenekf7639e12012-03-06 20:06:33 +0000142 edit::Commit commit(Editor);
Douglas Gregora771f462010-03-31 17:46:05 +0000143 for (unsigned Idx = 0, Last = Info.getNumFixItHints();
Douglas Gregor068913e2009-04-02 16:34:42 +0000144 Idx < Last; ++Idx) {
Douglas Gregora771f462010-03-31 17:46:05 +0000145 const FixItHint &Hint = Info.getFixItHint(Idx);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000146
147 if (Hint.CodeToInsert.empty()) {
148 if (Hint.InsertFromRange.isValid())
149 commit.insertFromRange(Hint.RemoveRange.getBegin(),
150 Hint.InsertFromRange, /*afterToken=*/false,
151 Hint.BeforePreviousInsertions);
152 else
153 commit.remove(Hint.RemoveRange);
154 } else {
155 if (Hint.RemoveRange.isTokenRange() ||
156 Hint.RemoveRange.getBegin() != Hint.RemoveRange.getEnd())
157 commit.replace(Hint.RemoveRange, Hint.CodeToInsert);
158 else
159 commit.insert(Hint.RemoveRange.getBegin(), Hint.CodeToInsert,
160 /*afterToken=*/false, Hint.BeforePreviousInsertions);
Douglas Gregor578dae52009-04-02 01:08:08 +0000161 }
Douglas Gregor578dae52009-04-02 01:08:08 +0000162 }
Ted Kremenekf7639e12012-03-06 20:06:33 +0000163 bool CanRewrite = Info.getNumFixItHints() > 0 && commit.isCommitable();
Douglas Gregor578dae52009-04-02 01:08:08 +0000164
Mike Stump11289f42009-09-09 15:08:12 +0000165 if (!CanRewrite) {
Douglas Gregora771f462010-03-31 17:46:05 +0000166 if (Info.getNumFixItHints() > 0)
Douglas Gregora42bd842009-04-02 17:13:00 +0000167 Diag(Info.getLocation(), diag::note_fixit_in_macro);
Douglas Gregor068913e2009-04-02 16:34:42 +0000168
169 // If this was an error, refuse to perform any rewriting.
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000170 if (DiagLevel >= DiagnosticsEngine::Error) {
Douglas Gregora42bd842009-04-02 17:13:00 +0000171 if (++NumFailures == 1)
172 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
Douglas Gregor068913e2009-04-02 16:34:42 +0000173 }
Douglas Gregor578dae52009-04-02 01:08:08 +0000174 return;
Douglas Gregor068913e2009-04-02 16:34:42 +0000175 }
Ted Kremenekf7639e12012-03-06 20:06:33 +0000176
177 if (!Editor.commit(commit)) {
Douglas Gregor578dae52009-04-02 01:08:08 +0000178 ++NumFailures;
Douglas Gregora42bd842009-04-02 17:13:00 +0000179 Diag(Info.getLocation(), diag::note_fixit_failed);
180 return;
181 }
182
183 Diag(Info.getLocation(), diag::note_fixit_applied);
184}
185
186/// \brief Emit a diagnostic via the adapted diagnostic client.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000187void FixItRewriter::Diag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregora42bd842009-04-02 17:13:00 +0000188 // When producing this diagnostic, we temporarily bypass ourselves,
189 // clear out any current diagnostic, and let the downstream client
190 // format the diagnostic.
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000191 Diags.takeClient();
Douglas Gregora42bd842009-04-02 17:13:00 +0000192 Diags.setClient(Client);
193 Diags.Clear();
194 Diags.Report(Loc, DiagID);
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000195 Diags.takeClient();
Mike Stump11289f42009-09-09 15:08:12 +0000196 Diags.setClient(this);
Douglas Gregor578dae52009-04-02 01:08:08 +0000197}
Nick Lewycky784fad72010-04-24 01:30:46 +0000198
Nick Lewycky078a5e22010-08-13 17:31:00 +0000199FixItOptions::~FixItOptions() {}