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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-05-20 05:10:40 +08:00
|
|
|
#include "clang/Frontend/AnalysisConsumer.h"
|
2008-07-02 08:03:09 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclObjC.h"
|
2009-11-05 10:41:58 +08:00
|
|
|
#include "clang/AST/ParentMap.h"
|
|
|
|
#include "clang/Analysis/Analyses/LiveVariables.h"
|
2008-07-02 08:03:09 +08:00
|
|
|
#include "clang/Analysis/Analyses/LiveVariables.h"
|
2009-11-05 10:41:58 +08:00
|
|
|
#include "clang/Analysis/CFG.h"
|
|
|
|
#include "clang/Analysis/LocalCheckers.h"
|
2008-07-02 08:03:09 +08:00
|
|
|
#include "clang/Analysis/PathDiagnostic.h"
|
2009-07-30 17:11:52 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/AnalysisManager.h"
|
2008-07-03 05:24:01 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/BugReporter.h"
|
2008-07-02 08:03:09 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/GRExprEngine.h"
|
2009-11-05 10:41:58 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Frontend/ManagerRegistry.h"
|
|
|
|
#include "clang/Frontend/PathDiagnosticClients.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2009-08-23 20:08:50 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-08-28 06:31:43 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/System/Path.h"
|
2008-08-28 11:54:51 +08:00
|
|
|
#include "llvm/System/Program.h"
|
2009-08-23 20:08:50 +08:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
2008-07-03 12:29:21 +08:00
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2009-08-06 09:32:16 +08:00
|
|
|
static ExplodedNode::Auditor* CreateUbiViz();
|
2008-12-22 09:52:37 +08:00
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Basic type definitions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
namespace {
|
2009-11-11 14:28:42 +08:00
|
|
|
class AnalysisConsumer;
|
|
|
|
typedef void (*CodeAction)(AnalysisConsumer &C, AnalysisManager &M, Decl *D);
|
2008-07-02 08:03:09 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2009-07-28 06:13:39 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Special PathDiagnosticClients.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static PathDiagnosticClient*
|
2009-11-05 10:41:58 +08:00
|
|
|
CreatePlistHTMLDiagnosticClient(const std::string& prefix,
|
|
|
|
const Preprocessor &PP) {
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::sys::Path F(prefix);
|
2009-11-05 10:09:23 +08:00
|
|
|
PathDiagnosticClient *PD = CreateHTMLDiagnosticClient(F.getDirname(), PP);
|
|
|
|
return CreatePlistDiagnosticClient(prefix, PP, PD);
|
2009-07-28 06:13:39 +08:00
|
|
|
}
|
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AnalysisConsumer declaration.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
class VISIBILITY_HIDDEN AnalysisConsumer : public ASTConsumer {
|
|
|
|
typedef std::vector<CodeAction> Actions;
|
|
|
|
Actions FunctionActions;
|
|
|
|
Actions ObjCMethodActions;
|
|
|
|
Actions ObjCImplementationActions;
|
|
|
|
Actions TranslationUnitActions;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
public:
|
|
|
|
ASTContext* Ctx;
|
|
|
|
const Preprocessor &PP;
|
|
|
|
const std::string OutDir;
|
|
|
|
AnalyzerOptions Opts;
|
|
|
|
bool declDisplayed;
|
2009-07-30 17:11:52 +08:00
|
|
|
|
2009-08-03 11:27:37 +08:00
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
// PD is owned by AnalysisManager.
|
|
|
|
PathDiagnosticClient *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
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
llvm::OwningPtr<AnalysisManager> Mgr;
|
2009-08-03 11:13:46 +08:00
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
AnalysisConsumer(const Preprocessor& pp,
|
|
|
|
const std::string& outdir,
|
|
|
|
const AnalyzerOptions& opts)
|
|
|
|
: Ctx(0), PP(pp), OutDir(outdir),
|
|
|
|
Opts(opts), declDisplayed(false), PD(0) {
|
|
|
|
DigestAnalyzerOptions();
|
|
|
|
}
|
2009-07-30 17:11:52 +08:00
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
void DigestAnalyzerOptions() {
|
|
|
|
// Create the PathDiagnosticClient.
|
|
|
|
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"
|
|
|
|
}
|
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.
|
|
|
|
if (ManagerRegistry::StoreMgrCreator != 0) {
|
|
|
|
CreateStoreMgr = ManagerRegistry::StoreMgrCreator;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
switch (Opts.AnalysisStoreOpt) {
|
|
|
|
default:
|
|
|
|
assert(0 && "Unknown store manager.");
|
2009-07-30 17:11:52 +08:00
|
|
|
#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \
|
2009-11-11 14:28:42 +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
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
if (ManagerRegistry::ConstraintMgrCreator != 0)
|
|
|
|
CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator;
|
|
|
|
else {
|
|
|
|
switch (Opts.AnalysisConstraintsOpt) {
|
|
|
|
default:
|
|
|
|
assert(0 && "Unknown store manager.");
|
2009-07-30 17:11:52 +08:00
|
|
|
#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \
|
2009-11-11 14:28:42 +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
|
|
|
}
|
|
|
|
|
|
|
|
void DisplayFunction(const Decl *D) {
|
|
|
|
if (!Opts.AnalyzerDisplayProgress || declDisplayed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
declDisplayed = true;
|
|
|
|
// FIXME: Is getCodeDecl() always a named decl?
|
|
|
|
if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
|
|
|
|
const NamedDecl *ND = cast<NamedDecl>(D);
|
|
|
|
SourceManager &SM = Mgr->getASTContext().getSourceManager();
|
|
|
|
llvm::errs() << "ANALYZE: "
|
|
|
|
<< SM.getPresumedLoc(ND->getLocation()).getFilename()
|
|
|
|
<< ' ' << ND->getNameAsString() << '\n';
|
2008-11-07 10:09:25 +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
|
|
|
void addCodeAction(CodeAction action) {
|
|
|
|
FunctionActions.push_back(action);
|
|
|
|
ObjCMethodActions.push_back(action);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
void addObjCImplementationAction(CodeAction action) {
|
|
|
|
ObjCImplementationActions.push_back(action);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
void addTranslationUnitAction(CodeAction action) {
|
|
|
|
TranslationUnitActions.push_back(action);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
virtual void Initialize(ASTContext &Context) {
|
|
|
|
Ctx = &Context;
|
|
|
|
Mgr.reset(new AnalysisManager(*Ctx, PP.getDiagnostics(),
|
|
|
|
PP.getLangOptions(), PD,
|
|
|
|
CreateStoreMgr, CreateConstraintMgr,
|
|
|
|
Opts.AnalyzerDisplayProgress,
|
|
|
|
Opts.VisualizeEGDot, Opts.VisualizeEGUbi,
|
|
|
|
Opts.PurgeDead, Opts.EagerlyAssume,
|
|
|
|
Opts.TrimGraph));
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
virtual void HandleTopLevelDecl(DeclGroupRef D) {
|
|
|
|
declDisplayed = false;
|
|
|
|
for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
|
|
|
|
HandleTopLevelSingleDecl(*I);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
void HandleTopLevelSingleDecl(Decl *D);
|
|
|
|
virtual void HandleTranslationUnit(ASTContext &C);
|
2008-11-27 09:55:08 +08:00
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
void HandleCode(Decl* D, Stmt* Body, Actions& actions);
|
|
|
|
};
|
2008-07-02 08:03:09 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
template <> struct FoldingSetTrait<CodeAction> {
|
|
|
|
static inline void Profile(CodeAction X, FoldingSetNodeID& ID) {
|
|
|
|
ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X)));
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
};
|
2008-07-02 08:03:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AnalysisConsumer implementation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void AnalysisConsumer::HandleTopLevelSingleDecl(Decl *D) {
|
2008-07-02 08:03:09 +08:00
|
|
|
switch (D->getKind()) {
|
|
|
|
case Decl::Function: {
|
|
|
|
FunctionDecl* FD = cast<FunctionDecl>(D);
|
2008-07-03 02:11:29 +08:00
|
|
|
|
2009-10-19 05:17:35 +08:00
|
|
|
if (!Opts.AnalyzeSpecificFunction.empty() &&
|
2009-05-19 18:18:02 +08:00
|
|
|
Opts.AnalyzeSpecificFunction != FD->getIdentifier()->getName())
|
2008-07-03 02:11:29 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-30 10:35:26 +08:00
|
|
|
Stmt* Body = FD->getBody();
|
2008-07-02 08:03:09 +08:00
|
|
|
if (Body) HandleCode(FD, Body, FunctionActions);
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
case Decl::ObjCMethod: {
|
|
|
|
ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-19 18:18:02 +08:00
|
|
|
if (Opts.AnalyzeSpecificFunction.size() > 0 &&
|
|
|
|
Opts.AnalyzeSpecificFunction != MD->getSelector().getAsString())
|
2008-07-03 02:11:29 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
Stmt* Body = MD->getBody();
|
|
|
|
if (Body) HandleCode(MD, Body, ObjCMethodActions);
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-28 12:11:33 +08:00
|
|
|
void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
|
2009-09-26 12:15:09 +08:00
|
|
|
|
|
|
|
TranslationUnitDecl *TU = C.getTranslationUnitDecl();
|
|
|
|
|
|
|
|
if (!TranslationUnitActions.empty()) {
|
|
|
|
// Find the entry function definition (if any).
|
|
|
|
FunctionDecl *FD = 0;
|
|
|
|
|
|
|
|
if (!Opts.AnalyzeSpecificFunction.empty()) {
|
|
|
|
for (DeclContext::decl_iterator I=TU->decls_begin(), E=TU->decls_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (FunctionDecl *fd = dyn_cast<FunctionDecl>(*I))
|
|
|
|
if (fd->isThisDeclarationADefinition() &&
|
|
|
|
fd->getNameAsString() == Opts.AnalyzeSpecificFunction) {
|
|
|
|
FD = fd;
|
|
|
|
break;
|
|
|
|
}
|
2009-09-10 13:44:00 +08:00
|
|
|
}
|
2009-09-26 12:15:09 +08:00
|
|
|
}
|
2009-09-10 13:44:00 +08:00
|
|
|
|
|
|
|
for (Actions::iterator I = TranslationUnitActions.begin(),
|
2008-11-07 10:09:25 +08:00
|
|
|
E = TranslationUnitActions.end(); I != E; ++I)
|
2009-11-11 14:28:42 +08:00
|
|
|
(*I)(*this, *Mgr, FD);
|
2008-11-07 10:09:25 +08:00
|
|
|
}
|
|
|
|
|
2009-03-28 11:29:40 +08:00
|
|
|
if (!ObjCImplementationActions.empty()) {
|
2009-09-26 12:15:09 +08:00
|
|
|
for (DeclContext::decl_iterator I = TU->decls_begin(),
|
|
|
|
E = TU->decls_end();
|
2009-03-28 11:29:40 +08:00
|
|
|
I != E; ++I)
|
2009-02-13 08:51:30 +08:00
|
|
|
if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I))
|
|
|
|
HandleCode(ID, 0, ObjCImplementationActions);
|
2009-03-28 11:29:40 +08:00
|
|
|
}
|
2009-08-03 11:27:37 +08:00
|
|
|
|
2009-08-02 13:43:14 +08:00
|
|
|
// Explicitly destroy the PathDiagnosticClient. This will flush its output.
|
|
|
|
// FIXME: This should be replaced with something that doesn't rely on
|
|
|
|
// side-effects in PathDiagnosticClient's destructor.
|
2009-08-03 11:27:37 +08:00
|
|
|
Mgr.reset(NULL);
|
2008-07-03 12:29:21 +08:00
|
|
|
}
|
|
|
|
|
2009-09-10 13:44:00 +08:00
|
|
|
void AnalysisConsumer::HandleCode(Decl *D, Stmt* Body, Actions& actions) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
// Don't run the actions if an error has occured with parsing the file.
|
2009-11-05 10:41:58 +08:00
|
|
|
if (PP.getDiagnostics().hasErrorOccurred())
|
2008-07-02 08:03:09 +08:00
|
|
|
return;
|
2009-02-03 04:52:40 +08:00
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
// Don't run the actions on declarations in header files unless
|
|
|
|
// otherwise specified.
|
2009-05-19 18:18:02 +08:00
|
|
|
if (!Opts.AnalyzeAll &&
|
|
|
|
!Ctx->getSourceManager().isFromMainFile(D->getLocation()))
|
2009-09-09 23:08:12 +08:00
|
|
|
return;
|
2008-07-02 08:03:09 +08:00
|
|
|
|
2009-10-21 05:39:41 +08:00
|
|
|
// Clear the AnalysisManager of old AnalysisContexts.
|
|
|
|
Mgr->ClearContexts();
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// Dispatch on the actions.
|
2008-10-30 13:03:28 +08:00
|
|
|
for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I)
|
2009-11-11 14:28:42 +08:00
|
|
|
(*I)(*this, *Mgr, D);
|
2008-07-02 08:03:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Analyses
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
static void ActionWarnDeadStores(AnalysisConsumer &C, AnalysisManager& mgr,
|
|
|
|
Decl *D) {
|
2009-09-10 13:44:00 +08:00
|
|
|
if (LiveVariables *L = mgr.getLiveVariables(D)) {
|
2009-11-11 14:28:42 +08:00
|
|
|
C.DisplayFunction(D);
|
2008-07-03 13:26:14 +08:00
|
|
|
BugReporter BR(mgr);
|
2009-09-10 13:44:00 +08:00
|
|
|
CheckDeadStores(*mgr.getCFG(D), *L, mgr.getParentMap(D), BR);
|
2008-07-03 13:26:14 +08:00
|
|
|
}
|
2008-07-02 08:03:09 +08:00
|
|
|
}
|
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
static void ActionWarnUninitVals(AnalysisConsumer &C, AnalysisManager& mgr,
|
|
|
|
Decl *D) {
|
|
|
|
if (CFG* c = mgr.getCFG(D)) {
|
|
|
|
C.DisplayFunction(D);
|
2009-08-25 14:51:30 +08:00
|
|
|
CheckUninitializedValues(*c, mgr.getASTContext(), mgr.getDiagnostic());
|
2009-11-11 14:28:42 +08:00
|
|
|
}
|
2008-07-02 08:03:09 +08:00
|
|
|
}
|
|
|
|
|
2008-07-02 08:44:58 +08:00
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
static void ActionGRExprEngine(AnalysisConsumer &C, AnalysisManager& mgr, Decl *D,
|
2009-09-18 13:37:41 +08:00
|
|
|
GRTransferFuncs* tf) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-03 00:35:50 +08:00
|
|
|
llvm::OwningPtr<GRTransferFuncs> TF(tf);
|
2008-07-03 13:26:14 +08:00
|
|
|
|
2008-11-25 04:53:32 +08:00
|
|
|
// Display progress.
|
2009-11-11 14:28:42 +08:00
|
|
|
C.DisplayFunction(D);
|
2008-11-25 04:53:32 +08:00
|
|
|
|
2009-09-19 06:29:35 +08:00
|
|
|
// Construct the analysis engine. We first query for the LiveVariables
|
|
|
|
// information to see if the CFG is valid.
|
|
|
|
// FIXME: Inter-procedural analysis will need to handle invalid CFGs.
|
|
|
|
if (!mgr.getLiveVariables(D))
|
|
|
|
return;
|
|
|
|
|
2009-08-25 14:51:30 +08:00
|
|
|
GRExprEngine Eng(mgr);
|
2008-10-24 09:04:59 +08:00
|
|
|
|
2008-07-03 00:35:50 +08:00
|
|
|
Eng.setTransferFunctions(tf);
|
2009-09-18 13:37:41 +08:00
|
|
|
Eng.RegisterInternalChecks(); // FIXME: Internal checks should just
|
|
|
|
// automatically register.
|
|
|
|
RegisterAppleChecks(Eng, *D);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-28 06:31:43 +08:00
|
|
|
|
|
|
|
// Set the graph auditor.
|
2009-08-06 09:32:16 +08:00
|
|
|
llvm::OwningPtr<ExplodedNode::Auditor> Auditor;
|
2008-08-28 06:31:43 +08:00
|
|
|
if (mgr.shouldVisualizeUbigraph()) {
|
|
|
|
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.
|
2009-09-10 13:44:00 +08:00
|
|
|
Eng.ExecuteWorkList(mgr.getStackFrame(D));
|
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.
|
2008-08-28 06:31:43 +08:00
|
|
|
if (mgr.shouldVisualizeGraphviz())
|
2008-07-03 00:49:11 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
static void ActionCheckerCFRefAux(AnalysisConsumer &C, AnalysisManager& mgr,
|
|
|
|
Decl *D, bool GCEnabled) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-25 14:51:30 +08:00
|
|
|
GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getASTContext(),
|
2008-07-03 00:35:50 +08:00
|
|
|
GCEnabled,
|
|
|
|
mgr.getLangOptions());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
ActionGRExprEngine(C, mgr, D, TF);
|
2008-07-02 08:44:58 +08:00
|
|
|
}
|
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
static void ActionCheckerCFRef(AnalysisConsumer &C, AnalysisManager& mgr,
|
|
|
|
Decl *D) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-02 08:44:58 +08:00
|
|
|
switch (mgr.getLangOptions().getGCMode()) {
|
|
|
|
default:
|
|
|
|
assert (false && "Invalid GC mode.");
|
|
|
|
case LangOptions::NonGC:
|
2009-11-11 14:28:42 +08:00
|
|
|
ActionCheckerCFRefAux(C, mgr, D, false);
|
2008-07-02 08:44:58 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-02 08:44:58 +08:00
|
|
|
case LangOptions::GCOnly:
|
2009-11-11 14:28:42 +08:00
|
|
|
ActionCheckerCFRefAux(C, mgr, D, true);
|
2008-07-02 08:44:58 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-02 08:44:58 +08:00
|
|
|
case LangOptions::HybridGC:
|
2009-11-11 14:28:42 +08:00
|
|
|
ActionCheckerCFRefAux(C, mgr, D, false);
|
|
|
|
ActionCheckerCFRefAux(C, mgr, D, true);
|
2008-07-02 08:44:58 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
static void ActionDisplayLiveVariables(AnalysisConsumer &C,
|
|
|
|
AnalysisManager& mgr, Decl *D) {
|
2009-09-10 13:44:00 +08:00
|
|
|
if (LiveVariables* L = mgr.getLiveVariables(D)) {
|
2009-11-11 14:28:42 +08:00
|
|
|
C.DisplayFunction(D);
|
2008-07-03 13:26:14 +08:00
|
|
|
L->dumpBlockLiveness(mgr.getSourceManager());
|
|
|
|
}
|
2008-07-03 02:11:29 +08:00
|
|
|
}
|
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
static void ActionCFGDump(AnalysisConsumer &C, AnalysisManager& mgr, Decl *D) {
|
|
|
|
if (CFG *cfg = mgr.getCFG(D)) {
|
|
|
|
C.DisplayFunction(D);
|
|
|
|
cfg->dump(mgr.getLangOptions());
|
2008-07-03 13:26:14 +08:00
|
|
|
}
|
2008-07-03 02:23:21 +08:00
|
|
|
}
|
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
static void ActionCFGView(AnalysisConsumer &C, AnalysisManager& mgr, Decl *D) {
|
|
|
|
if (CFG *cfg = mgr.getCFG(D)) {
|
|
|
|
C.DisplayFunction(D);
|
|
|
|
cfg->viewCFG(mgr.getLangOptions());
|
2008-07-03 13:26:14 +08:00
|
|
|
}
|
2008-07-03 02:23:21 +08:00
|
|
|
}
|
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
static void ActionSecuritySyntacticChecks(AnalysisConsumer &C,
|
|
|
|
AnalysisManager &mgr, Decl *D) {
|
|
|
|
C.DisplayFunction(D);
|
2009-09-09 23:08:12 +08:00
|
|
|
BugReporter BR(mgr);
|
2009-09-10 13:44:00 +08:00
|
|
|
CheckSecuritySyntaxOnly(D, BR);
|
2009-07-23 09:07:19 +08:00
|
|
|
}
|
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
static void ActionWarnObjCDealloc(AnalysisConsumer &C, AnalysisManager& mgr,
|
|
|
|
Decl *D) {
|
2008-08-05 01:14:10 +08:00
|
|
|
if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
C.DisplayFunction(D);
|
2008-07-03 12:29:21 +08:00
|
|
|
BugReporter BR(mgr);
|
2009-11-11 14:28:42 +08:00
|
|
|
CheckObjCDealloc(cast<ObjCImplementationDecl>(D), mgr.getLangOptions(), BR);
|
2008-07-03 12:29:21 +08:00
|
|
|
}
|
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
static void ActionWarnObjCUnusedIvars(AnalysisConsumer &C, AnalysisManager& mgr,
|
|
|
|
Decl *D) {
|
|
|
|
C.DisplayFunction(D);
|
2008-07-23 08:45:26 +08:00
|
|
|
BugReporter BR(mgr);
|
2009-11-11 14:28:42 +08:00
|
|
|
CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(D), BR);
|
2008-07-23 08:45:26 +08:00
|
|
|
}
|
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
static void ActionWarnObjCMethSigs(AnalysisConsumer &C, AnalysisManager& mgr,
|
|
|
|
Decl *D) {
|
|
|
|
C.DisplayFunction(D);
|
2008-07-12 06:40:47 +08:00
|
|
|
BugReporter BR(mgr);
|
2009-09-10 13:44:00 +08:00
|
|
|
CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(D), BR);
|
2008-07-12 06:40:47 +08:00
|
|
|
}
|
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
static void ActionWarnSizeofPointer(AnalysisConsumer &C, AnalysisManager &mgr,
|
|
|
|
Decl *D) {
|
|
|
|
C.DisplayFunction(D);
|
2009-11-08 21:10:34 +08:00
|
|
|
BugReporter BR(mgr);
|
|
|
|
CheckSizeofPointer(D, BR);
|
|
|
|
}
|
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
static void ActionInlineCall(AnalysisConsumer &C, AnalysisManager &mgr,
|
|
|
|
Decl *D) {
|
2009-09-11 12:13:42 +08:00
|
|
|
if (!D)
|
|
|
|
return;
|
|
|
|
|
2009-11-11 14:28:42 +08:00
|
|
|
C.DisplayFunction(D);
|
2009-09-11 12:13:42 +08:00
|
|
|
llvm::OwningPtr<GRTransferFuncs> TF(CreateCallInliner(mgr.getASTContext()));
|
|
|
|
|
|
|
|
// Construct the analysis engine.
|
|
|
|
GRExprEngine Eng(mgr);
|
|
|
|
|
|
|
|
Eng.setTransferFunctions(TF.get());
|
|
|
|
|
|
|
|
Eng.RegisterInternalChecks();
|
|
|
|
RegisterAppleChecks(Eng, *D);
|
|
|
|
|
|
|
|
// Execute the worklist algorithm.
|
|
|
|
Eng.ExecuteWorkList(mgr.getStackFrame(D));
|
|
|
|
|
|
|
|
// Visualize the exploded graph.
|
|
|
|
if (mgr.shouldVisualizeGraphviz())
|
|
|
|
Eng.ViewGraph(mgr.shouldTrimGraph());
|
|
|
|
}
|
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AnalysisConsumer creation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-11-05 10:41:58 +08:00
|
|
|
ASTConsumer* clang::CreateAnalysisConsumer(const Preprocessor& pp,
|
2009-05-19 18:18:02 +08:00
|
|
|
const std::string& OutDir,
|
|
|
|
const AnalyzerOptions& Opts) {
|
2009-11-05 10:41:58 +08:00
|
|
|
llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(pp, OutDir, Opts));
|
2009-02-17 12:27:41 +08:00
|
|
|
|
2009-05-19 18:18:02 +08:00
|
|
|
for (unsigned i = 0; i < Opts.AnalysisList.size(); ++i)
|
|
|
|
switch (Opts.AnalysisList[i]) {
|
2008-07-15 08:46:02 +08:00
|
|
|
#define ANALYSIS(NAME, CMD, DESC, SCOPE)\
|
2008-07-15 07:41:13 +08:00
|
|
|
case NAME:\
|
2008-07-15 08:46:02 +08:00
|
|
|
C->add ## SCOPE ## Action(&Action ## NAME);\
|
2008-07-02 08:03:09 +08:00
|
|
|
break;
|
2009-05-20 05:16:18 +08:00
|
|
|
#include "clang/Frontend/Analyses.def"
|
2008-07-02 08:03:09 +08:00
|
|
|
default: break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-08 03:02:53 +08:00
|
|
|
// Last, 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
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
return C.take();
|
|
|
|
}
|
|
|
|
|
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 {
|
2008-08-28 06:31:43 +08:00
|
|
|
llvm::OwningPtr<llvm::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:
|
2008-08-28 11:54:51 +08:00
|
|
|
UbigraphViz(llvm::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
|
|
|
|
|
|
|
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
|
|
|
|
2008-08-28 06:31:43 +08:00
|
|
|
llvm::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
|
|
|
}
|
|
|
|
|
2009-08-06 09:32:16 +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
|
|
|
}
|
|
|
|
|
2008-08-28 13:02:09 +08:00
|
|
|
UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
|
|
|
|
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
|
|
|
}
|