2010-09-10 08:44:44 +08:00
|
|
|
//==--AnalyzerStatsChecker.cpp - Analyzer visitation statistics --*- C++ -*-==//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-09-10 08:44:44 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// This file reports various statistics about analyzer visitation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
[analyzer][NFC] Move CheckerRegistry from the Core directory to Frontend
ClangCheckerRegistry is a very non-obvious, poorly documented, weird concept.
It derives from CheckerRegistry, and is placed in lib/StaticAnalyzer/Frontend,
whereas it's base is located in lib/StaticAnalyzer/Core. It was, from what I can
imagine, used to circumvent the problem that the registry functions of the
checkers are located in the clangStaticAnalyzerCheckers library, but that
library depends on clangStaticAnalyzerCore. However, clangStaticAnalyzerFrontend
depends on both of those libraries.
One can make the observation however, that CheckerRegistry has no place in Core,
it isn't used there at all! The only place where it is used is Frontend, which
is where it ultimately belongs.
This move implies that since
include/clang/StaticAnalyzer/Checkers/ClangCheckers.h only contained a single function:
class CheckerRegistry;
void registerBuiltinCheckers(CheckerRegistry ®istry);
it had to re purposed, as CheckerRegistry is no longer available to
clangStaticAnalyzerCheckers. It was renamed to BuiltinCheckerRegistration.h,
which actually describes it a lot better -- it does not contain the registration
functions for checkers, but only those generated by the tblgen files.
Differential Revision: https://reviews.llvm.org/D54436
llvm-svn: 349275
2018-12-16 00:23:51 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-28 09:26:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
|
2011-02-28 09:26:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
|
2010-09-10 08:44:44 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-03-23 05:06:03 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2012-12-02 01:12:56 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-09-10 08:44:44 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2010-09-10 08:44:44 +08:00
|
|
|
|
2014-04-22 11:17:02 +08:00
|
|
|
#define DEBUG_TYPE "StatsChecker"
|
|
|
|
|
2012-03-23 05:06:03 +08:00
|
|
|
STATISTIC(NumBlocks,
|
|
|
|
"The # of blocks in top level functions");
|
|
|
|
STATISTIC(NumBlocksUnreachable,
|
|
|
|
"The # of unreachable blocks in analyzing top level functions");
|
|
|
|
|
2010-09-10 08:44:44 +08:00
|
|
|
namespace {
|
2011-03-01 09:16:21 +08:00
|
|
|
class AnalyzerStatsChecker : public Checker<check::EndAnalysis> {
|
2010-09-10 08:44:44 +08:00
|
|
|
public:
|
2011-02-28 09:26:50 +08:00
|
|
|
void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const;
|
2010-09-10 08:44:44 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-02-28 09:26:50 +08:00
|
|
|
void AnalyzerStatsChecker::checkEndAnalysis(ExplodedGraph &G,
|
2010-09-10 08:44:44 +08:00
|
|
|
BugReporter &B,
|
2011-02-28 09:26:50 +08:00
|
|
|
ExprEngine &Eng) const {
|
2014-05-27 10:45:47 +08:00
|
|
|
const CFG *C = nullptr;
|
2010-09-10 08:44:44 +08:00
|
|
|
const SourceManager &SM = B.getSourceManager();
|
2016-01-30 09:27:06 +08:00
|
|
|
llvm::SmallPtrSet<const CFGBlock*, 32> reachable;
|
2010-09-10 08:44:44 +08:00
|
|
|
|
2012-03-23 05:05:57 +08:00
|
|
|
// Root node should have the location context of the top most function.
|
|
|
|
const ExplodedNode *GraphRoot = *G.roots_begin();
|
2012-03-29 01:05:46 +08:00
|
|
|
const LocationContext *LC = GraphRoot->getLocation().getLocationContext();
|
|
|
|
|
|
|
|
const Decl *D = LC->getDecl();
|
2012-03-23 05:05:57 +08:00
|
|
|
|
|
|
|
// Iterate over the exploded graph.
|
2010-09-10 08:44:44 +08:00
|
|
|
for (ExplodedGraph::node_iterator I = G.nodes_begin();
|
|
|
|
I != G.nodes_end(); ++I) {
|
|
|
|
const ProgramPoint &P = I->getLocation();
|
2012-03-23 05:05:57 +08:00
|
|
|
|
2012-03-29 01:05:46 +08:00
|
|
|
// Only check the coverage in the top level function (optimization).
|
|
|
|
if (D != P.getLocationContext()->getDecl())
|
2012-03-23 05:05:57 +08:00
|
|
|
continue;
|
2010-09-10 08:44:44 +08:00
|
|
|
|
2013-02-22 06:23:56 +08:00
|
|
|
if (Optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) {
|
2010-09-30 07:48:34 +08:00
|
|
|
const CFGBlock *CB = BE->getBlock();
|
2010-09-10 08:44:44 +08:00
|
|
|
reachable.insert(CB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-29 01:05:46 +08:00
|
|
|
// Get the CFG and the Decl of this block.
|
2010-09-10 08:44:44 +08:00
|
|
|
C = LC->getCFG();
|
|
|
|
|
|
|
|
unsigned total = 0, unreachable = 0;
|
|
|
|
|
|
|
|
// Find CFGBlocks that were not covered by any node
|
|
|
|
for (CFG::const_iterator I = C->begin(); I != C->end(); ++I) {
|
|
|
|
const CFGBlock *CB = *I;
|
|
|
|
++total;
|
|
|
|
// Check if the block is unreachable
|
|
|
|
if (!reachable.count(CB)) {
|
|
|
|
++unreachable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We never 'reach' the entry block, so correct the unreachable count
|
|
|
|
unreachable--;
|
2012-03-23 05:05:57 +08:00
|
|
|
// There is no BlockEntrance corresponding to the exit block as well, so
|
|
|
|
// assume it is reached as well.
|
|
|
|
unreachable--;
|
2010-09-10 08:44:44 +08:00
|
|
|
|
|
|
|
// Generate the warning string
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> buf;
|
2010-09-10 08:44:44 +08:00
|
|
|
llvm::raw_svector_ostream output(buf);
|
|
|
|
PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
|
2012-03-28 04:02:44 +08:00
|
|
|
if (!Loc.isValid())
|
|
|
|
return;
|
2010-09-10 08:44:44 +08:00
|
|
|
|
2012-03-28 04:02:44 +08:00
|
|
|
if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
|
|
|
|
const NamedDecl *ND = cast<NamedDecl>(D);
|
|
|
|
output << *ND;
|
|
|
|
}
|
|
|
|
else if (isa<BlockDecl>(D)) {
|
|
|
|
output << "block(line:" << Loc.getLine() << ":col:" << Loc.getColumn();
|
2010-09-10 08:44:44 +08:00
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-03-23 05:06:03 +08:00
|
|
|
NumBlocksUnreachable += unreachable;
|
|
|
|
NumBlocks += total;
|
2012-03-28 04:02:44 +08:00
|
|
|
std::string NameOfRootFunction = output.str();
|
2012-03-23 05:06:03 +08:00
|
|
|
|
2010-09-10 08:44:44 +08:00
|
|
|
output << " -> Total CFGBlocks: " << total << " | Unreachable CFGBlocks: "
|
2011-04-02 10:56:23 +08:00
|
|
|
<< unreachable << " | Exhausted Block: "
|
|
|
|
<< (Eng.wasBlocksExhausted() ? "yes" : "no")
|
2010-09-10 08:44:44 +08:00
|
|
|
<< " | Empty WorkList: "
|
2010-09-23 05:07:51 +08:00
|
|
|
<< (Eng.hasEmptyWorkList() ? "yes" : "no");
|
2010-09-10 08:44:44 +08:00
|
|
|
|
2014-02-12 05:49:21 +08:00
|
|
|
B.EmitBasicReport(D, this, "Analyzer Statistics", "Internal Statistics",
|
2012-04-05 02:11:35 +08:00
|
|
|
output.str(), PathDiagnosticLocation(D, SM));
|
2010-09-30 07:48:34 +08:00
|
|
|
|
2012-03-28 04:02:44 +08:00
|
|
|
// Emit warning for each block we bailed out on.
|
2011-04-02 10:56:23 +08:00
|
|
|
typedef CoreEngine::BlocksExhausted::const_iterator ExhaustedIterator;
|
2010-12-23 02:53:44 +08:00
|
|
|
const CoreEngine &CE = Eng.getCoreEngine();
|
2011-04-02 10:56:23 +08:00
|
|
|
for (ExhaustedIterator I = CE.blocks_exhausted_begin(),
|
|
|
|
E = CE.blocks_exhausted_end(); I != E; ++I) {
|
2010-09-30 07:48:34 +08:00
|
|
|
const BlockEdge &BE = I->first;
|
|
|
|
const CFGBlock *Exit = BE.getDst();
|
2018-02-22 00:06:56 +08:00
|
|
|
if (Exit->empty())
|
|
|
|
continue;
|
2010-09-30 07:48:34 +08:00
|
|
|
const CFGElement &CE = Exit->front();
|
2013-02-23 08:29:34 +08:00
|
|
|
if (Optional<CFGStmt> CS = CE.getAs<CFGStmt>()) {
|
2012-03-28 04:02:44 +08:00
|
|
|
SmallString<128> bufI;
|
|
|
|
llvm::raw_svector_ostream outputI(bufI);
|
|
|
|
outputI << "(" << NameOfRootFunction << ")" <<
|
|
|
|
": The analyzer generated a sink at this point";
|
2013-02-23 08:29:34 +08:00
|
|
|
B.EmitBasicReport(
|
2014-02-12 05:49:21 +08:00
|
|
|
D, this, "Sink Point", "Internal Statistics", outputI.str(),
|
2013-02-23 08:29:34 +08:00
|
|
|
PathDiagnosticLocation::createBegin(CS->getStmt(), SM, LC));
|
2012-03-28 04:02:44 +08:00
|
|
|
}
|
2010-09-30 07:48:34 +08:00
|
|
|
}
|
2010-09-10 08:44:44 +08:00
|
|
|
}
|
2011-02-28 09:26:50 +08:00
|
|
|
|
|
|
|
void ento::registerAnalyzerStatsChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<AnalyzerStatsChecker>();
|
|
|
|
}
|