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"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/ADT/OwningPtr.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"
|
2008-07-03 00:49:11 +08:00
|
|
|
#include "llvm/Support/Streams.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"
|
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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-07-15 07:41:13 +08:00
|
|
|
namespace {
|
|
|
|
typedef void (*CodeAction)(AnalysisManager& Mgr);
|
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) {
|
|
|
|
llvm::sys::Path F(prefix);
|
|
|
|
PathDiagnosticClientFactory *PF =
|
|
|
|
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;
|
2008-07-02 08:03:09 +08:00
|
|
|
|
|
|
|
public:
|
2008-10-24 09:04:59 +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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addObjCImplementationAction(CodeAction action) {
|
|
|
|
ObjCImplementationActions.push_back(action);
|
2008-07-02 08:03:09 +08:00
|
|
|
}
|
|
|
|
|
2008-11-07 10:09:25 +08:00
|
|
|
void addTranslationUnitAction(CodeAction action) {
|
|
|
|
TranslationUnitActions.push_back(action);
|
|
|
|
}
|
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
virtual void Initialize(ASTContext &Context) {
|
|
|
|
Ctx = &Context;
|
2009-08-03 11:27:37 +08:00
|
|
|
Mgr.reset(new AnalysisManager(*Ctx, Diags, LOpts, PD,
|
2009-08-03 11:13:46 +08:00
|
|
|
CreateStoreMgr, CreateConstraintMgr,
|
|
|
|
Opts.AnalyzerDisplayProgress,
|
|
|
|
Opts.VisualizeEGDot, Opts.VisualizeEGUbi,
|
|
|
|
Opts.PurgeDead, Opts.EagerlyAssume,
|
|
|
|
Opts.TrimGraph));
|
2008-07-02 08:03:09 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandleTopLevelSingleDecl(Decl *D);
|
2009-03-28 12:11:33 +08:00
|
|
|
virtual void HandleTranslationUnit(ASTContext &C);
|
2008-07-03 12:29:21 +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
|
|
|
};
|
|
|
|
|
|
|
|
|
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)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AnalysisConsumer implementation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-03-30 00:50:03 +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-05-19 18:18:02 +08:00
|
|
|
if (Opts.AnalyzeSpecificFunction.size() > 0 &&
|
|
|
|
Opts.AnalyzeSpecificFunction != FD->getIdentifier()->getName())
|
2008-07-03 02:11:29 +08:00
|
|
|
break;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Decl::ObjCMethod: {
|
|
|
|
ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D);
|
2008-07-03 02:11:29 +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;
|
|
|
|
|
2008-07-02 08:03:09 +08:00
|
|
|
Stmt* Body = MD->getBody();
|
|
|
|
if (Body) HandleCode(MD, Body, ObjCMethodActions);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-28 12:11:33 +08:00
|
|
|
void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
|
2008-11-07 10:09:25 +08:00
|
|
|
if(!TranslationUnitActions.empty()) {
|
|
|
|
for (Actions::iterator I = TranslationUnitActions.begin(),
|
|
|
|
E = TranslationUnitActions.end(); I != E; ++I)
|
2009-08-03 11:13:46 +08:00
|
|
|
(*I)(*Mgr);
|
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-03-28 11:29:40 +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-02-03 04:52:40 +08:00
|
|
|
void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions& actions) {
|
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()))
|
2008-07-02 08:03:09 +08:00
|
|
|
return;
|
|
|
|
|
2009-08-15 11:17:38 +08:00
|
|
|
Mgr->setEntryContext(D);
|
2008-07-02 08:03:09 +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-08-03 11:13:46 +08:00
|
|
|
(*I)(*Mgr);
|
2008-07-02 08:03:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Analyses
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-07-15 07:41:13 +08:00
|
|
|
static void ActionWarnDeadStores(AnalysisManager& mgr) {
|
2008-07-03 13:26:14 +08:00
|
|
|
if (LiveVariables* L = mgr.getLiveVariables()) {
|
|
|
|
BugReporter BR(mgr);
|
|
|
|
CheckDeadStores(*L, BR);
|
|
|
|
}
|
2008-07-02 08:03:09 +08:00
|
|
|
}
|
|
|
|
|
2008-07-15 07:41:13 +08:00
|
|
|
static void ActionWarnUninitVals(AnalysisManager& mgr) {
|
2008-07-03 13:26:14 +08:00
|
|
|
if (CFG* c = mgr.getCFG())
|
|
|
|
CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic());
|
2008-07-02 08:03:09 +08:00
|
|
|
}
|
|
|
|
|
2008-07-02 08:44:58 +08:00
|
|
|
|
2008-07-23 00:21:24 +08:00
|
|
|
static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf,
|
|
|
|
bool StandardWarnings = true) {
|
2008-07-03 00:35:50 +08:00
|
|
|
|
2008-07-03 13:26:14 +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.
|
|
|
|
mgr.DisplayFunction();
|
|
|
|
|
2008-07-03 13:26:14 +08:00
|
|
|
// Construct the analysis engine.
|
|
|
|
LiveVariables* L = mgr.getLiveVariables();
|
|
|
|
if (!L) return;
|
2008-11-25 04:53:32 +08:00
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L, mgr,
|
2009-05-19 18:18:02 +08:00
|
|
|
mgr.shouldPurgeDead(), mgr.shouldEagerlyAssume(),
|
2008-11-27 09:55:08 +08:00
|
|
|
mgr.getStoreManagerCreator(),
|
|
|
|
mgr.getConstraintManagerCreator());
|
2008-10-24 09:04:59 +08:00
|
|
|
|
2008-07-03 00:35:50 +08:00
|
|
|
Eng.setTransferFunctions(tf);
|
2008-07-02 08:44:58 +08:00
|
|
|
|
2008-07-23 00:21:24 +08:00
|
|
|
if (StandardWarnings) {
|
|
|
|
Eng.RegisterInternalChecks();
|
|
|
|
RegisterAppleChecks(Eng);
|
|
|
|
}
|
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
|
|
|
}
|
2008-07-23 00:21:24 +08:00
|
|
|
|
2008-07-02 08:44:58 +08:00
|
|
|
// Execute the worklist algorithm.
|
2009-08-15 11:17:38 +08:00
|
|
|
Eng.ExecuteWorkList(mgr.getEntryStackFrame());
|
2008-07-03 00:35:50 +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-03-11 09:42:29 +08:00
|
|
|
|
|
|
|
// Display warnings.
|
|
|
|
Eng.getBugReporter().FlushReports();
|
2008-07-03 00:35:50 +08:00
|
|
|
}
|
|
|
|
|
2008-07-15 07:41:13 +08:00
|
|
|
static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled,
|
2008-07-23 00:21:24 +08:00
|
|
|
bool StandardWarnings) {
|
|
|
|
|
2008-07-03 00:35:50 +08:00
|
|
|
GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(),
|
|
|
|
GCEnabled,
|
|
|
|
mgr.getLangOptions());
|
|
|
|
|
2008-07-23 00:21:24 +08:00
|
|
|
ActionGRExprEngine(mgr, TF, StandardWarnings);
|
2008-07-02 08:44:58 +08:00
|
|
|
}
|
|
|
|
|
2008-07-15 07:41:13 +08:00
|
|
|
static void ActionCheckerCFRef(AnalysisManager& mgr) {
|
2008-07-02 08:44:58 +08:00
|
|
|
|
|
|
|
switch (mgr.getLangOptions().getGCMode()) {
|
|
|
|
default:
|
|
|
|
assert (false && "Invalid GC mode.");
|
|
|
|
case LangOptions::NonGC:
|
2008-07-15 07:41:13 +08:00
|
|
|
ActionCheckerCFRefAux(mgr, false, true);
|
2008-07-02 08:44:58 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LangOptions::GCOnly:
|
2008-07-15 07:41:13 +08:00
|
|
|
ActionCheckerCFRefAux(mgr, true, true);
|
2008-07-02 08:44:58 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LangOptions::HybridGC:
|
2008-07-15 07:41:13 +08:00
|
|
|
ActionCheckerCFRefAux(mgr, false, true);
|
|
|
|
ActionCheckerCFRefAux(mgr, true, false);
|
2008-07-02 08:44:58 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-15 07:41:13 +08:00
|
|
|
static void ActionDisplayLiveVariables(AnalysisManager& mgr) {
|
2008-07-03 13:26:14 +08:00
|
|
|
if (LiveVariables* L = mgr.getLiveVariables()) {
|
|
|
|
mgr.DisplayFunction();
|
|
|
|
L->dumpBlockLiveness(mgr.getSourceManager());
|
|
|
|
}
|
2008-07-03 02:11:29 +08:00
|
|
|
}
|
|
|
|
|
2008-07-03 02:23:21 +08:00
|
|
|
static void ActionCFGDump(AnalysisManager& mgr) {
|
2008-07-03 13:26:14 +08:00
|
|
|
if (CFG* c = mgr.getCFG()) {
|
|
|
|
mgr.DisplayFunction();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static void ActionCFGView(AnalysisManager& mgr) {
|
2008-07-03 13:26:14 +08:00
|
|
|
if (CFG* c = mgr.getCFG()) {
|
|
|
|
mgr.DisplayFunction();
|
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-07-23 09:07:19 +08:00
|
|
|
static void ActionSecuritySyntacticChecks(AnalysisManager &mgr) {
|
|
|
|
BugReporter BR(mgr);
|
|
|
|
CheckSecuritySyntaxOnly(mgr.getCodeDecl(), BR);
|
|
|
|
}
|
|
|
|
|
2008-07-15 07:41:13 +08:00
|
|
|
static void ActionWarnObjCDealloc(AnalysisManager& mgr) {
|
2008-08-05 01:14:10 +08:00
|
|
|
if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
|
|
|
|
return;
|
|
|
|
|
2008-07-03 12:29:21 +08:00
|
|
|
BugReporter BR(mgr);
|
2008-07-03 22:35:01 +08:00
|
|
|
|
|
|
|
CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
|
|
|
|
mgr.getLangOptions(), BR);
|
2008-07-03 12:29:21 +08:00
|
|
|
}
|
|
|
|
|
2008-07-23 08:45:26 +08:00
|
|
|
static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) {
|
|
|
|
BugReporter BR(mgr);
|
|
|
|
CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR);
|
|
|
|
}
|
|
|
|
|
2008-07-15 07:41:13 +08:00
|
|
|
static void ActionWarnObjCMethSigs(AnalysisManager& mgr) {
|
2008-07-12 06:40:47 +08:00
|
|
|
BugReporter BR(mgr);
|
|
|
|
|
|
|
|
CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
|
|
|
|
BR);
|
|
|
|
}
|
|
|
|
|
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-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-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;
|
|
|
|
|
|
|
|
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);
|
2008-08-28 11:54:51 +08:00
|
|
|
|
|
|
|
~UbigraphViz();
|
|
|
|
|
2009-08-06 09:32:16 +08:00
|
|
|
virtual void AddEdge(ExplodedNode* Src, ExplodedNode* Dst);
|
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;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
llvm::cerr << "Writing '" << Filename << "'.\n";
|
|
|
|
|
|
|
|
llvm::OwningPtr<llvm::raw_fd_ostream> Stream;
|
|
|
|
std::string filename = Filename.toString();
|
2009-07-16 01:32:18 +08:00
|
|
|
Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), false,
|
|
|
|
/*Force=*/true, ErrMsg));
|
2008-08-28 06:31:43 +08:00
|
|
|
|
|
|
|
if (!ErrMsg.empty())
|
|
|
|
return 0;
|
|
|
|
|
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) {
|
2008-08-29 02:34:41 +08:00
|
|
|
|
|
|
|
assert (Src != Dst && "Self-edges are not allowed.");
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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 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.
|
2008-08-28 06:46:55 +08:00
|
|
|
*Out << "('edge', " << SrcID << ", " << DstID
|
|
|
|
<< ", ('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);
|
|
|
|
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 11:45:59 +08:00
|
|
|
}
|