简体   繁体   English

查找一个c ++项目中有多少个goto

[英]find how many gotos are in a c++ project

I am trying to see how many goto instructions are inside a c++ project. 我正在尝试查看c ++项目中有多少条goto指令。 Now I am using a simple matching with grep searching for "goto " : 现在,我正在使用与grep的简单匹配来搜索"goto "

grep -r --exclude-dir "third_party" --include "*.cpp" --include "*.h" --include "*.C" --include "*.cxx" --include "*.hcc" --include "*.c" --include "*.cc" "goto " .

I get a lot of fake positive, in particular inside comments: 我得到了很多假阳性,尤其是内部评论:

// Perform stack overflow check if this goto needs it before jumping.

or inside literals: 或内部文字:

stream->Add(" goto (");

what is the easiest way to avoid them? 避免它们的最简单方法是什么? I don't need a perfect solution. 我不需要完美的解决方案。

You might consider using a static analysis tool like eg cppcheck for doing this. 您可能会考虑使用像cppcheck这样的静态分析工具来执行此操作。 Handling all the possible situations only using regex might turn out not to be applicable. 仅使用正则表达式处理所有可能的情况可能并不适用。

Change your regular expression to: 'goto[ \\t]+[_A-Za-z0-9]+[ \\t]*;' 将您的正则表达式更改为: 'goto[ \\t]+[_A-Za-z0-9]+[ \\t]*;' This will search for the word goto, its target label and a semicolon all on the same line. 这将在同一行上搜索goto单词,其目标标签和一个分号。

I suggest using awk or another tool that can use more than one regular expression or write a script. 我建议使用awk或另一种可以使用多个正则表达式或编写脚本的工具。

The more difficult cases are: 更困难的情况是:

/*****
 * Goto system failure if this point is reached.
 */

Meaning anything inside C-style comments. 在C风格的注释中包含任何含义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM