c++ - 使用 gnu coreutils 在 C++ 中查找 include guard 拼写错误

标签 c++ grep include-guards gnu-coreutils

下面是 C++ include guard 的拼写错误。两者都应读取 _MYFILE_H_

#ifndef _MYFILE_H_
#define _MYFLIE_H_

您如何建议使用 GNU coreutils(例如 grep、awk)在一堆头文件中搜索这样的拼写错误?

最佳答案

你可以使用 awk:

{
  if ($1 == "#ifndef") { was_ifdef = 1; def = $2 }
  else if ($1 == "#define" && was_ifdef) 
    { 
      if ($2 != def)   printf("Mismatch in %s: expected %s found %s\n", FILENAME, def, $2);
    }
  else was_ifdef = 0;
}

可能有更聪明的方法来做到这一点,但这(对我来说)非常清楚和容易。

注意:如果文件包含类似

的内容,这将显示“误报”
#ifndef FOO_DEFINED
typedef int foo;
#define FOO_DEFINED 
#endif

关于c++ - 使用 gnu coreutils 在 C++ 中查找 include guard 拼写错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15011869/

相关文章:

c++ - 带有命名空间的 Concat 宏参数

Linux : Move files that have more than 100 commas in one line

c++ - 是否需要冗余包含 guard ?

linux - 如何在 %% 或 $$ 中间 grep/split 一个词

c++ - 为什么我不断得到类错误的重新定义? C++

c - 棘手的包含 C 中的情况

c++ - 在一个图层中添加2个CCScrollLayer时无法滚动

包含 Q_OBJECT 的 c++ 宏

c++ - 使用 Eclipse 的 RSE 插件更新远程文件

regex - Grep 搜索特定字符问题