我有一个极端的问题.
我已经在游戏上工作了大约两年(20000多行代码),最近我注意到了大量的内存泄漏.问题是我无法追踪他们中的每一个,因为我的游戏太大了......
我已经四处搜索并注意到CppCheck在我的情况下会很有用,但问题是因为我使用的是Windows,所以我不能使用CppCheck(仅适用于linux).
我想知道是否有一个库或插件是CppCheck等效的Windows,或者可能是一种在Windows上使用CppCheck的方法.
我提出的所有可能性,以及其他问题的解决方案(例如使用std :: deque的智能指针等)都意味着我的程序很小或更合适:重写我的整个程序,就像我一样 - 真的 - 不想做...
IDE:代码块10.05
编译:MinGW 3.81 GCC 4.4.1
CppCheck找到了一些调查结果:"变量'x'的范围可以减少".
如果我遇到这种情况怎么办:
int x;
for (int i = 0; i != 10; ++i)
{
x = someFunction();
// ... I use x variable here
}
Run Code Online (Sandbox Code Playgroud)
我认为我的代码没问题.你怎么看?它会变成这样的东西吗?
for (int i = 0; i != 10; ++i)
{
int x = someFunction();
// ... I use x variable here
}
Run Code Online (Sandbox Code Playgroud)
在第二个代码中,为所有迭代定义了变量x ...不是不好(不是最佳的),我猜...
CppCheck 1.67已经确定并且我的一个项目中的数组访问越界错误.我不认为代码是错误的,所以我已经将代码剥离到仍然引发相同错误的最小例子.为什么CppCheck为第一个C++示例(在命名空间内)给出以下错误,而不是第二个示例(没有命名空间)?
我在数组初始化时是否对命名空间做错了,或者这是CppCheck中的错误?
报告的错误:"数组'testArray [5]'在索引5访问,超出范围."
namespace TestNamespace
{
class TestClass
{
static const int testArray[5];
};
const int TestClass::testArray[] = { 1, 2, 3, 4, 5};
}
Run Code Online (Sandbox Code Playgroud)
没有报告错误:
class TestClass
{
static const int testArray[5];
};
const int TestClass::testArray[] = { 1, 2, 3, 4, 5};
Run Code Online (Sandbox Code Playgroud) c++ static-analysis namespaces cppcheck indexoutofboundsexception
我在使用 boost 库的项目上使用 cppcheck。这个库中的头文件包含大量的宏,我什至不在我的源代码中使用。尽管如此,cppcheck 会根据我认为无用的这些宏来探索路径。有没有办法告诉 cppcheck 忽略所有宏,除非它是使用 #define 在源代码中定义的?
最近cppcheck在一些C代码中引发了一个错误,它具有以下结构:
((void)(value_prev = value), value = new_value())
Run Code Online (Sandbox Code Playgroud)
在大多数情况下,这可以拆分为2行,但是在某些情况下,这在单个语句中很有用.
在实践中,我发现这适用于流行的编译器(GCC/Clang/MSVC),它们不会发出任何警告(即使警告级别设置为最高).
示例代码:
#include <stdio.h>
int get_next(int i);
int main() {
int i = 0, i_prev = 10;
do {
printf("%d\n", i);
} while ((void)(i_prev = i),
(i = get_next(i)) != 10);
}
Run Code Online (Sandbox Code Playgroud)
CppCheck 1.73 (撰写本文时的最新版本)给出了以下代码的错误:
(error) Expression '(void)(i_prev=i),(i=get_next(i))!=10'
depends on order of evaluation of side effects`
Run Code Online (Sandbox Code Playgroud)
虽然代码可以更改为安静警告,但订单是否真的未定义?
示例代码:
class Foo {
// cppcheck-suppress noExplicitConstructor
Foo(int foo) { }
}
Run Code Online (Sandbox Code Playgroud)
Cppcheck 调用:
$ cppcheck.exe --enable=all foo.cpp
Checking foo.cpp...
[foo.cpp:3]: (style) Class 'Foo' has a constructor with 1 argument that is not explicit.
Run Code Online (Sandbox Code Playgroud)
我怎样才能抑制这个错误?
工具:SonarQube Server 6.0,Sonar Scanner 3.0.3,Jenkins在Linux Build Slave上运行,具有管道格式,CppuTest,gcovr,Bitbucket,git,JDK 8
问题:声纳服务器上的Coverage测量值莫名显示0.0%覆盖率,单位测试值为非零值(如下所示).使用可访问覆盖率报告的路径sonar.cxx.coverage.reportPath=.CppuTest使用-ojunit标志,gcov生成.gcna文件,使用转换为单一格式gcovr.

我已手动打开coverage-report.xml,它似乎是正确的格式(代码段):
<package branch-rate="0.0" complexity="0.0" line-rate="1.0" name="path > to unit test object file in class notation">
<classes>
<class branch-rate="0.0" complexity="0.0" filename="path to > source file" line-rate="1.0" name="nameOfSourceFile_cpp">
<lines>
<line branch="false" hits="104" number="97"/>
<line branch="false" hits="104" number="101"/>
<line branch="false" hits="104" number="103"/>
<line branch="false" hits="1" number="85"/>
<line branch="false" hits="1" number="88"/>
<line branch="false" hits="104" number="95"/>
</lines>
</class>
</classes>
</package>
Run Code Online (Sandbox Code Playgroud)
Bitbucket Pull Request上的UT Coverage字段也显示0%.我确信这个数字应该是非零的.SonarQube的所有其他方面都在工作,包括查看CppCheck.以前有人见过这样的事吗?
当 if/for 语句后只有一行代码,没有大括号时,有没有办法通过 linting(或者可能seding/ ing)来摆脱或找到这些令人讨厌的情况?regexp像这个:
if(condition)
return;
Run Code Online (Sandbox Code Playgroud)
作为参考,为什么我想摆脱它 - 这个线程中给出了很多原因:
我维护一些遗留代码,并处理其他人的一些未真正完成的代码,并且在调试时时不时会发现这种代码风格像绊线一样工作的情况:
if(condition_for_early_return)
LOG("Im here") // surprise surprise, I just broke control logic
return;
Run Code Online (Sandbox Code Playgroud)
另外,我见过这样的代码:
if(condition)
<tabs> do_smth();
<spaces> do_smth_else();
Run Code Online (Sandbox Code Playgroud)
当然if只包含first do_smth(),编译器不会混淆。但由于这些do_功能在视觉上是一致的,我想知道这是预期的行为还是在遗留代码中从未发现的错误。
我知道cppcheck没有抓住这些情况 - 已经尝试过了。你有办法自动发现这些陷阱吗?
我在项目和TeamCity中使用cppcheck进行静态代码分析以实现持续集成.如果构建服务器在cppcheck发现某些错误或警告时没有构建项目,那将是很好的.是否有任何方法可以通过cppcheck分析使构建失败?
我有一个驱动 cppcheck 坚果的代码片段,因为它没有看到日志调用中使用的变量。所以我得到未使用的变量和范围缩小警告:
double start = GetTimeOfDayDoubleSec(), afterDb = 0;
if (LoadFromDatabase(serials)) {
afterDb = GetTimeOfDayDoubleSec();
Cache.ResetDebugFlags(serials);
}
double end = GetTimeOfDayDoubleSec();
ZLOG_INFO("DB time %f, total %f", afterDb ? afterDb - start : 0, end - start);
Run Code Online (Sandbox Code Playgroud)
Cppcheck 说:
The scope of the variable 'afterDb' can be reduced.
Variable 'afterDb' is assigned a value that is never used.
Run Code Online (Sandbox Code Playgroud)
我无法找出抑制这两者的语法,并且手册没有帮助。分隔线、空格、逗号、冒号和分号都失败。单独的行给我“抑制不匹配”,其余的都是无效的:
//cppcheck-suppress variableScope
//cppcheck-suppress unreadVariable
//cppcheck-suppress variableScope unreadVariable
//cppcheck-suppress variableScope,unreadVariable
//cppcheck-suppress variableScope;unreadVariable
double afterDb = 0;
Failed to add suppression. Invalid id …Run Code Online (Sandbox Code Playgroud) cppcheck ×10
c++ ×5
c ×3
coding-style ×1
memory-leaks ×1
namespaces ×1
performance ×1
sonarqube ×1
teamcity ×1