2008-03-27 11:49:32 +08:00
|
|
|
//===--- PathDiagnostic.cpp - Path-Specific Diagnostic Handling -*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the PathDiagnostic-related interfaces.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-01-26 01:10:22 +08:00
|
|
|
#include "clang/Checker/BugReporter/PathDiagnostic.h"
|
2009-03-27 05:39:39 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2009-04-07 06:33:35 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclObjC.h"
|
2009-04-27 04:35:05 +08:00
|
|
|
#include "clang/AST/StmtCXX.h"
|
2008-11-19 14:51:40 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2009-03-10 13:16:17 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2009-06-26 08:43:22 +08:00
|
|
|
|
2008-03-27 11:49:32 +08:00
|
|
|
using namespace clang;
|
2009-03-10 13:16:17 +08:00
|
|
|
using llvm::dyn_cast;
|
|
|
|
using llvm::isa;
|
|
|
|
|
|
|
|
bool PathDiagnosticMacroPiece::containsEvent() const {
|
|
|
|
for (const_iterator I = begin(), E = end(); I!=E; ++I) {
|
|
|
|
if (isa<PathDiagnosticEventPiece>(*I))
|
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-10 13:16:17 +08:00
|
|
|
if (PathDiagnosticMacroPiece *MP = dyn_cast<PathDiagnosticMacroPiece>(*I))
|
|
|
|
if (MP->containsEvent())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-02-27 05:30:32 +08:00
|
|
|
|
2009-12-12 05:09:27 +08:00
|
|
|
static llvm::StringRef StripTrailingDots(llvm::StringRef s) {
|
|
|
|
for (llvm::StringRef::size_type i = s.size(); i != 0; --i)
|
|
|
|
if (s[i - 1] != '.')
|
|
|
|
return s.substr(0, i);
|
|
|
|
return "";
|
2009-02-27 05:30:32 +08:00
|
|
|
}
|
|
|
|
|
2009-12-12 05:09:27 +08:00
|
|
|
PathDiagnosticPiece::PathDiagnosticPiece(llvm::StringRef s,
|
2009-03-03 03:39:50 +08:00
|
|
|
Kind k, DisplayHint hint)
|
2009-12-12 05:09:27 +08:00
|
|
|
: str(StripTrailingDots(s)), kind(k), Hint(hint) {}
|
2009-03-06 15:53:30 +08:00
|
|
|
|
2009-03-27 05:21:35 +08:00
|
|
|
PathDiagnosticPiece::PathDiagnosticPiece(Kind k, DisplayHint hint)
|
|
|
|
: kind(k), Hint(hint) {}
|
2009-03-13 02:41:53 +08:00
|
|
|
|
2009-03-07 06:10:49 +08:00
|
|
|
PathDiagnosticPiece::~PathDiagnosticPiece() {}
|
2009-03-07 07:58:11 +08:00
|
|
|
PathDiagnosticEventPiece::~PathDiagnosticEventPiece() {}
|
|
|
|
PathDiagnosticControlFlowPiece::~PathDiagnosticControlFlowPiece() {}
|
2009-03-07 06:10:49 +08:00
|
|
|
|
|
|
|
PathDiagnosticMacroPiece::~PathDiagnosticMacroPiece() {
|
|
|
|
for (iterator I = begin(), E = end(); I != E; ++I) delete *I;
|
|
|
|
}
|
|
|
|
|
2009-03-06 15:53:30 +08:00
|
|
|
PathDiagnostic::PathDiagnostic() : Size(0) {}
|
2009-02-27 05:30:32 +08:00
|
|
|
|
2008-03-27 11:49:32 +08:00
|
|
|
PathDiagnostic::~PathDiagnostic() {
|
|
|
|
for (iterator I = begin(), E = end(); I != E; ++I) delete &*I;
|
|
|
|
}
|
|
|
|
|
2009-03-10 13:16:17 +08:00
|
|
|
void PathDiagnostic::resetPath(bool deletePieces) {
|
|
|
|
Size = 0;
|
|
|
|
|
|
|
|
if (deletePieces)
|
|
|
|
for (iterator I=begin(), E=end(); I!=E; ++I)
|
|
|
|
delete &*I;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-10 13:16:17 +08:00
|
|
|
path.clear();
|
|
|
|
}
|
|
|
|
|
2009-03-06 15:08:50 +08:00
|
|
|
|
2009-12-12 05:09:27 +08:00
|
|
|
PathDiagnostic::PathDiagnostic(llvm::StringRef bugtype, llvm::StringRef desc,
|
|
|
|
llvm::StringRef category)
|
2009-03-06 15:53:30 +08:00
|
|
|
: Size(0),
|
2009-12-12 05:09:27 +08:00
|
|
|
BugType(StripTrailingDots(bugtype)),
|
|
|
|
Desc(StripTrailingDots(desc)),
|
|
|
|
Category(StripTrailingDots(category)) {}
|
2009-03-06 15:08:50 +08:00
|
|
|
|
This reworks some of the Diagnostic interfaces a bit to change how diagnostics
are formed. In particular, a diagnostic with all its strings and ranges is now
packaged up and sent to DiagnosticClients as a DiagnosticInfo instead of as a
ton of random stuff. This has the benefit of simplifying the interface, making
it more extensible, and allowing us to do more checking for things like access
past the end of the various arrays passed in.
In addition to introducing DiagnosticInfo, this also substantially changes how
Diagnostic::Report works. Instead of being passed in all of the info required
to issue a diagnostic, Report now takes only the required info (a location and
ID) and returns a fresh DiagnosticInfo *by value*. The caller is then free to
stuff strings and ranges into the DiagnosticInfo with the << operator. When
the dtor runs on the DiagnosticInfo object (which should happen at the end of
the statement), the diagnostic is actually emitted with all of the accumulated
information. This is a somewhat tricky dance, but it means that the
accumulated DiagnosticInfo is allowed to keep pointers to other expression
temporaries without those pointers getting invalidated.
This is just the minimal change to get this stuff working, but this will allow
us to eliminate the zillions of variant "Diag" methods scattered throughout
(e.g.) sema. For example, instead of calling:
Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
SourceRange(BuiltinLoc, RParenLoc));
We will soon be able to just do:
Diag(BuiltinLoc, diag::err_overload_no_match)
<< typeNames << SourceRange(BuiltinLoc, RParenLoc));
This scales better to support arbitrary types being passed in (not just
strings) in a type-safe way. Go operator overloading?!
llvm-svn: 59502
2008-11-18 15:04:44 +08:00
|
|
|
void PathDiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel,
|
|
|
|
const DiagnosticInfo &Info) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-27 11:49:32 +08:00
|
|
|
// Create a PathDiagnostic with a single piece.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-23 00:15:03 +08:00
|
|
|
PathDiagnostic* D = new PathDiagnostic();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-18 12:44:58 +08:00
|
|
|
const char *LevelStr;
|
2008-03-27 14:16:40 +08:00
|
|
|
switch (DiagLevel) {
|
2009-02-07 11:46:08 +08:00
|
|
|
default:
|
2009-02-06 11:57:44 +08:00
|
|
|
case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
|
2008-11-18 12:44:58 +08:00
|
|
|
case Diagnostic::Note: LevelStr = "note: "; break;
|
|
|
|
case Diagnostic::Warning: LevelStr = "warning: "; break;
|
|
|
|
case Diagnostic::Error: LevelStr = "error: "; break;
|
2009-02-06 11:57:44 +08:00
|
|
|
case Diagnostic::Fatal: LevelStr = "fatal error: "; break;
|
2008-03-27 14:16:40 +08:00
|
|
|
}
|
|
|
|
|
2008-11-19 14:51:40 +08:00
|
|
|
llvm::SmallString<100> StrC;
|
|
|
|
StrC += LevelStr;
|
|
|
|
Info.FormatDiagnostic(StrC);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
This reworks some of the Diagnostic interfaces a bit to change how diagnostics
are formed. In particular, a diagnostic with all its strings and ranges is now
packaged up and sent to DiagnosticClients as a DiagnosticInfo instead of as a
ton of random stuff. This has the benefit of simplifying the interface, making
it more extensible, and allowing us to do more checking for things like access
past the end of the various arrays passed in.
In addition to introducing DiagnosticInfo, this also substantially changes how
Diagnostic::Report works. Instead of being passed in all of the info required
to issue a diagnostic, Report now takes only the required info (a location and
ID) and returns a fresh DiagnosticInfo *by value*. The caller is then free to
stuff strings and ranges into the DiagnosticInfo with the << operator. When
the dtor runs on the DiagnosticInfo object (which should happen at the end of
the statement), the diagnostic is actually emitted with all of the accumulated
information. This is a somewhat tricky dance, but it means that the
accumulated DiagnosticInfo is allowed to keep pointers to other expression
temporaries without those pointers getting invalidated.
This is just the minimal change to get this stuff working, but this will allow
us to eliminate the zillions of variant "Diag" methods scattered throughout
(e.g.) sema. For example, instead of calling:
Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
SourceRange(BuiltinLoc, RParenLoc));
We will soon be able to just do:
Diag(BuiltinLoc, diag::err_overload_no_match)
<< typeNames << SourceRange(BuiltinLoc, RParenLoc));
This scales better to support arbitrary types being passed in (not just
strings) in a type-safe way. Go operator overloading?!
llvm-svn: 59502
2008-11-18 15:04:44 +08:00
|
|
|
PathDiagnosticPiece *P =
|
2009-12-12 05:09:27 +08:00
|
|
|
new PathDiagnosticEventPiece(Info.getLocation(), StrC.str());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
This reworks some of the Diagnostic interfaces a bit to change how diagnostics
are formed. In particular, a diagnostic with all its strings and ranges is now
packaged up and sent to DiagnosticClients as a DiagnosticInfo instead of as a
ton of random stuff. This has the benefit of simplifying the interface, making
it more extensible, and allowing us to do more checking for things like access
past the end of the various arrays passed in.
In addition to introducing DiagnosticInfo, this also substantially changes how
Diagnostic::Report works. Instead of being passed in all of the info required
to issue a diagnostic, Report now takes only the required info (a location and
ID) and returns a fresh DiagnosticInfo *by value*. The caller is then free to
stuff strings and ranges into the DiagnosticInfo with the << operator. When
the dtor runs on the DiagnosticInfo object (which should happen at the end of
the statement), the diagnostic is actually emitted with all of the accumulated
information. This is a somewhat tricky dance, but it means that the
accumulated DiagnosticInfo is allowed to keep pointers to other expression
temporaries without those pointers getting invalidated.
This is just the minimal change to get this stuff working, but this will allow
us to eliminate the zillions of variant "Diag" methods scattered throughout
(e.g.) sema. For example, instead of calling:
Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
SourceRange(BuiltinLoc, RParenLoc));
We will soon be able to just do:
Diag(BuiltinLoc, diag::err_overload_no_match)
<< typeNames << SourceRange(BuiltinLoc, RParenLoc));
This scales better to support arbitrary types being passed in (not just
strings) in a type-safe way. Go operator overloading?!
llvm-svn: 59502
2008-11-18 15:04:44 +08:00
|
|
|
for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i)
|
2010-06-19 06:45:06 +08:00
|
|
|
P->addRange(Info.getRange(i).getAsRange());
|
2010-04-01 01:46:05 +08:00
|
|
|
for (unsigned i = 0, e = Info.getNumFixItHints(); i != e; ++i)
|
|
|
|
P->addFixItHint(Info.getFixItHint(i));
|
2008-04-23 00:15:03 +08:00
|
|
|
D->push_front(P);
|
2008-03-27 11:49:32 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
HandlePathDiagnostic(D);
|
2008-03-27 11:49:32 +08:00
|
|
|
}
|
2009-03-27 05:39:39 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PathDiagnosticLocation methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
FullSourceLoc PathDiagnosticLocation::asLocation() const {
|
2009-04-01 14:13:56 +08:00
|
|
|
assert(isValid());
|
2009-03-27 05:42:51 +08:00
|
|
|
// Note that we want a 'switch' here so that the compiler can warn us in
|
|
|
|
// case we add more cases.
|
2009-03-27 05:39:39 +08:00
|
|
|
switch (K) {
|
2009-04-07 06:33:35 +08:00
|
|
|
case SingleLocK:
|
|
|
|
case RangeK:
|
2009-03-27 05:42:00 +08:00
|
|
|
break;
|
2009-04-07 06:33:35 +08:00
|
|
|
case StmtK:
|
2009-03-27 07:12:02 +08:00
|
|
|
return FullSourceLoc(S->getLocStart(), const_cast<SourceManager&>(*SM));
|
2009-04-07 06:33:35 +08:00
|
|
|
case DeclK:
|
|
|
|
return FullSourceLoc(D->getLocation(), const_cast<SourceManager&>(*SM));
|
2009-03-27 05:39:39 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-27 07:12:02 +08:00
|
|
|
return FullSourceLoc(R.getBegin(), const_cast<SourceManager&>(*SM));
|
2009-03-27 05:39:39 +08:00
|
|
|
}
|
2009-03-27 05:48:17 +08:00
|
|
|
|
2009-04-23 06:26:10 +08:00
|
|
|
PathDiagnosticRange PathDiagnosticLocation::asRange() const {
|
2009-04-01 14:13:56 +08:00
|
|
|
assert(isValid());
|
2009-03-27 05:48:17 +08:00
|
|
|
// Note that we want a 'switch' here so that the compiler can warn us in
|
|
|
|
// case we add more cases.
|
|
|
|
switch (K) {
|
2009-04-07 06:33:35 +08:00
|
|
|
case SingleLocK:
|
2009-04-23 06:26:10 +08:00
|
|
|
return PathDiagnosticRange(R, true);
|
2009-04-07 06:33:35 +08:00
|
|
|
case RangeK:
|
2009-03-27 05:48:17 +08:00
|
|
|
break;
|
2009-04-23 02:03:00 +08:00
|
|
|
case StmtK: {
|
|
|
|
const Stmt *S = asStmt();
|
|
|
|
switch (S->getStmtClass()) {
|
|
|
|
default:
|
|
|
|
break;
|
2009-05-15 10:05:25 +08:00
|
|
|
case Stmt::DeclStmtClass: {
|
|
|
|
const DeclStmt *DS = cast<DeclStmt>(S);
|
|
|
|
if (DS->isSingleDecl()) {
|
|
|
|
// Should always be the case, but we'll be defensive.
|
|
|
|
return SourceRange(DS->getLocStart(),
|
2009-09-09 23:08:12 +08:00
|
|
|
DS->getSingleDecl()->getLocation());
|
2009-05-15 10:05:25 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-04-23 02:03:00 +08:00
|
|
|
// FIXME: Provide better range information for different
|
|
|
|
// terminators.
|
|
|
|
case Stmt::IfStmtClass:
|
|
|
|
case Stmt::WhileStmtClass:
|
|
|
|
case Stmt::DoStmtClass:
|
|
|
|
case Stmt::ForStmtClass:
|
|
|
|
case Stmt::ChooseExprClass:
|
|
|
|
case Stmt::IndirectGotoStmtClass:
|
|
|
|
case Stmt::SwitchStmtClass:
|
|
|
|
case Stmt::ConditionalOperatorClass:
|
|
|
|
case Stmt::ObjCForCollectionStmtClass: {
|
|
|
|
SourceLocation L = S->getLocStart();
|
|
|
|
return SourceRange(L, L);
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-27 05:48:17 +08:00
|
|
|
return S->getSourceRange();
|
2009-04-23 02:03:00 +08:00
|
|
|
}
|
2009-04-07 06:33:35 +08:00
|
|
|
case DeclK:
|
|
|
|
if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
|
|
|
|
return MD->getSourceRange();
|
2009-04-18 08:02:19 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
2010-07-07 20:24:14 +08:00
|
|
|
if (Stmt *Body = FD->getBody())
|
|
|
|
return Body->getSourceRange();
|
2009-04-18 08:02:19 +08:00
|
|
|
}
|
2009-04-07 06:33:35 +08:00
|
|
|
else {
|
|
|
|
SourceLocation L = D->getLocation();
|
2009-04-23 06:26:10 +08:00
|
|
|
return PathDiagnosticRange(SourceRange(L, L), true);
|
2009-04-07 06:33:35 +08:00
|
|
|
}
|
2009-03-27 05:48:17 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-27 05:48:17 +08:00
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2009-04-07 06:33:35 +08:00
|
|
|
void PathDiagnosticLocation::flatten() {
|
|
|
|
if (K == StmtK) {
|
|
|
|
R = asRange();
|
|
|
|
K = RangeK;
|
|
|
|
S = 0;
|
|
|
|
D = 0;
|
|
|
|
}
|
|
|
|
else if (K == DeclK) {
|
|
|
|
SourceLocation L = D->getLocation();
|
|
|
|
R = SourceRange(L, L);
|
|
|
|
K = SingleLocK;
|
|
|
|
S = 0;
|
|
|
|
D = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-19 06:33:39 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// FoldingSet profiling methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void PathDiagnosticLocation::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
ID.AddInteger((unsigned) K);
|
|
|
|
switch (K) {
|
|
|
|
case RangeK:
|
|
|
|
ID.AddInteger(R.getBegin().getRawEncoding());
|
|
|
|
ID.AddInteger(R.getEnd().getRawEncoding());
|
|
|
|
break;
|
|
|
|
case SingleLocK:
|
|
|
|
ID.AddInteger(R.getBegin().getRawEncoding());
|
|
|
|
break;
|
|
|
|
case StmtK:
|
|
|
|
ID.Add(S);
|
|
|
|
break;
|
|
|
|
case DeclK:
|
|
|
|
ID.Add(D);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathDiagnosticPiece::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
ID.AddInteger((unsigned) getKind());
|
|
|
|
ID.AddString(str);
|
|
|
|
// FIXME: Add profiling support for code hints.
|
|
|
|
ID.AddInteger((unsigned) getDisplayHint());
|
|
|
|
for (range_iterator I = ranges_begin(), E = ranges_end(); I != E; ++I) {
|
|
|
|
ID.AddInteger(I->getBegin().getRawEncoding());
|
|
|
|
ID.AddInteger(I->getEnd().getRawEncoding());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathDiagnosticSpotPiece::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
PathDiagnosticPiece::Profile(ID);
|
|
|
|
ID.Add(Pos);
|
|
|
|
}
|
2009-04-07 06:33:35 +08:00
|
|
|
|
2009-09-19 06:33:39 +08:00
|
|
|
void PathDiagnosticControlFlowPiece::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
PathDiagnosticPiece::Profile(ID);
|
|
|
|
for (const_iterator I = begin(), E = end(); I != E; ++I)
|
|
|
|
ID.Add(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathDiagnosticMacroPiece::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
PathDiagnosticSpotPiece::Profile(ID);
|
|
|
|
for (const_iterator I = begin(), E = end(); I != E; ++I)
|
|
|
|
ID.Add(**I);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathDiagnostic::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
ID.AddInteger(Size);
|
|
|
|
ID.AddString(BugType);
|
|
|
|
ID.AddString(Desc);
|
|
|
|
ID.AddString(Category);
|
|
|
|
for (const_iterator I = begin(), E = end(); I != E; ++I)
|
|
|
|
ID.Add(*I);
|
|
|
|
|
|
|
|
for (meta_iterator I = meta_begin(), E = meta_end(); I != E; ++I)
|
|
|
|
ID.AddString(*I);
|
|
|
|
}
|