C++ C/C++;标题保护一致性
我需要一个实用工具来检查标题保护中的冲突。如果实用程序能够检查符号和文件名是否一致(使用regex或其他东西),那就太好了 问候,, rn141 编辑: 示例1。割台护罩断裂。不能防止多重包含。C++ C/C++;标题保护一致性,c++,c,C++,C,我需要一个实用工具来检查标题保护中的冲突。如果实用程序能够检查符号和文件名是否一致(使用regex或其他东西),那就太好了 问候,, rn141 编辑: 示例1。割台护罩断裂。不能防止多重包含。 // my_file1.h #ifndef my_project_my_file1_h__ #define my_project_my_fil1_h__ // should be my_project_my_file1_h__ // code goes here #endif // my_fi
// my_file1.h
#ifndef my_project_my_file1_h__
#define my_project_my_fil1_h__ // should be my_project_my_file1_h__
// code goes here
#endif
// my_file2.h
#ifndef my_project_my_file1_h__ // should be my_project_my_file2_h__
#define my_project_my_file1_h__ // should be my_project_my_file2_h__
// code goes here
#endif
示例2。与上述头部防护装置冲突。
// my_file1.h
#ifndef my_project_my_file1_h__
#define my_project_my_fil1_h__ // should be my_project_my_file1_h__
// code goes here
#endif
// my_file2.h
#ifndef my_project_my_file1_h__ // should be my_project_my_file2_h__
#define my_project_my_file1_h__ // should be my_project_my_file2_h__
// code goes here
#endif
改用
#pragma一次如何?我不知道有任何正则表达式或现成的实用程序可以轻松进行这种分析
只要您的项目/文件遵循特定的命名规则,就可以编写自己的此类程序。我在3分钟内编写了这段代码,因此它并不完美:
#!/bin/python
from glob import glob
import re
regex = re.compile(r"#ifndef +([a-zA-z0-9]+)\n *#define +([a-zA-z0-9]+)")
HEADER_EXTENSION = "h"
file_list = glob("*." + HEADER_EXTENSION)
guards_list = []
for file_name in file_list:
code = None
with open(file_name) as f:
code = f.read()
m = regex.match(code)
if m:
group1 = m.group(1)
group2 = m.group(2)
if group1 in guards_list:
print "duplicate %s" % group1
else:
guards_list.append(group1)
if group1 != group2:
print "guards differents in file %s" % file_name
else:
print "can't find guard in %s" % file_name
并非所有编译器都支持<代码>·* PrimMA,一旦< /COD> >您的头保护名是非法的-您不允许创建包含双下划线的名称-它们是为C++实现的保留。我的系统有处理这些错误的实用工具。它叫做vi。当我键入I++
时,我也遇到了类似的问题,而我真正的意思是j++
;你能推荐一个工具来帮我检查一下吗?奇怪的是,这些标题保护是由著名的VisualStudio插件生成的。手头的项目不使用双倍分数。