2009-04-02 09:08:08 +08:00
|
|
|
//===--- FixItRewriter.cpp - Fix-It Rewriter Diagnostic Client --*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This is a diagnostic client adaptor that performs rewrites as
|
|
|
|
// suggested by code modification hints attached to diagnostics. It
|
|
|
|
// then forwards any diagnostics to the adapted diagnostic client.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-04-03 00:34:42 +08:00
|
|
|
|
2010-06-16 01:48:49 +08:00
|
|
|
#include "clang/Rewrite/FixItRewriter.h"
|
2010-04-15 14:46:58 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/SourceLocation.h"
|
2009-04-03 00:34:42 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2009-04-03 01:13:00 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2009-04-02 09:08:08 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2009-08-23 20:08:50 +08:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
2009-08-24 21:25:12 +08:00
|
|
|
#include <cstdio>
|
|
|
|
|
2009-04-02 09:08:08 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2009-04-15 07:22:57 +08:00
|
|
|
FixItRewriter::FixItRewriter(Diagnostic &Diags, SourceManager &SourceMgr,
|
2010-04-24 09:30:46 +08:00
|
|
|
const LangOptions &LangOpts,
|
2010-08-14 01:31:00 +08:00
|
|
|
FixItOptions *FixItOpts)
|
2010-04-24 09:30:46 +08:00
|
|
|
: Diags(Diags),
|
|
|
|
Rewrite(SourceMgr, LangOpts),
|
2010-08-14 01:31:00 +08:00
|
|
|
FixItOpts(FixItOpts),
|
2010-04-24 09:30:46 +08:00
|
|
|
NumFailures(0) {
|
2010-08-19 06:29:43 +08:00
|
|
|
Client = Diags.takeClient();
|
2009-04-03 01:13:00 +08:00
|
|
|
Diags.setClient(this);
|
2009-04-02 09:08:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FixItRewriter::~FixItRewriter() {
|
2010-08-19 06:29:43 +08:00
|
|
|
Diags.takeClient();
|
2009-04-03 01:13:00 +08:00
|
|
|
Diags.setClient(Client);
|
2009-04-02 09:08:08 +08:00
|
|
|
}
|
|
|
|
|
2010-04-15 14:46:58 +08:00
|
|
|
bool FixItRewriter::WriteFixedFile(FileID ID, llvm::raw_ostream &OS) {
|
|
|
|
const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID);
|
|
|
|
if (!RewriteBuf) return true;
|
2010-04-17 02:49:45 +08:00
|
|
|
RewriteBuf->write(OS);
|
2010-04-15 14:46:58 +08:00
|
|
|
OS.flush();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FixItRewriter::WriteFixedFiles() {
|
2010-08-16 00:47:39 +08:00
|
|
|
if (NumFailures > 0 && !FixItOpts->FixWhatYouCan) {
|
2009-04-03 01:13:00 +08:00
|
|
|
Diag(FullSourceLoc(), diag::warn_fixit_no_changes);
|
2009-04-02 09:08:08 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-04-15 14:46:58 +08:00
|
|
|
for (iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
|
|
|
|
const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first);
|
2010-08-16 00:47:39 +08:00
|
|
|
std::string Filename = FixItOpts->RewriteFilename(Entry->getName());
|
2009-04-02 09:08:08 +08:00
|
|
|
std::string Err;
|
2010-04-24 09:30:46 +08:00
|
|
|
llvm::raw_fd_ostream OS(Filename.c_str(), Err,
|
|
|
|
llvm::raw_fd_ostream::F_Binary);
|
2010-04-15 14:46:58 +08:00
|
|
|
if (!Err.empty()) {
|
|
|
|
Diags.Report(clang::diag::err_fe_unable_to_open_output)
|
2010-04-24 09:30:46 +08:00
|
|
|
<< Filename << Err;
|
2010-04-15 14:46:58 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
RewriteBuffer &RewriteBuf = I->second;
|
2010-04-25 06:31:36 +08:00
|
|
|
RewriteBuf.write(OS);
|
2010-04-15 14:46:58 +08:00
|
|
|
OS.flush();
|
2009-04-02 09:08:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FixItRewriter::IncludeInDiagnosticCounts() const {
|
2010-04-15 14:46:58 +08:00
|
|
|
return Client ? Client->IncludeInDiagnosticCounts() : true;
|
2009-04-02 09:08:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FixItRewriter::HandleDiagnostic(Diagnostic::Level DiagLevel,
|
|
|
|
const DiagnosticInfo &Info) {
|
2010-11-19 05:13:54 +08:00
|
|
|
// Default implementation (Warnings/errors count).
|
|
|
|
DiagnosticClient::HandleDiagnostic(DiagLevel, Info);
|
|
|
|
|
2009-04-03 03:05:20 +08:00
|
|
|
Client->HandleDiagnostic(DiagLevel, Info);
|
|
|
|
|
2010-04-24 09:30:46 +08:00
|
|
|
// Skip over any diagnostics that are ignored or notes.
|
|
|
|
if (DiagLevel <= Diagnostic::Note)
|
2009-04-03 03:05:20 +08:00
|
|
|
return;
|
|
|
|
|
2009-04-02 09:08:08 +08:00
|
|
|
// Make sure that we can perform all of the modifications we
|
|
|
|
// in this diagnostic.
|
2010-04-01 01:46:05 +08:00
|
|
|
bool CanRewrite = Info.getNumFixItHints() > 0;
|
|
|
|
for (unsigned Idx = 0, Last = Info.getNumFixItHints();
|
2009-04-03 00:34:42 +08:00
|
|
|
Idx < Last; ++Idx) {
|
2010-04-01 01:46:05 +08:00
|
|
|
const FixItHint &Hint = Info.getFixItHint(Idx);
|
2009-04-02 09:08:08 +08:00
|
|
|
if (Hint.RemoveRange.isValid() &&
|
2009-04-03 01:13:00 +08:00
|
|
|
Rewrite.getRangeSize(Hint.RemoveRange) == -1) {
|
2009-04-02 09:08:08 +08:00
|
|
|
CanRewrite = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
if (!CanRewrite) {
|
2010-04-01 01:46:05 +08:00
|
|
|
if (Info.getNumFixItHints() > 0)
|
2009-04-03 01:13:00 +08:00
|
|
|
Diag(Info.getLocation(), diag::note_fixit_in_macro);
|
2009-04-03 00:34:42 +08:00
|
|
|
|
|
|
|
// If this was an error, refuse to perform any rewriting.
|
|
|
|
if (DiagLevel == Diagnostic::Error || DiagLevel == Diagnostic::Fatal) {
|
2009-04-03 01:13:00 +08:00
|
|
|
if (++NumFailures == 1)
|
|
|
|
Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
|
2009-04-03 00:34:42 +08:00
|
|
|
}
|
2009-04-02 09:08:08 +08:00
|
|
|
return;
|
2009-04-03 00:34:42 +08:00
|
|
|
}
|
2009-04-02 09:08:08 +08:00
|
|
|
|
|
|
|
bool Failed = false;
|
2010-04-01 01:46:05 +08:00
|
|
|
for (unsigned Idx = 0, Last = Info.getNumFixItHints();
|
2009-04-03 00:34:42 +08:00
|
|
|
Idx < Last; ++Idx) {
|
2010-04-01 01:46:05 +08:00
|
|
|
const FixItHint &Hint = Info.getFixItHint(Idx);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-03 00:34:42 +08:00
|
|
|
if (Hint.CodeToInsert.empty()) {
|
|
|
|
// We're removing code.
|
2009-04-03 01:13:00 +08:00
|
|
|
if (Rewrite.RemoveText(Hint.RemoveRange.getBegin(),
|
|
|
|
Rewrite.getRangeSize(Hint.RemoveRange)))
|
2009-04-03 00:34:42 +08:00
|
|
|
Failed = true;
|
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
|
|
|
|
2009-04-03 00:34:42 +08:00
|
|
|
// We're replacing code.
|
2009-04-03 01:13:00 +08:00
|
|
|
if (Rewrite.ReplaceText(Hint.RemoveRange.getBegin(),
|
|
|
|
Rewrite.getRangeSize(Hint.RemoveRange),
|
2009-08-20 03:10:30 +08:00
|
|
|
Hint.CodeToInsert))
|
2009-04-03 00:34:42 +08:00
|
|
|
Failed = true;
|
2009-04-02 09:08:08 +08:00
|
|
|
}
|
|
|
|
|
2009-04-03 01:13:00 +08:00
|
|
|
if (Failed) {
|
2009-04-02 09:08:08 +08:00
|
|
|
++NumFailures;
|
2009-04-03 01:13:00 +08:00
|
|
|
Diag(Info.getLocation(), diag::note_fixit_failed);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Diag(Info.getLocation(), diag::note_fixit_applied);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Emit a diagnostic via the adapted diagnostic client.
|
2010-11-19 04:06:41 +08:00
|
|
|
void FixItRewriter::Diag(SourceLocation Loc, unsigned DiagID) {
|
2009-04-03 01:13:00 +08:00
|
|
|
// When producing this diagnostic, we temporarily bypass ourselves,
|
|
|
|
// clear out any current diagnostic, and let the downstream client
|
|
|
|
// format the diagnostic.
|
2010-08-19 06:29:43 +08:00
|
|
|
Diags.takeClient();
|
2009-04-03 01:13:00 +08:00
|
|
|
Diags.setClient(Client);
|
|
|
|
Diags.Clear();
|
|
|
|
Diags.Report(Loc, DiagID);
|
2010-08-19 06:29:43 +08:00
|
|
|
Diags.takeClient();
|
2009-09-09 23:08:12 +08:00
|
|
|
Diags.setClient(this);
|
2009-04-02 09:08:08 +08:00
|
|
|
}
|
2010-04-24 09:30:46 +08:00
|
|
|
|
2010-08-14 01:31:00 +08:00
|
|
|
FixItOptions::~FixItOptions() {}
|