1 //===--- FixItRewriter.cpp - Fix-It Rewriter Diagnostic Client --*- C++ -*-===//
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/FileManager.h"
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/Basic/SourceManager.h"
20 #include "clang/Edit/Commit.h"
21 #include "clang/Edit/EditsReceiver.h"
22 #include "clang/Frontend/FrontendDiagnostic.h"
23 #include "llvm/ADT/OwningPtr.h"
24 #include "llvm/Support/Path.h"
25 #include "llvm/Support/raw_ostream.h"
28 using namespace clang;
30 FixItRewriter::FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
31 const LangOptions &LangOpts,
32 FixItOptions *FixItOpts)
34 Editor(SourceMgr, LangOpts),
35 Rewrite(SourceMgr, LangOpts),
38 PrevDiagSilenced(false) {
39 OwnsClient = Diags.ownsClient();
40 Client = Diags.takeClient();
41 Diags.setClient(this);
44 FixItRewriter::~FixItRewriter() {
46 Diags.setClient(Client, OwnsClient);
49 bool FixItRewriter::WriteFixedFile(FileID ID, raw_ostream &OS) {
50 const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID);
51 if (!RewriteBuf) return true;
52 RewriteBuf->write(OS);
59 class RewritesReceiver : public edit::EditsReceiver {
63 RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) { }
65 virtual void insert(SourceLocation loc, StringRef text) {
66 Rewrite.InsertText(loc, text);
68 virtual void replace(CharSourceRange range, StringRef text) {
69 Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text);
75 bool FixItRewriter::WriteFixedFiles(
76 std::vector<std::pair<std::string, std::string> > *RewrittenFiles) {
77 if (NumFailures > 0 && !FixItOpts->FixWhatYouCan) {
78 Diag(FullSourceLoc(), diag::warn_fixit_no_changes);
82 RewritesReceiver Rec(Rewrite);
83 Editor.applyRewrites(Rec);
85 for (iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
86 const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first);
88 std::string Filename = FixItOpts->RewriteFilename(Entry->getName(), fd);
90 OwningPtr<llvm::raw_fd_ostream> OS;
92 OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
94 OS.reset(new llvm::raw_fd_ostream(Filename.c_str(), Err,
95 llvm::raw_fd_ostream::F_Binary));
98 Diags.Report(clang::diag::err_fe_unable_to_open_output)
102 RewriteBuffer &RewriteBuf = I->second;
103 RewriteBuf.write(*OS);
107 RewrittenFiles->push_back(std::make_pair(Entry->getName(), Filename));
113 bool FixItRewriter::IncludeInDiagnosticCounts() const {
114 return Client ? Client->IncludeInDiagnosticCounts() : true;
117 void FixItRewriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
118 const Diagnostic &Info) {
119 // Default implementation (Warnings/errors count).
120 DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
122 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;
129 PrevDiagSilenced = true;
132 // Skip over any diagnostics that are ignored or notes.
133 if (DiagLevel <= DiagnosticsEngine::Note)
135 // Skip over errors if we are only fixing warnings.
136 if (DiagLevel >= DiagnosticsEngine::Error && FixItOpts->FixOnlyWarnings) {
141 // Make sure that we can perform all of the modifications we
142 // in this diagnostic.
143 edit::Commit commit(Editor);
144 for (unsigned Idx = 0, Last = Info.getNumFixItHints();
146 const FixItHint &Hint = Info.getFixItHint(Idx);
148 if (Hint.CodeToInsert.empty()) {
149 if (Hint.InsertFromRange.isValid())
150 commit.insertFromRange(Hint.RemoveRange.getBegin(),
151 Hint.InsertFromRange, /*afterToken=*/false,
152 Hint.BeforePreviousInsertions);
154 commit.remove(Hint.RemoveRange);
156 if (Hint.RemoveRange.isTokenRange() ||
157 Hint.RemoveRange.getBegin() != Hint.RemoveRange.getEnd())
158 commit.replace(Hint.RemoveRange, Hint.CodeToInsert);
160 commit.insert(Hint.RemoveRange.getBegin(), Hint.CodeToInsert,
161 /*afterToken=*/false, Hint.BeforePreviousInsertions);
164 bool CanRewrite = Info.getNumFixItHints() > 0 && commit.isCommitable();
167 if (Info.getNumFixItHints() > 0)
168 Diag(Info.getLocation(), diag::note_fixit_in_macro);
170 // If this was an error, refuse to perform any rewriting.
171 if (DiagLevel >= DiagnosticsEngine::Error) {
172 if (++NumFailures == 1)
173 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
178 if (!Editor.commit(commit)) {
180 Diag(Info.getLocation(), diag::note_fixit_failed);
184 Diag(Info.getLocation(), diag::note_fixit_applied);
187 /// \brief Emit a diagnostic via the adapted diagnostic client.
188 void FixItRewriter::Diag(SourceLocation Loc, unsigned DiagID) {
189 // When producing this diagnostic, we temporarily bypass ourselves,
190 // clear out any current diagnostic, and let the downstream client
191 // format the diagnostic.
193 Diags.setClient(Client);
195 Diags.Report(Loc, DiagID);
197 Diags.setClient(this);
200 FixItOptions::~FixItOptions() {}