2008-07-02 08:03:09 +08:00
|
|
|
//===--- AnalysisConsumer.cpp - ASTConsumer for running Analyses ----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// "Meta" ASTConsumer for running different source analyses.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-03-09 07:16:38 +08:00
|
|
|
#define DEBUG_TYPE "AnalysisConsumer"
|
|
|
|
|
2011-02-15 02:13:11 +08:00
|
|
|
#include "AnalysisConsumer.h"
|
2008-07-02 08:03:09 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
2009-12-16 13:29:59 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2008-07-02 08:03:09 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2009-11-05 10:41:58 +08:00
|
|
|
#include "clang/AST/ParentMap.h"
|
2012-03-14 03:32:00 +08:00
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
2009-11-05 10:41:58 +08:00
|
|
|
#include "clang/Analysis/CFG.h"
|
2012-03-09 07:16:38 +08:00
|
|
|
#include "clang/Analysis/CallGraph.h"
|
2011-02-16 00:54:12 +08:00
|
|
|
#include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h"
|
2011-02-15 02:13:31 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
2010-12-24 03:38:26 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/LocalCheckers.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
|
2011-09-27 09:43:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
|
2010-12-23 02:52:56 +08:00
|
|
|
|
2009-11-05 10:41:58 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2010-06-16 01:48:49 +08:00
|
|
|
#include "clang/Frontend/AnalyzerOptions.h"
|
2009-11-05 10:41:58 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2008-08-28 06:31:43 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/Program.h"
|
2012-03-06 04:53:59 +08:00
|
|
|
#include "llvm/Support/Timer.h"
|
2012-03-09 07:16:38 +08:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2009-08-23 20:08:50 +08:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
2012-02-28 05:33:16 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2008-07-03 12:29:21 +08:00
|
|
|
|
2012-03-14 03:32:13 +08:00
|
|
|
#include <queue>
|
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2012-03-09 07:16:38 +08:00
|
|
|
using llvm::SmallPtrSet;
|
2008-07-02 08:03:09 +08:00
|
|
|
|
2009-08-06 09:32:16 +08:00
|
|
|
static ExplodedNode::Auditor* CreateUbiViz();
|
2008-12-22 09:52:37 +08:00
|
|
|
|
2012-03-10 05:14:01 +08:00
|
|
|
STATISTIC(NumFunctionTopLevel, "The # of functions at top level.");
|
2012-03-09 07:16:38 +08:00
|
|
|
STATISTIC(NumFunctionsAnalyzed, "The # of functions analysed (as top level).");
|
|
|
|
|
2009-07-28 06:13:39 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2011-09-27 09:43:33 +08:00
|
|
|
// Special PathDiagnosticConsumers.
|
2009-07-28 06:13:39 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-09-26 08:51:36 +08:00
|
|
|
static PathDiagnosticConsumer*
|
|
|
|
createPlistHTMLDiagnosticConsumer(const std::string& prefix,
|
2009-11-05 10:41:58 +08:00
|
|
|
const Preprocessor &PP) {
|
2011-09-26 08:51:36 +08:00
|
|
|
PathDiagnosticConsumer *PD =
|
|
|
|
createHTMLDiagnosticConsumer(llvm::sys::path::parent_path(prefix), PP);
|
|
|
|
return createPlistDiagnosticConsumer(prefix, PP, PD);
|
2009-07-28 06:13:39 +08:00
|
|
|
}
|
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AnalysisConsumer declaration.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2012-03-14 03:32:00 +08:00
|
|
|
class AnalysisConsumer : public ASTConsumer,
|
|
|
|
public RecursiveASTVisitor<AnalysisConsumer> {
|
|
|
|
enum AnalysisMode {
|
|
|
|
ANALYSIS_SYNTAX,
|
|
|
|
ANALYSIS_PATH,
|
|
|
|
ANALYSIS_ALL
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Mode of the analyzes while recursively visiting Decls.
|
|
|
|
AnalysisMode RecVisitorMode;
|
|
|
|
/// Bug Reporter to use while recursively visiting Decls.
|
|
|
|
BugReporter *RecVisitorBR;
|
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
public:
|
2011-08-13 07:37:29 +08:00
|
|
|
ASTContext *Ctx;
|
2009-11-11 14:28:42 +08:00
|
|
|
const Preprocessor &PP;
|
|
|
|
const std::string OutDir;
|
|
|
|
AnalyzerOptions Opts;
|
2011-08-17 05:24:21 +08:00
|
|
|
ArrayRef<std::string> Plugins;
|
2009-08-03 11:27:37 +08:00
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
// PD is owned by AnalysisManager.
|
2011-09-26 08:51:36 +08:00
|
|
|
PathDiagnosticConsumer *PD;
|
2009-08-03 11:27:37 +08:00
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
StoreManagerCreator CreateStoreMgr;
|
|
|
|
ConstraintManagerCreator CreateConstraintMgr;
|
2008-07-02 08:03:09 +08:00
|
|
|
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<CheckerManager> checkerMgr;
|
|
|
|
OwningPtr<AnalysisManager> Mgr;
|
2009-08-03 11:13:46 +08:00
|
|
|
|
2012-03-06 04:53:59 +08:00
|
|
|
/// Time the analyzes time of each translation unit.
|
|
|
|
static llvm::Timer* TUTotalTimer;
|
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
AnalysisConsumer(const Preprocessor& pp,
|
|
|
|
const std::string& outdir,
|
2011-08-17 05:24:21 +08:00
|
|
|
const AnalyzerOptions& opts,
|
|
|
|
ArrayRef<std::string> plugins)
|
2012-03-14 03:32:00 +08:00
|
|
|
: RecVisitorMode(ANALYSIS_ALL), RecVisitorBR(0),
|
|
|
|
Ctx(0), PP(pp), OutDir(outdir), Opts(opts), Plugins(plugins), PD(0) {
|
2009-11-11 14:28:42 +08:00
|
|
|
DigestAnalyzerOptions();
|
2012-03-06 04:53:59 +08:00
|
|
|
if (Opts.PrintStats) {
|
|
|
|
llvm::EnableStatistics();
|
|
|
|
TUTotalTimer = new llvm::Timer("Analyzer Total Time");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~AnalysisConsumer() {
|
|
|
|
if (Opts.PrintStats)
|
|
|
|
delete TUTotalTimer;
|
2009-11-11 14:28:42 +08:00
|
|
|
}
|
2009-07-30 17:11:52 +08:00
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
void DigestAnalyzerOptions() {
|
2011-09-26 08:51:36 +08:00
|
|
|
// Create the PathDiagnosticConsumer.
|
2009-11-11 14:28:42 +08:00
|
|
|
if (!OutDir.empty()) {
|
|
|
|
switch (Opts.AnalysisDiagOpt) {
|
|
|
|
default:
|
2009-07-30 17:11:52 +08:00
|
|
|
#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE) \
|
2009-11-11 14:28:42 +08:00
|
|
|
case PD_##NAME: PD = CREATEFN(OutDir, PP); break;
|
2009-07-30 17:11:52 +08:00
|
|
|
#include "clang/Frontend/Analyses.def"
|
|
|
|
}
|
2010-12-03 09:17:19 +08:00
|
|
|
} else if (Opts.AnalysisDiagOpt == PD_TEXT) {
|
|
|
|
// Create the text client even without a specified output file since
|
|
|
|
// it just uses diagnostic notes.
|
2011-09-26 08:51:36 +08:00
|
|
|
PD = createTextPathDiagnosticConsumer("", PP);
|
2009-11-11 14:28:42 +08:00
|
|
|
}
|
2009-07-30 17:11:52 +08:00
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
// Create the analyzer component creators.
|
2011-02-15 02:13:17 +08:00
|
|
|
switch (Opts.AnalysisStoreOpt) {
|
|
|
|
default:
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Unknown store manager.");
|
2009-07-30 17:11:52 +08:00
|
|
|
#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \
|
2011-02-15 02:13:17 +08:00
|
|
|
case NAME##Model: CreateStoreMgr = CREATEFN; break;
|
2009-07-30 17:11:52 +08:00
|
|
|
#include "clang/Frontend/Analyses.def"
|
2009-11-11 14:28:42 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-15 02:13:17 +08:00
|
|
|
switch (Opts.AnalysisConstraintsOpt) {
|
|
|
|
default:
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Unknown store manager.");
|
2009-07-30 17:11:52 +08:00
|
|
|
#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \
|
2011-02-15 02:13:17 +08:00
|
|
|
case NAME##Model: CreateConstraintMgr = CREATEFN; break;
|
2009-07-30 17:11:52 +08:00
|
|
|
#include "clang/Frontend/Analyses.def"
|
|
|
|
}
|
2009-11-11 14:28:42 +08:00
|
|
|
}
|
2010-02-15 03:08:51 +08:00
|
|
|
|
2012-03-14 03:32:00 +08:00
|
|
|
void DisplayFunction(const Decl *D, AnalysisMode Mode) {
|
2010-06-26 04:59:24 +08:00
|
|
|
if (!Opts.AnalyzerDisplayProgress)
|
2009-11-11 14:28:42 +08:00
|
|
|
return;
|
2010-02-15 03:08:51 +08:00
|
|
|
|
2009-12-08 06:06:12 +08:00
|
|
|
SourceManager &SM = Mgr->getASTContext().getSourceManager();
|
|
|
|
PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
|
2010-11-12 15:15:47 +08:00
|
|
|
if (Loc.isValid()) {
|
2012-03-14 03:32:00 +08:00
|
|
|
llvm::errs() << "ANALYZE";
|
|
|
|
switch (Mode) {
|
|
|
|
case ANALYSIS_SYNTAX: llvm::errs() << "(Syntax)"; break;
|
|
|
|
case ANALYSIS_PATH: llvm::errs() << "(Path Sensitive)"; break;
|
|
|
|
case ANALYSIS_ALL: break;
|
|
|
|
};
|
|
|
|
llvm::errs() << ": " << Loc.getFilename();
|
2010-11-12 15:15:47 +08:00
|
|
|
if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
|
|
|
|
const NamedDecl *ND = cast<NamedDecl>(D);
|
2011-10-15 02:45:37 +08:00
|
|
|
llvm::errs() << ' ' << *ND << '\n';
|
2010-11-12 15:15:47 +08:00
|
|
|
}
|
|
|
|
else if (isa<BlockDecl>(D)) {
|
|
|
|
llvm::errs() << ' ' << "block(line:" << Loc.getLine() << ",col:"
|
|
|
|
<< Loc.getColumn() << '\n';
|
|
|
|
}
|
|
|
|
else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
|
|
|
|
Selector S = MD->getSelector();
|
|
|
|
llvm::errs() << ' ' << S.getAsString();
|
|
|
|
}
|
2010-10-23 06:08:29 +08:00
|
|
|
}
|
2009-11-11 14:28:42 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
virtual void Initialize(ASTContext &Context) {
|
|
|
|
Ctx = &Context;
|
2012-03-11 15:00:24 +08:00
|
|
|
checkerMgr.reset(createCheckerManager(Opts, PP.getLangOpts(), Plugins,
|
2011-08-17 05:24:21 +08:00
|
|
|
PP.getDiagnostics()));
|
2009-11-11 14:28:42 +08:00
|
|
|
Mgr.reset(new AnalysisManager(*Ctx, PP.getDiagnostics(),
|
2012-03-11 15:00:24 +08:00
|
|
|
PP.getLangOpts(), PD,
|
2009-11-11 14:28:42 +08:00
|
|
|
CreateStoreMgr, CreateConstraintMgr,
|
2011-02-15 02:13:31 +08:00
|
|
|
checkerMgr.get(),
|
2010-07-19 09:31:21 +08:00
|
|
|
/* Indexer */ 0,
|
2010-05-18 08:28:37 +08:00
|
|
|
Opts.MaxNodes, Opts.MaxLoop,
|
2009-11-11 14:28:42 +08:00
|
|
|
Opts.VisualizeEGDot, Opts.VisualizeEGUbi,
|
2011-09-30 10:03:00 +08:00
|
|
|
Opts.AnalysisPurgeOpt, Opts.EagerlyAssume,
|
2012-03-09 07:16:35 +08:00
|
|
|
Opts.TrimGraph,
|
2010-09-30 15:41:24 +08:00
|
|
|
Opts.UnoptimizedCFG, Opts.CFGAddImplicitDtors,
|
2011-02-09 09:27:33 +08:00
|
|
|
Opts.CFGAddInitializers,
|
2012-03-03 03:05:03 +08:00
|
|
|
Opts.EagerlyTrimEGraph,
|
2012-03-09 07:16:35 +08:00
|
|
|
Opts.IPAMode,
|
2012-03-03 03:05:03 +08:00
|
|
|
Opts.InlineMaxStackDepth,
|
2012-03-09 07:16:35 +08:00
|
|
|
Opts.InlineMaxFunctionSize,
|
|
|
|
Opts.InliningMode));
|
2009-11-11 14:28:42 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
virtual void HandleTranslationUnit(ASTContext &C);
|
2011-01-21 01:09:48 +08:00
|
|
|
|
2012-03-14 03:32:08 +08:00
|
|
|
/// \brief Build the call graph for the TU and use it to define the order
|
2012-03-14 03:32:00 +08:00
|
|
|
/// in which the functions should be visited.
|
2012-03-14 03:32:08 +08:00
|
|
|
void HandleDeclsGallGraph(TranslationUnitDecl *TU);
|
2012-03-14 03:32:00 +08:00
|
|
|
|
|
|
|
/// \brief Run analyzes(syntax or path sensitive) on the given function.
|
|
|
|
/// \param Mode - determines if we are requesting syntax only or path
|
|
|
|
/// sensitive only analysis.
|
|
|
|
/// \param VisitedCallees - The output parameter, which is populated with the
|
|
|
|
/// set of functions which should be considered analyzed after analyzing the
|
|
|
|
/// given root function.
|
|
|
|
void HandleCode(Decl *D, AnalysisMode Mode, SetOfDecls *VisitedCallees = 0);
|
|
|
|
|
|
|
|
/// \brief Check if we should skip (not analyze) the given function.
|
2012-03-14 03:31:54 +08:00
|
|
|
bool skipFunction(Decl *D);
|
2012-03-14 03:32:00 +08:00
|
|
|
|
2012-03-10 05:14:01 +08:00
|
|
|
void RunPathSensitiveChecks(Decl *D, SetOfDecls *VisitedCallees);
|
|
|
|
void ActionExprEngine(Decl *D, bool ObjCGCEnabled, SetOfDecls *VisitedCallees);
|
2012-03-14 03:32:00 +08:00
|
|
|
|
|
|
|
/// Visitors for the RecursiveASTVisitor.
|
|
|
|
|
|
|
|
/// Handle callbacks for arbitrary Decls.
|
|
|
|
bool VisitDecl(Decl *D) {
|
|
|
|
checkerMgr->runCheckersOnASTDecl(D, *Mgr, *RecVisitorBR);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitFunctionDecl(FunctionDecl *FD) {
|
|
|
|
IdentifierInfo *II = FD->getIdentifier();
|
|
|
|
if (II && II->getName().startswith("__inline"))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// We skip function template definitions, as their semantics is
|
|
|
|
// only determined when they are instantiated.
|
|
|
|
if (FD->isThisDeclarationADefinition() &&
|
|
|
|
!FD->isDependentContext()) {
|
|
|
|
HandleCode(FD, RecVisitorMode);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitObjCMethodDecl(ObjCMethodDecl *MD) {
|
|
|
|
checkerMgr->runCheckersOnASTDecl(MD, *Mgr, *RecVisitorBR);
|
|
|
|
if (MD->isThisDeclarationADefinition())
|
|
|
|
HandleCode(MD, RecVisitorMode);
|
|
|
|
return true;
|
|
|
|
}
|
2009-11-11 14:28:42 +08:00
|
|
|
};
|
2008-07-02 08:03:09 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2012-03-14 03:32:00 +08:00
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AnalysisConsumer implementation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-03-06 04:53:59 +08:00
|
|
|
llvm::Timer* AnalysisConsumer::TUTotalTimer = 0;
|
2008-07-02 08:03:09 +08:00
|
|
|
|
2012-03-14 03:32:08 +08:00
|
|
|
void AnalysisConsumer::HandleDeclsGallGraph(TranslationUnitDecl *TU) {
|
2012-03-09 07:16:38 +08:00
|
|
|
// Otherwise, use the Callgraph to derive the order.
|
|
|
|
// Build the Call Graph.
|
|
|
|
CallGraph CG;
|
2012-03-14 03:32:08 +08:00
|
|
|
CG.addToCallGraph(TU);
|
2012-03-09 07:16:38 +08:00
|
|
|
|
|
|
|
// Find the top level nodes - children of root + the unreachable (parentless)
|
|
|
|
// nodes.
|
2012-03-10 05:14:01 +08:00
|
|
|
llvm::SmallVector<CallGraphNode*, 24> TopLevelFunctions;
|
2012-03-14 03:32:19 +08:00
|
|
|
for (CallGraph::nodes_iterator TI = CG.parentless_begin(),
|
|
|
|
TE = CG.parentless_end(); TI != TE; ++TI) {
|
|
|
|
TopLevelFunctions.push_back(*TI);
|
|
|
|
NumFunctionTopLevel++;
|
|
|
|
}
|
2012-03-09 07:16:38 +08:00
|
|
|
CallGraphNode *Entry = CG.getRoot();
|
|
|
|
for (CallGraphNode::iterator I = Entry->begin(),
|
2012-03-10 05:14:01 +08:00
|
|
|
E = Entry->end(); I != E; ++I) {
|
2012-03-09 07:16:38 +08:00
|
|
|
TopLevelFunctions.push_back(*I);
|
2012-03-10 05:14:01 +08:00
|
|
|
NumFunctionTopLevel++;
|
|
|
|
}
|
2012-03-09 07:16:38 +08:00
|
|
|
|
2012-03-14 03:32:19 +08:00
|
|
|
// Make sure the nodes are sorted in order reverse of their definition in the
|
|
|
|
// translation unit. This step is very important for performance. It ensures
|
|
|
|
// that we analyze the root functions before the externally available
|
|
|
|
// subroutines.
|
2012-03-14 03:32:13 +08:00
|
|
|
std::queue<CallGraphNode*> BFSQueue;
|
2012-03-14 03:32:19 +08:00
|
|
|
for (llvm::SmallVector<CallGraphNode*, 24>::reverse_iterator
|
|
|
|
TI = TopLevelFunctions.rbegin(), TE = TopLevelFunctions.rend();
|
2012-03-14 03:32:13 +08:00
|
|
|
TI != TE; ++TI)
|
|
|
|
BFSQueue.push(*TI);
|
2012-03-09 07:16:38 +08:00
|
|
|
|
2012-03-14 03:32:13 +08:00
|
|
|
// BFS over all of the functions, while skipping the ones inlined into
|
|
|
|
// the previously processed functions. Use external Visited set, which is
|
2012-03-09 07:16:38 +08:00
|
|
|
// also modified when we inline a function.
|
|
|
|
SmallPtrSet<CallGraphNode*,24> Visited;
|
2012-03-14 03:32:13 +08:00
|
|
|
while(!BFSQueue.empty()) {
|
|
|
|
CallGraphNode *N = BFSQueue.front();
|
|
|
|
BFSQueue.pop();
|
|
|
|
|
|
|
|
// Skip the functions which have been processed already or previously
|
|
|
|
// inlined.
|
|
|
|
if (Visited.count(N))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Analyze the function.
|
|
|
|
SetOfDecls VisitedCallees;
|
|
|
|
Decl *D = N->getDecl();
|
|
|
|
assert(D);
|
|
|
|
HandleCode(D, ANALYSIS_PATH,
|
|
|
|
(Mgr->InliningMode == All ? 0 : &VisitedCallees));
|
|
|
|
|
|
|
|
// Add the visited callees to the global visited set.
|
|
|
|
for (SetOfDecls::const_iterator I = VisitedCallees.begin(),
|
|
|
|
E = VisitedCallees.end(); I != E; ++I) {
|
|
|
|
CallGraphNode *VN = CG.getNode(*I);
|
|
|
|
if (VN)
|
|
|
|
Visited.insert(VN);
|
|
|
|
}
|
|
|
|
Visited.insert(N);
|
|
|
|
|
|
|
|
// Push the children into the queue.
|
|
|
|
for (CallGraphNode::const_iterator CI = N->begin(),
|
|
|
|
CE = N->end(); CI != CE; ++CI) {
|
|
|
|
BFSQueue.push(*CI);
|
2012-03-09 07:16:38 +08:00
|
|
|
}
|
|
|
|
}
|
2011-08-28 05:28:09 +08:00
|
|
|
}
|
|
|
|
|
2011-01-21 01:09:48 +08:00
|
|
|
void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
|
2012-03-14 03:32:00 +08:00
|
|
|
// Don't run the actions if an error has occurred with parsing the file.
|
|
|
|
DiagnosticsEngine &Diags = PP.getDiagnostics();
|
|
|
|
if (Diags.hasErrorOccurred() || Diags.hasFatalErrorOccurred())
|
|
|
|
return;
|
|
|
|
|
2012-01-08 00:49:46 +08:00
|
|
|
{
|
2012-03-06 04:53:59 +08:00
|
|
|
if (TUTotalTimer) TUTotalTimer->startTimer();
|
|
|
|
|
2012-01-08 00:49:46 +08:00
|
|
|
// Introduce a scope to destroy BR before Mgr.
|
|
|
|
BugReporter BR(*Mgr);
|
|
|
|
TranslationUnitDecl *TU = C.getTranslationUnitDecl();
|
|
|
|
checkerMgr->runCheckersOnASTDecl(TU, *Mgr, BR);
|
2012-03-14 03:32:00 +08:00
|
|
|
|
|
|
|
// Run the AST-only checks using the order in which functions are defined.
|
|
|
|
// If inlining is not turned on, use the simplest function order for path
|
|
|
|
// sensitive analyzes as well.
|
|
|
|
RecVisitorMode = (Mgr->shouldInlineCall() ? ANALYSIS_SYNTAX : ANALYSIS_ALL);
|
|
|
|
RecVisitorBR = &BR;
|
|
|
|
TraverseDecl(TU);
|
|
|
|
|
|
|
|
if (Mgr->shouldInlineCall())
|
2012-03-14 03:32:08 +08:00
|
|
|
HandleDeclsGallGraph(TU);
|
2009-09-10 13:44:00 +08:00
|
|
|
|
2012-01-08 00:49:46 +08:00
|
|
|
// After all decls handled, run checkers on the entire TranslationUnit.
|
|
|
|
checkerMgr->runCheckersOnEndOfTranslationUnit(TU, *Mgr, BR);
|
2012-03-14 03:32:00 +08:00
|
|
|
|
|
|
|
RecVisitorBR = 0;
|
2012-01-08 00:49:46 +08:00
|
|
|
}
|
2011-05-05 11:41:17 +08:00
|
|
|
|
2011-09-26 08:51:36 +08:00
|
|
|
// Explicitly destroy the PathDiagnosticConsumer. This will flush its output.
|
2009-08-02 13:43:14 +08:00
|
|
|
// FIXME: This should be replaced with something that doesn't rely on
|
2011-09-26 08:51:36 +08:00
|
|
|
// side-effects in PathDiagnosticConsumer's destructor. This is required when
|
2009-12-15 17:32:42 +08:00
|
|
|
// used with option -disable-free.
|
2009-08-03 11:27:37 +08:00
|
|
|
Mgr.reset(NULL);
|
2012-03-06 04:53:59 +08:00
|
|
|
|
|
|
|
if (TUTotalTimer) TUTotalTimer->stopTimer();
|
2008-07-03 12:29:21 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
static void FindBlocks(DeclContext *D, SmallVectorImpl<Decl*> &WL) {
|
2009-12-08 06:06:12 +08:00
|
|
|
if (BlockDecl *BD = dyn_cast<BlockDecl>(D))
|
|
|
|
WL.push_back(BD);
|
2010-02-15 03:08:51 +08:00
|
|
|
|
2009-12-08 06:06:12 +08:00
|
|
|
for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end();
|
|
|
|
I!=E; ++I)
|
|
|
|
if (DeclContext *DC = dyn_cast<DeclContext>(*I))
|
|
|
|
FindBlocks(DC, WL);
|
|
|
|
}
|
|
|
|
|
2012-03-09 07:16:38 +08:00
|
|
|
static std::string getFunctionName(const Decl *D) {
|
|
|
|
if (const ObjCMethodDecl *ID = dyn_cast<ObjCMethodDecl>(D)) {
|
|
|
|
return ID->getSelector().getAsString();
|
|
|
|
}
|
|
|
|
if (const FunctionDecl *ND = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
IdentifierInfo *II = ND->getIdentifier();
|
|
|
|
if (II)
|
|
|
|
return II->getName();
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2012-03-14 03:31:54 +08:00
|
|
|
bool AnalysisConsumer::skipFunction(Decl *D) {
|
2012-03-09 07:16:38 +08:00
|
|
|
if (!Opts.AnalyzeSpecificFunction.empty() &&
|
|
|
|
getFunctionName(D) != Opts.AnalyzeSpecificFunction)
|
2012-03-14 03:31:54 +08:00
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
// Don't run the actions on declarations in header files unless
|
|
|
|
// otherwise specified.
|
2010-06-15 08:55:40 +08:00
|
|
|
SourceManager &SM = Ctx->getSourceManager();
|
2011-07-26 00:49:02 +08:00
|
|
|
SourceLocation SL = SM.getExpansionLoc(D->getLocation());
|
2010-06-15 08:55:40 +08:00
|
|
|
if (!Opts.AnalyzeAll && !SM.isFromMainFile(SL))
|
2012-03-14 03:31:54 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-14 03:32:00 +08:00
|
|
|
void AnalysisConsumer::HandleCode(Decl *D, AnalysisMode Mode,
|
|
|
|
SetOfDecls *VisitedCallees) {
|
2012-03-14 03:31:54 +08:00
|
|
|
if (skipFunction(D))
|
2009-09-09 23:08:12 +08:00
|
|
|
return;
|
2008-07-02 08:03:09 +08:00
|
|
|
|
2012-03-14 03:32:00 +08:00
|
|
|
DisplayFunction(D, Mode);
|
2012-03-14 03:31:54 +08:00
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
// Clear the AnalysisManager of old AnalysisDeclContexts.
|
2009-10-21 05:39:41 +08:00
|
|
|
Mgr->ClearContexts();
|
2010-02-15 03:08:51 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// Dispatch on the actions.
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<Decl*, 10> WL;
|
2009-12-08 06:06:12 +08:00
|
|
|
WL.push_back(D);
|
2010-02-15 03:08:51 +08:00
|
|
|
|
2010-07-07 19:31:19 +08:00
|
|
|
if (D->hasBody() && Opts.AnalyzeNestedBlocks)
|
2009-12-08 06:06:12 +08:00
|
|
|
FindBlocks(cast<DeclContext>(D), WL);
|
2010-02-15 03:08:51 +08:00
|
|
|
|
2011-02-18 05:39:24 +08:00
|
|
|
BugReporter BR(*Mgr);
|
2011-07-23 18:55:15 +08:00
|
|
|
for (SmallVectorImpl<Decl*>::iterator WI=WL.begin(), WE=WL.end();
|
2011-02-18 05:39:24 +08:00
|
|
|
WI != WE; ++WI)
|
2011-03-01 03:49:17 +08:00
|
|
|
if ((*WI)->hasBody()) {
|
2012-03-14 03:32:00 +08:00
|
|
|
if (Mode != ANALYSIS_PATH)
|
|
|
|
checkerMgr->runCheckersOnASTBody(*WI, *Mgr, BR);
|
|
|
|
if (Mode != ANALYSIS_SYNTAX && checkerMgr->hasPathSensitiveCheckers())
|
2012-03-10 05:14:01 +08:00
|
|
|
RunPathSensitiveChecks(*WI, VisitedCallees);
|
2011-03-01 03:49:17 +08:00
|
|
|
}
|
2012-03-09 07:16:38 +08:00
|
|
|
NumFunctionsAnalyzed++;
|
2008-07-02 08:03:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2011-03-01 03:49:17 +08:00
|
|
|
// Path-sensitive checking.
|
2008-07-02 08:03:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-03-10 05:14:01 +08:00
|
|
|
void AnalysisConsumer::ActionExprEngine(Decl *D, bool ObjCGCEnabled,
|
|
|
|
SetOfDecls *VisitedCallees) {
|
2011-10-08 06:21:02 +08:00
|
|
|
// Construct the analysis engine. First check if the CFG is valid.
|
2009-09-19 06:29:35 +08:00
|
|
|
// FIXME: Inter-procedural analysis will need to handle invalid CFGs.
|
2012-03-10 05:14:01 +08:00
|
|
|
if (!Mgr->getCFG(D))
|
2010-02-15 03:08:51 +08:00
|
|
|
return;
|
2012-03-10 05:14:01 +08:00
|
|
|
|
|
|
|
ExprEngine Eng(*Mgr, ObjCGCEnabled, VisitedCallees);
|
2010-02-15 03:08:51 +08:00
|
|
|
|
2008-08-28 06:31:43 +08:00
|
|
|
// Set the graph auditor.
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<ExplodedNode::Auditor> Auditor;
|
2012-03-10 05:14:01 +08:00
|
|
|
if (Mgr->shouldVisualizeUbigraph()) {
|
2008-08-28 06:31:43 +08:00
|
|
|
Auditor.reset(CreateUbiViz());
|
2009-08-06 09:32:16 +08:00
|
|
|
ExplodedNode::SetAuditor(Auditor.get());
|
2008-08-28 06:31:43 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-02 08:44:58 +08:00
|
|
|
// Execute the worklist algorithm.
|
2012-03-10 05:14:01 +08:00
|
|
|
Eng.ExecuteWorkList(Mgr->getAnalysisDeclContextManager().getStackFrame(D, 0),
|
|
|
|
Mgr->getMaxNodes());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-28 06:31:43 +08:00
|
|
|
// Release the auditor (if any) so that it doesn't monitor the graph
|
|
|
|
// created BugReporter.
|
2009-08-06 09:32:16 +08:00
|
|
|
ExplodedNode::SetAuditor(0);
|
2009-03-11 09:42:29 +08:00
|
|
|
|
2008-07-03 00:49:11 +08:00
|
|
|
// Visualize the exploded graph.
|
2012-03-10 05:14:01 +08:00
|
|
|
if (Mgr->shouldVisualizeGraphviz())
|
|
|
|
Eng.ViewGraph(Mgr->shouldTrimGraph());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-11 09:42:29 +08:00
|
|
|
// Display warnings.
|
|
|
|
Eng.getBugReporter().FlushReports();
|
2008-07-03 00:35:50 +08:00
|
|
|
}
|
|
|
|
|
2012-03-10 05:14:01 +08:00
|
|
|
void AnalysisConsumer::RunPathSensitiveChecks(Decl *D, SetOfDecls *Visited) {
|
2011-09-02 13:55:19 +08:00
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
switch (Mgr->getLangOpts().getGC()) {
|
2011-09-02 13:55:19 +08:00
|
|
|
case LangOptions::NonGC:
|
2012-03-10 05:14:01 +08:00
|
|
|
ActionExprEngine(D, false, Visited);
|
2011-09-02 13:55:19 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LangOptions::GCOnly:
|
2012-03-10 05:14:01 +08:00
|
|
|
ActionExprEngine(D, true, Visited);
|
2011-09-02 13:55:19 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LangOptions::HybridGC:
|
2012-03-10 05:14:01 +08:00
|
|
|
ActionExprEngine(D, false, Visited);
|
|
|
|
ActionExprEngine(D, true, Visited);
|
2011-09-02 13:55:19 +08:00
|
|
|
break;
|
|
|
|
}
|
2008-07-02 08:44:58 +08:00
|
|
|
}
|
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AnalysisConsumer creation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-12-23 15:20:52 +08:00
|
|
|
ASTConsumer* ento::CreateAnalysisConsumer(const Preprocessor& pp,
|
2011-08-17 05:24:21 +08:00
|
|
|
const std::string& outDir,
|
|
|
|
const AnalyzerOptions& opts,
|
|
|
|
ArrayRef<std::string> plugins) {
|
|
|
|
// Disable the effects of '-Werror' when using the AnalysisConsumer.
|
2009-11-05 10:41:58 +08:00
|
|
|
pp.getDiagnostics().setWarningsAsErrors(false);
|
2009-02-17 12:27:41 +08:00
|
|
|
|
2011-08-17 05:24:21 +08:00
|
|
|
return new AnalysisConsumer(pp, outDir, opts, plugins);
|
2008-07-02 08:03:09 +08:00
|
|
|
}
|
|
|
|
|
2008-08-28 06:31:43 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Ubigraph Visualization. FIXME: Move to separate file.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 09:32:16 +08:00
|
|
|
class UbigraphViz : public ExplodedNode::Auditor {
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<raw_ostream> Out;
|
2008-08-28 11:54:51 +08:00
|
|
|
llvm::sys::Path Dir, Filename;
|
2008-08-28 06:31:43 +08:00
|
|
|
unsigned Cntr;
|
|
|
|
|
|
|
|
typedef llvm::DenseMap<void*,unsigned> VMap;
|
|
|
|
VMap M;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-28 06:31:43 +08:00
|
|
|
public:
|
2011-08-13 07:37:29 +08:00
|
|
|
UbigraphViz(raw_ostream *out, llvm::sys::Path& dir,
|
2008-08-28 13:02:09 +08:00
|
|
|
llvm::sys::Path& filename);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-28 11:54:51 +08:00
|
|
|
~UbigraphViz();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
virtual void AddEdge(ExplodedNode *Src, ExplodedNode *Dst);
|
2008-08-28 06:31:43 +08:00
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-28 06:31:43 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2009-08-06 09:32:16 +08:00
|
|
|
static ExplodedNode::Auditor* CreateUbiViz() {
|
2008-08-28 06:31:43 +08:00
|
|
|
std::string ErrMsg;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-28 11:54:51 +08:00
|
|
|
llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
|
2008-08-28 06:31:43 +08:00
|
|
|
if (!ErrMsg.empty())
|
|
|
|
return 0;
|
|
|
|
|
2008-08-28 11:54:51 +08:00
|
|
|
llvm::sys::Path Filename = Dir;
|
2008-08-28 06:31:43 +08:00
|
|
|
Filename.appendComponent("llvm_ubi");
|
|
|
|
Filename.makeUnique(true,&ErrMsg);
|
|
|
|
|
|
|
|
if (!ErrMsg.empty())
|
|
|
|
return 0;
|
|
|
|
|
2009-08-24 06:45:33 +08:00
|
|
|
llvm::errs() << "Writing '" << Filename.str() << "'.\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<llvm::raw_fd_ostream> Stream;
|
2009-08-25 23:36:09 +08:00
|
|
|
Stream.reset(new llvm::raw_fd_ostream(Filename.c_str(), ErrMsg));
|
2008-08-28 06:31:43 +08:00
|
|
|
|
|
|
|
if (!ErrMsg.empty())
|
|
|
|
return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-28 11:54:51 +08:00
|
|
|
return new UbigraphViz(Stream.take(), Dir, Filename);
|
2008-08-28 06:31:43 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void UbigraphViz::AddEdge(ExplodedNode *Src, ExplodedNode *Dst) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-29 02:34:41 +08:00
|
|
|
assert (Src != Dst && "Self-edges are not allowed.");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-28 06:31:43 +08:00
|
|
|
// Lookup the Src. If it is a new node, it's a root.
|
|
|
|
VMap::iterator SrcI= M.find(Src);
|
|
|
|
unsigned SrcID;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-28 06:31:43 +08:00
|
|
|
if (SrcI == M.end()) {
|
|
|
|
M[Src] = SrcID = Cntr++;
|
|
|
|
*Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
SrcID = SrcI->second;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-28 06:31:43 +08:00
|
|
|
// Lookup the Dst.
|
|
|
|
VMap::iterator DstI= M.find(Dst);
|
|
|
|
unsigned DstID;
|
|
|
|
|
|
|
|
if (DstI == M.end()) {
|
|
|
|
M[Dst] = DstID = Cntr++;
|
|
|
|
*Out << "('vertex', " << DstID << ")\n";
|
|
|
|
}
|
2008-08-28 13:02:09 +08:00
|
|
|
else {
|
|
|
|
// We have hit DstID before. Change its style to reflect a cache hit.
|
2008-08-28 06:31:43 +08:00
|
|
|
DstID = DstI->second;
|
2008-08-28 13:02:09 +08:00
|
|
|
*Out << "('change_vertex_style', " << DstID << ", 1)\n";
|
|
|
|
}
|
2008-08-28 06:31:43 +08:00
|
|
|
|
|
|
|
// Add the edge.
|
2009-09-09 23:08:12 +08:00
|
|
|
*Out << "('edge', " << SrcID << ", " << DstID
|
2008-08-28 06:46:55 +08:00
|
|
|
<< ", ('arrow','true'), ('oriented', 'true'))\n";
|
2008-08-28 06:31:43 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
UbigraphViz::UbigraphViz(raw_ostream *out, llvm::sys::Path& dir,
|
2008-08-28 13:02:09 +08:00
|
|
|
llvm::sys::Path& filename)
|
|
|
|
: Out(out), Dir(dir), Filename(filename), Cntr(0) {
|
|
|
|
|
|
|
|
*Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n";
|
|
|
|
*Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66'),"
|
|
|
|
" ('size', '1.5'))\n";
|
|
|
|
}
|
|
|
|
|
2008-08-28 11:54:51 +08:00
|
|
|
UbigraphViz::~UbigraphViz() {
|
|
|
|
Out.reset(0);
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << "Running 'ubiviz' program... ";
|
2008-08-28 11:54:51 +08:00
|
|
|
std::string ErrMsg;
|
|
|
|
llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz");
|
|
|
|
std::vector<const char*> args;
|
|
|
|
args.push_back(Ubiviz.c_str());
|
|
|
|
args.push_back(Filename.c_str());
|
|
|
|
args.push_back(0);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-28 11:54:51 +08:00
|
|
|
if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) {
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << "Error viewing graph: " << ErrMsg << "\n";
|
2008-08-28 11:54:51 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-28 11:54:51 +08:00
|
|
|
// Delete the directory.
|
2009-09-09 23:08:12 +08:00
|
|
|
Dir.eraseFromDisk(true);
|
2008-08-29 11:45:59 +08:00
|
|
|
}
|