4 Tool to convert traditional include guards to #pragma once. 6 This tool is licensed under GPLv2. 7 See the LICENSE file for details. 9 Dowloaded from this: https://github.com/marcusmueller/include-guard-convert 10 Usage: In Linux/Mac Terminal (not Windows), go to the Source folder then execute the following command: 12 find . -iname "*.h" -exec python3 ~/Programming/include-guard-convert.py {} \; 14 Note: After the conversion for the header files, uncrustify need to be run to re-format them 24 'ifndef' :
'^\s*#(?:ifndef|IFNDEF)\s+([A-Za-z_0-9]{4,})\s*$',
25 'define' :
'^\s*#(?:define|DEFINE)\s+([A-Za-z_0-9]{4,})\s*$',
26 'endif' :
'^\s*#(?:endif|ENDIF)\s*(/\*.*\*/|//.+)?\s*$',
27 'blank' :
'^\s*(/\*.*\*/|//.+)?\s*$',
28 'pragma' :
'^\s*#(?:pragma|PRAGMA)\s+(?:once|ONCE)' 30 patterns = dict( [ (key, re.compile(regexes[key]) )
for key
in regexes.keys() ] )
33 'strip_comments' :
'cpp -w -dD -fpreprocessed -P -x c++ {file}',
34 'test_if_guarded' :
'cpp -w -P -x c++ -D{define} {file}' 39 Class representing an #include'able file (a.k.a. a header). 42 def __init__(self, filename, autoconvert = False):
48 def _test_readable(self):
49 return os.access(self.
filename, os.R_OK)
52 def test_oldstyle_guarded(self):
56 cpp_commands[
'strip_comments'].format(file=self.
filename)
58 stderr=subprocess.STDOUT
61 match_ifndef = patterns[
'ifndef'].search(self.
_stripped[:lineend])
64 define = match_ifndef.group(1)
65 with_define = subprocess.check_output(
67 cpp_commands[
'test_if_guarded'].format(file=self.
filename, define=define)
69 stderr=subprocess.STDOUT
71 if not len(with_define) < 2:
75 while not patterns[
'ifndef'].search(line):
83 return patterns[
'define'].search(line)
85 except subprocess.CalledProcessError
as err:
90 lines = freadh.readlines() 91 sep = '\r\n' if lines[0].endswith(
'\r\n')
else '\n' 94 for l_number
in range(1,len(lines)):
95 line = lines[-l_number]
96 if patterns[
'blank'].search(line):
98 elif patterns[
'endif'].search(line):
102 raise SyntaxError(
'encountered meaningful line after last #endif: \n'+line)
105 for l_number,line
in enumerate(lines):
110 pattern = patterns[
'ifndef']
112 pattern = patterns[
'define']
113 match = pattern.search(line)
115 newdefine = match.group(1)
118 elif define == newdefine:
119 fwriteh.write(
'#pragma once' + sep)
124 raise SyntaxError(
'could not find #ifndef')
125 elif define != newdefine:
126 raise SyntaxError(
'found #ifndef ' + define +
', does not match #define ' + newdefine)
129 if __name__ ==
'__main__':
131 parser = argparse.ArgumentParser()
132 parser.add_argument(
'filename', nargs=
'*')
133 args = parser.parse_args()
134 for filen
in args.filename:
137 if gi.test_oldstyle_guarded():
139 except SyntaxError
as e:
def test_oldstyle_guarded(self)