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/FixItRewriter.h"
17 #include "clang/Basic/FileManager.h"
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/Basic/SourceManager.h"
20 #include "clang/Frontend/FrontendDiagnostic.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Support/Path.h"
23 #include "llvm/ADT/OwningPtr.h"
26 using namespace clang;
28 FixItRewriter::FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
29 const LangOptions &LangOpts,
30 FixItOptions *FixItOpts)
32 Rewrite(SourceMgr, LangOpts),
35 Client = Diags.takeClient();
36 Diags.setClient(this);
39 FixItRewriter::~FixItRewriter() {
41 Diags.setClient(Client);
44 bool FixItRewriter::WriteFixedFile(FileID ID, raw_ostream &OS) {
45 const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID);
46 if (!RewriteBuf) return true;
47 RewriteBuf->write(OS);
52 bool FixItRewriter::WriteFixedFiles() {
53 if (NumFailures > 0 && !FixItOpts->FixWhatYouCan) {
54 Diag(FullSourceLoc(), diag::warn_fixit_no_changes);
58 for (iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
59 const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first);
60 std::string Filename = FixItOpts->RewriteFilename(Entry->getName());
62 llvm::raw_fd_ostream OS(Filename.c_str(), Err,
63 llvm::raw_fd_ostream::F_Binary);
65 Diags.Report(clang::diag::err_fe_unable_to_open_output)
69 RewriteBuffer &RewriteBuf = I->second;
77 bool FixItRewriter::IncludeInDiagnosticCounts() const {
78 return Client ? Client->IncludeInDiagnosticCounts() : true;
81 void FixItRewriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
82 const Diagnostic &Info) {
83 // Default implementation (Warnings/errors count).
84 DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
86 Client->HandleDiagnostic(DiagLevel, Info);
88 // Skip over any diagnostics that are ignored or notes.
89 if (DiagLevel <= DiagnosticsEngine::Note)
92 // Make sure that we can perform all of the modifications we
93 // in this diagnostic.
94 bool CanRewrite = Info.getNumFixItHints() > 0;
95 for (unsigned Idx = 0, Last = Info.getNumFixItHints();
97 const FixItHint &Hint = Info.getFixItHint(Idx);
98 if (Hint.RemoveRange.isValid() &&
99 Rewrite.getRangeSize(Hint.RemoveRange) == -1) {
106 if (Info.getNumFixItHints() > 0)
107 Diag(Info.getLocation(), diag::note_fixit_in_macro);
109 // If this was an error, refuse to perform any rewriting.
110 if (DiagLevel == DiagnosticsEngine::Error ||
111 DiagLevel == DiagnosticsEngine::Fatal) {
112 if (++NumFailures == 1)
113 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
119 for (unsigned Idx = 0, Last = Info.getNumFixItHints();
121 const FixItHint &Hint = Info.getFixItHint(Idx);
123 if (Hint.CodeToInsert.empty()) {
124 // We're removing code.
125 if (Rewrite.RemoveText(Hint.RemoveRange))
130 // We're replacing code.
131 if (Rewrite.ReplaceText(Hint.RemoveRange.getBegin(),
132 Rewrite.getRangeSize(Hint.RemoveRange),
139 Diag(Info.getLocation(), diag::note_fixit_failed);
143 Diag(Info.getLocation(), diag::note_fixit_applied);
146 /// \brief Emit a diagnostic via the adapted diagnostic client.
147 void FixItRewriter::Diag(SourceLocation Loc, unsigned DiagID) {
148 // When producing this diagnostic, we temporarily bypass ourselves,
149 // clear out any current diagnostic, and let the downstream client
150 // format the diagnostic.
152 Diags.setClient(Client);
154 Diags.Report(Loc, DiagID);
156 Diags.setClient(this);
159 DiagnosticConsumer *FixItRewriter::clone(DiagnosticsEngine &Diags) const {
160 return new FixItRewriter(Diags, Diags.getSourceManager(),
161 Rewrite.getLangOpts(), FixItOpts);
164 FixItOptions::~FixItOptions() {}