2018-03-09 06:45:13 +08:00
|
|
|
//===- PathDiagnostic.cpp - Path-Specific Diagnostic Handling -------------===//
|
2008-03-27 11:49:32 +08:00
|
|
|
//
|
|
|
|
// 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"
|
2018-03-09 06:45:13 +08:00
|
|
|
#include "clang/AST/DeclBase.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"
|
2018-05-25 21:18:38 +08:00
|
|
|
#include "clang/AST/DeclTemplate.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"
|
2018-03-09 06:45:13 +08:00
|
|
|
#include "clang/AST/OperationKinds.h"
|
2011-09-15 01:48:01 +08:00
|
|
|
#include "clang/AST/ParentMap.h"
|
2018-03-09 06:45:13 +08:00
|
|
|
#include "clang/AST/Stmt.h"
|
|
|
|
#include "clang/AST/Type.h"
|
|
|
|
#include "clang/Analysis/AnalysisDeclContext.h"
|
|
|
|
#include "clang/Analysis/CFG.h"
|
|
|
|
#include "clang/Analysis/ProgramPoint.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/LLVM.h"
|
|
|
|
#include "clang/Basic/SourceLocation.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2018-04-26 05:51:26 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
|
2018-03-09 06:45:13 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
|
|
|
#include "llvm/ADT/None.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2008-11-19 14:51:40 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2018-03-09 06:45:13 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2012-09-22 09:24:42 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2018-03-09 06:45:13 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-02 01:12:56 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-03-09 06:45:13 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
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 {
|
2018-03-09 06:45:13 +08:00
|
|
|
for (const auto &P : subPieces) {
|
2017-01-06 01:26:53 +08:00
|
|
|
if (isa<PathDiagnosticEventPiece>(*P))
|
2009-03-10 13:16:17 +08:00
|
|
|
return true;
|
2018-03-09 06:45:13 +08:00
|
|
|
if (const auto *MP = dyn_cast<PathDiagnosticMacroPiece>(P.get()))
|
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);
|
2018-03-09 06:45:13 +08:00
|
|
|
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)
|
2018-03-09 06:45:13 +08:00
|
|
|
: str(StripTrailingDots(s)), kind(k), Hint(hint) {}
|
2009-03-06 15:53:30 +08:00
|
|
|
|
2009-03-27 05:21:35 +08:00
|
|
|
PathDiagnosticPiece::PathDiagnosticPiece(Kind k, DisplayHint hint)
|
2018-03-09 06:45:13 +08:00
|
|
|
: kind(k), Hint(hint) {}
|
2009-03-13 02:41:53 +08:00
|
|
|
|
2018-03-09 06:45:13 +08:00
|
|
|
PathDiagnosticPiece::~PathDiagnosticPiece() = default;
|
|
|
|
|
|
|
|
PathDiagnosticEventPiece::~PathDiagnosticEventPiece() = default;
|
|
|
|
|
|
|
|
PathDiagnosticCallPiece::~PathDiagnosticCallPiece() = default;
|
|
|
|
|
|
|
|
PathDiagnosticControlFlowPiece::~PathDiagnosticControlFlowPiece() = default;
|
|
|
|
|
|
|
|
PathDiagnosticMacroPiece::~PathDiagnosticMacroPiece() = default;
|
|
|
|
|
|
|
|
PathDiagnosticNotePiece::~PathDiagnosticNotePiece() = default;
|
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 {
|
2017-01-06 01:26:53 +08:00
|
|
|
for (auto &Piece : *this) {
|
2012-08-04 07:08:54 +08:00
|
|
|
switch (Piece->getKind()) {
|
|
|
|
case PathDiagnosticPiece::Call: {
|
2017-01-06 01:26:53 +08:00
|
|
|
auto &Call = cast<PathDiagnosticCallPiece>(*Piece);
|
|
|
|
if (auto CallEnter = Call.getCallEnterEvent())
|
|
|
|
Current.push_back(std::move(CallEnter));
|
|
|
|
Call.path.flattenTo(Primary, Primary, ShouldFlattenMacros);
|
|
|
|
if (auto callExit = Call.getCallExitEvent())
|
|
|
|
Current.push_back(std::move(callExit));
|
2012-08-04 07:08:54 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PathDiagnosticPiece::Macro: {
|
2017-01-06 01:26:53 +08:00
|
|
|
auto &Macro = cast<PathDiagnosticMacroPiece>(*Piece);
|
2012-08-04 07:08:54 +08:00
|
|
|
if (ShouldFlattenMacros) {
|
2017-01-06 01:26:53 +08:00
|
|
|
Macro.subPieces.flattenTo(Primary, Primary, ShouldFlattenMacros);
|
2012-08-04 07:08:54 +08:00
|
|
|
} else {
|
|
|
|
Current.push_back(Piece);
|
|
|
|
PathPieces NewPath;
|
2017-01-06 01:26:53 +08:00
|
|
|
Macro.subPieces.flattenTo(Primary, NewPath, ShouldFlattenMacros);
|
2012-08-04 07:08:54 +08:00
|
|
|
// FIXME: This probably shouldn't mutate the original path piece.
|
2017-01-06 01:26:53 +08:00
|
|
|
Macro.subPieces = NewPath;
|
2012-08-04 07:08:54 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PathDiagnosticPiece::Event:
|
|
|
|
case PathDiagnosticPiece::ControlFlow:
|
2016-10-07 23:23:02 +08:00
|
|
|
case PathDiagnosticPiece::Note:
|
2012-08-04 07:08:54 +08:00
|
|
|
Current.push_back(Piece);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 06:45:13 +08:00
|
|
|
PathDiagnostic::~PathDiagnostic() = default;
|
2012-02-08 12:32:27 +08:00
|
|
|
|
2018-01-17 10:59:11 +08:00
|
|
|
PathDiagnostic::PathDiagnostic(
|
|
|
|
StringRef CheckName, const Decl *declWithIssue, StringRef bugtype,
|
|
|
|
StringRef verboseDesc, StringRef shortDesc, StringRef category,
|
|
|
|
PathDiagnosticLocation LocationToUnique, const Decl *DeclToUnique,
|
|
|
|
std::unique_ptr<FilesToLineNumsMap> ExecutedLines)
|
|
|
|
: CheckName(CheckName), DeclWithIssue(declWithIssue),
|
|
|
|
BugType(StripTrailingDots(bugtype)),
|
|
|
|
VerboseDesc(StripTrailingDots(verboseDesc)),
|
|
|
|
ShortDesc(StripTrailingDots(shortDesc)),
|
|
|
|
Category(StripTrailingDots(category)), UniqueingLoc(LocationToUnique),
|
|
|
|
UniqueingDecl(DeclToUnique), ExecutedLines(std::move(ExecutedLines)),
|
|
|
|
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
|
|
|
|
2018-04-26 05:51:26 +08:00
|
|
|
assert(AnalysisManager::isInCodeFile(CallLoc, SMgr) &&
|
|
|
|
"The call piece should not be in a header file.");
|
2013-05-17 06:30:45 +08:00
|
|
|
|
|
|
|
// Check if CP represents a path through a function outside of the main file.
|
2018-04-26 05:51:26 +08:00
|
|
|
if (!AnalysisManager::isInCodeFile(CP->callEnterWithin.asLocation(), SMgr))
|
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.
|
2018-03-09 06:45:13 +08:00
|
|
|
if (auto *CPInner = dyn_cast<PathDiagnosticCallPiece>(Path.back().get()))
|
2013-05-17 06:30:45 +08:00
|
|
|
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.
|
2018-03-09 06:45:13 +08:00
|
|
|
if (auto *CP = dyn_cast<PathDiagnosticCallPiece>(LastP)) {
|
2013-05-17 06:30:45 +08:00
|
|
|
CP = getFirstStackedCallToHeaderFile(CP, SMgr);
|
|
|
|
if (CP) {
|
|
|
|
// Mark the piece.
|
|
|
|
CP->setAsLastInMainSourceFile();
|
|
|
|
|
|
|
|
// Update the path diagnostic message.
|
2018-03-09 06:45:13 +08:00
|
|
|
const auto *ND = dyn_cast<NamedDecl>(CP->getCallee());
|
2013-05-17 06:30:45 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 06:45:13 +08:00
|
|
|
void PathDiagnosticConsumer::anchor() {}
|
2011-12-20 10:48:34 +08:00
|
|
|
|
2012-01-26 07:47:14 +08:00
|
|
|
PathDiagnosticConsumer::~PathDiagnosticConsumer() {
|
|
|
|
// Delete the contents of the FoldingSet if it isn't empty already.
|
2018-03-09 06:45:13 +08:00
|
|
|
for (auto &Diag : Diags)
|
|
|
|
delete &Diag;
|
2012-01-26 07:47:14 +08:00
|
|
|
}
|
|
|
|
|
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);
|
2016-10-07 05:42:21 +08:00
|
|
|
SmallString<128> buf;
|
|
|
|
llvm::raw_svector_ostream warning(buf);
|
|
|
|
warning << "warning: Path diagnostic report is not generated. Current "
|
|
|
|
<< "output format does not support diagnostics that cross file "
|
|
|
|
<< "boundaries. Refer to --analyzer-output for valid output "
|
|
|
|
<< "formats\n";
|
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
|
|
|
|
2018-03-09 06:45:13 +08:00
|
|
|
for (const auto &I : path) {
|
|
|
|
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);
|
2016-10-07 05:42:21 +08:00
|
|
|
} else if (SMgr.getFileID(L) != FID) {
|
|
|
|
llvm::errs() << warning.str();
|
|
|
|
return;
|
|
|
|
}
|
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();
|
2018-03-09 06:45:13 +08:00
|
|
|
for (const auto &I : Ranges) {
|
|
|
|
SourceLocation L = SMgr.getExpansionLoc(I.getBegin());
|
2016-10-07 05:42:21 +08:00
|
|
|
if (!L.isFileID() || SMgr.getFileID(L) != FID) {
|
|
|
|
llvm::errs() << warning.str();
|
|
|
|
return;
|
|
|
|
}
|
2018-03-09 06:45:13 +08:00
|
|
|
L = SMgr.getExpansionLoc(I.getEnd());
|
2016-10-07 05:42:21 +08:00
|
|
|
if (!L.isFileID() || SMgr.getFileID(L) != FID) {
|
|
|
|
llvm::errs() << warning.str();
|
|
|
|
return;
|
|
|
|
}
|
2012-03-01 07:59:20 +08:00
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2018-03-09 06:45:13 +08:00
|
|
|
if (const auto *call = dyn_cast<PathDiagnosticCallPiece>(piece))
|
2012-03-01 07:59:20 +08:00
|
|
|
WorkList.push_back(&call->path);
|
2018-03-09 06:45:13 +08:00
|
|
|
else if (const auto *macro = dyn_cast<PathDiagnosticMacroPiece>(piece))
|
2012-03-01 07:59:20 +08:00
|
|
|
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()) {
|
2016-10-07 23:23:02 +08:00
|
|
|
case PathDiagnosticPiece::ControlFlow:
|
2012-09-10 22:50:55 +08:00
|
|
|
return compareControlFlow(cast<PathDiagnosticControlFlowPiece>(X),
|
|
|
|
cast<PathDiagnosticControlFlowPiece>(Y));
|
2016-10-07 23:23:02 +08:00
|
|
|
case PathDiagnosticPiece::Event:
|
|
|
|
case PathDiagnosticPiece::Note:
|
2013-02-21 09:47:18 +08:00
|
|
|
return None;
|
2016-10-07 23:23:02 +08:00
|
|
|
case PathDiagnosticPiece::Macro:
|
2012-09-10 22:50:55 +08:00
|
|
|
return compareMacro(cast<PathDiagnosticMacroPiece>(X),
|
|
|
|
cast<PathDiagnosticMacroPiece>(Y));
|
2016-10-07 23:23:02 +08:00
|
|
|
case PathDiagnosticPiece::Call:
|
2012-09-10 22:50:55 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-03-01 22:54:16 +08:00
|
|
|
static bool compareCrossTUSourceLocs(FullSourceLoc XL, FullSourceLoc YL) {
|
|
|
|
std::pair<FileID, unsigned> XOffs = XL.getDecomposedLoc();
|
|
|
|
std::pair<FileID, unsigned> YOffs = YL.getDecomposedLoc();
|
|
|
|
const SourceManager &SM = XL.getManager();
|
|
|
|
std::pair<bool, bool> InSameTU = SM.isInTheSameTranslationUnit(XOffs, YOffs);
|
|
|
|
if (InSameTU.first)
|
|
|
|
return XL.isBeforeInTranslationUnitThan(YL);
|
2018-07-04 22:12:58 +08:00
|
|
|
const FileEntry *XFE = SM.getFileEntryForID(XL.getSpellingLoc().getFileID());
|
|
|
|
const FileEntry *YFE = SM.getFileEntryForID(YL.getSpellingLoc().getFileID());
|
2018-03-01 22:54:16 +08:00
|
|
|
if (!XFE || !YFE)
|
|
|
|
return XFE && !YFE;
|
2018-07-04 22:12:58 +08:00
|
|
|
int NameCmp = XFE->getName().compare(YFE->getName());
|
|
|
|
if (NameCmp != 0)
|
|
|
|
return NameCmp == -1;
|
|
|
|
// Last resort: Compare raw file IDs that are possibly expansions.
|
|
|
|
return XL.getFileID() < YL.getFileID();
|
2018-03-01 22:54:16 +08:00
|
|
|
}
|
|
|
|
|
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)
|
2018-03-01 22:54:16 +08:00
|
|
|
return compareCrossTUSourceLocs(XL, YL);
|
2012-09-10 22:50:55 +08:00
|
|
|
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();
|
2018-03-01 22:54:16 +08:00
|
|
|
return compareCrossTUSourceLocs(FullSourceLoc(XDL, SM),
|
|
|
|
FullSourceLoc(YDL, SM));
|
2012-09-11 03:07:56 +08:00
|
|
|
}
|
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;
|
2018-03-09 06:45:13 +08:00
|
|
|
for (const auto &D : Diags)
|
|
|
|
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.
|
2018-03-09 06:45:13 +08:00
|
|
|
for (const auto D : BatchDiags)
|
2012-01-26 07:47:14 +08:00
|
|
|
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) {
|
2018-08-10 05:09:38 +08:00
|
|
|
SourceLocation L = UseEnd ? S->getEndLoc() : S->getBeginLoc();
|
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)
|
2018-08-10 05:08:08 +08:00
|
|
|
L = Body->getBeginLoc();
|
2012-07-27 04:04:30 +08:00
|
|
|
else
|
2018-08-10 05:09:38 +08:00
|
|
|
L = ADC->getDecl()->getEndLoc();
|
2012-07-27 04:04:30 +08:00
|
|
|
break;
|
|
|
|
}
|
2011-09-21 00:37:36 +08:00
|
|
|
|
2018-08-10 05:09:38 +08:00
|
|
|
L = UseEnd ? Parent->getEndLoc() : Parent->getBeginLoc();
|
2012-07-27 04:04:30 +08:00
|
|
|
} 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:
|
[CFG] Add extra context to C++ constructor statement elements.
This patch adds a new CFGStmt sub-class, CFGConstructor, which replaces
the regular CFGStmt with CXXConstructExpr in it whenever the CFG has additional
information to provide regarding what sort of object is being constructed.
It is useful for figuring out what memory is initialized in client of the
CFG such as the Static Analyzer, which do not operate by recursive AST
traversal, but instead rely on the CFG to provide all the information when they
need it. Otherwise, the statement that triggers the construction and defines
what memory is being initialized would normally occur after the
construct-expression, and the client would need to peek to the next CFG element
or use statement parent map to understand the necessary facts about
the construct-expression.
As a proof of concept, CFGConstructors are added for new-expressions
and the respective test cases are provided to demonstrate how it works.
For now, the only additional data contained in the CFGConstructor element is
the "trigger statement", such as new-expression, which is the parent of the
constructor. It will be significantly expanded in later commits. The additional
data is organized as an auxiliary structure - the "construction context",
which is allocated separately from the CFGElement.
Differential Revision: https://reviews.llvm.org/D42672
llvm-svn: 324668
2018-02-09 06:58:15 +08:00
|
|
|
case CFGElement::Constructor:
|
2018-03-13 07:12:40 +08:00
|
|
|
case CFGElement::CXXRecordTypedCall:
|
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);
|
|
|
|
}
|
2018-01-18 08:10:21 +08:00
|
|
|
case CFGElement::NewAllocator: {
|
|
|
|
const CFGNewAllocator &Alloc = Source.castAs<CFGNewAllocator>();
|
|
|
|
return PathDiagnosticLocation(Alloc.getAllocatorExpr(), SM, CallerCtx);
|
|
|
|
}
|
2018-02-16 03:28:21 +08:00
|
|
|
case CFGElement::TemporaryDtor: {
|
|
|
|
// Temporary destructors are for temporaries. They die immediately at around
|
|
|
|
// the location of CXXBindTemporaryExpr. If they are lifetime-extended,
|
|
|
|
// they'd be dealt with via an AutomaticObjectDtor instead.
|
|
|
|
const auto &Dtor = Source.castAs<CFGTemporaryDtor>();
|
|
|
|
return PathDiagnosticLocation::createEnd(Dtor.getBindTemporaryExpr(), SM,
|
|
|
|
CallerCtx);
|
|
|
|
}
|
2018-03-12 20:26:15 +08:00
|
|
|
case CFGElement::ScopeBegin:
|
|
|
|
case CFGElement::ScopeEnd:
|
|
|
|
llvm_unreachable("not yet implemented!");
|
2017-08-19 19:19:16 +08:00
|
|
|
case CFGElement::LifetimeEnds:
|
|
|
|
case CFGElement::LoopExit:
|
|
|
|
llvm_unreachable("CFGElement kind should not be on callsite!");
|
2012-08-04 07:08:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2018-08-10 05:08:08 +08:00
|
|
|
return PathDiagnosticLocation(D->getBeginLoc(), SM, SingleLocK);
|
2011-09-21 05:38:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2018-03-09 06:45:13 +08:00
|
|
|
if (const auto *CS = dyn_cast<CompoundStmt>(S))
|
2012-07-27 04:04:05 +08:00
|
|
|
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++.
|
2018-03-09 06:45:13 +08:00
|
|
|
if (const auto *CS = dyn_cast_or_null<CompoundStmt>(LC->getDecl()->getBody()))
|
2011-09-17 03:18:30 +08:00
|
|
|
if (!CS->body_empty()) {
|
2018-08-10 05:08:08 +08:00
|
|
|
SourceLocation Loc = (*CS->body_begin())->getBeginLoc();
|
2011-09-17 03:18:30 +08:00
|
|
|
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);
|
2019-01-11 07:44:44 +08:00
|
|
|
} else if (Optional<PreImplicitCall> PIC = P.getAs<PreImplicitCall>()) {
|
|
|
|
return PathDiagnosticLocation(PIC->getLocation(), 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);
|
2019-01-19 03:24:55 +08:00
|
|
|
} else if (auto CEB = P.getAs<CallExitBegin>()) {
|
|
|
|
if (const ReturnStmt *RS = CEB->getReturnStmt())
|
|
|
|
return PathDiagnosticLocation::createBegin(RS, SMng,
|
|
|
|
CEB->getLocationContext());
|
|
|
|
return PathDiagnosticLocation(
|
|
|
|
CEB->getLocationContext()->getDecl()->getSourceRange().getEnd(), SMng);
|
2017-11-30 02:25:37 +08:00
|
|
|
} else if (Optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) {
|
|
|
|
CFGElement BlockFront = BE->getBlock()->front();
|
|
|
|
if (auto StmtElt = BlockFront.getAs<CFGStmt>()) {
|
2018-08-10 05:08:08 +08:00
|
|
|
return PathDiagnosticLocation(StmtElt->getStmt()->getBeginLoc(), SMng);
|
2017-11-30 02:25:37 +08:00
|
|
|
} else if (auto NewAllocElt = BlockFront.getAs<CFGNewAllocator>()) {
|
|
|
|
return PathDiagnosticLocation(
|
2018-08-10 05:08:08 +08:00
|
|
|
NewAllocElt->getAllocatorExpr()->getBeginLoc(), SMng);
|
2017-11-30 02:25:37 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("Unexpected CFG element at front of block");
|
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
|
|
|
}
|
|
|
|
|
2017-06-05 20:40:03 +08:00
|
|
|
static const LocationContext *
|
|
|
|
findTopAutosynthesizedParentContext(const LocationContext *LC) {
|
|
|
|
assert(LC->getAnalysisDeclContext()->isBodyAutosynthesized());
|
|
|
|
const LocationContext *ParentLC = LC->getParent();
|
|
|
|
assert(ParentLC && "We don't start analysis from autosynthesized code");
|
|
|
|
while (ParentLC->getAnalysisDeclContext()->isBodyAutosynthesized()) {
|
|
|
|
LC = ParentLC;
|
|
|
|
ParentLC = LC->getParent();
|
|
|
|
assert(ParentLC && "We don't start analysis from autosynthesized code");
|
|
|
|
}
|
|
|
|
return LC;
|
|
|
|
}
|
|
|
|
|
2013-04-24 07:57:43 +08:00
|
|
|
const Stmt *PathDiagnosticLocation::getStmt(const ExplodedNode *N) {
|
2017-06-05 20:40:03 +08:00
|
|
|
// We cannot place diagnostics on autosynthesized code.
|
|
|
|
// Put them onto the call site through which we jumped into autosynthesized
|
|
|
|
// code for the first time.
|
|
|
|
const LocationContext *LC = N->getLocationContext();
|
|
|
|
if (LC->getAnalysisDeclContext()->isBodyAutosynthesized()) {
|
|
|
|
// It must be a stack frame because we only autosynthesize functions.
|
|
|
|
return cast<StackFrameContext>(findTopAutosynthesizedParentContext(LC))
|
|
|
|
->getCallSite();
|
|
|
|
}
|
|
|
|
// Otherwise, see if the node's program point directly points to a statement.
|
2013-04-24 07:57:43 +08:00
|
|
|
ProgramPoint P = N->getLocation();
|
2018-09-22 04:36:41 +08:00
|
|
|
if (auto SP = P.getAs<StmtPoint>())
|
2013-04-24 07:57:43 +08:00
|
|
|
return SP->getStmt();
|
2018-09-22 04:36:41 +08:00
|
|
|
if (auto BE = P.getAs<BlockEdge>())
|
2013-04-24 07:57:43 +08:00
|
|
|
return BE->getSrc()->getTerminator();
|
2018-09-22 04:36:41 +08:00
|
|
|
if (auto CE = P.getAs<CallEnter>())
|
2013-04-24 07:57:43 +08:00
|
|
|
return CE->getCallExpr();
|
2018-09-22 04:36:41 +08:00
|
|
|
if (auto CEE = P.getAs<CallExitEnd>())
|
2013-04-24 07:57:43 +08:00
|
|
|
return CEE->getCalleeContext()->getCallSite();
|
2018-09-22 04:36:41 +08:00
|
|
|
if (auto PIPP = P.getAs<PostInitializer>())
|
2013-04-24 07:57:43 +08:00
|
|
|
return PIPP->getInitializer()->getInit();
|
2018-09-22 04:36:41 +08:00
|
|
|
if (auto CEB = P.getAs<CallExitBegin>())
|
2018-02-02 10:19:43 +08:00
|
|
|
return CEB->getReturnStmt();
|
2018-09-22 04:36:41 +08:00
|
|
|
if (auto FEP = P.getAs<FunctionExitPoint>())
|
|
|
|
return FEP->getStmt();
|
2013-04-24 07:57:43 +08:00
|
|
|
|
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);
|
2018-09-22 04:36:41 +08:00
|
|
|
const LocationContext *LC = N->getLocationContext();
|
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);
|
2018-09-22 04:36:41 +08:00
|
|
|
if (auto FE = N->getLocationAs<FunctionExitPoint>()) {
|
|
|
|
if (const ReturnStmt *RS = FE->getStmt())
|
|
|
|
return PathDiagnosticLocation::createBegin(RS, SM, LC);
|
|
|
|
}
|
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();
|
|
|
|
|
|
|
|
// For member expressions, return the location of the '.' or '->'.
|
2018-03-09 06:45:13 +08:00
|
|
|
if (const auto *ME = dyn_cast<MemberExpr>(S))
|
2013-04-24 07:57:43 +08:00
|
|
|
return PathDiagnosticLocation::createMemberLoc(ME, SM);
|
|
|
|
|
|
|
|
// For binary operators, return the location of the operator.
|
2018-03-09 06:45:13 +08:00
|
|
|
if (const auto *B = dyn_cast<BinaryOperator>(S))
|
2013-04-24 07:57:43 +08:00
|
|
|
return PathDiagnosticLocation::createOperatorLoc(B, SM);
|
|
|
|
|
|
|
|
if (P.getAs<PostStmtPurgeDeadSymbols>())
|
|
|
|
return PathDiagnosticLocation::createEnd(S, SM, LC);
|
|
|
|
|
2018-08-10 05:08:08 +08:00
|
|
|
if (S->getBeginLoc().isValid())
|
2012-10-06 09:19:30 +08:00
|
|
|
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: {
|
2018-03-09 06:45:13 +08:00
|
|
|
const auto *DS = cast<DeclStmt>(S);
|
2009-05-15 10:05:25 +08:00
|
|
|
if (DS->isSingleDecl()) {
|
|
|
|
// Should always be the case, but we'll be defensive.
|
2018-08-10 05:08:08 +08:00
|
|
|
return SourceRange(DS->getBeginLoc(),
|
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:
|
2018-03-09 06:45:13 +08:00
|
|
|
if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
|
2009-04-07 06:33:35 +08:00
|
|
|
return MD->getSourceRange();
|
2018-03-09 06:45:13 +08:00
|
|
|
if (const auto *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
|
|
|
|
2018-03-09 06:45:13 +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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-01-06 01:26:53 +08:00
|
|
|
std::shared_ptr<PathDiagnosticCallPiece>
|
2018-09-29 02:49:41 +08:00
|
|
|
PathDiagnosticCallPiece::construct(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);
|
2017-01-06 01:26:53 +08:00
|
|
|
return std::shared_ptr<PathDiagnosticCallPiece>(
|
|
|
|
new PathDiagnosticCallPiece(caller, pos));
|
2012-02-24 14:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PathDiagnosticCallPiece *
|
2012-03-15 02:58:28 +08:00
|
|
|
PathDiagnosticCallPiece::construct(PathPieces &path,
|
|
|
|
const Decl *caller) {
|
2017-01-06 01:26:53 +08:00
|
|
|
std::shared_ptr<PathDiagnosticCallPiece> C(
|
|
|
|
new PathDiagnosticCallPiece(path, caller));
|
2012-02-24 14:00:00 +08:00
|
|
|
path.clear();
|
2017-01-06 01:26:53 +08:00
|
|
|
auto *R = C.get();
|
|
|
|
path.push_front(std::move(C));
|
|
|
|
return R;
|
2012-02-24 14:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2017-06-05 20:40:03 +08:00
|
|
|
|
|
|
|
// Autosynthesized property accessors are special because we'd never
|
|
|
|
// pop back up to non-autosynthesized code until we leave them.
|
|
|
|
// This is not generally true for autosynthesized callees, which may call
|
|
|
|
// non-autosynthesized callbacks.
|
|
|
|
// Unless set here, the IsCalleeAnAutosynthesizedPropertyAccessor flag
|
|
|
|
// defaults to false.
|
2018-03-09 06:45:13 +08:00
|
|
|
if (const auto *MD = dyn_cast<ObjCMethodDecl>(Callee))
|
2017-06-05 20:40:03 +08:00
|
|
|
IsCalleeAnAutosynthesizedPropertyAccessor = (
|
|
|
|
MD->isPropertyAccessor() &&
|
|
|
|
CalleeCtx->getAnalysisDeclContext()->isBodyAutosynthesized());
|
2012-02-24 14:00:00 +08:00
|
|
|
}
|
|
|
|
|
2018-05-25 21:18:38 +08:00
|
|
|
static void describeTemplateParameters(raw_ostream &Out,
|
|
|
|
const ArrayRef<TemplateArgument> TAList,
|
|
|
|
const LangOptions &LO,
|
|
|
|
StringRef Prefix = StringRef(),
|
|
|
|
StringRef Postfix = StringRef());
|
|
|
|
|
|
|
|
static void describeTemplateParameter(raw_ostream &Out,
|
|
|
|
const TemplateArgument &TArg,
|
|
|
|
const LangOptions &LO) {
|
|
|
|
|
|
|
|
if (TArg.getKind() == TemplateArgument::ArgKind::Pack) {
|
|
|
|
describeTemplateParameters(Out, TArg.getPackAsArray(), LO);
|
|
|
|
} else {
|
|
|
|
TArg.print(PrintingPolicy(LO), Out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void describeTemplateParameters(raw_ostream &Out,
|
|
|
|
const ArrayRef<TemplateArgument> TAList,
|
|
|
|
const LangOptions &LO,
|
|
|
|
StringRef Prefix, StringRef Postfix) {
|
|
|
|
if (TAList.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Out << Prefix;
|
|
|
|
for (int I = 0, Last = TAList.size() - 1; I != Last; ++I) {
|
|
|
|
describeTemplateParameter(Out, TAList[I], LO);
|
|
|
|
Out << ", ";
|
|
|
|
}
|
|
|
|
describeTemplateParameter(Out, TAList[TAList.size() - 1], LO);
|
|
|
|
Out << Postfix;
|
|
|
|
}
|
|
|
|
|
2018-03-09 06:45:13 +08:00
|
|
|
static void describeClass(raw_ostream &Out, const CXXRecordDecl *D,
|
|
|
|
StringRef Prefix = StringRef()) {
|
2013-01-19 02:27:21 +08:00
|
|
|
if (!D->getIdentifier())
|
|
|
|
return;
|
2018-05-25 21:18:38 +08:00
|
|
|
Out << Prefix << '\'' << *D;
|
|
|
|
if (const auto T = dyn_cast<ClassTemplateSpecializationDecl>(D))
|
|
|
|
describeTemplateParameters(Out, T->getTemplateArgs().asArray(),
|
|
|
|
D->getASTContext().getLangOpts(), "<", ">");
|
|
|
|
|
|
|
|
Out << '\'';
|
2013-01-19 02:27:21 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-03-09 06:45:13 +08:00
|
|
|
if (const auto *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 ";
|
|
|
|
}
|
|
|
|
|
2018-03-09 06:45:13 +08:00
|
|
|
if (const auto *CD = dyn_cast<CXXConstructorDecl>(MD)) {
|
2013-01-19 02:27:21 +08:00
|
|
|
if (CD->isDefaultConstructor())
|
|
|
|
Out << "default ";
|
|
|
|
else if (CD->isCopyConstructor())
|
|
|
|
Out << "copy ";
|
|
|
|
else if (CD->isMoveConstructor())
|
|
|
|
Out << "move ";
|
|
|
|
|
|
|
|
Out << "constructor";
|
|
|
|
describeClass(Out, MD->getParent(), " for ");
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2018-05-25 21:18:38 +08:00
|
|
|
Out << Prefix << '\'' << cast<NamedDecl>(*D);
|
|
|
|
|
|
|
|
// Adding template parameters.
|
|
|
|
if (const auto FD = dyn_cast<FunctionDecl>(D))
|
|
|
|
if (const TemplateArgumentList *TAList =
|
|
|
|
FD->getTemplateSpecializationArgs())
|
|
|
|
describeTemplateParameters(Out, TAList->asArray(),
|
|
|
|
FD->getASTContext().getLangOpts(), "<", ">");
|
|
|
|
|
|
|
|
Out << '\'';
|
2013-01-19 02:27:14 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-06 01:26:53 +08:00
|
|
|
std::shared_ptr<PathDiagnosticEventPiece>
|
2012-02-24 14:00:00 +08:00
|
|
|
PathDiagnosticCallPiece::getCallEnterEvent() const {
|
2017-06-05 20:40:03 +08:00
|
|
|
// We do not produce call enters and call exits for autosynthesized property
|
|
|
|
// accessors. We do generally produce them for other functions coming from
|
|
|
|
// the body farm because they may call callbacks that bring us back into
|
|
|
|
// visible code.
|
|
|
|
if (!Callee || IsCalleeAnAutosynthesizedPropertyAccessor)
|
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());
|
2017-01-06 01:26:53 +08:00
|
|
|
return std::make_shared<PathDiagnosticEventPiece>(callEnter, Out.str());
|
2012-02-24 14:00:00 +08:00
|
|
|
}
|
|
|
|
|
2017-01-06 01:26:53 +08:00
|
|
|
std::shared_ptr<PathDiagnosticEventPiece>
|
2012-03-06 09:25:01 +08:00
|
|
|
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;
|
2018-03-09 06:45:13 +08:00
|
|
|
if (const auto *MD = dyn_cast<CXXMethodDecl>(Callee))
|
2013-01-20 03:52:57 +08:00
|
|
|
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
|
|
|
|
2017-01-06 01:26:53 +08:00
|
|
|
return std::make_shared<PathDiagnosticEventPiece>(callEnterWithin, Out.str());
|
2012-03-06 09:25:01 +08:00
|
|
|
}
|
|
|
|
|
2017-01-06 01:26:53 +08:00
|
|
|
std::shared_ptr<PathDiagnosticEventPiece>
|
2012-02-24 14:00:00 +08:00
|
|
|
PathDiagnosticCallPiece::getCallExitEvent() const {
|
2017-06-05 20:40:03 +08:00
|
|
|
// We do not produce call enters and call exits for autosynthesized property
|
|
|
|
// accessors. We do generally produce them for other functions coming from
|
|
|
|
// the body farm because they may call callbacks that bring us back into
|
|
|
|
// visible code.
|
|
|
|
if (NoExit || IsCalleeAnAutosynthesizedPropertyAccessor)
|
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());
|
2017-01-06 01:26:53 +08:00
|
|
|
return std::make_shared<PathDiagnosticEventPiece>(callReturn, Out.str());
|
2012-02-24 14:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void compute_path_size(const PathPieces &pieces, unsigned &size) {
|
2018-03-09 06:45:13 +08:00
|
|
|
for (const auto &I : pieces) {
|
|
|
|
const PathDiagnosticPiece *piece = I.get();
|
|
|
|
if (const auto *cp = dyn_cast<PathDiagnosticCallPiece>(piece))
|
2012-02-24 14:00:00 +08:00
|
|
|
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();
|
2018-03-09 06:45:13 +08:00
|
|
|
for (const auto &I : Ranges) {
|
|
|
|
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);
|
2018-03-09 06:45:13 +08:00
|
|
|
for (const auto &I : path)
|
|
|
|
ID.Add(*I);
|
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);
|
2018-03-09 06:45:13 +08:00
|
|
|
for (const auto &I : *this)
|
|
|
|
ID.Add(I);
|
2009-09-19 06:33:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PathDiagnosticMacroPiece::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
PathDiagnosticSpotPiece::Profile(ID);
|
2018-03-09 06:45:13 +08:00
|
|
|
for (const auto &I : subPieces)
|
|
|
|
ID.Add(*I);
|
2009-09-19 06:33:39 +08:00
|
|
|
}
|
|
|
|
|
2016-10-07 23:23:02 +08:00
|
|
|
void PathDiagnosticNotePiece::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
PathDiagnosticSpotPiece::Profile(ID);
|
|
|
|
}
|
|
|
|
|
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);
|
2018-03-09 06:45:13 +08:00
|
|
|
for (const auto &I : path)
|
|
|
|
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
|
|
|
|
2018-03-09 06:45:13 +08:00
|
|
|
StackHintGenerator::~StackHintGenerator() = default;
|
2012-03-17 07:24:20 +08:00
|
|
|
|
|
|
|
std::string StackHintGeneratorForSymbol::getMessage(const ExplodedNode *N){
|
2018-01-23 04:18:42 +08:00
|
|
|
if (!N)
|
|
|
|
return getMessageForSymbolNotFound();
|
|
|
|
|
2012-03-17 07:24:20 +08:00
|
|
|
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();
|
2018-03-09 06:45:13 +08:00
|
|
|
const auto *CE = dyn_cast_or_null<CallExpr>(CallSite);
|
2012-03-17 07:24:20 +08:00
|
|
|
if (!CE)
|
2018-03-09 06:45:13 +08:00
|
|
|
return {};
|
2012-03-17 07:24:20 +08:00
|
|
|
|
|
|
|
// Check if one of the parameters are set to the interesting symbol.
|
|
|
|
unsigned ArgIndex = 0;
|
|
|
|
for (CallExpr::const_arg_iterator I = CE->arg_begin(),
|
|
|
|
E = CE->arg_end(); I != E; ++I, ++ArgIndex){
|
2018-01-18 04:27:29 +08:00
|
|
|
SVal SV = N->getSVal(*I);
|
2012-03-17 07:24:20 +08:00
|
|
|
|
|
|
|
// 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>()) {
|
2018-01-25 06:17:30 +08:00
|
|
|
// Do not attempt to dereference void*.
|
|
|
|
if ((*I)->getType()->isVoidPointerType())
|
|
|
|
continue;
|
2018-01-18 04:27:29 +08:00
|
|
|
SVal PSV = N->getState()->getSVal(Reg->getRegion());
|
2012-03-17 07:24:20 +08:00
|
|
|
SymbolRef AS = PSV.getAsLocSymbol();
|
|
|
|
if (AS == Sym) {
|
|
|
|
return getMessageForArg(*I, ArgIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we are returning the interesting symbol.
|
2018-01-18 04:27:29 +08:00
|
|
|
SVal SV = N->getSVal(CE);
|
2012-03-17 07:24:20 +08:00
|
|
|
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();
|
|
|
|
}
|
2018-06-13 04:51:01 +08:00
|
|
|
|
|
|
|
LLVM_DUMP_METHOD void PathPieces::dump() const {
|
|
|
|
unsigned index = 0;
|
|
|
|
for (PathPieces::const_iterator I = begin(), E = end(); I != E; ++I) {
|
|
|
|
llvm::errs() << "[" << index++ << "] ";
|
|
|
|
(*I)->dump();
|
|
|
|
llvm::errs() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_DUMP_METHOD void PathDiagnosticCallPiece::dump() const {
|
|
|
|
llvm::errs() << "CALL\n--------------\n";
|
|
|
|
|
|
|
|
if (const Stmt *SLoc = getLocation().getStmtOrNull())
|
|
|
|
SLoc->dump();
|
|
|
|
else if (const auto *ND = dyn_cast_or_null<NamedDecl>(getCallee()))
|
|
|
|
llvm::errs() << *ND << "\n";
|
|
|
|
else
|
|
|
|
getLocation().dump();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_DUMP_METHOD void PathDiagnosticEventPiece::dump() const {
|
|
|
|
llvm::errs() << "EVENT\n--------------\n";
|
|
|
|
llvm::errs() << getString() << "\n";
|
|
|
|
llvm::errs() << " ---- at ----\n";
|
|
|
|
getLocation().dump();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_DUMP_METHOD void PathDiagnosticControlFlowPiece::dump() const {
|
|
|
|
llvm::errs() << "CONTROL\n--------------\n";
|
|
|
|
getStartLocation().dump();
|
|
|
|
llvm::errs() << " ---- to ----\n";
|
|
|
|
getEndLocation().dump();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_DUMP_METHOD void PathDiagnosticMacroPiece::dump() const {
|
|
|
|
llvm::errs() << "MACRO\n--------------\n";
|
|
|
|
// FIXME: Print which macro is being invoked.
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_DUMP_METHOD void PathDiagnosticNotePiece::dump() const {
|
|
|
|
llvm::errs() << "NOTE\n--------------\n";
|
|
|
|
llvm::errs() << getString() << "\n";
|
|
|
|
llvm::errs() << " ---- at ----\n";
|
|
|
|
getLocation().dump();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_DUMP_METHOD void PathDiagnosticLocation::dump() const {
|
|
|
|
if (!isValid()) {
|
|
|
|
llvm::errs() << "<INVALID>\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (K) {
|
|
|
|
case RangeK:
|
|
|
|
// FIXME: actually print the range.
|
|
|
|
llvm::errs() << "<range>\n";
|
|
|
|
break;
|
|
|
|
case SingleLocK:
|
|
|
|
asLocation().dump();
|
|
|
|
llvm::errs() << "\n";
|
|
|
|
break;
|
|
|
|
case StmtK:
|
|
|
|
if (S)
|
|
|
|
S->dump();
|
|
|
|
else
|
|
|
|
llvm::errs() << "<NULL STMT>\n";
|
|
|
|
break;
|
|
|
|
case DeclK:
|
|
|
|
if (const auto *ND = dyn_cast_or_null<NamedDecl>(D))
|
|
|
|
llvm::errs() << *ND << "\n";
|
|
|
|
else if (isa<BlockDecl>(D))
|
|
|
|
// FIXME: Make this nicer.
|
|
|
|
llvm::errs() << "<block>\n";
|
|
|
|
else if (D)
|
|
|
|
llvm::errs() << "<unknown decl>\n";
|
|
|
|
else
|
|
|
|
llvm::errs() << "<NULL DECL>\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|