blob: 1c2efe63aa19927c0acb8fb682d498f0d8b8a78e [file] [log] [blame]
Eugene Zelenkodb914a42018-03-27 00:01:49 +00001//===- FixItRewriter.cpp - Fix-It Rewriter Diagnostic Client --------------===//
Douglas Gregor578dae52009-04-02 01:08:08 +00002//
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"
Eugene Zelenkodb914a42018-03-27 00:01:49 +000017#include "clang/Basic/Diagnostic.h"
Nick Lewyckya1e20de2010-04-15 06:46:58 +000018#include "clang/Basic/FileManager.h"
Eugene Zelenkodb914a42018-03-27 00:01:49 +000019#include "clang/Basic/LLVM.h"
Nick Lewyckya1e20de2010-04-15 06:46:58 +000020#include "clang/Basic/SourceLocation.h"
Douglas Gregor068913e2009-04-02 16:34:42 +000021#include "clang/Basic/SourceManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Edit/Commit.h"
23#include "clang/Edit/EditsReceiver.h"
Douglas Gregora42bd842009-04-02 17:13:00 +000024#include "clang/Frontend/FrontendDiagnostic.h"
Eugene Zelenkodb914a42018-03-27 00:01:49 +000025#include "clang/Rewrite/Core/RewriteBuffer.h"
26#include "clang/Rewrite/Core/Rewriter.h"
27#include "llvm/ADT/StringRef.h"
28#include "llvm/Support/FileSystem.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000029#include "llvm/Support/raw_ostream.h"
Torok Edwindb714922009-08-24 13:25:12 +000030#include <cstdio>
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000031#include <memory>
Eugene Zelenkodb914a42018-03-27 00:01:49 +000032#include <string>
33#include <system_error>
34#include <utility>
Torok Edwindb714922009-08-24 13:25:12 +000035
Douglas Gregor578dae52009-04-02 01:08:08 +000036using namespace clang;
37
David Blaikie9c902b52011-09-25 23:23:43 +000038FixItRewriter::FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Nick Lewycky784fad72010-04-24 01:30:46 +000039 const LangOptions &LangOpts,
Nick Lewycky078a5e22010-08-13 17:31:00 +000040 FixItOptions *FixItOpts)
Eugene Zelenkodb914a42018-03-27 00:01:49 +000041 : Diags(Diags), Editor(SourceMgr, LangOpts), Rewrite(SourceMgr, LangOpts),
42 FixItOpts(FixItOpts) {
Alexander Kornienko41c247a2014-11-17 23:46:02 +000043 Owner = Diags.takeClient();
44 Client = Diags.getClient();
45 Diags.setClient(this, false);
Douglas Gregor578dae52009-04-02 01:08:08 +000046}
47
48FixItRewriter::~FixItRewriter() {
Alexander Kornienko41c247a2014-11-17 23:46:02 +000049 Diags.setClient(Client, Owner.release() != nullptr);
Douglas Gregor578dae52009-04-02 01:08:08 +000050}
51
Chris Lattner0e62c1c2011-07-23 10:55:15 +000052bool FixItRewriter::WriteFixedFile(FileID ID, raw_ostream &OS) {
Nick Lewyckya1e20de2010-04-15 06:46:58 +000053 const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID);
54 if (!RewriteBuf) return true;
Nick Lewycky40884c02010-04-16 18:49:45 +000055 RewriteBuf->write(OS);
Nick Lewyckya1e20de2010-04-15 06:46:58 +000056 OS.flush();
57 return false;
58}
59
Ted Kremenekf7639e12012-03-06 20:06:33 +000060namespace {
61
62class RewritesReceiver : public edit::EditsReceiver {
63 Rewriter &Rewrite;
64
65public:
Eugene Zelenkodb914a42018-03-27 00:01:49 +000066 RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) {}
Ted Kremenekf7639e12012-03-06 20:06:33 +000067
Craig Topperfb6b25b2014-03-15 04:29:04 +000068 void insert(SourceLocation loc, StringRef text) override {
Ted Kremenekf7639e12012-03-06 20:06:33 +000069 Rewrite.InsertText(loc, text);
70 }
Eugene Zelenkodb914a42018-03-27 00:01:49 +000071
Craig Topperfb6b25b2014-03-15 04:29:04 +000072 void replace(CharSourceRange range, StringRef text) override {
Ted Kremenekf7639e12012-03-06 20:06:33 +000073 Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text);
74 }
75};
76
Eugene Zelenkodb914a42018-03-27 00:01:49 +000077} // namespace
Ted Kremenekf7639e12012-03-06 20:06:33 +000078
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +000079bool FixItRewriter::WriteFixedFiles(
Eugene Zelenkodb914a42018-03-27 00:01:49 +000080 std::vector<std::pair<std::string, std::string>> *RewrittenFiles) {
Nick Lewycky53f10422010-08-15 16:47:39 +000081 if (NumFailures > 0 && !FixItOpts->FixWhatYouCan) {
Douglas Gregora42bd842009-04-02 17:13:00 +000082 Diag(FullSourceLoc(), diag::warn_fixit_no_changes);
Douglas Gregor578dae52009-04-02 01:08:08 +000083 return true;
84 }
85
Ted Kremenekf7639e12012-03-06 20:06:33 +000086 RewritesReceiver Rec(Rewrite);
87 Editor.applyRewrites(Rec);
88
Reid Kleckner3df5dd42015-06-17 17:47:30 +000089 if (FixItOpts->InPlace) {
90 // Overwriting open files on Windows is tricky, but the rewriter can do it
91 // for us.
92 Rewrite.overwriteChangedFiles();
93 return false;
94 }
95
Nick Lewyckya1e20de2010-04-15 06:46:58 +000096 for (iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
97 const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first);
Argyrios Kyrtzidis623e8772012-01-26 04:19:04 +000098 int fd;
99 std::string Filename = FixItOpts->RewriteFilename(Entry->getName(), fd);
Rafael Espindoladae941a2014-08-25 18:17:04 +0000100 std::error_code EC;
Ahmed Charlesb8984322014-03-07 20:03:18 +0000101 std::unique_ptr<llvm::raw_fd_ostream> OS;
Argyrios Kyrtzidis623e8772012-01-26 04:19:04 +0000102 if (fd != -1) {
103 OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
104 } else {
Rafael Espindoladae941a2014-08-25 18:17:04 +0000105 OS.reset(new llvm::raw_fd_ostream(Filename, EC, llvm::sys::fs::F_None));
Argyrios Kyrtzidis623e8772012-01-26 04:19:04 +0000106 }
Rafael Espindoladae941a2014-08-25 18:17:04 +0000107 if (EC) {
108 Diags.Report(clang::diag::err_fe_unable_to_open_output) << Filename
109 << EC.message();
Nick Lewyckya1e20de2010-04-15 06:46:58 +0000110 continue;
111 }
112 RewriteBuffer &RewriteBuf = I->second;
Argyrios Kyrtzidis623e8772012-01-26 04:19:04 +0000113 RewriteBuf.write(*OS);
114 OS->flush();
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000115
116 if (RewrittenFiles)
117 RewrittenFiles->push_back(std::make_pair(Entry->getName(), Filename));
Mike Stump11289f42009-09-09 15:08:12 +0000118 }
Douglas Gregor578dae52009-04-02 01:08:08 +0000119
Douglas Gregor578dae52009-04-02 01:08:08 +0000120 return false;
121}
122
123bool FixItRewriter::IncludeInDiagnosticCounts() const {
Nick Lewyckya1e20de2010-04-15 06:46:58 +0000124 return Client ? Client->IncludeInDiagnosticCounts() : true;
Douglas Gregor578dae52009-04-02 01:08:08 +0000125}
126
David Blaikie9c902b52011-09-25 23:23:43 +0000127void FixItRewriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikieb5784322011-09-26 01:18:08 +0000128 const Diagnostic &Info) {
Argyrios Kyrtzidis6d35b5a2010-11-18 21:13:54 +0000129 // Default implementation (Warnings/errors count).
David Blaikiee2eefae2011-09-25 23:39:51 +0000130 DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
Argyrios Kyrtzidis6d35b5a2010-11-18 21:13:54 +0000131
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000132 if (!FixItOpts->Silent ||
133 DiagLevel >= DiagnosticsEngine::Error ||
134 (DiagLevel == DiagnosticsEngine::Note && !PrevDiagSilenced) ||
135 (DiagLevel > DiagnosticsEngine::Note && Info.getNumFixItHints())) {
136 Client->HandleDiagnostic(DiagLevel, Info);
137 PrevDiagSilenced = false;
138 } else {
139 PrevDiagSilenced = true;
140 }
Douglas Gregor9c0d38a2009-04-02 19:05:20 +0000141
Nick Lewycky784fad72010-04-24 01:30:46 +0000142 // Skip over any diagnostics that are ignored or notes.
David Blaikie9c902b52011-09-25 23:23:43 +0000143 if (DiagLevel <= DiagnosticsEngine::Note)
Douglas Gregor9c0d38a2009-04-02 19:05:20 +0000144 return;
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000145 // Skip over errors if we are only fixing warnings.
146 if (DiagLevel >= DiagnosticsEngine::Error && FixItOpts->FixOnlyWarnings) {
147 ++NumFailures;
148 return;
149 }
Douglas Gregor9c0d38a2009-04-02 19:05:20 +0000150
Douglas Gregor578dae52009-04-02 01:08:08 +0000151 // Make sure that we can perform all of the modifications we
152 // in this diagnostic.
Ted Kremenekf7639e12012-03-06 20:06:33 +0000153 edit::Commit commit(Editor);
Douglas Gregora771f462010-03-31 17:46:05 +0000154 for (unsigned Idx = 0, Last = Info.getNumFixItHints();
Douglas Gregor068913e2009-04-02 16:34:42 +0000155 Idx < Last; ++Idx) {
Douglas Gregora771f462010-03-31 17:46:05 +0000156 const FixItHint &Hint = Info.getFixItHint(Idx);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000157
158 if (Hint.CodeToInsert.empty()) {
159 if (Hint.InsertFromRange.isValid())
160 commit.insertFromRange(Hint.RemoveRange.getBegin(),
161 Hint.InsertFromRange, /*afterToken=*/false,
162 Hint.BeforePreviousInsertions);
163 else
164 commit.remove(Hint.RemoveRange);
165 } else {
166 if (Hint.RemoveRange.isTokenRange() ||
167 Hint.RemoveRange.getBegin() != Hint.RemoveRange.getEnd())
168 commit.replace(Hint.RemoveRange, Hint.CodeToInsert);
169 else
170 commit.insert(Hint.RemoveRange.getBegin(), Hint.CodeToInsert,
171 /*afterToken=*/false, Hint.BeforePreviousInsertions);
Douglas Gregor578dae52009-04-02 01:08:08 +0000172 }
Douglas Gregor578dae52009-04-02 01:08:08 +0000173 }
Ted Kremenekf7639e12012-03-06 20:06:33 +0000174 bool CanRewrite = Info.getNumFixItHints() > 0 && commit.isCommitable();
Douglas Gregor578dae52009-04-02 01:08:08 +0000175
Mike Stump11289f42009-09-09 15:08:12 +0000176 if (!CanRewrite) {
Douglas Gregora771f462010-03-31 17:46:05 +0000177 if (Info.getNumFixItHints() > 0)
Douglas Gregora42bd842009-04-02 17:13:00 +0000178 Diag(Info.getLocation(), diag::note_fixit_in_macro);
Douglas Gregor068913e2009-04-02 16:34:42 +0000179
180 // If this was an error, refuse to perform any rewriting.
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000181 if (DiagLevel >= DiagnosticsEngine::Error) {
Douglas Gregora42bd842009-04-02 17:13:00 +0000182 if (++NumFailures == 1)
183 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
Douglas Gregor068913e2009-04-02 16:34:42 +0000184 }
Douglas Gregor578dae52009-04-02 01:08:08 +0000185 return;
Douglas Gregor068913e2009-04-02 16:34:42 +0000186 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000187
Ted Kremenekf7639e12012-03-06 20:06:33 +0000188 if (!Editor.commit(commit)) {
Douglas Gregor578dae52009-04-02 01:08:08 +0000189 ++NumFailures;
Douglas Gregora42bd842009-04-02 17:13:00 +0000190 Diag(Info.getLocation(), diag::note_fixit_failed);
191 return;
192 }
193
194 Diag(Info.getLocation(), diag::note_fixit_applied);
195}
196
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000197/// Emit a diagnostic via the adapted diagnostic client.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000198void FixItRewriter::Diag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregora42bd842009-04-02 17:13:00 +0000199 // When producing this diagnostic, we temporarily bypass ourselves,
200 // clear out any current diagnostic, and let the downstream client
201 // format the diagnostic.
Alexander Kornienko41c247a2014-11-17 23:46:02 +0000202 Diags.setClient(Client, false);
Douglas Gregora42bd842009-04-02 17:13:00 +0000203 Diags.Clear();
204 Diags.Report(Loc, DiagID);
Alexander Kornienko41c247a2014-11-17 23:46:02 +0000205 Diags.setClient(this, false);
Douglas Gregor578dae52009-04-02 01:08:08 +0000206}
Nick Lewycky784fad72010-04-24 01:30:46 +0000207
Eugene Zelenkodb914a42018-03-27 00:01:49 +0000208FixItOptions::~FixItOptions() = default;