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(Diagnostic &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, llvm::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(Diagnostic::Level DiagLevel,
82 const DiagnosticInfo &Info) {
83 // Default implementation (Warnings/errors count).
84 DiagnosticClient::HandleDiagnostic(DiagLevel, Info);
86 Client->HandleDiagnostic(DiagLevel, Info);
88 // Skip over any diagnostics that are ignored or notes.
89 if (DiagLevel <= Diagnostic::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 == Diagnostic::Error || DiagLevel == Diagnostic::Fatal) {
111 if (++NumFailures == 1)
112 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
118 for (unsigned Idx = 0, Last = Info.getNumFixItHints();
120 const FixItHint &Hint = Info.getFixItHint(Idx);
122 if (Hint.CodeToInsert.empty()) {
123 // We're removing code.
124 if (Rewrite.RemoveText(Hint.RemoveRange))
129 // We're replacing code.
130 if (Rewrite.ReplaceText(Hint.RemoveRange.getBegin(),
131 Rewrite.getRangeSize(Hint.RemoveRange),
138 Diag(Info.getLocation(), diag::note_fixit_failed);
142 Diag(Info.getLocation(), diag::note_fixit_applied);
145 /// \brief Emit a diagnostic via the adapted diagnostic client.
146 void FixItRewriter::Diag(SourceLocation Loc, unsigned DiagID) {
147 // When producing this diagnostic, we temporarily bypass ourselves,
148 // clear out any current diagnostic, and let the downstream client
149 // format the diagnostic.
151 Diags.setClient(Client);
153 Diags.Report(Loc, DiagID);
155 Diags.setClient(this);
158 FixItOptions::~FixItOptions() {}