MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / check_include_guards

Function check_include_guards

addons/namingng.py:199–269  ·  view source on GitHub ↗
(conf,cfg,unguarded_include_files)

Source from the content-addressed store, hash-verified

197 return guard_name,guard_column
198
199def 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

Callers 1

process_dataFunction · 0.85

Calls 5

report_pending_ifndefFunction · 0.85
check_include_guard_nameFunction · 0.85
reportFunction · 0.70
getMethod · 0.45
matchMethod · 0.45

Tested by

no test coverage detected