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"
|
2014-05-11 00:31:55 +08:00
|
|
|
#include "clang/AST/ExprCXX.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 {
|
2013-04-30 07:12:59 +08:00
|
|
|
for (PathPieces::const_iterator I = subPieces.begin(), E = subPieces.end();
|
2012-02-08 12:32:34 +08:00
|
|
|
I!=E; ++I) {
|
2009-03-10 13:16:17 +08:00
|
|
|
if (isa<PathDiagnosticEventPiece>(*I))
|
|
|
|
return true;
|
2013-04-30 07:12:59 +08:00
|
|
|
if (PathDiagnosticMacroPiece *MP = dyn_cast<PathDiagnosticMacroPiece>(*I))
|
2009-03-10 13:16:17 +08:00
|
|
|
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)
|
2013-05-17 06:30:45 +08:00
|
|
|
: str(StripTrailingDots(s)), kind(k), Hint(hint),
|
|
|
|
LastInMainSourceFile(false) {}
|
2009-03-06 15:53:30 +08:00
|
|
|
|
2009-03-27 05:21:35 +08:00
|
|
|
PathDiagnosticPiece::PathDiagnosticPiece(Kind k, DisplayHint hint)
|
2013-05-17 06:30:45 +08:00
|
|
|
: kind(k), Hint(hint), LastInMainSourceFile(false) {}
|
2009-03-13 02:41:53 +08:00
|
|
|
|
2015-10-20 21:23:58 +08:00
|
|
|
PathDiagnosticPiece::~PathDiagnosticPiece() {}
|
|
|
|
PathDiagnosticEventPiece::~PathDiagnosticEventPiece() {}
|
|
|
|
PathDiagnosticCallPiece::~PathDiagnosticCallPiece() {}
|
|
|
|
PathDiagnosticControlFlowPiece::~PathDiagnosticControlFlowPiece() {}
|
|
|
|
PathDiagnosticMacroPiece::~PathDiagnosticMacroPiece() {}
|
2012-04-05 02:11:35 +08:00
|
|
|
|
2012-08-04 07:08:54 +08:00
|
|
|
void PathPieces::flattenTo(PathPieces &Primary, PathPieces &Current,
|
|
|
|
bool ShouldFlattenMacros) const {
|
2013-04-30 07:12:59 +08:00
|
|
|
for (PathPieces::const_iterator I = begin(), E = end(); I != E; ++I) {
|
2014-07-05 11:08:06 +08:00
|
|
|
PathDiagnosticPiece *Piece = I->get();
|
2012-08-04 07:08:54 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 21:23:58 +08:00
|
|
|
PathDiagnostic::~PathDiagnostic() {}
|
2012-02-08 12:32:27 +08:00
|
|
|
|
2014-02-12 05:49:21 +08:00
|
|
|
PathDiagnostic::PathDiagnostic(StringRef CheckName, const Decl *declWithIssue,
|
2012-08-31 08:36:26 +08:00
|
|
|
StringRef bugtype, StringRef verboseDesc,
|
2013-01-08 08:25:29 +08:00
|
|
|
StringRef shortDesc, StringRef category,
|
|
|
|
PathDiagnosticLocation LocationToUnique,
|
|
|
|
const Decl *DeclToUnique)
|
2014-02-12 05:49:21 +08:00
|
|
|
: CheckName(CheckName),
|
|
|
|
DeclWithIssue(declWithIssue),
|
2012-04-05 02:11:35 +08:00
|
|
|
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)),
|
2013-01-08 08:25:29 +08:00
|
|
|
UniqueingLoc(LocationToUnique),
|
|
|
|
UniqueingDecl(DeclToUnique),
|
2012-02-24 14:00:00 +08:00
|
|
|
path(pathImpl) {}
|
2009-03-06 15:08:50 +08:00
|
|
|
|
2013-05-17 06:30:45 +08:00
|
|
|
static PathDiagnosticCallPiece *
|
|
|
|
getFirstStackedCallToHeaderFile(PathDiagnosticCallPiece *CP,
|
|
|
|
const SourceManager &SMgr) {
|
2013-05-22 09:54:34 +08:00
|
|
|
SourceLocation CallLoc = CP->callEnter.asLocation();
|
|
|
|
|
|
|
|
// If the call is within a macro, don't do anything (for now).
|
|
|
|
if (CallLoc.isMacroID())
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2013-05-22 09:54:34 +08:00
|
|
|
|
2013-08-22 08:27:10 +08:00
|
|
|
assert(SMgr.isInMainFile(CallLoc) &&
|
2013-05-17 06:30:45 +08:00
|
|
|
"The call piece should be in the main file.");
|
|
|
|
|
|
|
|
// Check if CP represents a path through a function outside of the main file.
|
2013-08-22 08:27:10 +08:00
|
|
|
if (!SMgr.isInMainFile(CP->callEnterWithin.asLocation()))
|
2013-05-17 06:30:45 +08:00
|
|
|
return CP;
|
|
|
|
|
2013-05-18 04:51:16 +08:00
|
|
|
const PathPieces &Path = CP->path;
|
|
|
|
if (Path.empty())
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2013-05-18 04:51:16 +08:00
|
|
|
|
2013-05-17 06:30:45 +08:00
|
|
|
// Check if the last piece in the callee path is a call to a function outside
|
|
|
|
// of the main file.
|
|
|
|
if (PathDiagnosticCallPiece *CPInner =
|
|
|
|
dyn_cast<PathDiagnosticCallPiece>(Path.back())) {
|
|
|
|
return getFirstStackedCallToHeaderFile(CPInner, SMgr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, the last piece is in the main file.
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2013-05-17 06:30:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PathDiagnostic::resetDiagnosticLocationToMainFile() {
|
|
|
|
if (path.empty())
|
|
|
|
return;
|
|
|
|
|
2014-07-05 11:08:06 +08:00
|
|
|
PathDiagnosticPiece *LastP = path.back().get();
|
2013-05-17 06:30:45 +08:00
|
|
|
assert(LastP);
|
2013-05-18 04:51:16 +08:00
|
|
|
const SourceManager &SMgr = LastP->getLocation().getManager();
|
2013-05-17 06:30:45 +08:00
|
|
|
|
|
|
|
// We only need to check if the report ends inside headers, if the last piece
|
|
|
|
// is a call piece.
|
|
|
|
if (PathDiagnosticCallPiece *CP = dyn_cast<PathDiagnosticCallPiece>(LastP)) {
|
|
|
|
CP = getFirstStackedCallToHeaderFile(CP, SMgr);
|
|
|
|
if (CP) {
|
|
|
|
// Mark the piece.
|
|
|
|
CP->setAsLastInMainSourceFile();
|
|
|
|
|
|
|
|
// Update the path diagnostic message.
|
|
|
|
const NamedDecl *ND = dyn_cast<NamedDecl>(CP->getCallee());
|
|
|
|
if (ND) {
|
|
|
|
SmallString<200> buf;
|
|
|
|
llvm::raw_svector_ostream os(buf);
|
2013-05-18 04:51:16 +08:00
|
|
|
os << " (within a call to '" << ND->getDeclName() << "')";
|
2013-05-17 06:30:45 +08:00
|
|
|
appendToDesc(os.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the report containing declaration and location.
|
|
|
|
DeclWithIssue = CP->getCaller();
|
|
|
|
Loc = CP->getLocation();
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2013-05-17 06:30:45 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-30 04:06:10 +08:00
|
|
|
void PathDiagnosticConsumer::HandlePathDiagnostic(
|
|
|
|
std::unique_ptr<PathDiagnostic> D) {
|
2012-02-29 07:27:39 +08:00
|
|
|
if (!D || D->path.empty())
|
2012-01-26 07:47:14 +08:00
|
|
|
return;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-01-26 07:47:14 +08:00
|
|
|
// 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;
|
2014-08-30 04:06:10 +08:00
|
|
|
const SourceManager &SMgr = D->path.front()->getLocation().getManager();
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVector<const PathPieces *, 5> WorkList;
|
2012-03-01 07:59:20 +08:00
|
|
|
WorkList.push_back(&D->path);
|
2012-02-29 07:27:39 +08:00
|
|
|
|
2012-03-01 07:59:20 +08:00
|
|
|
while (!WorkList.empty()) {
|
2013-08-24 00:11:15 +08:00
|
|
|
const PathPieces &path = *WorkList.pop_back_val();
|
2012-03-01 07:59:20 +08:00
|
|
|
|
2013-08-24 00:11:15 +08:00
|
|
|
for (PathPieces::const_iterator I = path.begin(), E = path.end(); I != E;
|
|
|
|
++I) {
|
2014-07-05 11:08:06 +08:00
|
|
|
const PathDiagnosticPiece *piece = I->get();
|
2012-03-01 07:59:20 +08:00
|
|
|
FullSourceLoc L = piece->getLocation().asLocation().getExpansionLoc();
|
2015-09-08 11:50:52 +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?
|
2015-09-08 11:50:52 +08:00
|
|
|
|
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?
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-03-01 07:59:20 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-02-29 07:27:39 +08:00
|
|
|
if (FID.isInvalid())
|
|
|
|
return; // FIXME: Emit a warning?
|
2015-09-08 11:50:52 +08:00
|
|
|
}
|
2012-02-29 07:27:39 +08:00
|
|
|
|
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);
|
2014-05-27 10:45:47 +08:00
|
|
|
void *InsertPos = nullptr;
|
2012-01-26 07:47:14 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2014-08-30 04:06:10 +08:00
|
|
|
assert(orig != D.get());
|
2012-01-26 07:47:14 +08:00
|
|
|
Diags.RemoveNode(orig);
|
|
|
|
delete orig;
|
|
|
|
}
|
2014-03-08 03:33:25 +08:00
|
|
|
|
2014-08-30 04:06:10 +08:00
|
|
|
Diags.InsertNode(D.release());
|
2012-01-26 07:47:14 +08:00
|
|
|
}
|
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
static Optional<bool> comparePath(const PathPieces &X, const PathPieces &Y);
|
2016-02-11 03:11:58 +08:00
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
static Optional<bool>
|
2012-09-10 22:50:55 +08:00
|
|
|
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);
|
2013-02-21 09:47:18 +08:00
|
|
|
return None;
|
2012-09-10 22:50:55 +08:00
|
|
|
}
|
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
static Optional<bool> compareMacro(const PathDiagnosticMacroPiece &X,
|
|
|
|
const PathDiagnosticMacroPiece &Y) {
|
2012-09-10 22:50:55 +08:00
|
|
|
return comparePath(X.subPieces, Y.subPieces);
|
|
|
|
}
|
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
static Optional<bool> compareCall(const PathDiagnosticCallPiece &X,
|
|
|
|
const PathDiagnosticCallPiece &Y) {
|
2012-09-10 22:50:55 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
static Optional<bool> comparePiece(const PathDiagnosticPiece &X,
|
|
|
|
const PathDiagnosticPiece &Y) {
|
2012-09-10 22:50:55 +08:00
|
|
|
if (X.getKind() != Y.getKind())
|
|
|
|
return X.getKind() < Y.getKind();
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-09-10 22:50:55 +08:00
|
|
|
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();
|
2015-09-08 11:50:52 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
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:
|
2013-02-21 09:47:18 +08:00
|
|
|
return None;
|
2012-09-10 22:50:55 +08:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
static Optional<bool> comparePath(const PathPieces &X, const PathPieces &Y) {
|
2012-09-10 22:50:55 +08:00
|
|
|
if (X.size() != Y.size())
|
|
|
|
return X.size() < Y.size();
|
2013-04-30 06:38:22 +08:00
|
|
|
|
2013-04-30 07:12:59 +08:00
|
|
|
PathPieces::const_iterator X_I = X.begin(), X_end = X.end();
|
|
|
|
PathPieces::const_iterator Y_I = Y.begin(), Y_end = Y.end();
|
2013-04-30 06:38:22 +08:00
|
|
|
|
|
|
|
for ( ; X_I != X_end && Y_I != Y_end; ++X_I, ++Y_I) {
|
2013-04-30 07:12:59 +08:00
|
|
|
Optional<bool> b = comparePiece(**X_I, **Y_I);
|
2012-09-10 22:50:55 +08:00
|
|
|
if (b.hasValue())
|
|
|
|
return b.getValue();
|
|
|
|
}
|
2013-04-30 06:38:22 +08:00
|
|
|
|
2013-02-21 09:47:18 +08:00
|
|
|
return None;
|
2012-09-10 22:50:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<bool> b = comparePath(X.path, Y.path);
|
2012-09-10 22:50:55 +08:00
|
|
|
assert(b.hasValue());
|
|
|
|
return b.getValue();
|
|
|
|
}
|
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;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-01-26 07:47:14 +08:00
|
|
|
flushed = true;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-01-26 07:47:14 +08:00
|
|
|
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.
|
2015-03-14 20:39:22 +08:00
|
|
|
int (*Comp)(const PathDiagnostic *const *, const PathDiagnostic *const *) =
|
|
|
|
[](const PathDiagnostic *const *X, const PathDiagnostic *const *Y) {
|
|
|
|
assert(*X != *Y && "PathDiagnostics not uniqued!");
|
|
|
|
if (compare(**X, **Y))
|
|
|
|
return -1;
|
|
|
|
assert(compare(**Y, **X) && "Not a total order!");
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
array_pod_sort(BatchDiags.begin(), BatchDiags.end(), Comp);
|
2014-03-01 22:48:57 +08:00
|
|
|
|
2012-01-26 07:47:14 +08:00
|
|
|
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;
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-09-08 12:26:37 +08:00
|
|
|
// Clear out the FoldingSet.
|
|
|
|
Diags.clear();
|
2011-08-28 05:39:14 +08:00
|
|
|
}
|
|
|
|
|
2014-05-03 18:39:05 +08:00
|
|
|
PathDiagnosticConsumer::FilesMade::~FilesMade() {
|
2015-03-23 02:16:22 +08:00
|
|
|
for (PDFileEntry &Entry : Set)
|
2014-05-03 18:39:05 +08:00
|
|
|
Entry.~PDFileEntry();
|
|
|
|
}
|
|
|
|
|
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;
|
2015-03-23 02:16:22 +08:00
|
|
|
PDFileEntry *Entry = Set.FindNodeOrInsertPos(NodeID, InsertPos);
|
2012-08-25 03:35:19 +08:00
|
|
|
if (!Entry) {
|
|
|
|
Entry = Alloc.Allocate<PDFileEntry>();
|
|
|
|
Entry = new (Entry) PDFileEntry(NodeID);
|
2015-03-23 02:16:22 +08:00
|
|
|
Set.InsertNode(Entry, InsertPos);
|
2012-08-25 03:35:19 +08:00
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-08-25 03:35:19 +08:00
|
|
|
// 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;
|
2015-03-23 02:16:22 +08:00
|
|
|
PDFileEntry *Entry = Set.FindNodeOrInsertPos(NodeID, InsertPos);
|
2012-08-25 03:35:19 +08:00
|
|
|
if (!Entry)
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2012-08-25 03:35:19 +08:00
|
|
|
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()) {
|
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::Statement:
|
2013-02-22 04:58:29 +08:00
|
|
|
return PathDiagnosticLocation(Source.castAs<CFGStmt>().getStmt(),
|
2012-08-04 07:08:44 +08:00
|
|
|
SM, CallerCtx);
|
|
|
|
case CFGElement::Initializer: {
|
2013-02-22 04:58:29 +08:00
|
|
|
const CFGInitializer &Init = Source.castAs<CFGInitializer>();
|
2012-08-04 07:08:44 +08:00
|
|
|
return PathDiagnosticLocation(Init.getInitializer()->getInit(),
|
|
|
|
SM, CallerCtx);
|
|
|
|
}
|
|
|
|
case CFGElement::AutomaticObjectDtor: {
|
2013-02-22 04:58:29 +08:00
|
|
|
const CFGAutomaticObjDtor &Dtor = Source.castAs<CFGAutomaticObjDtor>();
|
2012-08-04 07:08:44 +08:00
|
|
|
return PathDiagnosticLocation::createEnd(Dtor.getTriggerStmt(),
|
|
|
|
SM, CallerCtx);
|
|
|
|
}
|
2013-09-04 01:00:57 +08:00
|
|
|
case CFGElement::DeleteDtor: {
|
2013-11-01 02:41:15 +08:00
|
|
|
const CFGDeleteDtor &Dtor = Source.castAs<CFGDeleteDtor>();
|
|
|
|
return PathDiagnosticLocation(Dtor.getDeleteExpr(), SM, CallerCtx);
|
2013-09-04 01:00:57 +08:00
|
|
|
}
|
2012-08-04 07:08:44 +08:00
|
|
|
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:
|
2014-01-14 01:59:19 +08:00
|
|
|
case CFGElement::NewAllocator:
|
2012-08-04 07:08:44 +08:00
|
|
|
llvm_unreachable("not yet implemented!");
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Unknown CFGElement kind");
|
|
|
|
}
|
|
|
|
|
2011-09-17 03:18:30 +08:00
|
|
|
PathDiagnosticLocation
|
2016-02-11 03:11:58 +08:00
|
|
|
PathDiagnosticLocation::createBegin(const Decl *D,
|
|
|
|
const SourceManager &SM) {
|
2011-09-21 05:38:35 +08:00
|
|
|
return PathDiagnosticLocation(D->getLocStart(), SM, SingleLocK);
|
|
|
|
}
|
|
|
|
|
|
|
|
PathDiagnosticLocation
|
2016-02-11 03:11:58 +08:00
|
|
|
PathDiagnosticLocation::createBegin(const Stmt *S,
|
|
|
|
const SourceManager &SM,
|
|
|
|
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
|
2016-02-11 03:11:58 +08:00
|
|
|
PathDiagnosticLocation::createOperatorLoc(const BinaryOperator *BO,
|
|
|
|
const SourceManager &SM) {
|
2011-09-17 03:18:30 +08:00
|
|
|
return PathDiagnosticLocation(BO->getOperatorLoc(), SM, SingleLocK);
|
|
|
|
}
|
|
|
|
|
2013-12-11 02:18:06 +08:00
|
|
|
PathDiagnosticLocation
|
2016-02-11 03:11:58 +08:00
|
|
|
PathDiagnosticLocation::createConditionalColonLoc(
|
2013-12-11 02:18:06 +08:00
|
|
|
const ConditionalOperator *CO,
|
|
|
|
const SourceManager &SM) {
|
|
|
|
return PathDiagnosticLocation(CO->getColonLoc(), SM, SingleLocK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-21 05:38:35 +08:00
|
|
|
PathDiagnosticLocation
|
2016-02-11 03:11:58 +08:00
|
|
|
PathDiagnosticLocation::createMemberLoc(const MemberExpr *ME,
|
|
|
|
const SourceManager &SM) {
|
2011-09-21 05:38:35 +08:00
|
|
|
return PathDiagnosticLocation(ME->getMemberLoc(), SM, SingleLocK);
|
|
|
|
}
|
|
|
|
|
2011-09-17 03:18:30 +08:00
|
|
|
PathDiagnosticLocation
|
2016-02-11 03:11:58 +08:00
|
|
|
PathDiagnosticLocation::createBeginBrace(const CompoundStmt *CS,
|
|
|
|
const SourceManager &SM) {
|
2011-09-17 03:18:30 +08:00
|
|
|
SourceLocation L = CS->getLBracLoc();
|
|
|
|
return PathDiagnosticLocation(L, SM, SingleLocK);
|
|
|
|
}
|
|
|
|
|
|
|
|
PathDiagnosticLocation
|
2016-02-11 03:11:58 +08:00
|
|
|
PathDiagnosticLocation::createEndBrace(const CompoundStmt *CS,
|
|
|
|
const SourceManager &SM) {
|
2011-09-17 03:18:30 +08:00
|
|
|
SourceLocation L = CS->getRBracLoc();
|
|
|
|
return PathDiagnosticLocation(L, SM, SingleLocK);
|
|
|
|
}
|
|
|
|
|
|
|
|
PathDiagnosticLocation
|
2016-02-11 03:11:58 +08:00
|
|
|
PathDiagnosticLocation::createDeclBegin(const LocationContext *LC,
|
|
|
|
const SourceManager &SM) {
|
2011-09-17 03:18:30 +08:00
|
|
|
// 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
|
2016-02-11 03:11:58 +08:00
|
|
|
PathDiagnosticLocation::createDeclEnd(const LocationContext *LC,
|
|
|
|
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
|
2016-02-11 03:11:58 +08:00
|
|
|
PathDiagnosticLocation::create(const ProgramPoint& P,
|
|
|
|
const SourceManager &SMng) {
|
2014-05-27 10:45:47 +08:00
|
|
|
const Stmt* S = nullptr;
|
2013-02-22 06:23:56 +08:00
|
|
|
if (Optional<BlockEdge> BE = P.getAs<BlockEdge>()) {
|
2011-09-16 02:56:07 +08:00
|
|
|
const CFGBlock *BSrc = BE->getSrc();
|
|
|
|
S = BSrc->getTerminatorCondition();
|
2013-02-22 06:23:56 +08:00
|
|
|
} else if (Optional<StmtPoint> SP = P.getAs<StmtPoint>()) {
|
2012-10-30 01:31:59 +08:00
|
|
|
S = SP->getStmt();
|
2013-02-22 06:23:56 +08:00
|
|
|
if (P.getAs<PostStmtPurgeDeadSymbols>())
|
2012-11-16 03:11:43 +08:00
|
|
|
return PathDiagnosticLocation::createEnd(S, SMng, P.getLocationContext());
|
2013-04-05 08:59:33 +08:00
|
|
|
} else if (Optional<PostInitializer> PIP = P.getAs<PostInitializer>()) {
|
|
|
|
return PathDiagnosticLocation(PIP->getInitializer()->getSourceLocation(),
|
|
|
|
SMng);
|
2013-02-22 06:23:56 +08:00
|
|
|
} else if (Optional<PostImplicitCall> PIE = P.getAs<PostImplicitCall>()) {
|
2012-08-04 07:08:44 +08:00
|
|
|
return PathDiagnosticLocation(PIE->getLocation(), SMng);
|
2013-02-22 06:23:56 +08:00
|
|
|
} else if (Optional<CallEnter> CE = P.getAs<CallEnter>()) {
|
2012-08-04 07:08:44 +08:00
|
|
|
return getLocationForCaller(CE->getCalleeContext(),
|
|
|
|
CE->getLocationContext(),
|
|
|
|
SMng);
|
2013-02-22 06:23:56 +08:00
|
|
|
} else if (Optional<CallExitEnd> CEE = P.getAs<CallExitEnd>()) {
|
2012-08-04 07:08:44 +08:00
|
|
|
return getLocationForCaller(CEE->getCalleeContext(),
|
|
|
|
CEE->getLocationContext(),
|
|
|
|
SMng);
|
2013-02-22 06:23:56 +08:00
|
|
|
} else {
|
2012-10-30 01:31:59 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-04-24 07:57:43 +08:00
|
|
|
const Stmt *PathDiagnosticLocation::getStmt(const ExplodedNode *N) {
|
|
|
|
ProgramPoint P = N->getLocation();
|
|
|
|
if (Optional<StmtPoint> SP = P.getAs<StmtPoint>())
|
|
|
|
return SP->getStmt();
|
|
|
|
if (Optional<BlockEdge> BE = P.getAs<BlockEdge>())
|
|
|
|
return BE->getSrc()->getTerminator();
|
|
|
|
if (Optional<CallEnter> CE = P.getAs<CallEnter>())
|
|
|
|
return CE->getCallExpr();
|
|
|
|
if (Optional<CallExitEnd> CEE = P.getAs<CallExitEnd>())
|
|
|
|
return CEE->getCalleeContext()->getCallSite();
|
|
|
|
if (Optional<PostInitializer> PIPP = P.getAs<PostInitializer>())
|
|
|
|
return PIPP->getInitializer()->getInit();
|
|
|
|
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2013-04-24 07:57:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Stmt *PathDiagnosticLocation::getNextStmt(const ExplodedNode *N) {
|
|
|
|
for (N = N->getFirstSucc(); N; N = N->getFirstSucc()) {
|
|
|
|
if (const Stmt *S = getStmt(N)) {
|
|
|
|
// Check if the statement is '?' or '&&'/'||'. These are "merges",
|
|
|
|
// not actual statement points.
|
|
|
|
switch (S->getStmtClass()) {
|
|
|
|
case Stmt::ChooseExprClass:
|
|
|
|
case Stmt::BinaryConditionalOperatorClass:
|
|
|
|
case Stmt::ConditionalOperatorClass:
|
|
|
|
continue;
|
|
|
|
case Stmt::BinaryOperatorClass: {
|
|
|
|
BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
|
|
|
|
if (Op == BO_LAnd || Op == BO_LOr)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// We found the statement, so return it.
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2013-04-24 07:57:43 +08:00
|
|
|
}
|
|
|
|
|
2011-09-17 03:18:30 +08:00
|
|
|
PathDiagnosticLocation
|
2013-04-24 07:57:43 +08:00
|
|
|
PathDiagnosticLocation::createEndOfPath(const ExplodedNode *N,
|
2011-09-17 03:18:30 +08:00
|
|
|
const SourceManager &SM) {
|
2011-09-14 08:25:17 +08:00
|
|
|
assert(N && "Cannot create a location with a null node.");
|
2013-04-24 07:57:43 +08:00
|
|
|
const Stmt *S = getStmt(N);
|
2011-09-14 08:25:17 +08:00
|
|
|
|
2014-01-09 02:46:55 +08:00
|
|
|
if (!S) {
|
|
|
|
// If this is an implicit call, return the implicit call point location.
|
|
|
|
if (Optional<PreImplicitCall> PIE = N->getLocationAs<PreImplicitCall>())
|
|
|
|
return PathDiagnosticLocation(PIE->getLocation(), SM);
|
2013-04-24 07:57:43 +08:00
|
|
|
S = getNextStmt(N);
|
2014-01-09 02:46:55 +08:00
|
|
|
}
|
2011-09-14 08:25:17 +08:00
|
|
|
|
2012-10-06 09:19:30 +08:00
|
|
|
if (S) {
|
2013-04-24 07:57:43 +08:00
|
|
|
ProgramPoint P = N->getLocation();
|
|
|
|
const LocationContext *LC = N->getLocationContext();
|
|
|
|
|
|
|
|
// For member expressions, return the location of the '.' or '->'.
|
|
|
|
if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
|
|
|
|
return PathDiagnosticLocation::createMemberLoc(ME, SM);
|
|
|
|
|
|
|
|
// For binary operators, return the location of the operator.
|
|
|
|
if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
|
|
|
|
return PathDiagnosticLocation::createOperatorLoc(B, SM);
|
|
|
|
|
|
|
|
if (P.getAs<PostStmtPurgeDeadSymbols>())
|
|
|
|
return PathDiagnosticLocation::createEnd(S, SM, LC);
|
|
|
|
|
2012-10-06 09:19:30 +08:00
|
|
|
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;
|
2015-09-08 11:50:52 +08:00
|
|
|
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;
|
2014-05-27 10:45:47 +08:00
|
|
|
S = nullptr;
|
|
|
|
D = nullptr;
|
2009-04-07 06:33:35 +08:00
|
|
|
}
|
|
|
|
else if (K == DeclK) {
|
|
|
|
K = SingleLocK;
|
2014-05-27 10:45:47 +08:00
|
|
|
S = nullptr;
|
|
|
|
D = nullptr;
|
2009-04-07 06:33:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-01-19 02:27:21 +08:00
|
|
|
static inline void describeClass(raw_ostream &Out, const CXXRecordDecl *D,
|
|
|
|
StringRef Prefix = StringRef()) {
|
|
|
|
if (!D->getIdentifier())
|
|
|
|
return;
|
|
|
|
Out << Prefix << '\'' << *D << '\'';
|
|
|
|
}
|
|
|
|
|
2013-01-19 02:27:14 +08:00
|
|
|
static bool describeCodeDecl(raw_ostream &Out, const Decl *D,
|
2013-01-19 02:27:21 +08:00
|
|
|
bool ExtendedDescription,
|
2013-01-19 02:27:14 +08:00
|
|
|
StringRef Prefix = StringRef()) {
|
|
|
|
if (!D)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (isa<BlockDecl>(D)) {
|
2013-01-19 02:27:21 +08:00
|
|
|
if (ExtendedDescription)
|
2013-01-19 02:27:14 +08:00
|
|
|
Out << Prefix << "anonymous block";
|
2013-01-19 02:27:21 +08:00
|
|
|
return ExtendedDescription;
|
2013-01-19 02:27:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
|
2013-01-19 02:27:21 +08:00
|
|
|
Out << Prefix;
|
|
|
|
if (ExtendedDescription && !MD->isUserProvided()) {
|
|
|
|
if (MD->isExplicitlyDefaulted())
|
|
|
|
Out << "defaulted ";
|
|
|
|
else
|
|
|
|
Out << "implicit ";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(MD)) {
|
|
|
|
if (CD->isDefaultConstructor())
|
|
|
|
Out << "default ";
|
|
|
|
else if (CD->isCopyConstructor())
|
|
|
|
Out << "copy ";
|
|
|
|
else if (CD->isMoveConstructor())
|
|
|
|
Out << "move ";
|
|
|
|
|
|
|
|
Out << "constructor";
|
|
|
|
describeClass(Out, MD->getParent(), " for ");
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2013-01-19 02:27:21 +08:00
|
|
|
} else if (isa<CXXDestructorDecl>(MD)) {
|
|
|
|
if (!MD->isUserProvided()) {
|
|
|
|
Out << "destructor";
|
|
|
|
describeClass(Out, MD->getParent(), " for ");
|
|
|
|
} else {
|
|
|
|
// Use ~Foo for explicitly-written destructors.
|
|
|
|
Out << "'" << *MD << "'";
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (MD->isCopyAssignmentOperator()) {
|
|
|
|
Out << "copy assignment operator";
|
|
|
|
describeClass(Out, MD->getParent(), " for ");
|
|
|
|
|
|
|
|
} else if (MD->isMoveAssignmentOperator()) {
|
|
|
|
Out << "move assignment operator";
|
|
|
|
describeClass(Out, MD->getParent(), " for ");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (MD->getParent()->getIdentifier())
|
|
|
|
Out << "'" << *MD->getParent() << "::" << *MD << "'";
|
|
|
|
else
|
|
|
|
Out << "'" << *MD << "'";
|
|
|
|
}
|
2013-01-19 02:27:14 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << Prefix << '\'' << cast<NamedDecl>(*D) << '\'';
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-24 14:00:00 +08:00
|
|
|
IntrusiveRefCntPtr<PathDiagnosticEventPiece>
|
|
|
|
PathDiagnosticCallPiece::getCallEnterEvent() const {
|
|
|
|
if (!Callee)
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2013-01-19 02:27:14 +08:00
|
|
|
|
2012-02-24 14:00:00 +08:00
|
|
|
SmallString<256> buf;
|
|
|
|
llvm::raw_svector_ostream Out(buf);
|
2013-01-19 02:27:14 +08:00
|
|
|
|
|
|
|
Out << "Calling ";
|
2013-01-19 02:27:21 +08:00
|
|
|
describeCodeDecl(Out, Callee, /*ExtendedDescription=*/true);
|
2013-01-19 02:27:14 +08:00
|
|
|
|
2012-11-15 10:07:23 +08:00
|
|
|
assert(callEnter.asLocation().isValid());
|
2013-01-19 02:27:14 +08:00
|
|
|
return new PathDiagnosticEventPiece(callEnter, Out.str());
|
2012-02-24 14:00:00 +08:00
|
|
|
}
|
|
|
|
|
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())
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2013-05-25 05:43:11 +08:00
|
|
|
if (Callee->isImplicit() || !Callee->hasBody())
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2013-01-20 03:52:57 +08:00
|
|
|
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Callee))
|
|
|
|
if (MD->isDefaulted())
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2013-01-19 02:27:14 +08:00
|
|
|
|
2012-03-06 09:25:01 +08:00
|
|
|
SmallString<256> buf;
|
|
|
|
llvm::raw_svector_ostream Out(buf);
|
2013-01-19 02:27:14 +08:00
|
|
|
|
|
|
|
Out << "Entered call";
|
2013-01-19 02:27:21 +08:00
|
|
|
describeCodeDecl(Out, Caller, /*ExtendedDescription=*/false, " from ");
|
2013-01-19 02:27:14 +08:00
|
|
|
|
|
|
|
return new PathDiagnosticEventPiece(callEnterWithin, Out.str());
|
2012-03-06 09:25:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
IntrusiveRefCntPtr<PathDiagnosticEventPiece>
|
2012-02-24 14:00:00 +08:00
|
|
|
PathDiagnosticCallPiece::getCallExitEvent() const {
|
2012-03-15 02:58:28 +08:00
|
|
|
if (NoExit)
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2013-01-19 02:27:14 +08:00
|
|
|
|
2012-02-24 14:00:00 +08:00
|
|
|
SmallString<256> buf;
|
|
|
|
llvm::raw_svector_ostream Out(buf);
|
2013-01-19 02:27:14 +08:00
|
|
|
|
|
|
|
if (!CallStackMessage.empty()) {
|
2012-03-17 07:44:28 +08:00
|
|
|
Out << CallStackMessage;
|
2013-01-19 02:27:14 +08:00
|
|
|
} else {
|
2013-01-19 02:27:21 +08:00
|
|
|
bool DidDescribe = describeCodeDecl(Out, Callee,
|
|
|
|
/*ExtendedDescription=*/false,
|
2013-01-19 02:27:14 +08:00
|
|
|
"Returning from ");
|
|
|
|
if (!DidDescribe)
|
|
|
|
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) {
|
2013-04-30 07:12:59 +08:00
|
|
|
for (PathPieces::const_iterator it = pieces.begin(),
|
2012-02-24 14:00:00 +08:00
|
|
|
et = pieces.end(); it != et; ++it) {
|
2014-07-05 11:08:06 +08:00
|
|
|
const PathDiagnosticPiece *piece = it->get();
|
2015-09-08 11:50:52 +08:00
|
|
|
if (const PathDiagnosticCallPiece *cp =
|
2012-02-24 14:00:00 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
2015-09-08 11:50:52 +08:00
|
|
|
}
|
2009-09-19 06:33:39 +08:00
|
|
|
}
|
|
|
|
|
2012-02-24 14:00:00 +08:00
|
|
|
void PathDiagnosticCallPiece::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
PathDiagnosticPiece::Profile(ID);
|
2015-09-08 11:50:52 +08:00
|
|
|
for (PathPieces::const_iterator it = path.begin(),
|
2012-02-24 14:00:00 +08:00
|
|
|
et = path.end(); it != et; ++it) {
|
2013-04-30 07:12:59 +08:00
|
|
|
ID.Add(**it);
|
2012-02-24 14:00:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2013-04-30 07:12:59 +08:00
|
|
|
for (PathPieces::const_iterator I = subPieces.begin(), E = subPieces.end();
|
2012-02-08 12:32:34 +08:00
|
|
|
I != E; ++I)
|
2013-04-30 07:12:59 +08:00
|
|
|
ID.Add(**I);
|
2009-09-19 06:33:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2013-04-30 07:12:59 +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
|
|
|
|
2015-10-20 21:23:58 +08:00
|
|
|
StackHintGenerator::~StackHintGenerator() {}
|
2012-03-17 07:24:20 +08:00
|
|
|
|
|
|
|
std::string StackHintGeneratorForSymbol::getMessage(const ExplodedNode *N){
|
|
|
|
ProgramPoint P = N->getLocation();
|
2013-02-22 07:35:06 +08:00
|
|
|
CallExitEnd CExit = P.castAs<CallExitEnd>();
|
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.
|
2013-02-22 07:35:06 +08:00
|
|
|
const Stmt *CallSite = CExit.getCalleeContext()->getCallSite();
|
2012-07-11 06:07:52 +08:00
|
|
|
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.
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<loc::MemRegionVal> Reg = SV.getAs<loc::MemRegionVal>()) {
|
2012-03-17 07:24:20 +08:00
|
|
|
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();
|
|
|
|
}
|