Using "Effective C++" as a guide, I'm adding a number of static analysis
checks for C++. The first is a simple check that warns about the use of
C-Style casts instead of C++ style casts. This is useful because it is
hard to search for C-Style casts. There is a patch to a couple of files,
the checker itself and a test.
- jim
Index: lib/StaticAnalyzer/Checkers/Checkers.td
===================================================================
--- lib/StaticAnalyzer/Checkers/Checkers.td (revision 131677)
+++ lib/StaticAnalyzer/Checkers/Checkers.td (working copy)
@@ -176,6 +176,10 @@
HelpText<"Check improper uses of STL vector iterators">,
DescFile<"IteratorsChecker.cpp">;
+def CStyleCastChecker : Checker<"CStyleCasts">,
+ HelpText<"Check for deprecated use of C style casts">,
+ DescFile<"CStyleCastChecker.cpp">;
+
} // end: "cplusplus.experimental"
//===----------------------------------------------------------------------===//
Index: lib/StaticAnalyzer/Checkers/CMakeLists.txt
===================================================================
--- lib/StaticAnalyzer/Checkers/CMakeLists.txt (revision 131677)
+++ lib/StaticAnalyzer/Checkers/CMakeLists.txt (working copy)
@@ -14,6 +14,7 @@
BasicObjCFoundationChecks.cpp
BuiltinFunctionChecker.cpp
CStringChecker.cpp
+ CStyleCastChecker.cpp
CallAndMessageChecker.cpp
CastSizeChecker.cpp
CastToStructChecker.cpp
//=== CStyleCastChecker.cpp -------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// CStyleCastChecker issues a warning when using a C style cast in C++ code.
//
//===----------------------------------------------------------------------===//
#include "ClangSACheckers.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/AST/CharUnits.h"
using namespace clang;
using namespace ento;
namespace {
class CStyleCastChecker : public Checker< check::PreStmt<CastExpr> > {
mutable llvm::OwningPtr<BuiltinBug> BT;
public:
void checkPreStmt(const CastExpr *CE, CheckerContext &C) const;
};
}
void CStyleCastChecker::checkPreStmt(const CastExpr *CE,
CheckerContext &C) const {
if (dyn_cast<CStyleCastExpr>(CE) == NULL)
return;
if (ExplodedNode *errorNode = C.generateSink()) {
if (!BT)
BT.reset(new BuiltinBug("Use of C style cast",
"Using a C style cast is deprecated. "
"Use a more specific C++ style cast instead."));
RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription(),
errorNode);
R->addRange(CE->getSourceRange());
C.EmitReport(R);
}
}
void ento::registerCStyleCastChecker(CheckerManager &mgr) {
mgr.registerChecker<CStyleCastChecker>();
}
// RUN: %clang_cc1 -analyze
-analyzer-checker=core,cplusplus.experimental.CStyleCasts -verify %s
void foo1() {
int i;
char b = 0;
// Check if basic C style cast generates a warning.
i = (int)b; // expected-warning{{Using a C style cast is deprecated. Use a
more specific C++ style cast instead.}}
}
void foo2() {
typedef int tint;
int i;
char b = 0;
// Check if typedefs get picked up properly.
i = (tint)b; // expected-warning{{Using a C style cast is deprecated. Use a
more specific C++ style cast instead.}}
}
void foo3() {
int *ip;
char b = 0;
// Check if a pointer cast generates a warning.
ip = (int *)&b; // expected-warning{{Using a C style cast is deprecated.
Use a more specific C++ style cast instead.}}
}
void foo4() {
int i;
char b = 0;
// Just to show that C++ casts don't generate a warning.
i = static_cast<int>(b); // no-warning
}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits