2009-04-02 05:08:08 +04: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-02 20:34:42 +04:00
|
|
|
|
2009-04-02 05:08:08 +04:00
|
|
|
#include "clang/Frontend/FixItRewriter.h"
|
2009-04-02 20:34:42 +04:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2009-04-02 21:13:00 +04:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2009-04-02 05:08:08 +04:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/System/Path.h"
|
2009-08-23 16:08:50 +04:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
2009-08-24 17:25:12 +04:00
|
|
|
#include <cstdio>
|
|
|
|
|
2009-04-02 05:08:08 +04:00
|
|
|
using namespace clang;
|
|
|
|
|
2009-04-15 03:22:57 +04:00
|
|
|
FixItRewriter::FixItRewriter(Diagnostic &Diags, SourceManager &SourceMgr,
|
|
|
|
const LangOptions &LangOpts)
|
|
|
|
: Diags(Diags), Rewrite(SourceMgr, LangOpts), NumFailures(0) {
|
2009-04-02 21:13:00 +04:00
|
|
|
Client = Diags.getClient();
|
|
|
|
Diags.setClient(this);
|
2009-04-02 05:08:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
FixItRewriter::~FixItRewriter() {
|
2009-04-02 21:13:00 +04:00
|
|
|
Diags.setClient(Client);
|
2009-04-02 05:08:08 +04:00
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
bool FixItRewriter::WriteFixedFile(const std::string &InFileName,
|
2009-04-02 05:08:08 +04:00
|
|
|
const std::string &OutFileName) {
|
|
|
|
if (NumFailures > 0) {
|
2009-04-02 21:13:00 +04:00
|
|
|
Diag(FullSourceLoc(), diag::warn_fixit_no_changes);
|
2009-04-02 05:08:08 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::OwningPtr<llvm::raw_ostream> OwnedStream;
|
|
|
|
llvm::raw_ostream *OutFile;
|
2009-04-02 20:34:42 +04:00
|
|
|
if (!OutFileName.empty()) {
|
2009-04-02 05:08:08 +04:00
|
|
|
std::string Err;
|
2009-08-23 06:59:41 +04:00
|
|
|
OutFile = new llvm::raw_fd_ostream(OutFileName.c_str(), Err,
|
2009-08-25 19:36:09 +04:00
|
|
|
llvm::raw_fd_ostream::F_Binary);
|
2009-04-02 05:08:08 +04:00
|
|
|
OwnedStream.reset(OutFile);
|
|
|
|
} else if (InFileName == "-") {
|
|
|
|
OutFile = &llvm::outs();
|
|
|
|
} else {
|
|
|
|
llvm::sys::Path Path(InFileName);
|
2009-04-02 07:14:12 +04:00
|
|
|
std::string Suffix = Path.getSuffix();
|
2009-04-02 05:08:08 +04:00
|
|
|
Path.eraseSuffix();
|
2009-04-02 07:14:12 +04:00
|
|
|
Path.appendSuffix("fixit." + Suffix);
|
2009-04-02 05:08:08 +04:00
|
|
|
std::string Err;
|
2009-08-24 02:45:33 +04:00
|
|
|
OutFile = new llvm::raw_fd_ostream(Path.c_str(), Err,
|
2009-08-25 19:36:09 +04:00
|
|
|
llvm::raw_fd_ostream::F_Binary);
|
2009-04-02 05:08:08 +04:00
|
|
|
OwnedStream.reset(OutFile);
|
2009-09-09 19:08:12 +04:00
|
|
|
}
|
2009-04-02 05:08:08 +04:00
|
|
|
|
2009-04-02 21:13:00 +04:00
|
|
|
FileID MainFileID = Rewrite.getSourceMgr().getMainFileID();
|
2009-09-09 19:08:12 +04:00
|
|
|
if (const RewriteBuffer *RewriteBuf =
|
2009-04-02 21:13:00 +04:00
|
|
|
Rewrite.getRewriteBufferFor(MainFileID)) {
|
2009-04-02 05:08:08 +04:00
|
|
|
*OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
|
|
|
|
} else {
|
2009-12-03 12:14:02 +03:00
|
|
|
Diag(FullSourceLoc(), diag::note_fixit_main_file_unchanged);
|
2009-04-02 05:08:08 +04:00
|
|
|
}
|
|
|
|
OutFile->flush();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FixItRewriter::IncludeInDiagnosticCounts() const {
|
2009-04-02 20:34:42 +04:00
|
|
|
return Client? Client->IncludeInDiagnosticCounts() : true;
|
2009-04-02 05:08:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FixItRewriter::HandleDiagnostic(Diagnostic::Level DiagLevel,
|
|
|
|
const DiagnosticInfo &Info) {
|
2009-04-02 23:05:20 +04:00
|
|
|
Client->HandleDiagnostic(DiagLevel, Info);
|
|
|
|
|
|
|
|
// Skip over any diagnostics that are ignored.
|
|
|
|
if (DiagLevel == Diagnostic::Ignored)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!FixItLocations.empty()) {
|
|
|
|
// The user has specified the locations where we should perform
|
|
|
|
// the various fix-it modifications.
|
|
|
|
|
|
|
|
// If this diagnostic does not have any code modifications,
|
|
|
|
// completely ignore it, even if it's an error: fix-it locations
|
|
|
|
// are meant to perform specific fix-ups even in the presence of
|
|
|
|
// other errors.
|
|
|
|
if (Info.getNumCodeModificationHints() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// See if the location of the error is one that matches what the
|
|
|
|
// user requested.
|
|
|
|
bool AcceptableLocation = false;
|
2009-09-09 19:08:12 +04:00
|
|
|
const FileEntry *File
|
2009-04-02 23:05:20 +04:00
|
|
|
= Rewrite.getSourceMgr().getFileEntryForID(
|
|
|
|
Info.getLocation().getFileID());
|
|
|
|
unsigned Line = Info.getLocation().getSpellingLineNumber();
|
|
|
|
unsigned Column = Info.getLocation().getSpellingColumnNumber();
|
|
|
|
for (llvm::SmallVector<RequestedSourceLocation, 4>::iterator
|
|
|
|
Loc = FixItLocations.begin(), LocEnd = FixItLocations.end();
|
|
|
|
Loc != LocEnd; ++Loc) {
|
|
|
|
if (Loc->File == File && Loc->Line == Line && Loc->Column == Column) {
|
|
|
|
AcceptableLocation = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AcceptableLocation)
|
|
|
|
return;
|
|
|
|
}
|
2009-04-02 05:08:08 +04:00
|
|
|
|
|
|
|
// Make sure that we can perform all of the modifications we
|
|
|
|
// in this diagnostic.
|
2009-04-02 20:34:42 +04:00
|
|
|
bool CanRewrite = Info.getNumCodeModificationHints() > 0;
|
|
|
|
for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints();
|
|
|
|
Idx < Last; ++Idx) {
|
2009-04-02 05:08:08 +04:00
|
|
|
const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
|
|
|
|
if (Hint.RemoveRange.isValid() &&
|
2009-04-02 21:13:00 +04:00
|
|
|
Rewrite.getRangeSize(Hint.RemoveRange) == -1) {
|
2009-04-02 05:08:08 +04:00
|
|
|
CanRewrite = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
if (Hint.InsertionLoc.isValid() &&
|
2009-04-02 21:13:00 +04:00
|
|
|
!Rewrite.isRewritable(Hint.InsertionLoc)) {
|
2009-04-02 05:08:08 +04:00
|
|
|
CanRewrite = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
if (!CanRewrite) {
|
2009-04-02 21:13:00 +04:00
|
|
|
if (Info.getNumCodeModificationHints() > 0)
|
|
|
|
Diag(Info.getLocation(), diag::note_fixit_in_macro);
|
2009-04-02 20:34:42 +04:00
|
|
|
|
|
|
|
// If this was an error, refuse to perform any rewriting.
|
|
|
|
if (DiagLevel == Diagnostic::Error || DiagLevel == Diagnostic::Fatal) {
|
2009-04-02 21:13:00 +04:00
|
|
|
if (++NumFailures == 1)
|
|
|
|
Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
|
2009-04-02 20:34:42 +04:00
|
|
|
}
|
2009-04-02 05:08:08 +04:00
|
|
|
return;
|
2009-04-02 20:34:42 +04:00
|
|
|
}
|
2009-04-02 05:08:08 +04:00
|
|
|
|
|
|
|
bool Failed = false;
|
2009-09-09 19:08:12 +04:00
|
|
|
for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints();
|
2009-04-02 20:34:42 +04:00
|
|
|
Idx < Last; ++Idx) {
|
2009-04-02 05:08:08 +04:00
|
|
|
const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
|
2009-04-02 20:34:42 +04:00
|
|
|
if (!Hint.RemoveRange.isValid()) {
|
2009-04-02 05:08:08 +04:00
|
|
|
// We're adding code.
|
2009-08-20 00:32:38 +04:00
|
|
|
if (Rewrite.InsertTextBefore(Hint.InsertionLoc, Hint.CodeToInsert))
|
2009-04-02 05:08:08 +04:00
|
|
|
Failed = true;
|
2009-04-02 20:34:42 +04:00
|
|
|
continue;
|
2009-04-02 05:08:08 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-02 20:34:42 +04:00
|
|
|
if (Hint.CodeToInsert.empty()) {
|
|
|
|
// We're removing code.
|
2009-04-02 21:13:00 +04:00
|
|
|
if (Rewrite.RemoveText(Hint.RemoveRange.getBegin(),
|
|
|
|
Rewrite.getRangeSize(Hint.RemoveRange)))
|
2009-04-02 20:34:42 +04:00
|
|
|
Failed = true;
|
|
|
|
continue;
|
2009-09-09 19:08:12 +04:00
|
|
|
}
|
|
|
|
|
2009-04-02 20:34:42 +04:00
|
|
|
// We're replacing code.
|
2009-04-02 21:13:00 +04:00
|
|
|
if (Rewrite.ReplaceText(Hint.RemoveRange.getBegin(),
|
|
|
|
Rewrite.getRangeSize(Hint.RemoveRange),
|
2009-08-19 23:10:30 +04:00
|
|
|
Hint.CodeToInsert))
|
2009-04-02 20:34:42 +04:00
|
|
|
Failed = true;
|
2009-04-02 05:08:08 +04:00
|
|
|
}
|
|
|
|
|
2009-04-02 21:13:00 +04:00
|
|
|
if (Failed) {
|
2009-04-02 05:08:08 +04:00
|
|
|
++NumFailures;
|
2009-04-02 21:13:00 +04: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.
|
|
|
|
void FixItRewriter::Diag(FullSourceLoc Loc, unsigned DiagID) {
|
|
|
|
// When producing this diagnostic, we temporarily bypass ourselves,
|
|
|
|
// clear out any current diagnostic, and let the downstream client
|
|
|
|
// format the diagnostic.
|
|
|
|
Diags.setClient(Client);
|
|
|
|
Diags.Clear();
|
|
|
|
Diags.Report(Loc, DiagID);
|
2009-09-09 19:08:12 +04:00
|
|
|
Diags.setClient(this);
|
2009-04-02 05:08:08 +04:00
|
|
|
}
|