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"
|
2009-03-02 14:16:29 +08:00
|
|
|
#include "clang/Frontend/PathDiagnosticClients.h"
|
|
|
|
#include "clang/Frontend/ManagerRegistry.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-07-17 02:13:04 +08:00
|
|
|
#include "clang/Analysis/CFG.h"
|
2008-07-02 08:03:09 +08:00
|
|
|
#include "clang/Analysis/Analyses/LiveVariables.h"
|
|
|
|
#include "clang/Analysis/PathDiagnostic.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/AST/ParentMap.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/Analyses/LiveVariables.h"
|
|
|
|
#include "clang/Analysis/LocalCheckers.h"
|
|
|
|
#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
|
|
|
|
#include "clang/Analysis/PathSensitive/GRExprEngine.h"
|
2008-12-22 09:52:37 +08:00
|
|
|
#include "llvm/Support/CommandLine.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-09-10 13:44:00 +08:00
|
|
|
typedef void (*CodeAction)(AnalysisManager& Mgr, 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*
|
|
|
|
CreatePlistHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP,
|
|
|
|
PreprocessorFactory* PPF) {
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::sys::Path F(prefix);
|
|
|
|
PathDiagnosticClientFactory *PF =
|
2009-07-28 06:13:39 +08:00
|
|
|
CreateHTMLDiagnosticClientFactory(F.getDirname(), PP, PPF);
|
|
|
|
return CreatePlistDiagnosticClient(prefix, PP, PPF, PF);
|
|
|
|
}
|
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AnalysisConsumer declaration.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN AnalysisConsumer : public ASTConsumer {
|
2008-07-03 12:29:21 +08:00
|
|
|
typedef std::vector<CodeAction> Actions;
|
2008-07-02 08:03:09 +08:00
|
|
|
Actions FunctionActions;
|
|
|
|
Actions ObjCMethodActions;
|
2008-07-03 12:29:21 +08:00
|
|
|
Actions ObjCImplementationActions;
|
2008-11-07 10:09:25 +08:00
|
|
|
Actions TranslationUnitActions;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
public:
|
2009-09-09 23:08:12 +08:00
|
|
|
const LangOptions& LOpts;
|
2008-07-02 08:03:09 +08:00
|
|
|
Diagnostic &Diags;
|
|
|
|
ASTContext* Ctx;
|
|
|
|
Preprocessor* PP;
|
|
|
|
PreprocessorFactory* PPF;
|
2009-02-17 12:27:41 +08:00
|
|
|
const std::string OutDir;
|
2009-05-19 18:18:02 +08:00
|
|
|
AnalyzerOptions Opts;
|
2009-07-30 17:11:52 +08:00
|
|
|
|
2009-08-03 11:27:37 +08:00
|
|
|
|
|
|
|
// PD is owned by AnalysisManager.
|
|
|
|
PathDiagnosticClient *PD;
|
|
|
|
|
2009-07-30 17:11:52 +08:00
|
|
|
StoreManagerCreator CreateStoreMgr;
|
|
|
|
ConstraintManagerCreator CreateConstraintMgr;
|
2008-07-02 08:03:09 +08:00
|
|
|
|
2009-08-03 11:13:46 +08:00
|
|
|
llvm::OwningPtr<AnalysisManager> Mgr;
|
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
AnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
|
|
|
|
PreprocessorFactory* ppf,
|
|
|
|
const LangOptions& lopts,
|
2009-05-19 18:18:02 +08:00
|
|
|
const std::string& outdir,
|
|
|
|
const AnalyzerOptions& opts)
|
2009-02-17 12:27:41 +08:00
|
|
|
: LOpts(lopts), Diags(diags),
|
2008-07-02 08:03:09 +08:00
|
|
|
Ctx(0), PP(pp), PPF(ppf),
|
2009-07-31 00:10:26 +08:00
|
|
|
OutDir(outdir), Opts(opts), PD(0) {
|
2009-07-30 17:11:52 +08:00
|
|
|
DigestAnalyzerOptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DigestAnalyzerOptions() {
|
|
|
|
// Create the PathDiagnosticClient.
|
|
|
|
if (!OutDir.empty()) {
|
|
|
|
switch (Opts.AnalysisDiagOpt) {
|
|
|
|
default:
|
|
|
|
#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE) \
|
2009-08-03 11:27:37 +08:00
|
|
|
case PD_##NAME: PD = CREATEFN(OutDir, PP, PPF); break;
|
2009-07-30 17:11:52 +08:00
|
|
|
#include "clang/Frontend/Analyses.def"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the analyzer component creators.
|
|
|
|
if (ManagerRegistry::StoreMgrCreator != 0) {
|
|
|
|
CreateStoreMgr = ManagerRegistry::StoreMgrCreator;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
switch (Opts.AnalysisStoreOpt) {
|
|
|
|
default:
|
|
|
|
assert(0 && "Unknown store manager.");
|
|
|
|
#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \
|
|
|
|
case NAME##Model: CreateStoreMgr = CREATEFN; break;
|
|
|
|
#include "clang/Frontend/Analyses.def"
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-30 17:11:52 +08:00
|
|
|
if (ManagerRegistry::ConstraintMgrCreator != 0)
|
|
|
|
CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator;
|
|
|
|
else {
|
|
|
|
switch (Opts.AnalysisConstraintsOpt) {
|
|
|
|
default:
|
|
|
|
assert(0 && "Unknown store manager.");
|
|
|
|
#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \
|
|
|
|
case NAME##Model: CreateConstraintMgr = CREATEFN; break;
|
|
|
|
#include "clang/Frontend/Analyses.def"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
void addCodeAction(CodeAction action) {
|
2008-07-03 12:29:21 +08:00
|
|
|
FunctionActions.push_back(action);
|
|
|
|
ObjCMethodActions.push_back(action);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-03 12:29:21 +08:00
|
|
|
void addObjCImplementationAction(CodeAction action) {
|
|
|
|
ObjCImplementationActions.push_back(action);
|
2008-07-02 08:03:09 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-07 10:09:25 +08:00
|
|
|
void addTranslationUnitAction(CodeAction action) {
|
|
|
|
TranslationUnitActions.push_back(action);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
virtual void Initialize(ASTContext &Context) {
|
|
|
|
Ctx = &Context;
|
2009-09-09 23:08:12 +08:00
|
|
|
Mgr.reset(new AnalysisManager(*Ctx, Diags, LOpts, PD,
|
2009-08-03 11:13:46 +08:00
|
|
|
CreateStoreMgr, CreateConstraintMgr,
|
2009-09-09 23:08:12 +08:00
|
|
|
Opts.AnalyzerDisplayProgress,
|
|
|
|
Opts.VisualizeEGDot, Opts.VisualizeEGUbi,
|
2009-08-03 11:13:46 +08:00
|
|
|
Opts.PurgeDead, Opts.EagerlyAssume,
|
|
|
|
Opts.TrimGraph));
|
2008-07-02 08:03:09 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-30 00:50:03 +08:00
|
|
|
virtual void HandleTopLevelDecl(DeclGroupRef D) {
|
|
|
|
for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
|
|
|
|
HandleTopLevelSingleDecl(*I);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-30 00:50:03 +08:00
|
|
|
void HandleTopLevelSingleDecl(Decl *D);
|
2009-03-28 12:11:33 +08:00
|
|
|
virtual void HandleTranslationUnit(ASTContext &C);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-03 04:52:40 +08:00
|
|
|
void HandleCode(Decl* D, Stmt* Body, Actions& actions);
|
2008-07-02 08:03:09 +08:00
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
|
2008-11-27 09:55:08 +08:00
|
|
|
|
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-09-09 23:08:12 +08:00
|
|
|
if (Opts.AnalyzeSpecificFunction.size() > 0 &&
|
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-10 13:44:00 +08:00
|
|
|
// Find the entry function definition.
|
|
|
|
FunctionDecl *FD = 0;
|
|
|
|
TranslationUnitDecl *TU = Ctx->getTranslationUnitDecl();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!TranslationUnitActions.empty()) {
|
|
|
|
for (Actions::iterator I = TranslationUnitActions.begin(),
|
2008-11-07 10:09:25 +08:00
|
|
|
E = TranslationUnitActions.end(); I != E; ++I)
|
2009-09-10 13:44:00 +08:00
|
|
|
(*I)(*Mgr, FD);
|
2008-11-07 10:09:25 +08:00
|
|
|
}
|
|
|
|
|
2009-03-28 11:29:40 +08:00
|
|
|
if (!ObjCImplementationActions.empty()) {
|
2009-03-28 12:11:33 +08:00
|
|
|
TranslationUnitDecl *TUD = C.getTranslationUnitDecl();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-30 10:36:12 +08:00
|
|
|
for (DeclContext::decl_iterator I = TUD->decls_begin(),
|
|
|
|
E = TUD->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.
|
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
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-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-09-10 13:44:00 +08:00
|
|
|
(*I)(*Mgr, D);
|
2008-07-02 08:03:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Analyses
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-09-10 13:44:00 +08:00
|
|
|
static void ActionWarnDeadStores(AnalysisManager& mgr, Decl *D) {
|
|
|
|
if (LiveVariables *L = mgr.getLiveVariables(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-09-10 13:44:00 +08:00
|
|
|
static void ActionWarnUninitVals(AnalysisManager& mgr, Decl *D) {
|
|
|
|
if (CFG* c = mgr.getCFG(D))
|
2009-08-25 14:51:30 +08:00
|
|
|
CheckUninitializedValues(*c, mgr.getASTContext(), mgr.getDiagnostic());
|
2008-07-02 08:03:09 +08:00
|
|
|
}
|
|
|
|
|
2008-07-02 08:44:58 +08:00
|
|
|
|
2009-09-10 13:44:00 +08:00
|
|
|
static void ActionGRExprEngine(AnalysisManager& mgr, Decl *D,
|
|
|
|
GRTransferFuncs* tf,
|
2008-07-23 00:21:24 +08:00
|
|
|
bool StandardWarnings = true) {
|
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-09-10 13:44:00 +08:00
|
|
|
mgr.DisplayFunction(D);
|
2008-11-25 04:53:32 +08:00
|
|
|
|
2008-07-03 13:26:14 +08:00
|
|
|
// Construct the analysis engine.
|
2009-09-10 13:44:00 +08:00
|
|
|
LiveVariables* L = mgr.getLiveVariables(D);
|
2008-07-03 13:26:14 +08:00
|
|
|
if (!L) return;
|
2008-11-25 04:53:32 +08:00
|
|
|
|
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-09 23:08:12 +08:00
|
|
|
|
2008-07-23 00:21:24 +08:00
|
|
|
if (StandardWarnings) {
|
|
|
|
Eng.RegisterInternalChecks();
|
2009-09-10 13:44:00 +08:00
|
|
|
RegisterAppleChecks(Eng, *D);
|
2008-07-23 00:21:24 +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-09-10 13:44:00 +08:00
|
|
|
static void ActionCheckerCFRefAux(AnalysisManager& mgr, Decl *D,
|
|
|
|
bool GCEnabled, bool StandardWarnings) {
|
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-09-10 13:44:00 +08:00
|
|
|
ActionGRExprEngine(mgr, D, TF, StandardWarnings);
|
2008-07-02 08:44:58 +08:00
|
|
|
}
|
|
|
|
|
2009-09-10 13:44:00 +08:00
|
|
|
static void ActionCheckerCFRef(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-09-10 13:44:00 +08:00
|
|
|
ActionCheckerCFRefAux(mgr, D, false, 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::GCOnly:
|
2009-09-10 13:44:00 +08:00
|
|
|
ActionCheckerCFRefAux(mgr, D, true, 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-09-10 13:44:00 +08:00
|
|
|
ActionCheckerCFRefAux(mgr, D, false, true);
|
|
|
|
ActionCheckerCFRefAux(mgr, D, true, false);
|
2008-07-02 08:44:58 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-10 13:44:00 +08:00
|
|
|
static void ActionDisplayLiveVariables(AnalysisManager& mgr, Decl *D) {
|
|
|
|
if (LiveVariables* L = mgr.getLiveVariables(D)) {
|
|
|
|
mgr.DisplayFunction(D);
|
2008-07-03 13:26:14 +08:00
|
|
|
L->dumpBlockLiveness(mgr.getSourceManager());
|
|
|
|
}
|
2008-07-03 02:11:29 +08:00
|
|
|
}
|
|
|
|
|
2009-09-10 13:44:00 +08:00
|
|
|
static void ActionCFGDump(AnalysisManager& mgr, Decl *D) {
|
|
|
|
if (CFG* c = mgr.getCFG(D)) {
|
|
|
|
mgr.DisplayFunction(D);
|
2009-07-30 17:14:54 +08:00
|
|
|
c->dump(mgr.getLangOptions());
|
2008-07-03 13:26:14 +08:00
|
|
|
}
|
2008-07-03 02:23:21 +08:00
|
|
|
}
|
|
|
|
|
2009-09-10 13:44:00 +08:00
|
|
|
static void ActionCFGView(AnalysisManager& mgr, Decl *D) {
|
|
|
|
if (CFG* c = mgr.getCFG(D)) {
|
|
|
|
mgr.DisplayFunction(D);
|
2009-07-30 17:14:54 +08:00
|
|
|
c->viewCFG(mgr.getLangOptions());
|
2008-07-03 13:26:14 +08:00
|
|
|
}
|
2008-07-03 02:23:21 +08:00
|
|
|
}
|
|
|
|
|
2009-09-10 13:44:00 +08:00
|
|
|
static void ActionSecuritySyntacticChecks(AnalysisManager &mgr, Decl *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-09-10 13:44:00 +08:00
|
|
|
static void ActionWarnObjCDealloc(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
|
|
|
|
2008-07-03 12:29:21 +08:00
|
|
|
BugReporter BR(mgr);
|
2009-09-10 13:44:00 +08:00
|
|
|
CheckObjCDealloc(cast<ObjCImplementationDecl>(D), mgr.getLangOptions(), BR);
|
2008-07-03 12:29:21 +08:00
|
|
|
}
|
|
|
|
|
2009-09-10 13:44:00 +08:00
|
|
|
static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr, Decl *D) {
|
2008-07-23 08:45:26 +08:00
|
|
|
BugReporter BR(mgr);
|
2009-09-10 13:44:00 +08:00
|
|
|
CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(D), BR);
|
2008-07-23 08:45:26 +08:00
|
|
|
}
|
|
|
|
|
2009-09-10 13:44:00 +08:00
|
|
|
static void ActionWarnObjCMethSigs(AnalysisManager& mgr, Decl *D) {
|
2008-07-12 06:40:47 +08:00
|
|
|
BugReporter BR(mgr);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-10 13:44:00 +08:00
|
|
|
CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(D), BR);
|
2008-07-12 06:40:47 +08:00
|
|
|
}
|
|
|
|
|
2009-09-11 12:13:42 +08:00
|
|
|
static void ActionInlineCall(AnalysisManager &mgr, Decl *D) {
|
|
|
|
if (!D)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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-02-17 12:27:41 +08:00
|
|
|
ASTConsumer* clang::CreateAnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
|
2008-07-02 08:03:09 +08:00
|
|
|
PreprocessorFactory* ppf,
|
|
|
|
const LangOptions& lopts,
|
2009-05-19 18:18:02 +08:00
|
|
|
const std::string& OutDir,
|
|
|
|
const AnalyzerOptions& Opts) {
|
2009-02-17 12:27:41 +08:00
|
|
|
|
|
|
|
llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(diags, pp, ppf,
|
2009-05-19 18:18:02 +08:00
|
|
|
lopts, 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.
|
|
|
|
diags.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
|
|
|
}
|