2008-03-27 03:49:32 +00: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 01:03:03 +00:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
|
2011-09-14 00:25:17 +00:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
|
2012-02-28 23:27:39 +00:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2009-03-26 21:39:39 +00:00
|
|
|
#include "clang/AST/Expr.h"
|
2009-04-06 22:33:35 +00:00
|
|
|
#include "clang/AST/Decl.h"
|
2012-07-26 20:04:05 +00:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2009-04-06 22:33:35 +00:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2011-09-14 17:48:01 +00:00
|
|
|
#include "clang/AST/ParentMap.h"
|
2009-04-26 20:35:05 +00:00
|
|
|
#include "clang/AST/StmtCXX.h"
|
2008-11-19 06:51:40 +00:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2009-06-26 00:43:22 +00:00
|
|
|
|
2008-03-27 03:49:32 +00:00
|
|
|
using namespace clang;
|
2010-12-23 07:20:52 +00:00
|
|
|
using namespace ento;
|
2009-03-10 05:16:17 +00:00
|
|
|
|
|
|
|
bool PathDiagnosticMacroPiece::containsEvent() const {
|
2012-02-08 04:32:34 +00:00
|
|
|
for (PathPieces::const_iterator I = subPieces.begin(), E = subPieces.end();
|
|
|
|
I!=E; ++I) {
|
2009-03-10 05:16:17 +00:00
|
|
|
if (isa<PathDiagnosticEventPiece>(*I))
|
|
|
|
return true;
|
|
|
|
if (PathDiagnosticMacroPiece *MP = dyn_cast<PathDiagnosticMacroPiece>(*I))
|
|
|
|
if (MP->containsEvent())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-02-26 21:30:32 +00:00
|
|
|
|
2011-07-23 10:55:15 +00:00
|
|
|
static StringRef StripTrailingDots(StringRef s) {
|
|
|
|
for (StringRef::size_type i = s.size(); i != 0; --i)
|
2009-12-11 21:09:27 +00:00
|
|
|
if (s[i - 1] != '.')
|
|
|
|
return s.substr(0, i);
|
|
|
|
return "";
|
2009-02-26 21:30:32 +00:00
|
|
|
}
|
|
|
|
|
2011-07-23 10:55:15 +00:00
|
|
|
PathDiagnosticPiece::PathDiagnosticPiece(StringRef s,
|
2009-03-02 19:39:50 +00:00
|
|
|
Kind k, DisplayHint hint)
|
2009-12-11 21:09:27 +00:00
|
|
|
: str(StripTrailingDots(s)), kind(k), Hint(hint) {}
|
2009-03-06 07:53:30 +00:00
|
|
|
|
2009-03-26 21:21:35 +00:00
|
|
|
PathDiagnosticPiece::PathDiagnosticPiece(Kind k, DisplayHint hint)
|
|
|
|
: kind(k), Hint(hint) {}
|
2009-03-12 18:41:53 +00:00
|
|
|
|
2009-03-06 22:10:49 +00:00
|
|
|
PathDiagnosticPiece::~PathDiagnosticPiece() {}
|
2009-03-06 23:58:11 +00:00
|
|
|
PathDiagnosticEventPiece::~PathDiagnosticEventPiece() {}
|
2012-02-24 06:00:00 +00:00
|
|
|
PathDiagnosticCallPiece::~PathDiagnosticCallPiece() {}
|
2009-03-06 23:58:11 +00:00
|
|
|
PathDiagnosticControlFlowPiece::~PathDiagnosticControlFlowPiece() {}
|
2012-02-08 04:32:34 +00:00
|
|
|
PathDiagnosticMacroPiece::~PathDiagnosticMacroPiece() {}
|
2012-04-04 18:11:35 +00:00
|
|
|
|
|
|
|
|
2012-02-08 04:32:34 +00:00
|
|
|
PathPieces::~PathPieces() {}
|
2012-02-08 04:32:27 +00:00
|
|
|
PathDiagnostic::~PathDiagnostic() {}
|
|
|
|
|
2012-04-04 18:11:35 +00:00
|
|
|
PathDiagnostic::PathDiagnostic(const Decl *declWithIssue,
|
|
|
|
StringRef bugtype, StringRef desc,
|
2011-07-23 10:55:15 +00:00
|
|
|
StringRef category)
|
2012-04-04 18:11:35 +00:00
|
|
|
: DeclWithIssue(declWithIssue),
|
|
|
|
BugType(StripTrailingDots(bugtype)),
|
2009-12-11 21:09:27 +00:00
|
|
|
Desc(StripTrailingDots(desc)),
|
2012-02-24 06:00:00 +00:00
|
|
|
Category(StripTrailingDots(category)),
|
|
|
|
path(pathImpl) {}
|
2009-03-06 07:08:50 +00:00
|
|
|
|
2011-12-20 02:48:34 +00:00
|
|
|
void PathDiagnosticConsumer::anchor() { }
|
|
|
|
|
2012-01-25 23:47:14 +00: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-28 23:27:39 +00:00
|
|
|
llvm::OwningPtr<PathDiagnostic> OwningD(D);
|
2012-01-25 23:47:14 +00:00
|
|
|
|
2012-02-28 23:27:39 +00:00
|
|
|
if (!D || D->path.empty())
|
2012-01-25 23:47:14 +00: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-28 23:27:39 +00: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-02-29 23:59:20 +00:00
|
|
|
llvm::SmallVector<const PathPieces *, 5> WorkList;
|
|
|
|
WorkList.push_back(&D->path);
|
2012-02-28 23:27:39 +00:00
|
|
|
|
2012-02-29 23:59:20 +00: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-28 23:27:39 +00:00
|
|
|
|
2012-02-29 23:59:20 +00:00
|
|
|
if (FID.isInvalid()) {
|
|
|
|
FID = SMgr.getFileID(L);
|
|
|
|
} else if (SMgr.getFileID(L) != FID)
|
2012-02-28 23:27:39 +00:00
|
|
|
return; // FIXME: Emit a warning?
|
2012-02-29 23:59:20 +00:00
|
|
|
|
|
|
|
// Check the source ranges.
|
|
|
|
for (PathDiagnosticPiece::range_iterator RI = piece->ranges_begin(),
|
|
|
|
RE = piece->ranges_end();
|
|
|
|
RI != RE; ++RI) {
|
|
|
|
SourceLocation L = SMgr.getExpansionLoc(RI->getBegin());
|
|
|
|
if (!L.isFileID() || SMgr.getFileID(L) != FID)
|
|
|
|
return; // FIXME: Emit a warning?
|
|
|
|
L = SMgr.getExpansionLoc(RI->getEnd());
|
|
|
|
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-28 23:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FID.isInvalid())
|
|
|
|
return; // FIXME: Emit a warning?
|
|
|
|
}
|
|
|
|
|
2012-01-25 23:47:14 +00: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-02-24 06:00:00 +00:00
|
|
|
const unsigned orig_size = orig->full_size();
|
|
|
|
const unsigned new_size = D->full_size();
|
|
|
|
|
|
|
|
if (orig_size <= new_size) {
|
2012-01-25 23:47:14 +00:00
|
|
|
bool shouldKeepOriginal = true;
|
2012-02-24 06:00:00 +00:00
|
|
|
if (orig_size == new_size) {
|
2012-01-25 23:47:14 +00:00
|
|
|
// Here we break ties in a fairly arbitrary, but deterministic, way.
|
|
|
|
llvm::FoldingSetNodeID fullProfile, fullProfileOrig;
|
|
|
|
D->FullProfile(fullProfile);
|
|
|
|
orig->FullProfile(fullProfileOrig);
|
|
|
|
if (fullProfile.ComputeHash() < fullProfileOrig.ComputeHash())
|
|
|
|
shouldKeepOriginal = false;
|
|
|
|
}
|
|
|
|
|
2012-02-28 23:27:39 +00:00
|
|
|
if (shouldKeepOriginal)
|
2012-01-25 23:47:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Diags.RemoveNode(orig);
|
|
|
|
delete orig;
|
|
|
|
}
|
|
|
|
|
2012-02-28 23:27:39 +00:00
|
|
|
Diags.InsertNode(OwningD.take());
|
2012-01-25 23:47:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct CompareDiagnostics {
|
|
|
|
// Compare if 'X' is "<" than 'Y'.
|
|
|
|
bool operator()(const PathDiagnostic *X, const PathDiagnostic *Y) const {
|
|
|
|
// First compare by location
|
|
|
|
const FullSourceLoc &XLoc = X->getLocation().asLocation();
|
|
|
|
const FullSourceLoc &YLoc = Y->getLocation().asLocation();
|
|
|
|
if (XLoc < YLoc)
|
|
|
|
return true;
|
|
|
|
if (XLoc != YLoc)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Next, compare by bug type.
|
|
|
|
StringRef XBugType = X->getBugType();
|
|
|
|
StringRef YBugType = Y->getBugType();
|
|
|
|
if (XBugType < YBugType)
|
|
|
|
return true;
|
|
|
|
if (XBugType != YBugType)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Next, compare by bug description.
|
|
|
|
StringRef XDesc = X->getDescription();
|
|
|
|
StringRef YDesc = Y->getDescription();
|
|
|
|
if (XDesc < YDesc)
|
|
|
|
return true;
|
|
|
|
if (XDesc != YDesc)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// FIXME: Further refine by comparing PathDiagnosticPieces?
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PathDiagnosticConsumer::FlushDiagnostics(SmallVectorImpl<std::string> *Files) {
|
|
|
|
if (flushed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
flushed = true;
|
|
|
|
|
|
|
|
std::vector<const PathDiagnostic *> BatchDiags;
|
|
|
|
for (llvm::FoldingSet<PathDiagnostic>::iterator it = Diags.begin(),
|
|
|
|
et = Diags.end(); it != et; ++it) {
|
|
|
|
BatchDiags.push_back(&*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear out the FoldingSet.
|
|
|
|
Diags.clear();
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2011-08-27 21:39:14 +00:00
|
|
|
}
|
|
|
|
|
2009-03-26 21:39:39 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PathDiagnosticLocation methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-09-16 19:18:30 +00:00
|
|
|
static SourceLocation getValidSourceLocation(const Stmt* S,
|
2012-07-26 20:04:05 +00:00
|
|
|
LocationOrAnalysisDeclContext LAC,
|
|
|
|
bool UseEnd = false) {
|
|
|
|
SourceLocation L = UseEnd ? S->getLocEnd() : S->getLocStart();
|
2011-10-24 01:32:45 +00:00
|
|
|
assert(!LAC.isNull() && "A valid LocationContext or AnalysisDeclContext should "
|
2011-09-20 18:23:52 +00:00
|
|
|
"be passed to PathDiagnosticLocation upon creation.");
|
2011-09-16 19:18:30 +00:00
|
|
|
|
|
|
|
// S might be a temporary statement that does not have a location in the
|
2012-07-26 20:04:05 +00:00
|
|
|
// source code, so find an enclosing statement and use its location.
|
2011-09-20 16:37:36 +00:00
|
|
|
if (!L.isValid()) {
|
2011-09-20 18:23:52 +00:00
|
|
|
|
2012-07-26 20:04:30 +00:00
|
|
|
AnalysisDeclContext *ADC;
|
2011-09-20 18:23:52 +00:00
|
|
|
if (LAC.is<const LocationContext*>())
|
2012-07-26 20:04:30 +00:00
|
|
|
ADC = LAC.get<const LocationContext*>()->getAnalysisDeclContext();
|
2011-09-20 18:23:52 +00:00
|
|
|
else
|
2012-07-26 20:04:30 +00: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-20 16:37:36 +00:00
|
|
|
|
2012-07-26 20:04:30 +00:00
|
|
|
L = UseEnd ? Parent->getLocEnd() : Parent->getLocStart();
|
|
|
|
} while (!L.isValid());
|
2011-09-16 19:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return L;
|
|
|
|
}
|
|
|
|
|
|
|
|
PathDiagnosticLocation
|
2011-09-20 21:38:35 +00: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 01:32:45 +00:00
|
|
|
LocationOrAnalysisDeclContext LAC) {
|
2011-09-20 18:23:52 +00:00
|
|
|
return PathDiagnosticLocation(getValidSourceLocation(S, LAC),
|
2011-09-20 01:51:40 +00:00
|
|
|
SM, SingleLocK);
|
2011-09-16 19:18:30 +00:00
|
|
|
}
|
|
|
|
|
2012-07-26 20:04:05 +00: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-16 19:18:30 +00:00
|
|
|
PathDiagnosticLocation
|
|
|
|
PathDiagnosticLocation::createOperatorLoc(const BinaryOperator *BO,
|
|
|
|
const SourceManager &SM) {
|
|
|
|
return PathDiagnosticLocation(BO->getOperatorLoc(), SM, SingleLocK);
|
|
|
|
}
|
|
|
|
|
2011-09-20 21:38:35 +00:00
|
|
|
PathDiagnosticLocation
|
|
|
|
PathDiagnosticLocation::createMemberLoc(const MemberExpr *ME,
|
|
|
|
const SourceManager &SM) {
|
|
|
|
return PathDiagnosticLocation(ME->getMemberLoc(), SM, SingleLocK);
|
|
|
|
}
|
|
|
|
|
2011-09-16 19:18:30 +00: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 01:38:47 +00:00
|
|
|
const SourceManager &SM) {
|
2011-09-15 18:56:07 +00:00
|
|
|
SourceLocation L = LC->getDecl()->getBodyRBrace();
|
2011-09-16 19:18:30 +00:00
|
|
|
return PathDiagnosticLocation(L, SM, SingleLocK);
|
2011-09-15 18:56:07 +00:00
|
|
|
}
|
|
|
|
|
2011-09-20 01:38:47 +00:00
|
|
|
PathDiagnosticLocation
|
|
|
|
PathDiagnosticLocation::create(const ProgramPoint& P,
|
|
|
|
const SourceManager &SMng) {
|
2011-09-15 18:56:07 +00:00
|
|
|
|
2011-09-20 01:38:47 +00:00
|
|
|
const Stmt* S = 0;
|
2011-09-15 18:56:07 +00:00
|
|
|
if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
|
|
|
|
const CFGBlock *BSrc = BE->getSrc();
|
|
|
|
S = BSrc->getTerminatorCondition();
|
|
|
|
}
|
|
|
|
else if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
|
|
|
|
S = PS->getStmt();
|
|
|
|
}
|
|
|
|
|
2011-09-20 01:38:47 +00:00
|
|
|
return PathDiagnosticLocation(S, SMng, P.getLocationContext());
|
2011-09-15 18:56:07 +00:00
|
|
|
}
|
|
|
|
|
2011-09-16 19:18:30 +00:00
|
|
|
PathDiagnosticLocation
|
|
|
|
PathDiagnosticLocation::createEndOfPath(const ExplodedNode* N,
|
|
|
|
const SourceManager &SM) {
|
2011-09-14 00:25:17 +00:00
|
|
|
assert(N && "Cannot create a location with a null node.");
|
|
|
|
|
|
|
|
const ExplodedNode *NI = N;
|
|
|
|
|
|
|
|
while (NI) {
|
|
|
|
ProgramPoint P = NI->getLocation();
|
2011-09-14 17:48:01 +00:00
|
|
|
const LocationContext *LC = P.getLocationContext();
|
2011-09-20 01:38:47 +00:00
|
|
|
if (const StmtPoint *PS = dyn_cast<StmtPoint>(&P))
|
2011-09-14 17:48:01 +00:00
|
|
|
return PathDiagnosticLocation(PS->getStmt(), SM, LC);
|
2011-09-14 00:25:17 +00:00
|
|
|
else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
|
|
|
|
const Stmt *Term = BE->getSrc()->getTerminator();
|
2012-02-16 03:41:01 +00:00
|
|
|
if (Term) {
|
|
|
|
return PathDiagnosticLocation(Term, SM, LC);
|
|
|
|
}
|
2011-09-14 00:25:17 +00:00
|
|
|
}
|
|
|
|
NI = NI->succ_empty() ? 0 : *(NI->succ_begin());
|
|
|
|
}
|
|
|
|
|
2011-09-16 19:18:30 +00:00
|
|
|
return createDeclEnd(N->getLocationContext(), SM);
|
2011-09-14 17:48:01 +00:00
|
|
|
}
|
|
|
|
|
2011-09-20 01:38:47 +00:00
|
|
|
PathDiagnosticLocation PathDiagnosticLocation::createSingleLocation(
|
|
|
|
const PathDiagnosticLocation &PDL) {
|
|
|
|
FullSourceLoc L = PDL.asLocation();
|
|
|
|
return PathDiagnosticLocation(L, L.getManager(), SingleLocK);
|
|
|
|
}
|
|
|
|
|
2011-09-20 01:51:40 +00:00
|
|
|
FullSourceLoc
|
2011-09-21 00:35:58 +00:00
|
|
|
PathDiagnosticLocation::genLocation(SourceLocation L,
|
2011-10-24 01:32:45 +00:00
|
|
|
LocationOrAnalysisDeclContext LAC) const {
|
2009-04-01 06:13:56 +00:00
|
|
|
assert(isValid());
|
2009-03-26 21:42:51 +00:00
|
|
|
// Note that we want a 'switch' here so that the compiler can warn us in
|
|
|
|
// case we add more cases.
|
2009-03-26 21:39:39 +00:00
|
|
|
switch (K) {
|
2009-04-06 22:33:35 +00:00
|
|
|
case SingleLocK:
|
|
|
|
case RangeK:
|
2009-03-26 21:42:00 +00:00
|
|
|
break;
|
2009-04-06 22:33:35 +00:00
|
|
|
case StmtK:
|
2012-01-10 15:26:13 +00:00
|
|
|
// Defensive checking.
|
|
|
|
if (!S)
|
|
|
|
break;
|
2011-09-20 18:23:52 +00:00
|
|
|
return FullSourceLoc(getValidSourceLocation(S, LAC),
|
2011-09-14 17:48:01 +00:00
|
|
|
const_cast<SourceManager&>(*SM));
|
2009-04-06 22:33:35 +00:00
|
|
|
case DeclK:
|
2012-01-10 15:26:13 +00:00
|
|
|
// Defensive checking.
|
|
|
|
if (!D)
|
|
|
|
break;
|
2009-04-06 22:33:35 +00:00
|
|
|
return FullSourceLoc(D->getLocation(), const_cast<SourceManager&>(*SM));
|
2009-03-26 21:39:39 +00:00
|
|
|
}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2011-09-20 23:27:32 +00:00
|
|
|
return FullSourceLoc(L, const_cast<SourceManager&>(*SM));
|
2009-03-26 21:39:39 +00:00
|
|
|
}
|
2009-03-26 21:48:17 +00:00
|
|
|
|
2011-09-20 01:51:40 +00:00
|
|
|
PathDiagnosticRange
|
2011-10-24 01:32:45 +00:00
|
|
|
PathDiagnosticLocation::genRange(LocationOrAnalysisDeclContext LAC) const {
|
2009-04-01 06:13:56 +00:00
|
|
|
assert(isValid());
|
2009-03-26 21:48:17 +00: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-06 22:33:35 +00:00
|
|
|
case SingleLocK:
|
2011-09-21 00:35:58 +00:00
|
|
|
return PathDiagnosticRange(SourceRange(Loc,Loc), true);
|
2009-04-06 22:33:35 +00:00
|
|
|
case RangeK:
|
2009-03-26 21:48:17 +00:00
|
|
|
break;
|
2009-04-22 18:03:00 +00:00
|
|
|
case StmtK: {
|
|
|
|
const Stmt *S = asStmt();
|
|
|
|
switch (S->getStmtClass()) {
|
|
|
|
default:
|
|
|
|
break;
|
2009-05-15 02:05:25 +00: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 15:08:12 +00:00
|
|
|
DS->getSingleDecl()->getLocation());
|
2009-05-15 02:05:25 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-04-22 18:03:00 +00: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 10:25:35 +00:00
|
|
|
case Stmt::BinaryConditionalOperatorClass:
|
2009-04-22 18:03:00 +00:00
|
|
|
case Stmt::ConditionalOperatorClass:
|
|
|
|
case Stmt::ObjCForCollectionStmtClass: {
|
2011-09-20 18:23:52 +00:00
|
|
|
SourceLocation L = getValidSourceLocation(S, LAC);
|
2009-04-22 18:03:00 +00:00
|
|
|
return SourceRange(L, L);
|
|
|
|
}
|
|
|
|
}
|
2011-09-21 00:35:58 +00:00
|
|
|
SourceRange R = S->getSourceRange();
|
|
|
|
if (R.isValid())
|
|
|
|
return R;
|
|
|
|
break;
|
2009-04-22 18:03:00 +00:00
|
|
|
}
|
2009-04-06 22:33:35 +00:00
|
|
|
case DeclK:
|
|
|
|
if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
|
|
|
|
return MD->getSourceRange();
|
2009-04-18 00:02:19 +00:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
2010-07-07 12:24:14 +00:00
|
|
|
if (Stmt *Body = FD->getBody())
|
|
|
|
return Body->getSourceRange();
|
2009-04-18 00:02:19 +00:00
|
|
|
}
|
2009-04-06 22:33:35 +00:00
|
|
|
else {
|
|
|
|
SourceLocation L = D->getLocation();
|
2009-04-22 22:26:10 +00:00
|
|
|
return PathDiagnosticRange(SourceRange(L, L), true);
|
2009-04-06 22:33:35 +00:00
|
|
|
}
|
2009-03-26 21:48:17 +00:00
|
|
|
}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2011-09-21 00:35:58 +00:00
|
|
|
return SourceRange(Loc,Loc);
|
2009-03-26 21:48:17 +00:00
|
|
|
}
|
|
|
|
|
2009-04-06 22:33:35 +00: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 06:00:00 +00:00
|
|
|
PathDiagnosticLocation PathDiagnostic::getLocation() const {
|
|
|
|
assert(path.size() > 0 &&
|
|
|
|
"getLocation() requires a non-empty PathDiagnostic.");
|
|
|
|
|
|
|
|
PathDiagnosticPiece *p = path.rbegin()->getPtr();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (PathDiagnosticCallPiece *cp = dyn_cast<PathDiagnosticCallPiece>(p)) {
|
|
|
|
assert(!cp->path.empty());
|
|
|
|
p = cp->path.rbegin()->getPtr();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return p->getLocation();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Manipulation of PathDiagnosticCallPieces.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-07-26 20:04:05 +00: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);
|
2012-02-24 06:00:00 +00:00
|
|
|
}
|
2012-07-26 20:04:05 +00:00
|
|
|
case CFGElement::AutomaticObjectDtor: {
|
|
|
|
const CFGAutomaticObjDtor &Dtor = cast<CFGAutomaticObjDtor>(Source);
|
|
|
|
return PathDiagnosticLocation::createEnd(Dtor.getTriggerStmt(),
|
|
|
|
SM, CallerCtx);
|
|
|
|
}
|
|
|
|
case CFGElement::BaseDtor:
|
2012-07-26 20:04:13 +00:00
|
|
|
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);
|
|
|
|
}
|
2012-07-26 20:04:05 +00:00
|
|
|
case CFGElement::TemporaryDtor:
|
|
|
|
llvm_unreachable("not yet implemented!");
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Unknown CFGElement kind");
|
2012-02-24 06:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PathDiagnosticCallPiece *
|
|
|
|
PathDiagnosticCallPiece::construct(const ExplodedNode *N,
|
2012-04-20 21:59:08 +00:00
|
|
|
const CallExitEnd &CE,
|
2012-02-24 06:00:00 +00:00
|
|
|
const SourceManager &SM) {
|
2012-04-20 21:59:08 +00:00
|
|
|
const Decl *caller = CE.getLocationContext()->getDecl();
|
2012-07-26 20:04:05 +00:00
|
|
|
PathDiagnosticLocation pos = getLocationForCaller(CE.getCalleeContext(),
|
|
|
|
CE.getLocationContext(),
|
|
|
|
SM);
|
2012-02-24 06:00:00 +00:00
|
|
|
return new PathDiagnosticCallPiece(caller, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
PathDiagnosticCallPiece *
|
2012-03-14 18:58:28 +00:00
|
|
|
PathDiagnosticCallPiece::construct(PathPieces &path,
|
|
|
|
const Decl *caller) {
|
|
|
|
PathDiagnosticCallPiece *C = new PathDiagnosticCallPiece(path, caller);
|
2012-02-24 06:00:00 +00:00
|
|
|
path.clear();
|
|
|
|
path.push_front(C);
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathDiagnosticCallPiece::setCallee(const CallEnter &CE,
|
|
|
|
const SourceManager &SM) {
|
2012-07-26 20:04:05 +00:00
|
|
|
const StackFrameContext *CalleeCtx = CE.getCalleeContext();
|
|
|
|
Callee = CalleeCtx->getDecl();
|
|
|
|
|
|
|
|
callEnterWithin = PathDiagnosticLocation::createBegin(Callee, SM);
|
|
|
|
callEnter = getLocationForCaller(CalleeCtx, CE.getLocationContext(), SM);
|
2012-02-24 06:00:00 +00: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 01:25:01 +00:00
|
|
|
Out << "Calling anonymous block";
|
2012-02-24 06:00:00 +00:00
|
|
|
else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Callee))
|
2012-03-06 01:25:01 +00:00
|
|
|
Out << "Calling '" << *ND << "'";
|
2012-02-24 06:00:00 +00:00
|
|
|
StringRef msg = Out.str();
|
|
|
|
if (msg.empty())
|
|
|
|
return 0;
|
|
|
|
return new PathDiagnosticEventPiece(callEnter, msg);
|
|
|
|
}
|
|
|
|
|
2012-03-06 01:25:01 +00:00
|
|
|
IntrusiveRefCntPtr<PathDiagnosticEventPiece>
|
|
|
|
PathDiagnosticCallPiece::getCallEnterWithinCallerEvent() const {
|
|
|
|
SmallString<256> buf;
|
|
|
|
llvm::raw_svector_ostream Out(buf);
|
2012-03-13 22:15:55 +00:00
|
|
|
if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Caller))
|
|
|
|
Out << "Entered call from '" << *ND << "'";
|
|
|
|
else
|
|
|
|
Out << "Entered call";
|
2012-03-06 01:25:01 +00:00
|
|
|
StringRef msg = Out.str();
|
|
|
|
if (msg.empty())
|
|
|
|
return 0;
|
|
|
|
return new PathDiagnosticEventPiece(callEnterWithin, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
IntrusiveRefCntPtr<PathDiagnosticEventPiece>
|
2012-02-24 06:00:00 +00:00
|
|
|
PathDiagnosticCallPiece::getCallExitEvent() const {
|
2012-03-14 18:58:28 +00:00
|
|
|
if (NoExit)
|
2012-02-24 06:00:00 +00:00
|
|
|
return 0;
|
|
|
|
SmallString<256> buf;
|
|
|
|
llvm::raw_svector_ostream Out(buf);
|
2012-03-16 23:44:28 +00:00
|
|
|
if (!CallStackMessage.empty())
|
|
|
|
Out << CallStackMessage;
|
|
|
|
else if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Callee))
|
2012-03-13 22:15:55 +00:00
|
|
|
Out << "Returning from '" << *ND << "'";
|
2012-02-24 06:00:00 +00:00
|
|
|
else
|
|
|
|
Out << "Returning to caller";
|
|
|
|
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-18 22:33:39 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// FoldingSet profiling methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void PathDiagnosticLocation::Profile(llvm::FoldingSetNodeID &ID) const {
|
2011-09-20 21:25:00 +00:00
|
|
|
ID.AddInteger(Range.getBegin().getRawEncoding());
|
|
|
|
ID.AddInteger(Range.getEnd().getRawEncoding());
|
|
|
|
ID.AddInteger(Loc.getRawEncoding());
|
2009-09-18 22:33:39 +00: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());
|
|
|
|
for (range_iterator I = ranges_begin(), E = ranges_end(); I != E; ++I) {
|
|
|
|
ID.AddInteger(I->getBegin().getRawEncoding());
|
|
|
|
ID.AddInteger(I->getEnd().getRawEncoding());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-24 06:00:00 +00: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-18 22:33:39 +00:00
|
|
|
void PathDiagnosticSpotPiece::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
PathDiagnosticPiece::Profile(ID);
|
|
|
|
ID.Add(Pos);
|
|
|
|
}
|
2009-04-06 22:33:35 +00:00
|
|
|
|
2009-09-18 22:33:39 +00: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 04:32:34 +00:00
|
|
|
for (PathPieces::const_iterator I = subPieces.begin(), E = subPieces.end();
|
|
|
|
I != E; ++I)
|
2009-09-18 22:33:39 +00:00
|
|
|
ID.Add(**I);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathDiagnostic::Profile(llvm::FoldingSetNodeID &ID) const {
|
2012-02-08 04:32:34 +00:00
|
|
|
if (!path.empty())
|
2012-01-25 23:47:14 +00:00
|
|
|
getLocation().Profile(ID);
|
2009-09-18 22:33:39 +00:00
|
|
|
ID.AddString(BugType);
|
|
|
|
ID.AddString(Desc);
|
|
|
|
ID.AddString(Category);
|
2012-01-25 23:47:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PathDiagnostic::FullProfile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
Profile(ID);
|
2012-02-08 04:32:34 +00:00
|
|
|
for (PathPieces::const_iterator I = path.begin(), E = path.end(); I != E; ++I)
|
|
|
|
ID.Add(**I);
|
2009-09-18 22:33:39 +00:00
|
|
|
for (meta_iterator I = meta_begin(), E = meta_end(); I != E; ++I)
|
|
|
|
ID.AddString(*I);
|
|
|
|
}
|
2012-03-16 23:24:20 +00:00
|
|
|
|
|
|
|
StackHintGenerator::~StackHintGenerator() {}
|
|
|
|
|
|
|
|
std::string StackHintGeneratorForSymbol::getMessage(const ExplodedNode *N){
|
|
|
|
ProgramPoint P = N->getLocation();
|
2012-04-20 21:59:08 +00:00
|
|
|
const CallExitEnd *CExit = dyn_cast<CallExitEnd>(&P);
|
|
|
|
assert(CExit && "Stack Hints should be constructed at CallExitEnd points.");
|
2012-03-16 23:24:20 +00:00
|
|
|
|
2012-07-26 20:04:05 +00:00
|
|
|
// FIXME: Use CallEvent to abstract this over all calls.
|
2012-07-10 22:07:52 +00:00
|
|
|
const Stmt *CallSite = CExit->getCalleeContext()->getCallSite();
|
|
|
|
const CallExpr *CE = dyn_cast_or_null<CallExpr>(CallSite);
|
2012-03-16 23:24:20 +00: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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// TODO: This is copied from clang diagnostics. Maybe we could just move it to
|
|
|
|
/// some common place. (Same as HandleOrdinalModifier.)
|
|
|
|
void StackHintGeneratorForSymbol::printOrdinal(unsigned ValNo,
|
|
|
|
llvm::raw_svector_ostream &Out) {
|
|
|
|
assert(ValNo != 0 && "ValNo must be strictly positive!");
|
|
|
|
|
|
|
|
// We could use text forms for the first N ordinals, but the numeric
|
|
|
|
// forms are actually nicer in diagnostics because they stand out.
|
|
|
|
Out << ValNo;
|
|
|
|
|
|
|
|
// It is critically important that we do this perfectly for
|
|
|
|
// user-written sequences with over 100 elements.
|
|
|
|
switch (ValNo % 100) {
|
|
|
|
case 11:
|
|
|
|
case 12:
|
|
|
|
case 13:
|
|
|
|
Out << "th"; return;
|
|
|
|
default:
|
|
|
|
switch (ValNo % 10) {
|
|
|
|
case 1: Out << "st"; return;
|
|
|
|
case 2: Out << "nd"; return;
|
|
|
|
case 3: Out << "rd"; return;
|
|
|
|
default: Out << "th"; return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string StackHintGeneratorForSymbol::getMessageForArg(const Expr *ArgE,
|
|
|
|
unsigned ArgIndex) {
|
|
|
|
SmallString<200> buf;
|
|
|
|
llvm::raw_svector_ostream os(buf);
|
|
|
|
|
|
|
|
os << Msg << " via ";
|
|
|
|
// Printed parameters start at 1, not 0.
|
|
|
|
printOrdinal(++ArgIndex, os);
|
|
|
|
os << " parameter";
|
|
|
|
|
|
|
|
return os.str();
|
|
|
|
}
|