opnsense-src/lib/Frontend/AnalysisConsumer.cpp

661 lines
20 KiB
C++
Raw Normal View History

2009-06-02 13:58:47 -04: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.
//
//===----------------------------------------------------------------------===//
#include "clang/Frontend/AnalysisConsumer.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/Decl.h"
2010-01-01 05:34:51 -05:00
#include "clang/AST/DeclCXX.h"
2009-06-02 13:58:47 -04:00
#include "clang/AST/DeclObjC.h"
2009-11-05 12:18:09 -05:00
#include "clang/AST/ParentMap.h"
#include "clang/Analysis/Analyses/LiveVariables.h"
2010-02-16 04:31:36 -05:00
#include "clang/Analysis/Analyses/UninitializedValues.h"
2009-11-05 12:18:09 -05:00
#include "clang/Analysis/CFG.h"
2010-02-16 04:31:36 -05:00
#include "clang/Checker/Checkers/LocalCheckers.h"
#include "clang/Checker/ManagerRegistry.h"
#include "clang/Checker/BugReporter/PathDiagnostic.h"
#include "clang/Checker/PathSensitive/AnalysisManager.h"
#include "clang/Checker/BugReporter/BugReporter.h"
#include "clang/Checker/PathSensitive/GRExprEngine.h"
#include "clang/Checker/PathSensitive/GRTransferFuncs.h"
2009-11-05 12:18:09 -05:00
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/PathDiagnosticClients.h"
#include "clang/Lex/Preprocessor.h"
2009-06-02 13:58:47 -04:00
#include "llvm/Support/raw_ostream.h"
#include "llvm/System/Path.h"
#include "llvm/System/Program.h"
2009-10-14 14:03:49 -04:00
#include "llvm/ADT/OwningPtr.h"
2009-06-02 13:58:47 -04:00
using namespace clang;
2009-10-14 14:03:49 -04:00
static ExplodedNode::Auditor* CreateUbiViz();
2009-06-02 13:58:47 -04:00
//===----------------------------------------------------------------------===//
// Basic type definitions.
//===----------------------------------------------------------------------===//
2009-10-14 14:03:49 -04:00
//===----------------------------------------------------------------------===//
// Special PathDiagnosticClients.
//===----------------------------------------------------------------------===//
static PathDiagnosticClient*
2009-11-05 12:18:09 -05:00
CreatePlistHTMLDiagnosticClient(const std::string& prefix,
const Preprocessor &PP) {
2009-10-14 14:03:49 -04:00
llvm::sys::Path F(prefix);
2009-11-05 12:18:09 -05:00
PathDiagnosticClient *PD = CreateHTMLDiagnosticClient(F.getDirname(), PP);
return CreatePlistDiagnosticClient(prefix, PP, PD);
2009-10-14 14:03:49 -04:00
}
2009-06-02 13:58:47 -04:00
//===----------------------------------------------------------------------===//
// AnalysisConsumer declaration.
//===----------------------------------------------------------------------===//
namespace {
2009-12-01 06:08:04 -05:00
class AnalysisConsumer : public ASTConsumer {
2009-11-18 09:59:57 -05:00
public:
typedef void (*CodeAction)(AnalysisConsumer &C, AnalysisManager &M, Decl *D);
2010-02-16 04:31:36 -05:00
typedef void (*TUAction)(AnalysisConsumer &C, AnalysisManager &M,
TranslationUnitDecl &TU);
2009-11-18 09:59:57 -05:00
private:
typedef std::vector<CodeAction> Actions;
2010-02-16 04:31:36 -05:00
typedef std::vector<TUAction> TUActions;
2009-11-18 09:59:57 -05:00
Actions FunctionActions;
Actions ObjCMethodActions;
Actions ObjCImplementationActions;
2010-02-16 04:31:36 -05:00
TUActions TranslationUnitActions;
2009-11-18 09:59:57 -05:00
public:
ASTContext* Ctx;
const Preprocessor &PP;
const std::string OutDir;
AnalyzerOptions Opts;
bool declDisplayed;
// PD is owned by AnalysisManager.
PathDiagnosticClient *PD;
StoreManagerCreator CreateStoreMgr;
ConstraintManagerCreator CreateConstraintMgr;
llvm::OwningPtr<AnalysisManager> Mgr;
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-06-02 13:58:47 -04:00
2009-11-18 09:59:57 -05:00
void DigestAnalyzerOptions() {
// Create the PathDiagnosticClient.
if (!OutDir.empty()) {
switch (Opts.AnalysisDiagOpt) {
default:
2009-10-14 14:03:49 -04:00
#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE) \
2009-11-18 09:59:57 -05:00
case PD_##NAME: PD = CREATEFN(OutDir, PP); break;
2009-06-02 13:58:47 -04:00
#include "clang/Frontend/Analyses.def"
}
2009-11-18 09:59:57 -05:00
}
2009-06-02 13:58:47 -04:00
2009-11-18 09:59:57 -05:00
// Create the analyzer component creators.
if (ManagerRegistry::StoreMgrCreator != 0) {
CreateStoreMgr = ManagerRegistry::StoreMgrCreator;
}
else {
switch (Opts.AnalysisStoreOpt) {
default:
assert(0 && "Unknown store manager.");
2009-10-14 14:03:49 -04:00
#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \
2009-11-18 09:59:57 -05:00
case NAME##Model: CreateStoreMgr = CREATEFN; break;
2009-06-02 13:58:47 -04:00
#include "clang/Frontend/Analyses.def"
}
2009-11-18 09:59:57 -05:00
}
2009-06-02 13:58:47 -04:00
2009-11-18 09:59:57 -05:00
if (ManagerRegistry::ConstraintMgrCreator != 0)
CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator;
else {
switch (Opts.AnalysisConstraintsOpt) {
default:
assert(0 && "Unknown store manager.");
2009-06-02 13:58:47 -04:00
#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \
2009-11-18 09:59:57 -05:00
case NAME##Model: CreateConstraintMgr = CREATEFN; break;
2009-06-02 13:58:47 -04:00
#include "clang/Frontend/Analyses.def"
}
2009-10-14 14:03:49 -04:00
}
2009-11-18 09:59:57 -05:00
}
2010-02-16 04:31:36 -05:00
2009-11-18 09:59:57 -05:00
void DisplayFunction(const Decl *D) {
if (!Opts.AnalyzerDisplayProgress || declDisplayed)
return;
2010-02-16 04:31:36 -05:00
2009-11-18 09:59:57 -05:00
declDisplayed = true;
2009-12-15 13:49:47 -05:00
SourceManager &SM = Mgr->getASTContext().getSourceManager();
PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
llvm::errs() << "ANALYZE: " << Loc.getFilename();
2009-11-18 09:59:57 -05:00
if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
const NamedDecl *ND = cast<NamedDecl>(D);
2009-12-15 13:49:47 -05:00
llvm::errs() << ' ' << ND->getNameAsString() << '\n';
}
else if (isa<BlockDecl>(D)) {
llvm::errs() << ' ' << "block(line:" << Loc.getLine() << ",col:"
<< Loc.getColumn() << '\n';
2009-10-14 14:03:49 -04:00
}
2009-11-18 09:59:57 -05:00
}
2009-10-14 14:03:49 -04:00
2009-11-18 09:59:57 -05:00
void addCodeAction(CodeAction action) {
FunctionActions.push_back(action);
ObjCMethodActions.push_back(action);
}
2009-10-14 14:03:49 -04:00
2009-11-18 09:59:57 -05:00
void addObjCImplementationAction(CodeAction action) {
ObjCImplementationActions.push_back(action);
}
2009-10-14 14:03:49 -04:00
2010-02-16 04:31:36 -05:00
void addTranslationUnitAction(TUAction action) {
2009-11-18 09:59:57 -05:00
TranslationUnitActions.push_back(action);
}
2009-10-14 14:03:49 -04:00
2009-11-18 09:59:57 -05:00
virtual void Initialize(ASTContext &Context) {
Ctx = &Context;
Mgr.reset(new AnalysisManager(*Ctx, PP.getDiagnostics(),
PP.getLangOptions(), PD,
CreateStoreMgr, CreateConstraintMgr,
Opts.VisualizeEGDot, Opts.VisualizeEGUbi,
Opts.PurgeDead, Opts.EagerlyAssume,
Opts.TrimGraph));
}
2009-06-02 13:58:47 -04:00
2009-11-18 09:59:57 -05:00
virtual void HandleTopLevelDecl(DeclGroupRef D) {
declDisplayed = false;
for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
HandleTopLevelSingleDecl(*I);
}
2009-10-14 14:03:49 -04:00
2009-11-18 09:59:57 -05:00
void HandleTopLevelSingleDecl(Decl *D);
virtual void HandleTranslationUnit(ASTContext &C);
2009-10-14 14:03:49 -04:00
2009-11-18 09:59:57 -05:00
void HandleCode(Decl* D, Stmt* Body, Actions& actions);
};
2009-06-02 13:58:47 -04:00
} // end anonymous namespace
namespace llvm {
2009-11-18 09:59:57 -05:00
template <> struct FoldingSetTrait<AnalysisConsumer::CodeAction> {
2010-02-16 04:31:36 -05:00
static inline void Profile(AnalysisConsumer::CodeAction X,
2009-11-18 09:59:57 -05:00
FoldingSetNodeID& ID) {
2009-06-02 13:58:47 -04:00
ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X)));
}
2009-10-14 14:03:49 -04:00
};
2009-06-02 13:58:47 -04:00
}
//===----------------------------------------------------------------------===//
// AnalysisConsumer implementation.
//===----------------------------------------------------------------------===//
2009-10-14 14:03:49 -04:00
void AnalysisConsumer::HandleTopLevelSingleDecl(Decl *D) {
2009-06-02 13:58:47 -04:00
switch (D->getKind()) {
2010-02-16 04:31:36 -05:00
case Decl::CXXConstructor:
case Decl::CXXDestructor:
case Decl::CXXConversion:
case Decl::CXXMethod:
2010-01-23 06:10:26 -05:00
case Decl::Function: {
FunctionDecl* FD = cast<FunctionDecl>(D);
if (!Opts.AnalyzeSpecificFunction.empty() &&
2010-02-16 04:31:36 -05:00
FD->getDeclName().getAsString() != Opts.AnalyzeSpecificFunction)
break;
2009-10-14 14:03:49 -04:00
2010-02-16 04:31:36 -05:00
if (Stmt *Body = FD->getBody())
HandleCode(FD, Body, FunctionActions);
2010-01-23 06:10:26 -05:00
break;
}
2009-10-14 14:03:49 -04:00
2010-01-23 06:10:26 -05:00
case Decl::ObjCMethod: {
ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D);
2009-10-14 14:03:49 -04:00
2010-02-16 04:31:36 -05:00
if (!Opts.AnalyzeSpecificFunction.empty() &&
2010-01-23 06:10:26 -05:00
Opts.AnalyzeSpecificFunction != MD->getSelector().getAsString())
return;
2010-02-16 04:31:36 -05:00
if (Stmt* Body = MD->getBody())
HandleCode(MD, Body, ObjCMethodActions);
2010-01-23 06:10:26 -05:00
break;
}
2010-01-01 05:34:51 -05:00
2010-01-23 06:10:26 -05:00
default:
break;
2009-06-02 13:58:47 -04:00
}
}
void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
2010-02-16 04:31:36 -05:00
2009-10-14 14:03:49 -04:00
TranslationUnitDecl *TU = C.getTranslationUnitDecl();
2009-06-02 13:58:47 -04:00
2010-02-16 04:31:36 -05:00
for (TUActions::iterator I = TranslationUnitActions.begin(),
E = TranslationUnitActions.end(); I != E; ++I) {
(*I)(*this, *Mgr, *TU);
2009-06-02 13:58:47 -04:00
}
if (!ObjCImplementationActions.empty()) {
2009-10-14 14:03:49 -04:00
for (DeclContext::decl_iterator I = TU->decls_begin(),
E = TU->decls_end();
2009-06-02 13:58:47 -04:00
I != E; ++I)
if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I))
HandleCode(ID, 0, ObjCImplementationActions);
}
2009-10-14 14:03:49 -04:00
// Explicitly destroy the PathDiagnosticClient. This will flush its output.
// FIXME: This should be replaced with something that doesn't rely on
2009-12-15 13:49:47 -05:00
// side-effects in PathDiagnosticClient's destructor. This is required when
// used with option -disable-free.
2009-10-14 14:03:49 -04:00
Mgr.reset(NULL);
2009-06-02 13:58:47 -04:00
}
2009-12-15 13:49:47 -05:00
static void FindBlocks(DeclContext *D, llvm::SmallVectorImpl<Decl*> &WL) {
if (BlockDecl *BD = dyn_cast<BlockDecl>(D))
WL.push_back(BD);
2010-02-16 04:31:36 -05:00
2009-12-15 13:49:47 -05: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);
}
2009-10-14 14:03:49 -04:00
void AnalysisConsumer::HandleCode(Decl *D, Stmt* Body, Actions& actions) {
2009-06-02 13:58:47 -04:00
// Don't run the actions if an error has occured with parsing the file.
2009-11-05 12:18:09 -05:00
if (PP.getDiagnostics().hasErrorOccurred())
2009-06-02 13:58:47 -04:00
return;
// Don't run the actions on declarations in header files unless
// otherwise specified.
if (!Opts.AnalyzeAll &&
!Ctx->getSourceManager().isFromMainFile(D->getLocation()))
2009-10-14 14:03:49 -04:00
return;
2009-06-02 13:58:47 -04:00
2009-10-23 10:22:18 -04:00
// Clear the AnalysisManager of old AnalysisContexts.
Mgr->ClearContexts();
2010-02-16 04:31:36 -05:00
2009-10-14 14:03:49 -04:00
// Dispatch on the actions.
2009-12-15 13:49:47 -05:00
llvm::SmallVector<Decl*, 10> WL;
WL.push_back(D);
2010-02-16 04:31:36 -05:00
2009-12-15 13:49:47 -05:00
if (Body && Opts.AnalyzeNestedBlocks)
FindBlocks(cast<DeclContext>(D), WL);
2010-02-16 04:31:36 -05:00
2009-06-02 13:58:47 -04:00
for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I)
2009-12-15 13:49:47 -05:00
for (llvm::SmallVectorImpl<Decl*>::iterator WI=WL.begin(), WE=WL.end();
WI != WE; ++WI)
(*I)(*this, *Mgr, *WI);
2009-06-02 13:58:47 -04:00
}
//===----------------------------------------------------------------------===//
// Analyses
//===----------------------------------------------------------------------===//
2009-11-18 09:59:57 -05:00
static void ActionWarnDeadStores(AnalysisConsumer &C, AnalysisManager& mgr,
Decl *D) {
2009-10-14 14:03:49 -04:00
if (LiveVariables *L = mgr.getLiveVariables(D)) {
2009-11-18 09:59:57 -05:00
C.DisplayFunction(D);
2009-06-02 13:58:47 -04:00
BugReporter BR(mgr);
2009-10-14 14:03:49 -04:00
CheckDeadStores(*mgr.getCFG(D), *L, mgr.getParentMap(D), BR);
2009-06-02 13:58:47 -04:00
}
}
2009-11-18 09:59:57 -05:00
static void ActionWarnUninitVals(AnalysisConsumer &C, AnalysisManager& mgr,
Decl *D) {
if (CFG* c = mgr.getCFG(D)) {
C.DisplayFunction(D);
2009-10-14 14:03:49 -04:00
CheckUninitializedValues(*c, mgr.getASTContext(), mgr.getDiagnostic());
2009-11-18 09:59:57 -05:00
}
2009-06-02 13:58:47 -04:00
}
2009-12-01 06:08:04 -05:00
static void ActionGRExprEngine(AnalysisConsumer &C, AnalysisManager& mgr,
2010-02-16 04:31:36 -05:00
Decl *D,
2009-10-14 14:03:49 -04:00
GRTransferFuncs* tf) {
2009-06-02 13:58:47 -04:00
llvm::OwningPtr<GRTransferFuncs> TF(tf);
// Display progress.
2009-11-18 09:59:57 -05:00
C.DisplayFunction(D);
2009-06-02 13:58:47 -04:00
2009-10-14 14:03:49 -04: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))
2010-02-16 04:31:36 -05:00
return;
2010-01-15 10:39:40 -05:00
GRExprEngine Eng(mgr, TF.take());
2010-02-16 04:31:36 -05:00
2009-11-18 09:59:57 -05:00
if (C.Opts.EnableExperimentalInternalChecks)
RegisterExperimentalInternalChecks(Eng);
2010-02-16 04:31:36 -05:00
2009-10-14 14:03:49 -04:00
RegisterAppleChecks(Eng, *D);
2010-02-16 04:31:36 -05:00
2009-11-18 09:59:57 -05:00
if (C.Opts.EnableExperimentalChecks)
RegisterExperimentalChecks(Eng);
2010-02-16 04:31:36 -05:00
2009-06-02 13:58:47 -04:00
// Set the graph auditor.
2009-10-14 14:03:49 -04:00
llvm::OwningPtr<ExplodedNode::Auditor> Auditor;
2009-06-02 13:58:47 -04:00
if (mgr.shouldVisualizeUbigraph()) {
Auditor.reset(CreateUbiViz());
2009-10-14 14:03:49 -04:00
ExplodedNode::SetAuditor(Auditor.get());
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Execute the worklist algorithm.
2009-10-14 14:03:49 -04:00
Eng.ExecuteWorkList(mgr.getStackFrame(D));
2009-06-02 13:58:47 -04:00
// Release the auditor (if any) so that it doesn't monitor the graph
// created BugReporter.
2009-10-14 14:03:49 -04:00
ExplodedNode::SetAuditor(0);
2009-06-02 13:58:47 -04:00
// Visualize the exploded graph.
if (mgr.shouldVisualizeGraphviz())
Eng.ViewGraph(mgr.shouldTrimGraph());
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Display warnings.
Eng.getBugReporter().FlushReports();
}
2010-02-16 04:31:36 -05:00
static void ActionObjCMemCheckerAux(AnalysisConsumer &C, AnalysisManager& mgr,
2009-11-18 09:59:57 -05:00
Decl *D, bool GCEnabled) {
2009-10-14 14:03:49 -04:00
GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getASTContext(),
2009-06-02 13:58:47 -04:00
GCEnabled,
mgr.getLangOptions());
2009-10-14 14:03:49 -04:00
2009-11-18 09:59:57 -05:00
ActionGRExprEngine(C, mgr, D, TF);
2009-06-02 13:58:47 -04:00
}
2010-02-16 04:31:36 -05:00
static void ActionObjCMemChecker(AnalysisConsumer &C, AnalysisManager& mgr,
2009-11-18 09:59:57 -05:00
Decl *D) {
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
switch (mgr.getLangOptions().getGCMode()) {
2010-01-23 06:10:26 -05:00
default:
assert (false && "Invalid GC mode.");
case LangOptions::NonGC:
2010-02-16 04:31:36 -05:00
ActionObjCMemCheckerAux(C, mgr, D, false);
2010-01-23 06:10:26 -05:00
break;
case LangOptions::GCOnly:
2010-02-16 04:31:36 -05:00
ActionObjCMemCheckerAux(C, mgr, D, true);
2010-01-23 06:10:26 -05:00
break;
case LangOptions::HybridGC:
2010-02-16 04:31:36 -05:00
ActionObjCMemCheckerAux(C, mgr, D, false);
ActionObjCMemCheckerAux(C, mgr, D, true);
2010-01-23 06:10:26 -05:00
break;
2009-06-02 13:58:47 -04:00
}
}
2009-11-18 09:59:57 -05:00
static void ActionDisplayLiveVariables(AnalysisConsumer &C,
AnalysisManager& mgr, Decl *D) {
2009-10-14 14:03:49 -04:00
if (LiveVariables* L = mgr.getLiveVariables(D)) {
2009-11-18 09:59:57 -05:00
C.DisplayFunction(D);
2009-06-02 13:58:47 -04:00
L->dumpBlockLiveness(mgr.getSourceManager());
}
}
2009-11-18 09:59:57 -05:00
static void ActionCFGDump(AnalysisConsumer &C, AnalysisManager& mgr, Decl *D) {
if (CFG *cfg = mgr.getCFG(D)) {
C.DisplayFunction(D);
cfg->dump(mgr.getLangOptions());
2009-06-02 13:58:47 -04:00
}
}
2009-11-18 09:59:57 -05:00
static void ActionCFGView(AnalysisConsumer &C, AnalysisManager& mgr, Decl *D) {
if (CFG *cfg = mgr.getCFG(D)) {
C.DisplayFunction(D);
cfg->viewCFG(mgr.getLangOptions());
2009-06-02 13:58:47 -04:00
}
}
2009-11-18 09:59:57 -05:00
static void ActionSecuritySyntacticChecks(AnalysisConsumer &C,
AnalysisManager &mgr, Decl *D) {
C.DisplayFunction(D);
2009-10-14 14:03:49 -04:00
BugReporter BR(mgr);
CheckSecuritySyntaxOnly(D, BR);
}
2010-02-16 04:31:36 -05:00
static void ActionLLVMConventionChecker(AnalysisConsumer &C,
AnalysisManager &mgr,
TranslationUnitDecl &TU) {
BugReporter BR(mgr);
CheckLLVMConventions(TU, BR);
}
2009-11-18 09:59:57 -05:00
static void ActionWarnObjCDealloc(AnalysisConsumer &C, AnalysisManager& mgr,
Decl *D) {
2009-06-02 13:58:47 -04:00
if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
return;
2009-10-14 14:03:49 -04:00
2009-11-18 09:59:57 -05:00
C.DisplayFunction(D);
2009-06-02 13:58:47 -04:00
BugReporter BR(mgr);
2009-11-18 09:59:57 -05:00
CheckObjCDealloc(cast<ObjCImplementationDecl>(D), mgr.getLangOptions(), BR);
2009-06-02 13:58:47 -04:00
}
2009-11-18 09:59:57 -05:00
static void ActionWarnObjCUnusedIvars(AnalysisConsumer &C, AnalysisManager& mgr,
Decl *D) {
C.DisplayFunction(D);
2009-06-02 13:58:47 -04:00
BugReporter BR(mgr);
2009-11-18 09:59:57 -05:00
CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(D), BR);
2009-06-02 13:58:47 -04:00
}
2009-11-18 09:59:57 -05:00
static void ActionWarnObjCMethSigs(AnalysisConsumer &C, AnalysisManager& mgr,
Decl *D) {
C.DisplayFunction(D);
2009-06-02 13:58:47 -04:00
BugReporter BR(mgr);
2009-10-14 14:03:49 -04:00
CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(D), BR);
}
2009-11-18 09:59:57 -05:00
static void ActionWarnSizeofPointer(AnalysisConsumer &C, AnalysisManager &mgr,
Decl *D) {
C.DisplayFunction(D);
BugReporter BR(mgr);
CheckSizeofPointer(D, BR);
}
static void ActionInlineCall(AnalysisConsumer &C, AnalysisManager &mgr,
2010-02-16 04:31:36 -05:00
TranslationUnitDecl &TU) {
// Find the entry function definition (if any).
FunctionDecl *D = 0;
// Must specify an entry function.
if (!C.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() == C.Opts.AnalyzeSpecificFunction) {
D = fd;
break;
}
}
}
if (!D)
return;
// FIXME: This is largely copy of ActionGRExprEngine. Needs cleanup.
2010-01-01 05:34:51 -05:00
// Display progress.
C.DisplayFunction(D);
2010-01-15 10:39:40 -05:00
// FIXME: Make a fake transfer function. The GRTransferFunc interface
// eventually will be removed.
GRExprEngine Eng(mgr, new GRTransferFuncs());
2010-01-01 05:34:51 -05:00
if (C.Opts.EnableExperimentalInternalChecks)
RegisterExperimentalInternalChecks(Eng);
2010-02-16 04:31:36 -05:00
2010-01-01 05:34:51 -05:00
RegisterAppleChecks(Eng, *D);
2010-02-16 04:31:36 -05:00
2010-01-01 05:34:51 -05:00
if (C.Opts.EnableExperimentalChecks)
RegisterExperimentalChecks(Eng);
// Register call inliner as the last checker.
RegisterCallInliner(Eng);
// Execute the worklist algorithm.
Eng.ExecuteWorkList(mgr.getStackFrame(D));
// Visualize the exploded graph.
if (mgr.shouldVisualizeGraphviz())
Eng.ViewGraph(mgr.shouldTrimGraph());
// Display warnings.
Eng.getBugReporter().FlushReports();
2009-06-02 13:58:47 -04:00
}
//===----------------------------------------------------------------------===//
// AnalysisConsumer creation.
//===----------------------------------------------------------------------===//
2009-11-05 12:18:09 -05:00
ASTConsumer* clang::CreateAnalysisConsumer(const Preprocessor& pp,
2009-06-02 13:58:47 -04:00
const std::string& OutDir,
const AnalyzerOptions& Opts) {
2009-11-05 12:18:09 -05:00
llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(pp, OutDir, Opts));
2009-06-02 13:58:47 -04:00
for (unsigned i = 0; i < Opts.AnalysisList.size(); ++i)
switch (Opts.AnalysisList[i]) {
#define ANALYSIS(NAME, CMD, DESC, SCOPE)\
2010-01-23 06:10:26 -05:00
case NAME:\
C->add ## SCOPE ## Action(&Action ## NAME);\
break;
2009-06-02 13:58:47 -04:00
#include "clang/Frontend/Analyses.def"
2010-01-23 06:10:26 -05:00
default: break;
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Last, disable the effects of '-Werror' when using the AnalysisConsumer.
2009-11-05 12:18:09 -05:00
pp.getDiagnostics().setWarningsAsErrors(false);
2009-06-02 13:58:47 -04:00
return C.take();
}
//===----------------------------------------------------------------------===//
// Ubigraph Visualization. FIXME: Move to separate file.
//===----------------------------------------------------------------------===//
namespace {
2009-10-14 14:03:49 -04:00
class UbigraphViz : public ExplodedNode::Auditor {
2009-06-02 13:58:47 -04:00
llvm::OwningPtr<llvm::raw_ostream> Out;
llvm::sys::Path Dir, Filename;
unsigned Cntr;
typedef llvm::DenseMap<void*,unsigned> VMap;
VMap M;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
public:
UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
llvm::sys::Path& filename);
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
~UbigraphViz();
2009-10-14 14:03:49 -04:00
virtual void AddEdge(ExplodedNode* Src, ExplodedNode* Dst);
2009-06-02 13:58:47 -04:00
};
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
} // end anonymous namespace
2009-10-14 14:03:49 -04:00
static ExplodedNode::Auditor* CreateUbiViz() {
2009-06-02 13:58:47 -04:00
std::string ErrMsg;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
if (!ErrMsg.empty())
return 0;
llvm::sys::Path Filename = Dir;
Filename.appendComponent("llvm_ubi");
Filename.makeUnique(true,&ErrMsg);
if (!ErrMsg.empty())
return 0;
2009-10-14 14:03:49 -04:00
llvm::errs() << "Writing '" << Filename.str() << "'.\n";
2009-06-02 13:58:47 -04:00
llvm::OwningPtr<llvm::raw_fd_ostream> Stream;
2009-10-14 14:03:49 -04:00
Stream.reset(new llvm::raw_fd_ostream(Filename.c_str(), ErrMsg));
2009-06-02 13:58:47 -04:00
if (!ErrMsg.empty())
return 0;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
return new UbigraphViz(Stream.take(), Dir, Filename);
}
2009-10-14 14:03:49 -04:00
void UbigraphViz::AddEdge(ExplodedNode* Src, ExplodedNode* Dst) {
2009-06-02 13:58:47 -04:00
assert (Src != Dst && "Self-edges are not allowed.");
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Lookup the Src. If it is a new node, it's a root.
VMap::iterator SrcI= M.find(Src);
unsigned SrcID;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
if (SrcI == M.end()) {
M[Src] = SrcID = Cntr++;
*Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n";
}
else
SrcID = SrcI->second;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Lookup the Dst.
VMap::iterator DstI= M.find(Dst);
unsigned DstID;
if (DstI == M.end()) {
M[Dst] = DstID = Cntr++;
*Out << "('vertex', " << DstID << ")\n";
}
else {
// We have hit DstID before. Change its style to reflect a cache hit.
DstID = DstI->second;
*Out << "('change_vertex_style', " << DstID << ", 1)\n";
}
// Add the edge.
2009-10-14 14:03:49 -04:00
*Out << "('edge', " << SrcID << ", " << DstID
2009-06-02 13:58:47 -04:00
<< ", ('arrow','true'), ('oriented', 'true'))\n";
}
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";
}
UbigraphViz::~UbigraphViz() {
Out.reset(0);
2009-10-14 14:03:49 -04:00
llvm::errs() << "Running 'ubiviz' program... ";
2009-06-02 13:58:47 -04: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-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) {
2009-10-14 14:03:49 -04:00
llvm::errs() << "Error viewing graph: " << ErrMsg << "\n";
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Delete the directory.
2009-10-14 14:03:49 -04:00
Dir.eraseFromDisk(true);
2009-06-02 13:58:47 -04:00
}