blob: dc787ac9557c41c5a27146580532e4123e3eea0f [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) {
Alexander Kornienko41c247a2014-11-17 23:46:02 +000039 Owner = Diags.takeClient();
40 Client = Diags.getClient();
41 Diags.setClient(this, false);
Douglas Gregor578dae52009-04-02 01:08:08 +000042}
43
44FixItRewriter::~FixItRewriter() {
Alexander Kornienko41c247a2014-11-17 23:46:02 +000045 Diags.setClient(Client, Owner.release() != nullptr);
Douglas Gregor578dae52009-04-02 01:08:08 +000046}
47
Chris Lattner0e62c1c2011-07-23 10:55:15 +000048bool FixItRewriter::WriteFixedFile(FileID ID, raw_ostream &OS) {
Nick Lewyckya1e20de2010-04-15 06:46:58 +000049 const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID);
50 if (!RewriteBuf) return true;
Nick Lewycky40884c02010-04-16 18:49:45 +000051 RewriteBuf->write(OS);
Nick Lewyckya1e20de2010-04-15 06:46:58 +000052 OS.flush();
53 return false;
54}
55
Ted Kremenekf7639e12012-03-06 20:06:33 +000056namespace {
57
58class RewritesReceiver : public edit::EditsReceiver {
59 Rewriter &Rewrite;
60
61public:
62 RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) { }
63
Craig Topperfb6b25b2014-03-15 04:29:04 +000064 void insert(SourceLocation loc, StringRef text) override {
Ted Kremenekf7639e12012-03-06 20:06:33 +000065 Rewrite.InsertText(loc, text);
66 }
Craig Topperfb6b25b2014-03-15 04:29:04 +000067 void replace(CharSourceRange range, StringRef text) override {
Ted Kremenekf7639e12012-03-06 20:06:33 +000068 Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text);
69 }
70};
71
Alexander Kornienkoab9db512015-06-22 23:07:51 +000072}
Ted Kremenekf7639e12012-03-06 20:06:33 +000073
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +000074bool FixItRewriter::WriteFixedFiles(
75 std::vector<std::pair<std::string, std::string> > *RewrittenFiles) {
Nick Lewycky53f10422010-08-15 16:47:39 +000076 if (NumFailures > 0 && !FixItOpts->FixWhatYouCan) {
Douglas Gregora42bd842009-04-02 17:13:00 +000077 Diag(FullSourceLoc(), diag::warn_fixit_no_changes);
Douglas Gregor578dae52009-04-02 01:08:08 +000078 return true;
79 }
80
Ted Kremenekf7639e12012-03-06 20:06:33 +000081 RewritesReceiver Rec(Rewrite);
82 Editor.applyRewrites(Rec);
83
Reid Kleckner3df5dd42015-06-17 17:47:30 +000084 if (FixItOpts->InPlace) {
85 // Overwriting open files on Windows is tricky, but the rewriter can do it
86 // for us.
87 Rewrite.overwriteChangedFiles();
88 return false;
89 }
90
Nick Lewyckya1e20de2010-04-15 06:46:58 +000091 for (iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
92 const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first);
Argyrios Kyrtzidis623e8772012-01-26 04:19:04 +000093 int fd;
94 std::string Filename = FixItOpts->RewriteFilename(Entry->getName(), fd);
Rafael Espindoladae941a2014-08-25 18:17:04 +000095 std::error_code EC;
Ahmed Charlesb8984322014-03-07 20:03:18 +000096 std::unique_ptr<llvm::raw_fd_ostream> OS;
Argyrios Kyrtzidis623e8772012-01-26 04:19:04 +000097 if (fd != -1) {
98 OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
99 } else {
Rafael Espindoladae941a2014-08-25 18:17:04 +0000100 OS.reset(new llvm::raw_fd_ostream(Filename, EC, llvm::sys::fs::F_None));
Argyrios Kyrtzidis623e8772012-01-26 04:19:04 +0000101 }
Rafael Espindoladae941a2014-08-25 18:17:04 +0000102 if (EC) {
103 Diags.Report(clang::diag::err_fe_unable_to_open_output) << Filename
104 << EC.message();
Nick Lewyckya1e20de2010-04-15 06:46:58 +0000105 continue;
106 }
107 RewriteBuffer &RewriteBuf = I->second;
Argyrios Kyrtzidis623e8772012-01-26 04:19:04 +0000108 RewriteBuf.write(*OS);
109 OS->flush();
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000110
111 if (RewrittenFiles)
112 RewrittenFiles->push_back(std::make_pair(Entry->getName(), Filename));
Mike Stump11289f42009-09-09 15:08:12 +0000113 }
Douglas Gregor578dae52009-04-02 01:08:08 +0000114
Douglas Gregor578dae52009-04-02 01:08:08 +0000115 return false;
116}
117
118bool FixItRewriter::IncludeInDiagnosticCounts() const {
Nick Lewyckya1e20de2010-04-15 06:46:58 +0000119 return Client ? Client->IncludeInDiagnosticCounts() : true;
Douglas Gregor578dae52009-04-02 01:08:08 +0000120}
121
David Blaikie9c902b52011-09-25 23:23:43 +0000122void FixItRewriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikieb5784322011-09-26 01:18:08 +0000123 const Diagnostic &Info) {
Argyrios Kyrtzidis6d35b5a2010-11-18 21:13:54 +0000124 // Default implementation (Warnings/errors count).
David Blaikiee2eefae2011-09-25 23:39:51 +0000125 DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
Argyrios Kyrtzidis6d35b5a2010-11-18 21:13:54 +0000126
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000127 if (!FixItOpts->Silent ||
128 DiagLevel >= DiagnosticsEngine::Error ||
129 (DiagLevel == DiagnosticsEngine::Note && !PrevDiagSilenced) ||
130 (DiagLevel > DiagnosticsEngine::Note && Info.getNumFixItHints())) {
131 Client->HandleDiagnostic(DiagLevel, Info);
132 PrevDiagSilenced = false;
133 } else {
134 PrevDiagSilenced = true;
135 }
Douglas Gregor9c0d38a2009-04-02 19:05:20 +0000136
Nick Lewycky784fad72010-04-24 01:30:46 +0000137 // Skip over any diagnostics that are ignored or notes.
David Blaikie9c902b52011-09-25 23:23:43 +0000138 if (DiagLevel <= DiagnosticsEngine::Note)
Douglas Gregor9c0d38a2009-04-02 19:05:20 +0000139 return;
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000140 // Skip over errors if we are only fixing warnings.
141 if (DiagLevel >= DiagnosticsEngine::Error && FixItOpts->FixOnlyWarnings) {
142 ++NumFailures;
143 return;
144 }
Douglas Gregor9c0d38a2009-04-02 19:05:20 +0000145
Douglas Gregor578dae52009-04-02 01:08:08 +0000146 // Make sure that we can perform all of the modifications we
147 // in this diagnostic.
Ted Kremenekf7639e12012-03-06 20:06:33 +0000148 edit::Commit commit(Editor);
Douglas Gregora771f462010-03-31 17:46:05 +0000149 for (unsigned Idx = 0, Last = Info.getNumFixItHints();
Douglas Gregor068913e2009-04-02 16:34:42 +0000150 Idx < Last; ++Idx) {
Douglas Gregora771f462010-03-31 17:46:05 +0000151 const FixItHint &Hint = Info.getFixItHint(Idx);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000152
153 if (Hint.CodeToInsert.empty()) {
154 if (Hint.InsertFromRange.isValid())
155 commit.insertFromRange(Hint.RemoveRange.getBegin(),
156 Hint.InsertFromRange, /*afterToken=*/false,
157 Hint.BeforePreviousInsertions);
158 else
159 commit.remove(Hint.RemoveRange);
160 } else {
161 if (Hint.RemoveRange.isTokenRange() ||
162 Hint.RemoveRange.getBegin() != Hint.RemoveRange.getEnd())
163 commit.replace(Hint.RemoveRange, Hint.CodeToInsert);
164 else
165 commit.insert(Hint.RemoveRange.getBegin(), Hint.CodeToInsert,
166 /*afterToken=*/false, Hint.BeforePreviousInsertions);
Douglas Gregor578dae52009-04-02 01:08:08 +0000167 }
Douglas Gregor578dae52009-04-02 01:08:08 +0000168 }
Ted Kremenekf7639e12012-03-06 20:06:33 +0000169 bool CanRewrite = Info.getNumFixItHints() > 0 && commit.isCommitable();
Douglas Gregor578dae52009-04-02 01:08:08 +0000170
Mike Stump11289f42009-09-09 15:08:12 +0000171 if (!CanRewrite) {
Douglas Gregora771f462010-03-31 17:46:05 +0000172 if (Info.getNumFixItHints() > 0)
Douglas Gregora42bd842009-04-02 17:13:00 +0000173 Diag(Info.getLocation(), diag::note_fixit_in_macro);
Douglas Gregor068913e2009-04-02 16:34:42 +0000174
175 // If this was an error, refuse to perform any rewriting.
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000176 if (DiagLevel >= DiagnosticsEngine::Error) {
Douglas Gregora42bd842009-04-02 17:13:00 +0000177 if (++NumFailures == 1)
178 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
Douglas Gregor068913e2009-04-02 16:34:42 +0000179 }
Douglas Gregor578dae52009-04-02 01:08:08 +0000180 return;
Douglas Gregor068913e2009-04-02 16:34:42 +0000181 }
Ted Kremenekf7639e12012-03-06 20:06:33 +0000182
183 if (!Editor.commit(commit)) {
Douglas Gregor578dae52009-04-02 01:08:08 +0000184 ++NumFailures;
Douglas Gregora42bd842009-04-02 17:13:00 +0000185 Diag(Info.getLocation(), diag::note_fixit_failed);
186 return;
187 }
188
189 Diag(Info.getLocation(), diag::note_fixit_applied);
190}
191
192/// \brief Emit a diagnostic via the adapted diagnostic client.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000193void FixItRewriter::Diag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregora42bd842009-04-02 17:13:00 +0000194 // When producing this diagnostic, we temporarily bypass ourselves,
195 // clear out any current diagnostic, and let the downstream client
196 // format the diagnostic.
Alexander Kornienko41c247a2014-11-17 23:46:02 +0000197 Diags.setClient(Client, false);
Douglas Gregora42bd842009-04-02 17:13:00 +0000198 Diags.Clear();
199 Diags.Report(Loc, DiagID);
Alexander Kornienko41c247a2014-11-17 23:46:02 +0000200 Diags.setClient(this, false);
Douglas Gregor578dae52009-04-02 01:08:08 +0000201}
Nick Lewycky784fad72010-04-24 01:30:46 +0000202
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000203FixItOptions::~FixItOptions() {}