|
3 | 3 | # cppcheck addon for naming conventions |
4 | 4 | # An enhanced version. Configuration is taken from a json file |
5 | 5 | # It supports to check for type-based prefixes in function or variable names. |
| 6 | +# Aside from include guard naming, include guard presence can also be tested. |
6 | 7 | # |
7 | 8 | # Example usage (variable name must start with lowercase, function name must start with uppercase): |
8 | 9 | # $ cppcheck --dump path-to-src/ |
|
14 | 15 | # "RE_VARNAME": "[a-z]*[a-zA-Z0-9_]*\\Z", |
15 | 16 | # "RE_PRIVATE_MEMBER_VARIABLE": null, |
16 | 17 | # "RE_FUNCTIONNAME": "[a-z0-9A-Z]*\\Z", |
| 18 | +# "include_guard": { |
| 19 | +# "input": "path", |
| 20 | +# "prefix": "GUARD_", |
| 21 | +# "case": "upper", |
| 22 | +# "max_linenr": 5, |
| 23 | +# "RE_HEADERFILE": "[^/].*\\.h\\Z", |
| 24 | +# "required": true |
| 25 | +# }, |
17 | 26 | # "var_prefixes": {"uint32_t": "ui32"}, |
18 | 27 | # "function_prefixes": {"uint16_t": "ui16", |
19 | 28 | # "uint32_t": "ui32"} |
@@ -73,10 +82,115 @@ def evalExpr(conf, exp, mockToken, msgType): |
73 | 82 | msg = msgType + ' ' + mockToken.str + ' violates naming convention' |
74 | 83 | checkFalseRegex(mockToken, exp, msg) |
75 | 84 |
|
| 85 | +def check_include_guard_name(conf_ig,cfg,directive): |
| 86 | + parts = directive.str.split() |
| 87 | + if len(parts) != 2: |
| 88 | + msg = 'syntax error' |
| 89 | + reportNamingError(directive,msg,'syntax') |
| 90 | + return None |
| 91 | + guard_name = parts[1] |
| 92 | + |
| 93 | + filename = directive.file |
| 94 | + if conf_ig.get('input','path') == 'basename': |
| 95 | + filename = os.path.basename(filename) |
| 96 | + use_case = conf_ig.get('case','upper') |
| 97 | + if use_case == 'upper': |
| 98 | + filename = filename.upper() |
| 99 | + elif use_case == 'lower': |
| 100 | + filename = filename.lower() |
| 101 | + elif use_case == 'keep': |
| 102 | + pass # keep filename case as-is |
| 103 | + else: |
| 104 | + print("invalid config value for 'case': '%s'"%use_case,file=sys.stderr) |
| 105 | + sys.exit(1) |
| 106 | + |
| 107 | + barename = re.sub('[^A-Za-z0-9]','_',filename).strip('_') |
| 108 | + expect_guard_name = conf_ig.get('prefix','') + barename + conf_ig.get('suffix','') |
| 109 | + if expect_guard_name != guard_name: |
| 110 | + msg = 'include guard naming violation; %s != %s'%(guard_name,expect_guard_name) |
| 111 | + reportNamingError(directive,msg,'includeGuardName') |
| 112 | + |
| 113 | + return guard_name |
| 114 | + |
| 115 | +def check_include_guard(conf_ig,cfg,unguarded_include_files): |
| 116 | + # Scan for '#ifndef FILE_H' as the first directive, in the first N lines. |
| 117 | + # Then test whether the next directive #defines the found name. |
| 118 | + # Various tests are done: |
| 119 | + # - check include guards for their naming and consistency |
| 120 | + # - test whether include guards are in place |
| 121 | + max_linenr = conf_ig.get('max_linenr', 5) |
| 122 | + |
| 123 | + def report(directive,msg,errorId): |
| 124 | + reportNamingError(directive,msg,errorId) |
| 125 | + |
| 126 | + def report_pending_ifndef(directive): |
| 127 | + report(directive,'include guard #ifndef is not followed by #define','includeGuardIncomplete') |
| 128 | + |
| 129 | + last_fn = None |
| 130 | + pending_ifndef = None |
| 131 | + phase = 0 |
| 132 | + for directive in cfg.directives: |
| 133 | + if last_fn != directive.file: |
| 134 | + if pending_ifndef: |
| 135 | + report_pending_ifndef(pending_ifndef) |
| 136 | + pending_ifndef = None |
| 137 | + last_fn = directive.file |
| 138 | + phase = 0 |
| 139 | + if phase == -1: |
| 140 | + # ignore (the remainder of) this file |
| 141 | + continue |
| 142 | + if not re.match(include_guard_header_re,directive.file): |
| 143 | + phase = -1 |
| 144 | + continue |
| 145 | + |
| 146 | + if directive.linenr > max_linenr: |
| 147 | + if phase == 0 and conf_ig.get('required',1): |
| 148 | + report(directive,'include guard not found before line %d'%max_linenr,'includeGuardMissing') |
| 149 | + pass |
| 150 | + phase = -1 |
| 151 | + continue |
| 152 | + |
| 153 | + if phase == 0: |
| 154 | + # looking for '#ifndef FILE_H' |
| 155 | + if not directive.str.startswith('#ifndef'): |
| 156 | + if conf_ig.get('required',1): |
| 157 | + report(directive,'first preprocessor directive should be include guard #ifndef','includeGuardMissing') |
| 158 | + phase = -1 |
| 159 | + continue |
| 160 | + guard_name = check_include_guard_name(conf_ig,cfg,directive) |
| 161 | + if guard_name == None: |
| 162 | + phase = -1 |
| 163 | + continue |
| 164 | + pending_ifndef = directive |
| 165 | + phase = 1 |
| 166 | + elif phase == 1: |
| 167 | + pending_ifndef = None |
| 168 | + # looking for '#define FILE_H' |
| 169 | + if not directive.str.startswith('#define'): |
| 170 | + report(directive,'second preprocessor directive should be include guard #define','includeGuardIncomplete') |
| 171 | + phase = -1 |
| 172 | + continue |
| 173 | + parts = directive.str.split() |
| 174 | + if len(parts) == 1: |
| 175 | + report(directive,'syntax error','syntax') |
| 176 | + phase = -1 |
| 177 | + continue |
| 178 | + if guard_name != parts[1]: |
| 179 | + report(directive,'include guard does not guard; %s != %s'%(guard_name,parts[1]),'includeGuardAwayFromDuty',severity='warning') |
| 180 | + |
| 181 | + unguarded_include_files.remove(directive.file) |
| 182 | + |
| 183 | + phase = -1 |
| 184 | + if pending_ifndef: |
| 185 | + report_pending_ifndef(pending_ifndef) |
76 | 186 |
|
77 | 187 | def process(dumpfiles, configfile, debugprint=False): |
78 | 188 | conf = loadConfig(configfile) |
79 | 189 |
|
| 190 | + if "include_guard" in conf and conf["include_guard"]: |
| 191 | + global include_guard_header_re |
| 192 | + include_guard_header_re = conf["include_guard"].get('RE_HEADERFILE',"[^/].*\\.h\\Z") |
| 193 | + |
80 | 194 | for afile in dumpfiles: |
81 | 195 | if not afile[-5:] == '.dump': |
82 | 196 | continue |
@@ -105,6 +219,11 @@ def process(dumpfiles, configfile, debugprint=False): |
105 | 219 | for exp in conf["RE_NAMESPACE"]: |
106 | 220 | evalExpr(conf["RE_NAMESPACE"], exp, mockToken, msgType) |
107 | 221 |
|
| 222 | + unguarded_include_files = [] |
| 223 | + if "include_guard" in conf and conf["include_guard"]: |
| 224 | + if conf["include_guard"].get('required',1): |
| 225 | + unguarded_include_files = [fn for fn in data.files if re.match(include_guard_header_re,fn)] |
| 226 | + |
108 | 227 | for cfg in data.configurations: |
109 | 228 | if not args.cli: |
110 | 229 | print('Checking %s, config %s...' % (afile, cfg.name)) |
@@ -203,6 +322,14 @@ def process(dumpfiles, configfile, debugprint=False): |
203 | 322 | for exp in conf["RE_CLASS_NAME"]: |
204 | 323 | evalExpr(conf["RE_CLASS_NAME"], exp, mockToken, msgType) |
205 | 324 |
|
| 325 | + # Check include guard naming |
| 326 | + if "include_guard" in conf and conf["include_guard"]: |
| 327 | + check_include_guard(conf["include_guard"],cfg,unguarded_include_files) |
| 328 | + |
| 329 | + for fn in unguarded_include_files: |
| 330 | + mockToken = DataStruct(fn,0,os.path.basename(fn)) |
| 331 | + reportNamingError(mockToken,'Missing include guard','includeGuardMissing') |
| 332 | + |
206 | 333 | if __name__ == "__main__": |
207 | 334 | parser = cppcheckdata.ArgumentParser() |
208 | 335 | parser.add_argument("--debugprint", action="store_true", default=False, |
|
0 commit comments