C++静态分析,模板类
C++ static analysis, template classes
我需要一个静态分析器来查找未初始化的变量成员/变量...的模板化类类型。
任何分析仪都能做到这一点吗?我尝试了clang/cppcheck和其他几个没有运气。
这是我的测试代码:
enum class ViewMode
{
One = 1,
Two = 2,
Three = 3,
Four = 4
};
class TestClass {
public:
TestClass() {}
};
template<typename T, bool C = std::is_copy_constructible<T>::value>
class TemplateTest
{
public:
TemplateTest() {}
TemplateTest(const T& value)
: value_(value)
{}
TemplateTest(const TemplateTest&) = delete;
TemplateTest(TemplateTest<T, C>&& rhs)
: value_(std::move(rhs.value_))
{}
TemplateTest(T&& value)
: value_(std::move(value))
{}
private:
T value_;
};
class StaticAnalysisTest {
public:
StaticAnalysisTest() {}
void DoSomething() {
}
private:
ViewMode viewMode_; //this uninitialized warning is found
TemplateTest<ViewMode> viewMode2_; //this one is not
};
我进一步将问题提炼为:
class Foo
{
private:
int m_nValue;
public:
Foo() {};
Foo(int value) : m_nValue(value) {}
int GetValue() { return m_nValue; }
};
class Bar
{
public:
Bar(){}
void DoSomething() {
Foo foo;
}
};
这不会生成单位化变量警告,但是当我注释掉时:
//Foo(int value) : m_nValue(value) {}
它确实如此
感谢您评估 Cppcheck。对于第二个示例,如果您添加 --inconclusive 标志,则会发出警告,例如:
class Foo
{
private:
int m_nValue;
public:
Foo() {};
explicit Foo(int value) : m_nValue(value) {}
int GetValue() const
{
return m_nValue;
}
};
class Bar
{
public:
Bar() {}
static void DoSomething() {
Foo foo;
}
};
Cppcheck 的输出
$ cppcheck --enable=all --inconclusive uninitmembervar.cpp
[uninitmembervar.cpp:6]: (warning, inconclusive) Member variable 'Foo::m_nValue' is not initialized in the constructor.
相关文章:
- 如何处理来自核心指南检查器的关于gsl::at的静态分析警告
- 如何执行 cppcheck 交叉翻译单元 (CTU) 静态分析?
- 如何将 AST 用于自定义前端操作和 clang 静态分析
- C++静态分析,模板类
- 覆盖性静态分析说无符号的int是char(C++)
- Android NDK-天然C 代码的静态分析
- 静态分析,用于检测 Visual Studio C++ 2012 上的缓冲区溢出
- 这个C++静态分析规则按原样有意义吗
- 如何使用模板化库控制静态分析编译器错误消息
- 了解COM的静态分析工具
- 是否有任何c++静态分析工具来检测向量的潜在错误
- 用于静态分析本机Windows二进制文件大小的工具
- 静态分析工具,用于检测由于类型提升而导致的无休止循环
- 为什么Xcode 4.3静态分析报告此代码的逻辑错误
- 如何获得与C++0x标准兼容的Coverity静态分析
- Visual Studio 11终极静态分析显示没有输出
- Clang静态分析-根据执行流检查可为空的变量初始化
- 用MPI进行Clang静态分析
- 如何使用Eclipse cdt对c++代码进行静态分析
- 头文件包含静态分析工具