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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
|
2009-04-07 06:33:35 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2012-07-27 04:04:05 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2009-04-07 06:33:35 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2011-09-15 01:48:01 +08:00
|
|
|
#include "clang/AST/ParentMap.h"
|
2009-04-27 04:35:05 +08:00
|
|
|
#include "clang/AST/StmtCXX.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
|
2008-11-19 14:51:40 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-09-22 09:24:42 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2012-12-02 01:12:56 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-06-26 08:43:22 +08:00
|
|
|
|
2008-03-27 11:49:32 +08:00
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-03-10 13:16:17 +08:00
|
|
|
|
|
|
|
bool PathDiagnosticMacroPiece::containsEvent() const {
|
2012-02-08 12:32:34 +08:00
|
|
|
for (PathPieces::const_iterator I = subPieces.begin(), E = subPieces.end();
|
|
|
|
I!=E; ++I) {
|
2009-03-10 13:16:17 +08:00
|
|
|
if (isa<PathDiagnosticEventPiece>(*I))
|
|
|
|
return true;
|
|
|
|
if (PathDiagnosticMacroPiece *MP = dyn_cast<PathDiagnosticMacroPiece>(*I))
|
|
|
|
if (MP->containsEvent())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-02-27 05:30:32 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
static StringRef StripTrailingDots(StringRef s) {
|
|
|
|
for (StringRef::size_type i = s.size(); i != 0; --i)
|
2009-12-12 05:09:27 +08:00
|
|
|
if (s[i - 1] != '.')
|
|
|
|
return s.substr(0, i);
|
|
|
|
return "";
|
2009-02-27 05:30:32 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
PathDiagnosticPiece::PathDiagnosticPiece(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() {}
|
2012-02-24 14:00:00 +08:00
|
|
|
PathDiagnosticCallPiece::~PathDiagnosticCallPiece() {}
|
2009-03-07 07:58:11 +08:00
|
|
|
PathDiagnosticControlFlowPiece::~PathDiagnosticControlFlowPiece() {}
|
2012-02-08 12:32:34 +08:00
|
|
|
PathDiagnosticMacroPiece::~PathDiagnosticMacroPiece() {}
|
2012-04-05 02:11:35 +08:00
|
|
|
|
|
|
|
|
2012-02-08 12:32:34 +08:00
|
|
|
PathPieces::~PathPieces() {}
|
2012-08-04 07:08:54 +08:00
|
|
|
|
|
|
|
void PathPieces::flattenTo(PathPieces &Primary, PathPieces &Current,
|
|
|
|
bool ShouldFlattenMacros) const {
|
|
|
|
for (PathPieces::const_iterator I = begin(), E = end(); I != E; ++I) {
|
|
|
|
PathDiagnosticPiece *Piece = I->getPtr();
|
|
|
|
|
|
|
|
switch (Piece->getKind()) {
|
|
|
|
case PathDiagnosticPiece::Call: {
|
|
|
|
PathDiagnosticCallPiece *Call = cast<PathDiagnosticCallPiece>(Piece);
|
|
|
|
IntrusiveRefCntPtr<PathDiagnosticEventPiece> CallEnter =
|
|
|
|
Call->getCallEnterEvent();
|
|
|
|
if (CallEnter)
|
|
|
|
Current.push_back(CallEnter);
|
|
|
|
Call->path.flattenTo(Primary, Primary, ShouldFlattenMacros);
|
|
|
|
IntrusiveRefCntPtr<PathDiagnosticEventPiece> callExit =
|
|
|
|
Call->getCallExitEvent();
|
|
|
|
if (callExit)
|
|
|
|
Current.push_back(callExit);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PathDiagnosticPiece::Macro: {
|
|
|
|
PathDiagnosticMacroPiece *Macro = cast<PathDiagnosticMacroPiece>(Piece);
|
|
|
|
if (ShouldFlattenMacros) {
|
|
|
|
Macro->subPieces.flattenTo(Primary, Primary, ShouldFlattenMacros);
|
|
|
|
} else {
|
|
|
|
Current.push_back(Piece);
|
|
|
|
PathPieces NewPath;
|
|
|
|
Macro->subPieces.flattenTo(Primary, NewPath, ShouldFlattenMacros);
|
|
|
|
// FIXME: This probably shouldn't mutate the original path piece.
|
|
|
|
Macro->subPieces = NewPath;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PathDiagnosticPiece::Event:
|
|
|
|
case PathDiagnosticPiece::ControlFlow:
|
|
|
|
Current.push_back(Piece);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-08 12:32:27 +08:00
|
|
|
PathDiagnostic::~PathDiagnostic() {}
|
|
|
|
|
2012-04-05 02:11:35 +08:00
|
|
|
PathDiagnostic::PathDiagnostic(const Decl *declWithIssue,
|
2012-08-31 08:36:26 +08:00
|
|
|
StringRef bugtype, StringRef verboseDesc,
|
|
|
|
StringRef shortDesc, StringRef category)
|
2012-04-05 02:11:35 +08:00
|
|
|
: DeclWithIssue(declWithIssue),
|
|
|
|
BugType(StripTrailingDots(bugtype)),
|
2012-08-31 08:36:26 +08:00
|
|
|
VerboseDesc(StripTrailingDots(verboseDesc)),
|
|
|
|
ShortDesc(StripTrailingDots(shortDesc)),
|
2012-02-24 14:00:00 +08:00
|
|
|
Category(StripTrailingDots(category)),
|
|
|
|
path(pathImpl) {}
|
2009-03-06 15:08:50 +08:00
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void PathDiagnosticConsumer::anchor() { }
|
|
|
|
|
2012-01-26 07:47:14 +08:00
|
|
|
PathDiagnosticConsumer::~PathDiagnosticConsumer() {
|
|
|
|
// Delete the contents of the FoldingSet if it isn't empty already.
|
|
|
|
for (llvm::FoldingSet<PathDiagnostic>::iterator it =
|
|
|
|
Diags.begin(), et = Diags.end() ; it != et ; ++it) {
|
|
|
|
delete &*it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathDiagnosticConsumer::HandlePathDiagnostic(PathDiagnostic *D) {
|
2012-02-29 07:27:39 +08:00
|
|
|
llvm::OwningPtr<PathDiagnostic> OwningD(D);
|
2012-01-26 07:47:14 +08:00
|
|
|
|
2012-02-29 07:27:39 +08:00
|
|
|
if (!D || D->path.empty())
|
2012-01-26 07:47:14 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// We need to flatten the locations (convert Stmt* to locations) because
|
|
|
|
// the referenced statements may be freed by the time the diagnostics
|
|
|
|
// are emitted.
|
|
|
|
D->flattenLocations();
|
|
|
|
|
2012-02-29 07:27:39 +08:00
|
|
|
// If the PathDiagnosticConsumer does not support diagnostics that
|
|
|
|
// cross file boundaries, prune out such diagnostics now.
|
|
|
|
if (!supportsCrossFileDiagnostics()) {
|
|
|
|
// Verify that the entire path is from the same FileID.
|
|
|
|
FileID FID;
|
|
|
|
const SourceManager &SMgr = (*D->path.begin())->getLocation().getManager();
|
2012-03-01 07:59:20 +08:00
|
|
|
llvm::SmallVector<const PathPieces *, 5> WorkList;
|
|
|
|
WorkList.push_back(&D->path);
|
2012-02-29 07:27:39 +08:00
|
|
|
|
2012-03-01 07:59:20 +08:00
|
|
|
while (!WorkList.empty()) {
|
|
|
|
const PathPieces &path = *WorkList.back();
|
|
|
|
WorkList.pop_back();
|
|
|
|
|
|
|
|
for (PathPieces::const_iterator I = path.begin(), E = path.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
const PathDiagnosticPiece *piece = I->getPtr();
|
|
|
|
FullSourceLoc L = piece->getLocation().asLocation().getExpansionLoc();
|
2012-02-29 07:27:39 +08:00
|
|
|
|
2012-03-01 07:59:20 +08:00
|
|
|
if (FID.isInvalid()) {
|
|
|
|
FID = SMgr.getFileID(L);
|
|
|
|
} else if (SMgr.getFileID(L) != FID)
|
2012-02-29 07:27:39 +08:00
|
|
|
return; // FIXME: Emit a warning?
|
2012-03-01 07:59:20 +08:00
|
|
|
|
|
|
|
// Check the source ranges.
|
2012-08-17 01:45:29 +08:00
|
|
|
ArrayRef<SourceRange> Ranges = piece->getRanges();
|
|
|
|
for (ArrayRef<SourceRange>::iterator I = Ranges.begin(),
|
|
|
|
E = Ranges.end(); I != E; ++I) {
|
|
|
|
SourceLocation L = SMgr.getExpansionLoc(I->getBegin());
|
2012-03-01 07:59:20 +08:00
|
|
|
if (!L.isFileID() || SMgr.getFileID(L) != FID)
|
|
|
|
return; // FIXME: Emit a warning?
|
2012-08-17 01:45:29 +08:00
|
|
|
L = SMgr.getExpansionLoc(I->getEnd());
|
2012-03-01 07:59:20 +08:00
|
|
|
if (!L.isFileID() || SMgr.getFileID(L) != FID)
|
|
|
|
return; // FIXME: Emit a warning?
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const PathDiagnosticCallPiece *call =
|
|
|
|
dyn_cast<PathDiagnosticCallPiece>(piece)) {
|
|
|
|
WorkList.push_back(&call->path);
|
|
|
|
}
|
|
|
|
else if (const PathDiagnosticMacroPiece *macro =
|
|
|
|
dyn_cast<PathDiagnosticMacroPiece>(piece)) {
|
|
|
|
WorkList.push_back(¯o->subPieces);
|
|
|
|
}
|
2012-02-29 07:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FID.isInvalid())
|
|
|
|
return; // FIXME: Emit a warning?
|
|
|
|
}
|
|
|
|
|
2012-01-26 07:47:14 +08:00
|
|
|
// Profile the node to see if we already have something matching it
|
|
|
|
llvm::FoldingSetNodeID profile;
|
|
|
|
D->Profile(profile);
|
|
|
|
void *InsertPos = 0;
|
|
|
|
|
|
|
|
if (PathDiagnostic *orig = Diags.FindNodeOrInsertPos(profile, InsertPos)) {
|
|
|
|
// Keep the PathDiagnostic with the shorter path.
|
2012-08-04 02:30:20 +08:00
|
|
|
// Note, the enclosing routine is called in deterministic order, so the
|
2012-08-03 07:41:05 +08:00
|
|
|
// results will be consistent between runs (no reason to break ties if the
|
|
|
|
// size is the same).
|
2012-02-24 14:00:00 +08:00
|
|
|
const unsigned orig_size = orig->full_size();
|
|
|
|
const unsigned new_size = D->full_size();
|
2012-08-03 07:41:05 +08:00
|
|
|
if (orig_size <= new_size)
|
|
|
|
return;
|
2012-01-26 07:47:14 +08:00
|
|
|
|
2012-09-10 22:50:55 +08:00
|
|
|
assert(orig != D);
|
2012-01-26 07:47:14 +08:00
|
|
|
Diags.RemoveNode(orig);
|
|
|
|
delete orig;
|
|
|
|
}
|
|
|
|
|
2012-02-29 07:27:39 +08:00
|
|
|
Diags.InsertNode(OwningD.take());
|
2012-01-26 07:47:14 +08:00
|
|
|
}
|
|
|
|
|
2012-09-10 22:50:55 +08:00
|
|
|
static llvm::Optional<bool> comparePath(const PathPieces &X,
|
|
|
|
const PathPieces &Y);
|
|
|
|
static llvm::Optional<bool>
|
|
|
|
compareControlFlow(const PathDiagnosticControlFlowPiece &X,
|
|
|
|
const PathDiagnosticControlFlowPiece &Y) {
|
|
|
|
FullSourceLoc XSL = X.getStartLocation().asLocation();
|
|
|
|
FullSourceLoc YSL = Y.getStartLocation().asLocation();
|
|
|
|
if (XSL != YSL)
|
|
|
|
return XSL.isBeforeInTranslationUnitThan(YSL);
|
|
|
|
FullSourceLoc XEL = X.getEndLocation().asLocation();
|
|
|
|
FullSourceLoc YEL = Y.getEndLocation().asLocation();
|
|
|
|
if (XEL != YEL)
|
|
|
|
return XEL.isBeforeInTranslationUnitThan(YEL);
|
|
|
|
return llvm::Optional<bool>();
|
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::Optional<bool>
|
|
|
|
compareMacro(const PathDiagnosticMacroPiece &X,
|
|
|
|
const PathDiagnosticMacroPiece &Y) {
|
|
|
|
return comparePath(X.subPieces, Y.subPieces);
|
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::Optional<bool>
|
|
|
|
compareCall(const PathDiagnosticCallPiece &X,
|
|
|
|
const PathDiagnosticCallPiece &Y) {
|
|
|
|
FullSourceLoc X_CEL = X.callEnter.asLocation();
|
|
|
|
FullSourceLoc Y_CEL = Y.callEnter.asLocation();
|
|
|
|
if (X_CEL != Y_CEL)
|
|
|
|
return X_CEL.isBeforeInTranslationUnitThan(Y_CEL);
|
|
|
|
FullSourceLoc X_CEWL = X.callEnterWithin.asLocation();
|
|
|
|
FullSourceLoc Y_CEWL = Y.callEnterWithin.asLocation();
|
|
|
|
if (X_CEWL != Y_CEWL)
|
|
|
|
return X_CEWL.isBeforeInTranslationUnitThan(Y_CEWL);
|
|
|
|
FullSourceLoc X_CRL = X.callReturn.asLocation();
|
|
|
|
FullSourceLoc Y_CRL = Y.callReturn.asLocation();
|
|
|
|
if (X_CRL != Y_CRL)
|
|
|
|
return X_CRL.isBeforeInTranslationUnitThan(Y_CRL);
|
|
|
|
return comparePath(X.path, Y.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::Optional<bool> comparePiece(const PathDiagnosticPiece &X,
|
|
|
|
const PathDiagnosticPiece &Y) {
|
|
|
|
if (X.getKind() != Y.getKind())
|
|
|
|
return X.getKind() < Y.getKind();
|
|
|
|
|
|
|
|
FullSourceLoc XL = X.getLocation().asLocation();
|
|
|
|
FullSourceLoc YL = Y.getLocation().asLocation();
|
|
|
|
if (XL != YL)
|
|
|
|
return XL.isBeforeInTranslationUnitThan(YL);
|
|
|
|
|
|
|
|
if (X.getString() != Y.getString())
|
|
|
|
return X.getString() < Y.getString();
|
|
|
|
|
|
|
|
if (X.getRanges().size() != Y.getRanges().size())
|
|
|
|
return X.getRanges().size() < Y.getRanges().size();
|
|
|
|
|
2012-09-11 03:02:33 +08:00
|
|
|
const SourceManager &SM = XL.getManager();
|
|
|
|
|
2012-09-10 22:50:55 +08:00
|
|
|
for (unsigned i = 0, n = X.getRanges().size(); i < n; ++i) {
|
|
|
|
SourceRange XR = X.getRanges()[i];
|
|
|
|
SourceRange YR = Y.getRanges()[i];
|
|
|
|
if (XR != YR) {
|
|
|
|
if (XR.getBegin() != YR.getBegin())
|
2012-09-11 03:02:33 +08:00
|
|
|
return SM.isBeforeInTranslationUnit(XR.getBegin(), YR.getBegin());
|
|
|
|
return SM.isBeforeInTranslationUnit(XR.getEnd(), YR.getEnd());
|
2012-09-10 22:50:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (X.getKind()) {
|
|
|
|
case clang::ento::PathDiagnosticPiece::ControlFlow:
|
|
|
|
return compareControlFlow(cast<PathDiagnosticControlFlowPiece>(X),
|
|
|
|
cast<PathDiagnosticControlFlowPiece>(Y));
|
|
|
|
case clang::ento::PathDiagnosticPiece::Event:
|
|
|
|
return llvm::Optional<bool>();
|
|
|
|
case clang::ento::PathDiagnosticPiece::Macro:
|
|
|
|
return compareMacro(cast<PathDiagnosticMacroPiece>(X),
|
|
|
|
cast<PathDiagnosticMacroPiece>(Y));
|
|
|
|
case clang::ento::PathDiagnosticPiece::Call:
|
|
|
|
return compareCall(cast<PathDiagnosticCallPiece>(X),
|
|
|
|
cast<PathDiagnosticCallPiece>(Y));
|
|
|
|
}
|
|
|
|
llvm_unreachable("all cases handled");
|
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::Optional<bool> comparePath(const PathPieces &X,
|
|
|
|
const PathPieces &Y) {
|
|
|
|
if (X.size() != Y.size())
|
|
|
|
return X.size() < Y.size();
|
|
|
|
for (unsigned i = 0, n = X.size(); i != n; ++i) {
|
|
|
|
llvm::Optional<bool> b = comparePiece(*X[i], *Y[i]);
|
|
|
|
if (b.hasValue())
|
|
|
|
return b.getValue();
|
|
|
|
}
|
|
|
|
return llvm::Optional<bool>();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool compare(const PathDiagnostic &X, const PathDiagnostic &Y) {
|
|
|
|
FullSourceLoc XL = X.getLocation().asLocation();
|
|
|
|
FullSourceLoc YL = Y.getLocation().asLocation();
|
|
|
|
if (XL != YL)
|
|
|
|
return XL.isBeforeInTranslationUnitThan(YL);
|
|
|
|
if (X.getBugType() != Y.getBugType())
|
|
|
|
return X.getBugType() < Y.getBugType();
|
|
|
|
if (X.getCategory() != Y.getCategory())
|
|
|
|
return X.getCategory() < Y.getCategory();
|
|
|
|
if (X.getVerboseDescription() != Y.getVerboseDescription())
|
|
|
|
return X.getVerboseDescription() < Y.getVerboseDescription();
|
|
|
|
if (X.getShortDescription() != Y.getShortDescription())
|
|
|
|
return X.getShortDescription() < Y.getShortDescription();
|
|
|
|
if (X.getDeclWithIssue() != Y.getDeclWithIssue()) {
|
|
|
|
const Decl *XD = X.getDeclWithIssue();
|
|
|
|
if (!XD)
|
|
|
|
return true;
|
|
|
|
const Decl *YD = Y.getDeclWithIssue();
|
|
|
|
if (!YD)
|
|
|
|
return false;
|
|
|
|
SourceLocation XDL = XD->getLocation();
|
|
|
|
SourceLocation YDL = YD->getLocation();
|
2012-09-11 03:07:56 +08:00
|
|
|
if (XDL != YDL) {
|
|
|
|
const SourceManager &SM = XL.getManager();
|
|
|
|
return SM.isBeforeInTranslationUnit(XDL, YDL);
|
|
|
|
}
|
2012-09-10 22:50:55 +08:00
|
|
|
}
|
|
|
|
PathDiagnostic::meta_iterator XI = X.meta_begin(), XE = X.meta_end();
|
|
|
|
PathDiagnostic::meta_iterator YI = Y.meta_begin(), YE = Y.meta_end();
|
|
|
|
if (XE - XI != YE - YI)
|
|
|
|
return (XE - XI) < (YE - YI);
|
|
|
|
for ( ; XI != XE ; ++XI, ++YI) {
|
|
|
|
if (*XI != *YI)
|
|
|
|
return (*XI) < (*YI);
|
|
|
|
}
|
|
|
|
llvm::Optional<bool> b = comparePath(X.path, Y.path);
|
|
|
|
assert(b.hasValue());
|
|
|
|
return b.getValue();
|
|
|
|
}
|
2012-01-26 07:47:14 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct CompareDiagnostics {
|
|
|
|
// Compare if 'X' is "<" than 'Y'.
|
|
|
|
bool operator()(const PathDiagnostic *X, const PathDiagnostic *Y) const {
|
2012-09-10 22:50:55 +08:00
|
|
|
if (X == Y)
|
|
|
|
return false;
|
|
|
|
return compare(*X, *Y);
|
2012-09-08 12:26:37 +08:00
|
|
|
}
|
|
|
|
};
|
2012-01-26 07:47:14 +08:00
|
|
|
}
|
|
|
|
|
Allow multiple PathDiagnosticConsumers to be used with a BugReporter at the same time.
This fixes several issues:
- removes egregious hack where PlistDiagnosticConsumer would forward to HTMLDiagnosticConsumer,
but diagnostics wouldn't be generated consistently in the same way if PlistDiagnosticConsumer
was used by itself.
- emitting diagnostics to the terminal (using clang's diagnostic machinery) is no longer a special
case, just another PathDiagnosticConsumer. This also magically resolved some duplicate warnings,
as we now use PathDiagnosticConsumer's diagnostic pruning, which has scope for the entire translation
unit, not just the scope of a BugReporter (which is limited to a particular ExprEngine).
As an interesting side-effect, diagnostics emitted to the terminal also have their trailing "." stripped,
just like with diagnostics emitted to plists and HTML. This required some tests to be updated, but now
the tests have higher fidelity with what users will see.
There are some inefficiencies in this patch. We currently generate the report graph (from the ExplodedGraph)
once per PathDiagnosticConsumer, which is a bit wasteful, but that could be pulled up higher in the
logic stack. There is some intended duplication, however, as we now generate different PathDiagnostics (for the same issue)
for different PathDiagnosticConsumers. This is necessary to produce the diagnostics that a particular
consumer expects.
llvm-svn: 162028
2012-08-17 01:45:23 +08:00
|
|
|
void PathDiagnosticConsumer::FlushDiagnostics(
|
|
|
|
PathDiagnosticConsumer::FilesMade *Files) {
|
2012-01-26 07:47:14 +08:00
|
|
|
if (flushed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
flushed = true;
|
|
|
|
|
|
|
|
std::vector<const PathDiagnostic *> BatchDiags;
|
|
|
|
for (llvm::FoldingSet<PathDiagnostic>::iterator it = Diags.begin(),
|
|
|
|
et = Diags.end(); it != et; ++it) {
|
2012-09-08 12:26:37 +08:00
|
|
|
const PathDiagnostic *D = &*it;
|
|
|
|
BatchDiags.push_back(D);
|
2012-01-26 07:47:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the diagnostics so that they are always emitted in a deterministic
|
|
|
|
// order.
|
|
|
|
if (!BatchDiags.empty())
|
|
|
|
std::sort(BatchDiags.begin(), BatchDiags.end(), CompareDiagnostics());
|
|
|
|
|
|
|
|
FlushDiagnosticsImpl(BatchDiags, Files);
|
|
|
|
|
|
|
|
// Delete the flushed diagnostics.
|
|
|
|
for (std::vector<const PathDiagnostic *>::iterator it = BatchDiags.begin(),
|
|
|
|
et = BatchDiags.end(); it != et; ++it) {
|
|
|
|
const PathDiagnostic *D = *it;
|
|
|
|
delete D;
|
|
|
|
}
|
2012-09-08 12:26:37 +08:00
|
|
|
|
|
|
|
// Clear out the FoldingSet.
|
|
|
|
Diags.clear();
|
2011-08-28 05:39:14 +08:00
|
|
|
}
|
|
|
|
|
2012-08-25 03:35:19 +08:00
|
|
|
void PathDiagnosticConsumer::FilesMade::addDiagnostic(const PathDiagnostic &PD,
|
|
|
|
StringRef ConsumerName,
|
|
|
|
StringRef FileName) {
|
|
|
|
llvm::FoldingSetNodeID NodeID;
|
2012-08-31 08:36:26 +08:00
|
|
|
NodeID.Add(PD);
|
2012-08-25 03:35:19 +08:00
|
|
|
void *InsertPos;
|
|
|
|
PDFileEntry *Entry = FindNodeOrInsertPos(NodeID, InsertPos);
|
|
|
|
if (!Entry) {
|
|
|
|
Entry = Alloc.Allocate<PDFileEntry>();
|
|
|
|
Entry = new (Entry) PDFileEntry(NodeID);
|
|
|
|
InsertNode(Entry, InsertPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate persistent storage for the file name.
|
|
|
|
char *FileName_cstr = (char*) Alloc.Allocate(FileName.size(), 1);
|
|
|
|
memcpy(FileName_cstr, FileName.data(), FileName.size());
|
|
|
|
|
|
|
|
Entry->files.push_back(std::make_pair(ConsumerName,
|
|
|
|
StringRef(FileName_cstr,
|
|
|
|
FileName.size())));
|
|
|
|
}
|
|
|
|
|
|
|
|
PathDiagnosticConsumer::PDFileEntry::ConsumerFiles *
|
|
|
|
PathDiagnosticConsumer::FilesMade::getFiles(const PathDiagnostic &PD) {
|
|
|
|
llvm::FoldingSetNodeID NodeID;
|
2012-08-31 08:36:26 +08:00
|
|
|
NodeID.Add(PD);
|
2012-08-25 03:35:19 +08:00
|
|
|
void *InsertPos;
|
|
|
|
PDFileEntry *Entry = FindNodeOrInsertPos(NodeID, InsertPos);
|
|
|
|
if (!Entry)
|
|
|
|
return 0;
|
|
|
|
return &Entry->files;
|
|
|
|
}
|
|
|
|
|
2009-03-27 05:39:39 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PathDiagnosticLocation methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-09-17 03:18:30 +08:00
|
|
|
static SourceLocation getValidSourceLocation(const Stmt* S,
|
2012-07-27 04:04:05 +08:00
|
|
|
LocationOrAnalysisDeclContext LAC,
|
|
|
|
bool UseEnd = false) {
|
|
|
|
SourceLocation L = UseEnd ? S->getLocEnd() : S->getLocStart();
|
2011-10-24 09:32:45 +08:00
|
|
|
assert(!LAC.isNull() && "A valid LocationContext or AnalysisDeclContext should "
|
2011-09-21 02:23:52 +08:00
|
|
|
"be passed to PathDiagnosticLocation upon creation.");
|
2011-09-17 03:18:30 +08:00
|
|
|
|
|
|
|
// S might be a temporary statement that does not have a location in the
|
2012-07-27 04:04:05 +08:00
|
|
|
// source code, so find an enclosing statement and use its location.
|
2011-09-21 00:37:36 +08:00
|
|
|
if (!L.isValid()) {
|
2011-09-21 02:23:52 +08:00
|
|
|
|
2012-07-27 04:04:30 +08:00
|
|
|
AnalysisDeclContext *ADC;
|
2011-09-21 02:23:52 +08:00
|
|
|
if (LAC.is<const LocationContext*>())
|
2012-07-27 04:04:30 +08:00
|
|
|
ADC = LAC.get<const LocationContext*>()->getAnalysisDeclContext();
|
2011-09-21 02:23:52 +08:00
|
|
|
else
|
2012-07-27 04:04:30 +08:00
|
|
|
ADC = LAC.get<AnalysisDeclContext*>();
|
|
|
|
|
|
|
|
ParentMap &PM = ADC->getParentMap();
|
|
|
|
|
|
|
|
const Stmt *Parent = S;
|
|
|
|
do {
|
|
|
|
Parent = PM.getParent(Parent);
|
|
|
|
|
|
|
|
// In rare cases, we have implicit top-level expressions,
|
|
|
|
// such as arguments for implicit member initializers.
|
|
|
|
// In this case, fall back to the start of the body (even if we were
|
|
|
|
// asked for the statement end location).
|
|
|
|
if (!Parent) {
|
|
|
|
const Stmt *Body = ADC->getBody();
|
|
|
|
if (Body)
|
|
|
|
L = Body->getLocStart();
|
|
|
|
else
|
|
|
|
L = ADC->getDecl()->getLocEnd();
|
|
|
|
break;
|
|
|
|
}
|
2011-09-21 00:37:36 +08:00
|
|
|
|
2012-07-27 04:04:30 +08:00
|
|
|
L = UseEnd ? Parent->getLocEnd() : Parent->getLocStart();
|
|
|
|
} while (!L.isValid());
|
2011-09-17 03:18:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return L;
|
|
|
|
}
|
|
|
|
|
2012-08-04 07:08:44 +08:00
|
|
|
static PathDiagnosticLocation
|
|
|
|
getLocationForCaller(const StackFrameContext *SFC,
|
|
|
|
const LocationContext *CallerCtx,
|
|
|
|
const SourceManager &SM) {
|
|
|
|
const CFGBlock &Block = *SFC->getCallSiteBlock();
|
|
|
|
CFGElement Source = Block[SFC->getIndex()];
|
|
|
|
|
|
|
|
switch (Source.getKind()) {
|
|
|
|
case CFGElement::Invalid:
|
|
|
|
llvm_unreachable("Invalid CFGElement");
|
|
|
|
case CFGElement::Statement:
|
|
|
|
return PathDiagnosticLocation(cast<CFGStmt>(Source).getStmt(),
|
|
|
|
SM, CallerCtx);
|
|
|
|
case CFGElement::Initializer: {
|
|
|
|
const CFGInitializer &Init = cast<CFGInitializer>(Source);
|
|
|
|
return PathDiagnosticLocation(Init.getInitializer()->getInit(),
|
|
|
|
SM, CallerCtx);
|
|
|
|
}
|
|
|
|
case CFGElement::AutomaticObjectDtor: {
|
|
|
|
const CFGAutomaticObjDtor &Dtor = cast<CFGAutomaticObjDtor>(Source);
|
|
|
|
return PathDiagnosticLocation::createEnd(Dtor.getTriggerStmt(),
|
|
|
|
SM, CallerCtx);
|
|
|
|
}
|
|
|
|
case CFGElement::BaseDtor:
|
|
|
|
case CFGElement::MemberDtor: {
|
|
|
|
const AnalysisDeclContext *CallerInfo = CallerCtx->getAnalysisDeclContext();
|
|
|
|
if (const Stmt *CallerBody = CallerInfo->getBody())
|
|
|
|
return PathDiagnosticLocation::createEnd(CallerBody, SM, CallerCtx);
|
|
|
|
return PathDiagnosticLocation::create(CallerInfo->getDecl(), SM);
|
|
|
|
}
|
|
|
|
case CFGElement::TemporaryDtor:
|
|
|
|
llvm_unreachable("not yet implemented!");
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Unknown CFGElement kind");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-17 03:18:30 +08:00
|
|
|
PathDiagnosticLocation
|
2011-09-21 05:38:35 +08:00
|
|
|
PathDiagnosticLocation::createBegin(const Decl *D,
|
|
|
|
const SourceManager &SM) {
|
|
|
|
return PathDiagnosticLocation(D->getLocStart(), SM, SingleLocK);
|
|
|
|
}
|
|
|
|
|
|
|
|
PathDiagnosticLocation
|
|
|
|
PathDiagnosticLocation::createBegin(const Stmt *S,
|
|
|
|
const SourceManager &SM,
|
2011-10-24 09:32:45 +08:00
|
|
|
LocationOrAnalysisDeclContext LAC) {
|
2011-09-21 02:23:52 +08:00
|
|
|
return PathDiagnosticLocation(getValidSourceLocation(S, LAC),
|
2011-09-20 09:51:40 +08:00
|
|
|
SM, SingleLocK);
|
2011-09-17 03:18:30 +08:00
|
|
|
}
|
|
|
|
|
2012-07-27 04:04:05 +08:00
|
|
|
|
|
|
|
PathDiagnosticLocation
|
|
|
|
PathDiagnosticLocation::createEnd(const Stmt *S,
|
|
|
|
const SourceManager &SM,
|
|
|
|
LocationOrAnalysisDeclContext LAC) {
|
|
|
|
if (const CompoundStmt *CS = dyn_cast<CompoundStmt>(S))
|
|
|
|
return createEndBrace(CS, SM);
|
|
|
|
return PathDiagnosticLocation(getValidSourceLocation(S, LAC, /*End=*/true),
|
|
|
|
SM, SingleLocK);
|
|
|
|
}
|
|
|
|
|
2011-09-17 03:18:30 +08:00
|
|
|
PathDiagnosticLocation
|
|
|
|
PathDiagnosticLocation::createOperatorLoc(const BinaryOperator *BO,
|
|
|
|
const SourceManager &SM) {
|
|
|
|
return PathDiagnosticLocation(BO->getOperatorLoc(), SM, SingleLocK);
|
|
|
|
}
|
|
|
|
|
2011-09-21 05:38:35 +08:00
|
|
|
PathDiagnosticLocation
|
|
|
|
PathDiagnosticLocation::createMemberLoc(const MemberExpr *ME,
|
|
|
|
const SourceManager &SM) {
|
|
|
|
return PathDiagnosticLocation(ME->getMemberLoc(), SM, SingleLocK);
|
|
|
|
}
|
|
|
|
|
2011-09-17 03:18:30 +08:00
|
|
|
PathDiagnosticLocation
|
|
|
|
PathDiagnosticLocation::createBeginBrace(const CompoundStmt *CS,
|
|
|
|
const SourceManager &SM) {
|
|
|
|
SourceLocation L = CS->getLBracLoc();
|
|
|
|
return PathDiagnosticLocation(L, SM, SingleLocK);
|
|
|
|
}
|
|
|
|
|
|
|
|
PathDiagnosticLocation
|
|
|
|
PathDiagnosticLocation::createEndBrace(const CompoundStmt *CS,
|
|
|
|
const SourceManager &SM) {
|
|
|
|
SourceLocation L = CS->getRBracLoc();
|
|
|
|
return PathDiagnosticLocation(L, SM, SingleLocK);
|
|
|
|
}
|
|
|
|
|
|
|
|
PathDiagnosticLocation
|
|
|
|
PathDiagnosticLocation::createDeclBegin(const LocationContext *LC,
|
|
|
|
const SourceManager &SM) {
|
|
|
|
// FIXME: Should handle CXXTryStmt if analyser starts supporting C++.
|
|
|
|
if (const CompoundStmt *CS =
|
|
|
|
dyn_cast_or_null<CompoundStmt>(LC->getDecl()->getBody()))
|
|
|
|
if (!CS->body_empty()) {
|
|
|
|
SourceLocation Loc = (*CS->body_begin())->getLocStart();
|
|
|
|
return PathDiagnosticLocation(Loc, SM, SingleLocK);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PathDiagnosticLocation();
|
|
|
|
}
|
|
|
|
|
|
|
|
PathDiagnosticLocation
|
|
|
|
PathDiagnosticLocation::createDeclEnd(const LocationContext *LC,
|
2011-09-20 09:38:47 +08:00
|
|
|
const SourceManager &SM) {
|
2011-09-16 02:56:07 +08:00
|
|
|
SourceLocation L = LC->getDecl()->getBodyRBrace();
|
2011-09-17 03:18:30 +08:00
|
|
|
return PathDiagnosticLocation(L, SM, SingleLocK);
|
2011-09-16 02:56:07 +08:00
|
|
|
}
|
|
|
|
|
2011-09-20 09:38:47 +08:00
|
|
|
PathDiagnosticLocation
|
|
|
|
PathDiagnosticLocation::create(const ProgramPoint& P,
|
|
|
|
const SourceManager &SMng) {
|
2011-09-16 02:56:07 +08:00
|
|
|
|
2011-09-20 09:38:47 +08:00
|
|
|
const Stmt* S = 0;
|
2011-09-16 02:56:07 +08:00
|
|
|
if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
|
|
|
|
const CFGBlock *BSrc = BE->getSrc();
|
|
|
|
S = BSrc->getTerminatorCondition();
|
|
|
|
}
|
2012-10-30 01:31:59 +08:00
|
|
|
else if (const StmtPoint *SP = dyn_cast<StmtPoint>(&P)) {
|
|
|
|
S = SP->getStmt();
|
2012-11-16 03:11:43 +08:00
|
|
|
if (isa<PostStmtPurgeDeadSymbols>(P))
|
|
|
|
return PathDiagnosticLocation::createEnd(S, SMng, P.getLocationContext());
|
2011-09-16 02:56:07 +08:00
|
|
|
}
|
2012-08-04 07:08:44 +08:00
|
|
|
else if (const PostImplicitCall *PIE = dyn_cast<PostImplicitCall>(&P)) {
|
|
|
|
return PathDiagnosticLocation(PIE->getLocation(), SMng);
|
|
|
|
}
|
|
|
|
else if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
|
|
|
|
return getLocationForCaller(CE->getCalleeContext(),
|
|
|
|
CE->getLocationContext(),
|
|
|
|
SMng);
|
|
|
|
}
|
|
|
|
else if (const CallExitEnd *CEE = dyn_cast<CallExitEnd>(&P)) {
|
|
|
|
return getLocationForCaller(CEE->getCalleeContext(),
|
|
|
|
CEE->getLocationContext(),
|
|
|
|
SMng);
|
|
|
|
}
|
2012-10-30 01:31:59 +08:00
|
|
|
else {
|
|
|
|
llvm_unreachable("Unexpected ProgramPoint");
|
|
|
|
}
|
2011-09-16 02:56:07 +08:00
|
|
|
|
2011-09-20 09:38:47 +08:00
|
|
|
return PathDiagnosticLocation(S, SMng, P.getLocationContext());
|
2011-09-16 02:56:07 +08:00
|
|
|
}
|
|
|
|
|
2011-09-17 03:18:30 +08:00
|
|
|
PathDiagnosticLocation
|
|
|
|
PathDiagnosticLocation::createEndOfPath(const ExplodedNode* N,
|
|
|
|
const SourceManager &SM) {
|
2011-09-14 08:25:17 +08:00
|
|
|
assert(N && "Cannot create a location with a null node.");
|
|
|
|
|
|
|
|
const ExplodedNode *NI = N;
|
2012-10-06 09:19:30 +08:00
|
|
|
const Stmt *S = 0;
|
2011-09-14 08:25:17 +08:00
|
|
|
|
|
|
|
while (NI) {
|
|
|
|
ProgramPoint P = NI->getLocation();
|
2012-11-16 03:11:43 +08:00
|
|
|
if (const StmtPoint *PS = dyn_cast<StmtPoint>(&P)) {
|
2012-10-06 09:19:30 +08:00
|
|
|
S = PS->getStmt();
|
2012-11-16 03:11:43 +08:00
|
|
|
if (isa<PostStmtPurgeDeadSymbols>(P))
|
|
|
|
return PathDiagnosticLocation::createEnd(S, SM,
|
|
|
|
NI->getLocationContext());
|
|
|
|
break;
|
|
|
|
} else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
|
2012-10-06 09:19:30 +08:00
|
|
|
S = BE->getSrc()->getTerminator();
|
|
|
|
break;
|
2012-11-16 03:11:43 +08:00
|
|
|
}
|
2011-09-14 08:25:17 +08:00
|
|
|
NI = NI->succ_empty() ? 0 : *(NI->succ_begin());
|
|
|
|
}
|
|
|
|
|
2012-10-06 09:19:30 +08:00
|
|
|
if (S) {
|
|
|
|
const LocationContext *LC = NI->getLocationContext();
|
|
|
|
if (S->getLocStart().isValid())
|
|
|
|
return PathDiagnosticLocation(S, SM, LC);
|
|
|
|
return PathDiagnosticLocation(getValidSourceLocation(S, LC), SM);
|
|
|
|
}
|
|
|
|
|
2011-09-17 03:18:30 +08:00
|
|
|
return createDeclEnd(N->getLocationContext(), SM);
|
2011-09-15 01:48:01 +08:00
|
|
|
}
|
|
|
|
|
2011-09-20 09:38:47 +08:00
|
|
|
PathDiagnosticLocation PathDiagnosticLocation::createSingleLocation(
|
|
|
|
const PathDiagnosticLocation &PDL) {
|
|
|
|
FullSourceLoc L = PDL.asLocation();
|
|
|
|
return PathDiagnosticLocation(L, L.getManager(), SingleLocK);
|
|
|
|
}
|
|
|
|
|
2011-09-20 09:51:40 +08:00
|
|
|
FullSourceLoc
|
2011-09-21 08:35:58 +08:00
|
|
|
PathDiagnosticLocation::genLocation(SourceLocation L,
|
2011-10-24 09:32:45 +08:00
|
|
|
LocationOrAnalysisDeclContext LAC) 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:
|
2012-01-10 23:26:13 +08:00
|
|
|
// Defensive checking.
|
|
|
|
if (!S)
|
|
|
|
break;
|
2011-09-21 02:23:52 +08:00
|
|
|
return FullSourceLoc(getValidSourceLocation(S, LAC),
|
2011-09-15 01:48:01 +08:00
|
|
|
const_cast<SourceManager&>(*SM));
|
2009-04-07 06:33:35 +08:00
|
|
|
case DeclK:
|
2012-01-10 23:26:13 +08:00
|
|
|
// Defensive checking.
|
|
|
|
if (!D)
|
|
|
|
break;
|
2009-04-07 06:33:35 +08:00
|
|
|
return FullSourceLoc(D->getLocation(), const_cast<SourceManager&>(*SM));
|
2009-03-27 05:39:39 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-09-21 07:27:32 +08:00
|
|
|
return FullSourceLoc(L, const_cast<SourceManager&>(*SM));
|
2009-03-27 05:39:39 +08:00
|
|
|
}
|
2009-03-27 05:48:17 +08:00
|
|
|
|
2011-09-20 09:51:40 +08:00
|
|
|
PathDiagnosticRange
|
2011-10-24 09:32:45 +08:00
|
|
|
PathDiagnosticLocation::genRange(LocationOrAnalysisDeclContext LAC) 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:
|
2011-09-21 08:35:58 +08:00
|
|
|
return PathDiagnosticRange(SourceRange(Loc,Loc), 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:
|
2011-02-17 18:25:35 +08:00
|
|
|
case Stmt::BinaryConditionalOperatorClass:
|
2009-04-23 02:03:00 +08:00
|
|
|
case Stmt::ConditionalOperatorClass:
|
|
|
|
case Stmt::ObjCForCollectionStmtClass: {
|
2011-09-21 02:23:52 +08:00
|
|
|
SourceLocation L = getValidSourceLocation(S, LAC);
|
2009-04-23 02:03:00 +08:00
|
|
|
return SourceRange(L, L);
|
|
|
|
}
|
|
|
|
}
|
2011-09-21 08:35:58 +08:00
|
|
|
SourceRange R = S->getSourceRange();
|
|
|
|
if (R.isValid())
|
|
|
|
return R;
|
|
|
|
break;
|
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
|
|
|
|
2011-09-21 08:35:58 +08:00
|
|
|
return SourceRange(Loc,Loc);
|
2009-03-27 05:48:17 +08:00
|
|
|
}
|
|
|
|
|
2009-04-07 06:33:35 +08:00
|
|
|
void PathDiagnosticLocation::flatten() {
|
|
|
|
if (K == StmtK) {
|
|
|
|
K = RangeK;
|
|
|
|
S = 0;
|
|
|
|
D = 0;
|
|
|
|
}
|
|
|
|
else if (K == DeclK) {
|
|
|
|
K = SingleLocK;
|
|
|
|
S = 0;
|
|
|
|
D = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-24 14:00:00 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Manipulation of PathDiagnosticCallPieces.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
PathDiagnosticCallPiece *
|
|
|
|
PathDiagnosticCallPiece::construct(const ExplodedNode *N,
|
2012-04-21 05:59:08 +08:00
|
|
|
const CallExitEnd &CE,
|
2012-02-24 14:00:00 +08:00
|
|
|
const SourceManager &SM) {
|
2012-04-21 05:59:08 +08:00
|
|
|
const Decl *caller = CE.getLocationContext()->getDecl();
|
2012-07-27 04:04:05 +08:00
|
|
|
PathDiagnosticLocation pos = getLocationForCaller(CE.getCalleeContext(),
|
|
|
|
CE.getLocationContext(),
|
|
|
|
SM);
|
2012-02-24 14:00:00 +08:00
|
|
|
return new PathDiagnosticCallPiece(caller, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
PathDiagnosticCallPiece *
|
2012-03-15 02:58:28 +08:00
|
|
|
PathDiagnosticCallPiece::construct(PathPieces &path,
|
|
|
|
const Decl *caller) {
|
|
|
|
PathDiagnosticCallPiece *C = new PathDiagnosticCallPiece(path, caller);
|
2012-02-24 14:00:00 +08:00
|
|
|
path.clear();
|
|
|
|
path.push_front(C);
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathDiagnosticCallPiece::setCallee(const CallEnter &CE,
|
|
|
|
const SourceManager &SM) {
|
2012-07-27 04:04:05 +08:00
|
|
|
const StackFrameContext *CalleeCtx = CE.getCalleeContext();
|
|
|
|
Callee = CalleeCtx->getDecl();
|
|
|
|
|
|
|
|
callEnterWithin = PathDiagnosticLocation::createBegin(Callee, SM);
|
|
|
|
callEnter = getLocationForCaller(CalleeCtx, CE.getLocationContext(), SM);
|
2012-02-24 14:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
IntrusiveRefCntPtr<PathDiagnosticEventPiece>
|
|
|
|
PathDiagnosticCallPiece::getCallEnterEvent() const {
|
|
|
|
if (!Callee)
|
|
|
|
return 0;
|
|
|
|
SmallString<256> buf;
|
|
|
|
llvm::raw_svector_ostream Out(buf);
|
|
|
|
if (isa<BlockDecl>(Callee))
|
2012-03-06 09:25:01 +08:00
|
|
|
Out << "Calling anonymous block";
|
2012-02-24 14:00:00 +08:00
|
|
|
else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Callee))
|
2012-03-06 09:25:01 +08:00
|
|
|
Out << "Calling '" << *ND << "'";
|
2012-02-24 14:00:00 +08:00
|
|
|
StringRef msg = Out.str();
|
|
|
|
if (msg.empty())
|
|
|
|
return 0;
|
2012-11-15 10:07:23 +08:00
|
|
|
assert(callEnter.asLocation().isValid());
|
2012-02-24 14:00:00 +08:00
|
|
|
return new PathDiagnosticEventPiece(callEnter, msg);
|
|
|
|
}
|
|
|
|
|
2012-03-06 09:25:01 +08:00
|
|
|
IntrusiveRefCntPtr<PathDiagnosticEventPiece>
|
|
|
|
PathDiagnosticCallPiece::getCallEnterWithinCallerEvent() const {
|
2012-11-15 10:07:23 +08:00
|
|
|
if (!callEnterWithin.asLocation().isValid())
|
|
|
|
return 0;
|
2012-03-06 09:25:01 +08:00
|
|
|
SmallString<256> buf;
|
|
|
|
llvm::raw_svector_ostream Out(buf);
|
2012-03-14 06:15:55 +08:00
|
|
|
if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Caller))
|
|
|
|
Out << "Entered call from '" << *ND << "'";
|
|
|
|
else
|
|
|
|
Out << "Entered call";
|
2012-03-06 09:25:01 +08:00
|
|
|
StringRef msg = Out.str();
|
|
|
|
if (msg.empty())
|
|
|
|
return 0;
|
|
|
|
return new PathDiagnosticEventPiece(callEnterWithin, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
IntrusiveRefCntPtr<PathDiagnosticEventPiece>
|
2012-02-24 14:00:00 +08:00
|
|
|
PathDiagnosticCallPiece::getCallExitEvent() const {
|
2012-03-15 02:58:28 +08:00
|
|
|
if (NoExit)
|
2012-02-24 14:00:00 +08:00
|
|
|
return 0;
|
|
|
|
SmallString<256> buf;
|
|
|
|
llvm::raw_svector_ostream Out(buf);
|
2012-03-17 07:44:28 +08:00
|
|
|
if (!CallStackMessage.empty())
|
|
|
|
Out << CallStackMessage;
|
|
|
|
else if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Callee))
|
2012-03-14 06:15:55 +08:00
|
|
|
Out << "Returning from '" << *ND << "'";
|
2012-02-24 14:00:00 +08:00
|
|
|
else
|
|
|
|
Out << "Returning to caller";
|
2012-11-15 10:07:23 +08:00
|
|
|
assert(callReturn.asLocation().isValid());
|
2012-02-24 14:00:00 +08:00
|
|
|
return new PathDiagnosticEventPiece(callReturn, Out.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void compute_path_size(const PathPieces &pieces, unsigned &size) {
|
|
|
|
for (PathPieces::const_iterator it = pieces.begin(),
|
|
|
|
et = pieces.end(); it != et; ++it) {
|
|
|
|
const PathDiagnosticPiece *piece = it->getPtr();
|
|
|
|
if (const PathDiagnosticCallPiece *cp =
|
|
|
|
dyn_cast<PathDiagnosticCallPiece>(piece)) {
|
|
|
|
compute_path_size(cp->path, size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned PathDiagnostic::full_size() {
|
|
|
|
unsigned size = 0;
|
|
|
|
compute_path_size(path, size);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2009-09-19 06:33:39 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// FoldingSet profiling methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void PathDiagnosticLocation::Profile(llvm::FoldingSetNodeID &ID) const {
|
2011-09-21 05:25:00 +08:00
|
|
|
ID.AddInteger(Range.getBegin().getRawEncoding());
|
|
|
|
ID.AddInteger(Range.getEnd().getRawEncoding());
|
|
|
|
ID.AddInteger(Loc.getRawEncoding());
|
2009-09-19 06:33:39 +08:00
|
|
|
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());
|
2012-08-17 01:45:29 +08:00
|
|
|
ArrayRef<SourceRange> Ranges = getRanges();
|
|
|
|
for (ArrayRef<SourceRange>::iterator I = Ranges.begin(), E = Ranges.end();
|
|
|
|
I != E; ++I) {
|
2009-09-19 06:33:39 +08:00
|
|
|
ID.AddInteger(I->getBegin().getRawEncoding());
|
|
|
|
ID.AddInteger(I->getEnd().getRawEncoding());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-24 14:00:00 +08:00
|
|
|
void PathDiagnosticCallPiece::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
PathDiagnosticPiece::Profile(ID);
|
|
|
|
for (PathPieces::const_iterator it = path.begin(),
|
|
|
|
et = path.end(); it != et; ++it) {
|
|
|
|
ID.Add(**it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-19 06:33:39 +08:00
|
|
|
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);
|
2012-02-08 12:32:34 +08:00
|
|
|
for (PathPieces::const_iterator I = subPieces.begin(), E = subPieces.end();
|
|
|
|
I != E; ++I)
|
2009-09-19 06:33:39 +08:00
|
|
|
ID.Add(**I);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathDiagnostic::Profile(llvm::FoldingSetNodeID &ID) const {
|
2012-08-31 08:36:26 +08:00
|
|
|
ID.Add(getLocation());
|
2009-09-19 06:33:39 +08:00
|
|
|
ID.AddString(BugType);
|
2012-08-31 08:36:26 +08:00
|
|
|
ID.AddString(VerboseDesc);
|
2009-09-19 06:33:39 +08:00
|
|
|
ID.AddString(Category);
|
2012-01-26 07:47:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PathDiagnostic::FullProfile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
Profile(ID);
|
2012-02-08 12:32:34 +08:00
|
|
|
for (PathPieces::const_iterator I = path.begin(), E = path.end(); I != E; ++I)
|
|
|
|
ID.Add(**I);
|
2009-09-19 06:33:39 +08:00
|
|
|
for (meta_iterator I = meta_begin(), E = meta_end(); I != E; ++I)
|
|
|
|
ID.AddString(*I);
|
|
|
|
}
|
2012-03-17 07:24:20 +08:00
|
|
|
|
|
|
|
StackHintGenerator::~StackHintGenerator() {}
|
|
|
|
|
|
|
|
std::string StackHintGeneratorForSymbol::getMessage(const ExplodedNode *N){
|
|
|
|
ProgramPoint P = N->getLocation();
|
2012-04-21 05:59:08 +08:00
|
|
|
const CallExitEnd *CExit = dyn_cast<CallExitEnd>(&P);
|
|
|
|
assert(CExit && "Stack Hints should be constructed at CallExitEnd points.");
|
2012-03-17 07:24:20 +08:00
|
|
|
|
2012-07-27 04:04:05 +08:00
|
|
|
// FIXME: Use CallEvent to abstract this over all calls.
|
2012-07-11 06:07:52 +08:00
|
|
|
const Stmt *CallSite = CExit->getCalleeContext()->getCallSite();
|
|
|
|
const CallExpr *CE = dyn_cast_or_null<CallExpr>(CallSite);
|
2012-03-17 07:24:20 +08:00
|
|
|
if (!CE)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
if (!N)
|
|
|
|
return getMessageForSymbolNotFound();
|
|
|
|
|
|
|
|
// Check if one of the parameters are set to the interesting symbol.
|
|
|
|
ProgramStateRef State = N->getState();
|
|
|
|
const LocationContext *LCtx = N->getLocationContext();
|
|
|
|
unsigned ArgIndex = 0;
|
|
|
|
for (CallExpr::const_arg_iterator I = CE->arg_begin(),
|
|
|
|
E = CE->arg_end(); I != E; ++I, ++ArgIndex){
|
|
|
|
SVal SV = State->getSVal(*I, LCtx);
|
|
|
|
|
|
|
|
// Check if the variable corresponding to the symbol is passed by value.
|
|
|
|
SymbolRef AS = SV.getAsLocSymbol();
|
|
|
|
if (AS == Sym) {
|
|
|
|
return getMessageForArg(*I, ArgIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the parameter is a pointer to the symbol.
|
|
|
|
if (const loc::MemRegionVal *Reg = dyn_cast<loc::MemRegionVal>(&SV)) {
|
|
|
|
SVal PSV = State->getSVal(Reg->getRegion());
|
|
|
|
SymbolRef AS = PSV.getAsLocSymbol();
|
|
|
|
if (AS == Sym) {
|
|
|
|
return getMessageForArg(*I, ArgIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we are returning the interesting symbol.
|
|
|
|
SVal SV = State->getSVal(CE, LCtx);
|
|
|
|
SymbolRef RetSym = SV.getAsLocSymbol();
|
|
|
|
if (RetSym == Sym) {
|
|
|
|
return getMessageForReturn(CE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return getMessageForSymbolNotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string StackHintGeneratorForSymbol::getMessageForArg(const Expr *ArgE,
|
2012-09-22 09:24:42 +08:00
|
|
|
unsigned ArgIndex) {
|
|
|
|
// Printed parameters start at 1, not 0.
|
|
|
|
++ArgIndex;
|
|
|
|
|
2012-03-17 07:24:20 +08:00
|
|
|
SmallString<200> buf;
|
|
|
|
llvm::raw_svector_ostream os(buf);
|
|
|
|
|
2012-09-22 09:24:42 +08:00
|
|
|
os << Msg << " via " << ArgIndex << llvm::getOrdinalSuffix(ArgIndex)
|
|
|
|
<< " parameter";
|
2012-03-17 07:24:20 +08:00
|
|
|
|
|
|
|
return os.str();
|
|
|
|
}
|