2008-07-02 04:03:09 +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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-05-20 01:10:40 +04:00
|
|
|
#include "clang/Frontend/AnalysisConsumer.h"
|
2009-03-02 09:16:29 +03:00
|
|
|
#include "clang/Frontend/PathDiagnosticClients.h"
|
|
|
|
#include "clang/Frontend/ManagerRegistry.h"
|
2008-07-02 04:03:09 +04:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclObjC.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
|
|
|
#include "clang/AST/CFG.h"
|
|
|
|
#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"
|
2008-07-03 01:24:01 +04:00
|
|
|
#include "clang/Analysis/PathSensitive/BugReporter.h"
|
2008-07-02 04:03:09 +04: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 04:52:37 +03:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2008-07-02 20:49:11 +04:00
|
|
|
#include "llvm/Support/Streams.h"
|
2008-08-28 02:31:43 +04:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/System/Path.h"
|
2008-08-28 07:54:51 +04:00
|
|
|
#include "llvm/System/Program.h"
|
2008-07-03 08:29:21 +04:00
|
|
|
|
2008-07-02 04:03:09 +04:00
|
|
|
using namespace clang;
|
|
|
|
|
2008-08-28 02:31:43 +04:00
|
|
|
static ExplodedNodeImpl::Auditor* CreateUbiViz();
|
2008-12-22 04:52:37 +03:00
|
|
|
|
2008-07-02 04:03:09 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Basic type definitions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-07-15 03:41:13 +04:00
|
|
|
namespace {
|
2008-07-02 04:03:09 +04:00
|
|
|
class AnalysisManager;
|
2008-07-15 03:41:13 +04:00
|
|
|
typedef void (*CodeAction)(AnalysisManager& Mgr);
|
2008-07-02 04:03:09 +04:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AnalysisConsumer declaration.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN AnalysisConsumer : public ASTConsumer {
|
2008-07-03 08:29:21 +04:00
|
|
|
typedef std::vector<CodeAction> Actions;
|
2008-07-02 04:03:09 +04:00
|
|
|
Actions FunctionActions;
|
|
|
|
Actions ObjCMethodActions;
|
2008-07-03 08:29:21 +04:00
|
|
|
Actions ObjCImplementationActions;
|
2008-11-07 05:09:25 +03:00
|
|
|
Actions TranslationUnitActions;
|
2008-07-02 04:03:09 +04:00
|
|
|
|
|
|
|
public:
|
2008-10-24 05:04:59 +04:00
|
|
|
const LangOptions& LOpts;
|
2008-07-02 04:03:09 +04:00
|
|
|
Diagnostic &Diags;
|
|
|
|
ASTContext* Ctx;
|
|
|
|
Preprocessor* PP;
|
|
|
|
PreprocessorFactory* PPF;
|
2009-02-17 07:27:41 +03:00
|
|
|
const std::string OutDir;
|
2009-05-19 14:18:02 +04:00
|
|
|
AnalyzerOptions Opts;
|
2008-07-02 04:03:09 +04:00
|
|
|
llvm::OwningPtr<PathDiagnosticClient> PD;
|
|
|
|
|
|
|
|
AnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
|
|
|
|
PreprocessorFactory* ppf,
|
|
|
|
const LangOptions& lopts,
|
2009-05-19 14:18:02 +04:00
|
|
|
const std::string& outdir,
|
|
|
|
const AnalyzerOptions& opts)
|
2009-02-17 07:27:41 +03:00
|
|
|
: LOpts(lopts), Diags(diags),
|
2008-07-02 04:03:09 +04:00
|
|
|
Ctx(0), PP(pp), PPF(ppf),
|
2009-05-19 14:18:02 +04:00
|
|
|
OutDir(outdir), Opts(opts) {}
|
2008-07-02 04:03:09 +04:00
|
|
|
|
|
|
|
void addCodeAction(CodeAction action) {
|
2008-07-03 08:29:21 +04:00
|
|
|
FunctionActions.push_back(action);
|
|
|
|
ObjCMethodActions.push_back(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addObjCImplementationAction(CodeAction action) {
|
|
|
|
ObjCImplementationActions.push_back(action);
|
2008-07-02 04:03:09 +04:00
|
|
|
}
|
|
|
|
|
2008-11-07 05:09:25 +03:00
|
|
|
void addTranslationUnitAction(CodeAction action) {
|
|
|
|
TranslationUnitActions.push_back(action);
|
|
|
|
}
|
|
|
|
|
2008-07-02 04:03:09 +04:00
|
|
|
virtual void Initialize(ASTContext &Context) {
|
|
|
|
Ctx = &Context;
|
|
|
|
}
|
|
|
|
|
2009-03-29 20:50:03 +04:00
|
|
|
virtual void HandleTopLevelDecl(DeclGroupRef D) {
|
|
|
|
for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
|
|
|
|
HandleTopLevelSingleDecl(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandleTopLevelSingleDecl(Decl *D);
|
2009-03-28 07:11:33 +03:00
|
|
|
virtual void HandleTranslationUnit(ASTContext &C);
|
2008-07-03 08:29:21 +04:00
|
|
|
|
2009-02-02 23:52:40 +03:00
|
|
|
void HandleCode(Decl* D, Stmt* Body, Actions& actions);
|
2008-07-02 04:03:09 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-07-03 01:24:01 +04:00
|
|
|
class VISIBILITY_HIDDEN AnalysisManager : public BugReporterData {
|
2008-11-05 22:05:06 +03:00
|
|
|
Decl* D; Stmt* Body;
|
|
|
|
|
|
|
|
enum AnalysisScope { ScopeTU, ScopeDecl } AScope;
|
|
|
|
|
2008-07-02 04:03:09 +04:00
|
|
|
AnalysisConsumer& C;
|
2008-07-02 20:49:11 +04:00
|
|
|
bool DisplayedFunction;
|
2008-07-02 04:03:09 +04:00
|
|
|
|
|
|
|
llvm::OwningPtr<CFG> cfg;
|
|
|
|
llvm::OwningPtr<LiveVariables> liveness;
|
|
|
|
llvm::OwningPtr<ParentMap> PM;
|
|
|
|
|
2008-11-27 04:55:08 +03:00
|
|
|
// Configurable components creators.
|
|
|
|
StoreManagerCreator CreateStoreMgr;
|
|
|
|
ConstraintManagerCreator CreateConstraintMgr;
|
|
|
|
|
2008-07-02 04:03:09 +04:00
|
|
|
public:
|
2009-01-23 23:52:26 +03:00
|
|
|
AnalysisManager(AnalysisConsumer& c, Decl* d, Stmt* b, bool displayProgress)
|
2009-03-28 07:05:05 +03:00
|
|
|
: D(d), Body(b), AScope(ScopeDecl), C(c),
|
2009-01-23 23:52:26 +03:00
|
|
|
DisplayedFunction(!displayProgress) {
|
2008-11-27 04:55:08 +03:00
|
|
|
setManagerCreators();
|
|
|
|
}
|
2008-11-05 22:05:06 +03:00
|
|
|
|
2009-03-28 07:05:05 +03:00
|
|
|
AnalysisManager(AnalysisConsumer& c, bool displayProgress)
|
|
|
|
: D(0), Body(0), AScope(ScopeTU), C(c),
|
2009-01-23 23:52:26 +03:00
|
|
|
DisplayedFunction(!displayProgress) {
|
2008-11-27 04:55:08 +03:00
|
|
|
setManagerCreators();
|
|
|
|
}
|
2008-07-02 04:03:09 +04:00
|
|
|
|
2008-11-05 22:05:06 +03:00
|
|
|
Decl* getCodeDecl() const {
|
|
|
|
assert (AScope == ScopeDecl);
|
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stmt* getBody() const {
|
|
|
|
assert (AScope == ScopeDecl);
|
|
|
|
return Body;
|
|
|
|
}
|
2008-07-02 04:03:09 +04:00
|
|
|
|
2008-11-27 04:55:08 +03:00
|
|
|
StoreManagerCreator getStoreManagerCreator() {
|
|
|
|
return CreateStoreMgr;
|
2008-10-24 05:04:59 +04:00
|
|
|
};
|
2008-11-27 04:55:08 +03:00
|
|
|
|
|
|
|
ConstraintManagerCreator getConstraintManagerCreator() {
|
|
|
|
return CreateConstraintMgr;
|
|
|
|
}
|
2008-10-24 05:04:59 +04:00
|
|
|
|
2008-07-03 09:26:14 +04:00
|
|
|
virtual CFG* getCFG() {
|
2008-07-02 04:03:09 +04:00
|
|
|
if (!cfg) cfg.reset(CFG::buildCFG(getBody()));
|
2008-07-03 09:26:14 +04:00
|
|
|
return cfg.get();
|
2008-07-02 04:03:09 +04:00
|
|
|
}
|
|
|
|
|
2008-07-03 01:24:01 +04:00
|
|
|
virtual ParentMap& getParentMap() {
|
2008-07-03 03:16:33 +04:00
|
|
|
if (!PM)
|
|
|
|
PM.reset(new ParentMap(getBody()));
|
2008-07-03 01:24:01 +04:00
|
|
|
return *PM.get();
|
2008-07-02 04:03:09 +04:00
|
|
|
}
|
|
|
|
|
2008-07-03 01:24:01 +04:00
|
|
|
virtual ASTContext& getContext() {
|
2008-07-02 04:03:09 +04:00
|
|
|
return *C.Ctx;
|
|
|
|
}
|
|
|
|
|
2008-07-03 01:24:01 +04:00
|
|
|
virtual SourceManager& getSourceManager() {
|
2008-07-02 22:11:29 +04:00
|
|
|
return getContext().getSourceManager();
|
|
|
|
}
|
|
|
|
|
2008-07-03 01:24:01 +04:00
|
|
|
virtual Diagnostic& getDiagnostic() {
|
2008-07-02 04:03:09 +04:00
|
|
|
return C.Diags;
|
|
|
|
}
|
2008-07-02 04:44:58 +04:00
|
|
|
|
|
|
|
const LangOptions& getLangOptions() const {
|
|
|
|
return C.LOpts;
|
|
|
|
}
|
|
|
|
|
2008-07-03 01:24:01 +04:00
|
|
|
virtual PathDiagnosticClient* getPathDiagnosticClient() {
|
2009-02-17 07:27:41 +03:00
|
|
|
if (C.PD.get() == 0 && !C.OutDir.empty()) {
|
2009-05-19 14:18:02 +04:00
|
|
|
switch (C.Opts.AnalysisDiagOpt) {
|
2008-11-04 02:18:07 +03:00
|
|
|
default:
|
2009-01-23 23:06:20 +03:00
|
|
|
#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE)\
|
2009-02-17 07:27:41 +03:00
|
|
|
case PD_##NAME: C.PD.reset(CREATEFN(C.OutDir, C.PP, C.PPF)); break;
|
2009-05-20 01:16:18 +04:00
|
|
|
#include "clang/Frontend/Analyses.def"
|
2008-11-04 02:18:07 +03:00
|
|
|
}
|
|
|
|
}
|
2008-07-03 03:16:33 +04:00
|
|
|
return C.PD.get();
|
2008-07-02 04:44:58 +04:00
|
|
|
}
|
2008-07-02 04:03:09 +04:00
|
|
|
|
2008-07-03 09:26:14 +04:00
|
|
|
virtual LiveVariables* getLiveVariables() {
|
2008-07-02 22:11:29 +04:00
|
|
|
if (!liveness) {
|
2008-07-03 09:26:14 +04:00
|
|
|
CFG* c = getCFG();
|
|
|
|
if (!c) return 0;
|
|
|
|
|
2008-12-09 03:17:51 +03:00
|
|
|
liveness.reset(new LiveVariables(getContext(), *c));
|
2008-07-03 09:26:14 +04:00
|
|
|
liveness->runOnCFG(*c);
|
|
|
|
liveness->runOnAllBlocks(*c, 0, true);
|
2008-07-02 22:11:29 +04:00
|
|
|
}
|
2008-07-03 01:24:01 +04:00
|
|
|
|
2008-07-03 09:26:14 +04:00
|
|
|
return liveness.get();
|
2008-07-02 04:03:09 +04:00
|
|
|
}
|
2008-07-02 20:49:11 +04:00
|
|
|
|
2009-05-19 14:18:02 +04:00
|
|
|
bool shouldVisualizeGraphviz() const { return C.Opts.VisualizeEGDot; }
|
2009-02-17 07:27:41 +03:00
|
|
|
|
2009-05-19 14:18:02 +04:00
|
|
|
bool shouldVisualizeUbigraph() const { return C.Opts.VisualizeEGUbi; }
|
2008-11-27 04:55:08 +03:00
|
|
|
|
2008-07-02 20:49:11 +04:00
|
|
|
bool shouldVisualize() const {
|
2009-05-19 14:18:02 +04:00
|
|
|
return C.Opts.VisualizeEGDot || C.Opts.VisualizeEGUbi;
|
2008-07-02 20:49:11 +04:00
|
|
|
}
|
2009-02-17 07:27:41 +03:00
|
|
|
|
2009-05-19 14:18:02 +04:00
|
|
|
bool shouldTrimGraph() const { return C.Opts.TrimGraph; }
|
|
|
|
|
|
|
|
bool shouldPurgeDead() const { return C.Opts.PurgeDead; }
|
|
|
|
|
|
|
|
bool shouldEagerlyAssume() const { return C.Opts.EagerlyAssume; }
|
2009-02-17 07:27:41 +03:00
|
|
|
|
2008-07-02 20:49:11 +04:00
|
|
|
void DisplayFunction() {
|
|
|
|
|
|
|
|
if (DisplayedFunction)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DisplayedFunction = true;
|
|
|
|
|
2009-01-27 10:57:44 +03:00
|
|
|
// FIXME: Is getCodeDecl() always a named decl?
|
|
|
|
if (isa<FunctionDecl>(getCodeDecl()) ||
|
|
|
|
isa<ObjCMethodDecl>(getCodeDecl())) {
|
|
|
|
NamedDecl *ND = cast<NamedDecl>(getCodeDecl());
|
|
|
|
SourceManager &SM = getContext().getSourceManager();
|
2008-11-20 19:14:48 +03:00
|
|
|
llvm::cerr << "ANALYZE: "
|
2009-01-27 10:57:44 +03:00
|
|
|
<< SM.getPresumedLoc(ND->getLocation()).getFilename()
|
|
|
|
<< ' ' << ND->getNameAsString() << '\n';
|
2008-07-02 20:49:11 +04:00
|
|
|
}
|
|
|
|
}
|
2008-11-27 04:55:08 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// Set configurable analyzer components creators. First check if there are
|
|
|
|
/// components registered at runtime. Otherwise fall back to builtin
|
|
|
|
/// components.
|
|
|
|
void setManagerCreators() {
|
|
|
|
if (ManagerRegistry::StoreMgrCreator != 0) {
|
|
|
|
CreateStoreMgr = ManagerRegistry::StoreMgrCreator;
|
|
|
|
}
|
|
|
|
else {
|
2009-05-19 14:18:02 +04:00
|
|
|
switch (C.Opts.AnalysisStoreOpt) {
|
2008-11-27 04:55:08 +03:00
|
|
|
default:
|
|
|
|
assert(0 && "Unknown store manager.");
|
2009-02-17 07:27:41 +03:00
|
|
|
#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \
|
|
|
|
case NAME##Model: CreateStoreMgr = CREATEFN; break;
|
2009-05-20 01:16:18 +04:00
|
|
|
#include "clang/Frontend/Analyses.def"
|
2008-11-27 04:55:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ManagerRegistry::ConstraintMgrCreator != 0)
|
|
|
|
CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator;
|
2009-02-17 07:27:41 +03:00
|
|
|
else {
|
2009-05-19 14:18:02 +04:00
|
|
|
switch (C.Opts.AnalysisConstraintsOpt) {
|
2009-02-17 07:27:41 +03:00
|
|
|
default:
|
|
|
|
assert(0 && "Unknown store manager.");
|
|
|
|
#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \
|
|
|
|
case NAME##Model: CreateConstraintMgr = CREATEFN; break;
|
2009-05-20 01:16:18 +04:00
|
|
|
#include "clang/Frontend/Analyses.def"
|
2009-02-17 07:27:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-23 23:06:20 +03:00
|
|
|
|
|
|
|
// Some DiagnosticClients should be created all the time instead of
|
|
|
|
// lazily. Create those now.
|
2009-05-19 14:18:02 +04:00
|
|
|
switch (C.Opts.AnalysisDiagOpt) {
|
2009-01-23 23:06:20 +03:00
|
|
|
default: break;
|
|
|
|
#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE)\
|
|
|
|
case PD_##NAME: if (AUTOCREATE) getPathDiagnosticClient(); break;
|
2009-05-20 01:16:18 +04:00
|
|
|
#include "clang/Frontend/Analyses.def"
|
2009-01-23 23:06:20 +03:00
|
|
|
}
|
2008-11-27 04:55:08 +03:00
|
|
|
}
|
|
|
|
|
2008-07-02 04:03:09 +04:00
|
|
|
};
|
2008-11-27 04:55:08 +03:00
|
|
|
|
2008-07-02 04:03:09 +04: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)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AnalysisConsumer implementation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-03-29 20:50:03 +04:00
|
|
|
void AnalysisConsumer::HandleTopLevelSingleDecl(Decl *D) {
|
2008-07-02 04:03:09 +04:00
|
|
|
switch (D->getKind()) {
|
|
|
|
case Decl::Function: {
|
|
|
|
FunctionDecl* FD = cast<FunctionDecl>(D);
|
2008-07-02 22:11:29 +04:00
|
|
|
|
2009-05-19 14:18:02 +04:00
|
|
|
if (Opts.AnalyzeSpecificFunction.size() > 0 &&
|
|
|
|
Opts.AnalyzeSpecificFunction != FD->getIdentifier()->getName())
|
2008-07-02 22:11:29 +04:00
|
|
|
break;
|
|
|
|
|
2009-04-18 04:02:19 +04:00
|
|
|
Stmt* Body = FD->getBody(*Ctx);
|
2008-07-02 04:03:09 +04:00
|
|
|
if (Body) HandleCode(FD, Body, FunctionActions);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Decl::ObjCMethod: {
|
|
|
|
ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D);
|
2008-07-02 22:11:29 +04:00
|
|
|
|
2009-05-19 14:18:02 +04:00
|
|
|
if (Opts.AnalyzeSpecificFunction.size() > 0 &&
|
|
|
|
Opts.AnalyzeSpecificFunction != MD->getSelector().getAsString())
|
2008-07-02 22:11:29 +04:00
|
|
|
return;
|
|
|
|
|
2008-07-02 04:03:09 +04:00
|
|
|
Stmt* Body = MD->getBody();
|
|
|
|
if (Body) HandleCode(MD, Body, ObjCMethodActions);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-28 07:11:33 +03:00
|
|
|
void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
|
2008-07-03 08:29:21 +04:00
|
|
|
|
2008-11-07 05:09:25 +03:00
|
|
|
if(!TranslationUnitActions.empty()) {
|
2009-05-19 14:18:02 +04:00
|
|
|
AnalysisManager mgr(*this, Opts.AnalyzerDisplayProgress);
|
2008-11-07 05:09:25 +03:00
|
|
|
for (Actions::iterator I = TranslationUnitActions.begin(),
|
|
|
|
E = TranslationUnitActions.end(); I != E; ++I)
|
|
|
|
(*I)(mgr);
|
|
|
|
}
|
|
|
|
|
2009-03-28 06:29:40 +03:00
|
|
|
if (!ObjCImplementationActions.empty()) {
|
2009-03-28 07:11:33 +03:00
|
|
|
TranslationUnitDecl *TUD = C.getTranslationUnitDecl();
|
2009-03-28 06:29:40 +03:00
|
|
|
|
2009-04-10 01:40:53 +04:00
|
|
|
for (DeclContext::decl_iterator I = TUD->decls_begin(C),
|
|
|
|
E = TUD->decls_end(C);
|
2009-03-28 06:29:40 +03:00
|
|
|
I != E; ++I)
|
2009-02-13 03:51:30 +03:00
|
|
|
if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I))
|
|
|
|
HandleCode(ID, 0, ObjCImplementationActions);
|
2009-03-28 06:29:40 +03:00
|
|
|
}
|
2009-02-13 03:51:30 +03:00
|
|
|
|
|
|
|
// Delete the PathDiagnosticClient here just in case the AnalysisConsumer
|
|
|
|
// object doesn't get released. This will cause any side-effects in the
|
|
|
|
// destructor of the PathDiagnosticClient to get executed.
|
|
|
|
PD.reset();
|
2008-07-03 08:29:21 +04:00
|
|
|
}
|
|
|
|
|
2009-02-02 23:52:40 +03:00
|
|
|
void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions& actions) {
|
2008-07-02 04:03:09 +04:00
|
|
|
|
|
|
|
// Don't run the actions if an error has occured with parsing the file.
|
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
return;
|
2009-02-02 23:52:40 +03:00
|
|
|
|
2008-07-02 04:03:09 +04:00
|
|
|
// Don't run the actions on declarations in header files unless
|
|
|
|
// otherwise specified.
|
2009-05-19 14:18:02 +04:00
|
|
|
if (!Opts.AnalyzeAll &&
|
|
|
|
!Ctx->getSourceManager().isFromMainFile(D->getLocation()))
|
2008-07-02 04:03:09 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Create an AnalysisManager that will manage the state for analyzing
|
|
|
|
// this method/function.
|
2009-05-19 14:18:02 +04:00
|
|
|
AnalysisManager mgr(*this, D, Body, Opts.AnalyzerDisplayProgress);
|
2008-07-02 04:03:09 +04:00
|
|
|
|
|
|
|
// Dispatch on the actions.
|
2008-10-30 08:03:28 +03:00
|
|
|
for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I)
|
2008-07-03 08:29:21 +04:00
|
|
|
(*I)(mgr);
|
2008-07-02 04:03:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Analyses
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-07-15 03:41:13 +04:00
|
|
|
static void ActionWarnDeadStores(AnalysisManager& mgr) {
|
2008-07-03 09:26:14 +04:00
|
|
|
if (LiveVariables* L = mgr.getLiveVariables()) {
|
|
|
|
BugReporter BR(mgr);
|
|
|
|
CheckDeadStores(*L, BR);
|
|
|
|
}
|
2008-07-02 04:03:09 +04:00
|
|
|
}
|
|
|
|
|
2008-07-15 03:41:13 +04:00
|
|
|
static void ActionWarnUninitVals(AnalysisManager& mgr) {
|
2008-07-03 09:26:14 +04:00
|
|
|
if (CFG* c = mgr.getCFG())
|
|
|
|
CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic());
|
2008-07-02 04:03:09 +04:00
|
|
|
}
|
|
|
|
|
2008-07-02 04:44:58 +04:00
|
|
|
|
2008-07-22 20:21:24 +04:00
|
|
|
static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf,
|
|
|
|
bool StandardWarnings = true) {
|
2008-07-02 20:35:50 +04:00
|
|
|
|
2008-07-03 09:26:14 +04:00
|
|
|
|
2008-07-02 20:35:50 +04:00
|
|
|
llvm::OwningPtr<GRTransferFuncs> TF(tf);
|
2008-07-03 09:26:14 +04:00
|
|
|
|
2008-11-24 23:53:32 +03:00
|
|
|
// Display progress.
|
|
|
|
mgr.DisplayFunction();
|
|
|
|
|
2008-07-03 09:26:14 +04:00
|
|
|
// Construct the analysis engine.
|
|
|
|
LiveVariables* L = mgr.getLiveVariables();
|
|
|
|
if (!L) return;
|
2008-11-24 23:53:32 +03:00
|
|
|
|
2009-02-05 02:49:09 +03:00
|
|
|
GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L, mgr,
|
2009-05-19 14:18:02 +04:00
|
|
|
mgr.shouldPurgeDead(), mgr.shouldEagerlyAssume(),
|
2008-11-27 04:55:08 +03:00
|
|
|
mgr.getStoreManagerCreator(),
|
|
|
|
mgr.getConstraintManagerCreator());
|
2008-10-24 05:04:59 +04:00
|
|
|
|
2008-07-02 20:35:50 +04:00
|
|
|
Eng.setTransferFunctions(tf);
|
2008-07-02 04:44:58 +04:00
|
|
|
|
2008-07-22 20:21:24 +04:00
|
|
|
if (StandardWarnings) {
|
|
|
|
Eng.RegisterInternalChecks();
|
|
|
|
RegisterAppleChecks(Eng);
|
|
|
|
}
|
2008-08-28 02:31:43 +04:00
|
|
|
|
|
|
|
// Set the graph auditor.
|
|
|
|
llvm::OwningPtr<ExplodedNodeImpl::Auditor> Auditor;
|
|
|
|
if (mgr.shouldVisualizeUbigraph()) {
|
|
|
|
Auditor.reset(CreateUbiViz());
|
|
|
|
ExplodedNodeImpl::SetAuditor(Auditor.get());
|
|
|
|
}
|
2008-07-22 20:21:24 +04:00
|
|
|
|
2008-07-02 04:44:58 +04:00
|
|
|
// Execute the worklist algorithm.
|
|
|
|
Eng.ExecuteWorkList();
|
2008-07-02 20:35:50 +04:00
|
|
|
|
2008-08-28 02:31:43 +04:00
|
|
|
// Release the auditor (if any) so that it doesn't monitor the graph
|
|
|
|
// created BugReporter.
|
|
|
|
ExplodedNodeImpl::SetAuditor(0);
|
2009-03-11 04:42:29 +03:00
|
|
|
|
2008-07-02 20:49:11 +04:00
|
|
|
// Visualize the exploded graph.
|
2008-08-28 02:31:43 +04:00
|
|
|
if (mgr.shouldVisualizeGraphviz())
|
2008-07-02 20:49:11 +04:00
|
|
|
Eng.ViewGraph(mgr.shouldTrimGraph());
|
2009-03-11 04:42:29 +03:00
|
|
|
|
|
|
|
// Display warnings.
|
|
|
|
Eng.getBugReporter().FlushReports();
|
2008-07-02 20:35:50 +04:00
|
|
|
}
|
|
|
|
|
2008-07-15 03:41:13 +04:00
|
|
|
static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled,
|
2008-07-22 20:21:24 +04:00
|
|
|
bool StandardWarnings) {
|
|
|
|
|
2008-07-02 20:35:50 +04:00
|
|
|
GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(),
|
|
|
|
GCEnabled,
|
|
|
|
mgr.getLangOptions());
|
|
|
|
|
2008-07-22 20:21:24 +04:00
|
|
|
ActionGRExprEngine(mgr, TF, StandardWarnings);
|
2008-07-02 04:44:58 +04:00
|
|
|
}
|
|
|
|
|
2008-07-15 03:41:13 +04:00
|
|
|
static void ActionCheckerCFRef(AnalysisManager& mgr) {
|
2008-07-02 04:44:58 +04:00
|
|
|
|
|
|
|
switch (mgr.getLangOptions().getGCMode()) {
|
|
|
|
default:
|
|
|
|
assert (false && "Invalid GC mode.");
|
|
|
|
case LangOptions::NonGC:
|
2008-07-15 03:41:13 +04:00
|
|
|
ActionCheckerCFRefAux(mgr, false, true);
|
2008-07-02 04:44:58 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LangOptions::GCOnly:
|
2008-07-15 03:41:13 +04:00
|
|
|
ActionCheckerCFRefAux(mgr, true, true);
|
2008-07-02 04:44:58 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LangOptions::HybridGC:
|
2008-07-15 03:41:13 +04:00
|
|
|
ActionCheckerCFRefAux(mgr, false, true);
|
|
|
|
ActionCheckerCFRefAux(mgr, true, false);
|
2008-07-02 04:44:58 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-15 03:41:13 +04:00
|
|
|
static void ActionCheckerSimple(AnalysisManager& mgr) {
|
2008-07-02 20:35:50 +04:00
|
|
|
ActionGRExprEngine(mgr, MakeGRSimpleValsTF());
|
|
|
|
}
|
|
|
|
|
2008-07-15 03:41:13 +04:00
|
|
|
static void ActionDisplayLiveVariables(AnalysisManager& mgr) {
|
2008-07-03 09:26:14 +04:00
|
|
|
if (LiveVariables* L = mgr.getLiveVariables()) {
|
|
|
|
mgr.DisplayFunction();
|
|
|
|
L->dumpBlockLiveness(mgr.getSourceManager());
|
|
|
|
}
|
2008-07-02 22:11:29 +04:00
|
|
|
}
|
|
|
|
|
2008-07-02 22:23:21 +04:00
|
|
|
static void ActionCFGDump(AnalysisManager& mgr) {
|
2008-07-03 09:26:14 +04:00
|
|
|
if (CFG* c = mgr.getCFG()) {
|
|
|
|
mgr.DisplayFunction();
|
|
|
|
c->dump();
|
|
|
|
}
|
2008-07-02 22:23:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ActionCFGView(AnalysisManager& mgr) {
|
2008-07-03 09:26:14 +04:00
|
|
|
if (CFG* c = mgr.getCFG()) {
|
|
|
|
mgr.DisplayFunction();
|
|
|
|
c->viewCFG();
|
|
|
|
}
|
2008-07-02 22:23:21 +04:00
|
|
|
}
|
|
|
|
|
2008-07-15 03:41:13 +04:00
|
|
|
static void ActionWarnObjCDealloc(AnalysisManager& mgr) {
|
2008-08-04 21:14:10 +04:00
|
|
|
if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
|
|
|
|
return;
|
|
|
|
|
2008-07-03 08:29:21 +04:00
|
|
|
BugReporter BR(mgr);
|
2008-07-03 18:35:01 +04:00
|
|
|
|
|
|
|
CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
|
|
|
|
mgr.getLangOptions(), BR);
|
2008-07-03 08:29:21 +04:00
|
|
|
}
|
|
|
|
|
2008-07-23 04:45:26 +04:00
|
|
|
static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) {
|
|
|
|
BugReporter BR(mgr);
|
|
|
|
CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR);
|
|
|
|
}
|
|
|
|
|
2008-07-15 03:41:13 +04:00
|
|
|
static void ActionWarnObjCMethSigs(AnalysisManager& mgr) {
|
2008-07-12 02:40:47 +04:00
|
|
|
BugReporter BR(mgr);
|
|
|
|
|
|
|
|
CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
|
|
|
|
BR);
|
|
|
|
}
|
|
|
|
|
2008-07-02 04:03:09 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AnalysisConsumer creation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-02-17 07:27:41 +03:00
|
|
|
ASTConsumer* clang::CreateAnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
|
2008-07-02 04:03:09 +04:00
|
|
|
PreprocessorFactory* ppf,
|
|
|
|
const LangOptions& lopts,
|
2009-05-19 14:18:02 +04:00
|
|
|
const std::string& OutDir,
|
|
|
|
const AnalyzerOptions& Opts) {
|
2009-02-17 07:27:41 +03:00
|
|
|
|
|
|
|
llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(diags, pp, ppf,
|
2009-05-19 14:18:02 +04:00
|
|
|
lopts, OutDir,
|
|
|
|
Opts));
|
2009-02-17 07:27:41 +03:00
|
|
|
|
2009-05-19 14:18:02 +04:00
|
|
|
for (unsigned i = 0; i < Opts.AnalysisList.size(); ++i)
|
|
|
|
switch (Opts.AnalysisList[i]) {
|
2008-07-15 04:46:02 +04:00
|
|
|
#define ANALYSIS(NAME, CMD, DESC, SCOPE)\
|
2008-07-15 03:41:13 +04:00
|
|
|
case NAME:\
|
2008-07-15 04:46:02 +04:00
|
|
|
C->add ## SCOPE ## Action(&Action ## NAME);\
|
2008-07-02 04:03:09 +04:00
|
|
|
break;
|
2009-05-20 01:16:18 +04:00
|
|
|
#include "clang/Frontend/Analyses.def"
|
2008-07-02 04:03:09 +04:00
|
|
|
default: break;
|
|
|
|
}
|
2009-05-07 23:02:53 +04:00
|
|
|
|
|
|
|
// Last, disable the effects of '-Werror' when using the AnalysisConsumer.
|
|
|
|
diags.setWarningsAsErrors(false);
|
2009-02-17 07:27:41 +03:00
|
|
|
|
2008-07-02 04:03:09 +04:00
|
|
|
return C.take();
|
|
|
|
}
|
|
|
|
|
2008-08-28 02:31:43 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Ubigraph Visualization. FIXME: Move to separate file.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class UbigraphViz : public ExplodedNodeImpl::Auditor {
|
|
|
|
llvm::OwningPtr<llvm::raw_ostream> Out;
|
2008-08-28 07:54:51 +04:00
|
|
|
llvm::sys::Path Dir, Filename;
|
2008-08-28 02:31:43 +04:00
|
|
|
unsigned Cntr;
|
|
|
|
|
|
|
|
typedef llvm::DenseMap<void*,unsigned> VMap;
|
|
|
|
VMap M;
|
|
|
|
|
|
|
|
public:
|
2008-08-28 07:54:51 +04:00
|
|
|
UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
|
2008-08-28 09:02:09 +04:00
|
|
|
llvm::sys::Path& filename);
|
2008-08-28 07:54:51 +04:00
|
|
|
|
|
|
|
~UbigraphViz();
|
|
|
|
|
2008-08-28 02:31:43 +04:00
|
|
|
virtual void AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
static ExplodedNodeImpl::Auditor* CreateUbiViz() {
|
|
|
|
std::string ErrMsg;
|
|
|
|
|
2008-08-28 07:54:51 +04:00
|
|
|
llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
|
2008-08-28 02:31:43 +04:00
|
|
|
if (!ErrMsg.empty())
|
|
|
|
return 0;
|
|
|
|
|
2008-08-28 07:54:51 +04:00
|
|
|
llvm::sys::Path Filename = Dir;
|
2008-08-28 02:31:43 +04:00
|
|
|
Filename.appendComponent("llvm_ubi");
|
|
|
|
Filename.makeUnique(true,&ErrMsg);
|
|
|
|
|
|
|
|
if (!ErrMsg.empty())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
llvm::cerr << "Writing '" << Filename << "'.\n";
|
|
|
|
|
|
|
|
llvm::OwningPtr<llvm::raw_fd_ostream> Stream;
|
|
|
|
std::string filename = Filename.toString();
|
2008-11-13 08:09:21 +03:00
|
|
|
Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), false, ErrMsg));
|
2008-08-28 02:31:43 +04:00
|
|
|
|
|
|
|
if (!ErrMsg.empty())
|
|
|
|
return 0;
|
|
|
|
|
2008-08-28 07:54:51 +04:00
|
|
|
return new UbigraphViz(Stream.take(), Dir, Filename);
|
2008-08-28 02:31:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void UbigraphViz::AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst) {
|
2008-08-28 22:34:41 +04:00
|
|
|
|
|
|
|
assert (Src != Dst && "Self-edges are not allowed.");
|
|
|
|
|
2008-08-28 02:31:43 +04:00
|
|
|
// Lookup the Src. If it is a new node, it's a root.
|
|
|
|
VMap::iterator SrcI= M.find(Src);
|
|
|
|
unsigned SrcID;
|
|
|
|
|
|
|
|
if (SrcI == M.end()) {
|
|
|
|
M[Src] = SrcID = Cntr++;
|
|
|
|
*Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
SrcID = SrcI->second;
|
|
|
|
|
|
|
|
// 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 09:02:09 +04:00
|
|
|
else {
|
|
|
|
// We have hit DstID before. Change its style to reflect a cache hit.
|
2008-08-28 02:31:43 +04:00
|
|
|
DstID = DstI->second;
|
2008-08-28 09:02:09 +04:00
|
|
|
*Out << "('change_vertex_style', " << DstID << ", 1)\n";
|
|
|
|
}
|
2008-08-28 02:31:43 +04:00
|
|
|
|
|
|
|
// Add the edge.
|
2008-08-28 02:46:55 +04:00
|
|
|
*Out << "('edge', " << SrcID << ", " << DstID
|
|
|
|
<< ", ('arrow','true'), ('oriented', 'true'))\n";
|
2008-08-28 02:31:43 +04:00
|
|
|
}
|
|
|
|
|
2008-08-28 09:02:09 +04: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 07:54:51 +04:00
|
|
|
UbigraphViz::~UbigraphViz() {
|
|
|
|
Out.reset(0);
|
|
|
|
llvm::cerr << "Running 'ubiviz' program... ";
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) {
|
|
|
|
llvm::cerr << "Error viewing graph: " << ErrMsg << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the directory.
|
|
|
|
Dir.eraseFromDisk(true);
|
2008-08-29 07:45:59 +04:00
|
|
|
}
|