1 //===- FixItRewriter.cpp - Fix-It Rewriter Diagnostic Client --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "clang/Rewrite/Frontend/FixItRewriter.h"
17 #include "clang/Basic/Diagnostic.h"
18 #include "clang/Basic/FileManager.h"
19 #include "clang/Basic/LLVM.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "clang/Basic/SourceManager.h"
22 #include "clang/Edit/Commit.h"
23 #include "clang/Edit/EditsReceiver.h"
24 #include "clang/Frontend/FrontendDiagnostic.h"
25 #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"
29 #include "llvm/Support/raw_ostream.h"
33 #include <system_error>
36 using namespace clang;
38 FixItRewriter::FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
39 const LangOptions &LangOpts,
40 FixItOptions *FixItOpts)
41 : Diags(Diags), Editor(SourceMgr, LangOpts), Rewrite(SourceMgr, LangOpts),
42 FixItOpts(FixItOpts) {
43 Owner = Diags.takeClient();
44 Client = Diags.getClient();
45 Diags.setClient(this, false);
48 FixItRewriter::~FixItRewriter() {
49 Diags.setClient(Client, Owner.release() != nullptr);
52 bool FixItRewriter::WriteFixedFile(FileID ID, raw_ostream &OS) {
53 const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID);
54 if (!RewriteBuf) return true;
55 RewriteBuf->write(OS);
62 class RewritesReceiver : public edit::EditsReceiver {
66 RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) {}
68 void insert(SourceLocation loc, StringRef text) override {
69 Rewrite.InsertText(loc, text);
72 void replace(CharSourceRange range, StringRef text) override {
73 Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text);
79 bool FixItRewriter::WriteFixedFiles(
80 std::vector<std::pair<std::string, std::string>> *RewrittenFiles) {
81 if (NumFailures > 0 && !FixItOpts->FixWhatYouCan) {
82 Diag(FullSourceLoc(), diag::warn_fixit_no_changes);
86 RewritesReceiver Rec(Rewrite);
87 Editor.applyRewrites(Rec);
89 if (FixItOpts->InPlace) {
90 // Overwriting open files on Windows is tricky, but the rewriter can do it
92 Rewrite.overwriteChangedFiles();
96 for (iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
97 const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first);
99 std::string Filename = FixItOpts->RewriteFilename(Entry->getName(), fd);
101 std::unique_ptr<llvm::raw_fd_ostream> OS;
103 OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
105 OS.reset(new llvm::raw_fd_ostream(Filename, EC, llvm::sys::fs::F_None));
108 Diags.Report(clang::diag::err_fe_unable_to_open_output) << Filename
112 RewriteBuffer &RewriteBuf = I->second;
113 RewriteBuf.write(*OS);
117 RewrittenFiles->push_back(std::make_pair(Entry->getName(), Filename));
123 bool FixItRewriter::IncludeInDiagnosticCounts() const {
124 return Client ? Client->IncludeInDiagnosticCounts() : true;
127 void FixItRewriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
128 const Diagnostic &Info) {
129 // Default implementation (Warnings/errors count).
130 DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
132 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;
139 PrevDiagSilenced = true;
142 // Skip over any diagnostics that are ignored or notes.
143 if (DiagLevel <= DiagnosticsEngine::Note)
145 // Skip over errors if we are only fixing warnings.
146 if (DiagLevel >= DiagnosticsEngine::Error && FixItOpts->FixOnlyWarnings) {
151 // Make sure that we can perform all of the modifications we
152 // in this diagnostic.
153 edit::Commit commit(Editor);
154 for (unsigned Idx = 0, Last = Info.getNumFixItHints();
156 const FixItHint &Hint = Info.getFixItHint(Idx);
158 if (Hint.CodeToInsert.empty()) {
159 if (Hint.InsertFromRange.isValid())
160 commit.insertFromRange(Hint.RemoveRange.getBegin(),
161 Hint.InsertFromRange, /*afterToken=*/false,
162 Hint.BeforePreviousInsertions);
164 commit.remove(Hint.RemoveRange);
166 if (Hint.RemoveRange.isTokenRange() ||
167 Hint.RemoveRange.getBegin() != Hint.RemoveRange.getEnd())
168 commit.replace(Hint.RemoveRange, Hint.CodeToInsert);
170 commit.insert(Hint.RemoveRange.getBegin(), Hint.CodeToInsert,
171 /*afterToken=*/false, Hint.BeforePreviousInsertions);
174 bool CanRewrite = Info.getNumFixItHints() > 0 && commit.isCommitable();
177 if (Info.getNumFixItHints() > 0)
178 Diag(Info.getLocation(), diag::note_fixit_in_macro);
180 // If this was an error, refuse to perform any rewriting.
181 if (DiagLevel >= DiagnosticsEngine::Error) {
182 if (++NumFailures == 1)
183 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
188 if (!Editor.commit(commit)) {
190 Diag(Info.getLocation(), diag::note_fixit_failed);
194 Diag(Info.getLocation(), diag::note_fixit_applied);
197 /// Emit a diagnostic via the adapted diagnostic client.
198 void FixItRewriter::Diag(SourceLocation Loc, unsigned DiagID) {
199 // When producing this diagnostic, we temporarily bypass ourselves,
200 // clear out any current diagnostic, and let the downstream client
201 // format the diagnostic.
202 Diags.setClient(Client, false);
204 Diags.Report(Loc, DiagID);
205 Diags.setClient(this, false);
208 FixItOptions::~FixItOptions() = default;