(conf,cfg,unguarded_include_files)
| 197 | return guard_name,guard_column |
| 198 | |
| 199 | def check_include_guards(conf,cfg,unguarded_include_files): |
| 200 | # Scan for '#ifndef FILE_H' as the first directive, in the first N lines. |
| 201 | # Then test whether the next directive #defines the found name. |
| 202 | # Various tests are done: |
| 203 | # - check include guards for their naming and consistency |
| 204 | # - test whether include guards are in place |
| 205 | max_linenr = conf.include_guard.get('max_linenr', 5) |
| 206 | |
| 207 | def report(directive,msg,errorId,severity='style',column=0): |
| 208 | reportNamingError(directive,msg,errorId,severity=severity,column=column) |
| 209 | |
| 210 | def report_pending_ifndef(directive,column): |
| 211 | report(directive,'include guard #ifndef is not followed by #define','includeGuardIncomplete',column=column) |
| 212 | |
| 213 | last_fn = None |
| 214 | pending_ifndef = None |
| 215 | guard_column = None |
| 216 | phase = 0 |
| 217 | for directive in cfg.directives: |
| 218 | if last_fn != directive.file: |
| 219 | if pending_ifndef: |
| 220 | report_pending_ifndef(pending_ifndef,guard_column) |
| 221 | pending_ifndef = None |
| 222 | last_fn = directive.file |
| 223 | phase = 0 |
| 224 | if phase == -1: |
| 225 | # ignore (the remainder of) this file |
| 226 | continue |
| 227 | if not re.match(conf.include_guard_header_re,directive.file): |
| 228 | phase = -1 |
| 229 | continue |
| 230 | |
| 231 | if directive.linenr > max_linenr: |
| 232 | if phase == 0 and conf.include_guard.get('required',1): |
| 233 | report(directive,'include guard not found before line %d'%max_linenr,'includeGuardMissing') |
| 234 | phase = -1 |
| 235 | continue |
| 236 | |
| 237 | if phase == 0: |
| 238 | # looking for '#ifndef FILE_H' |
| 239 | if not directive.str.startswith('#ifndef'): |
| 240 | if conf.include_guard.get('required',1): |
| 241 | report(directive,'first preprocessor directive should be include guard #ifndef','includeGuardMissing') |
| 242 | phase = -1 |
| 243 | continue |
| 244 | guard_name,guard_column = check_include_guard_name(conf,directive) |
| 245 | if guard_name is None: |
| 246 | phase = -1 |
| 247 | continue |
| 248 | pending_ifndef = directive |
| 249 | phase = 1 |
| 250 | elif phase == 1: |
| 251 | pending_ifndef = None |
| 252 | # looking for '#define FILE_H' |
| 253 | if not directive.str.startswith('#define'): |
| 254 | report(directive,'second preprocessor directive should be include guard #define','includeGuardIncomplete') |
| 255 | phase = -1 |
| 256 | continue |
no test coverage detected