Skip to content
Snippets Groups Projects
Commit 3c5def57 authored by David Garcia Quintas's avatar David Garcia Quintas
Browse files

addressed pr comments

parent 5b385dcb
No related branches found
No related tags found
No related merge requests found
...@@ -95,56 +95,59 @@ class GuardValidator(object): ...@@ -95,56 +95,59 @@ class GuardValidator(object):
fcontents = load(fpath) fcontents = load(fpath)
match = self.ifndef_re.search(fcontents) match = self.ifndef_re.search(fcontents)
if match.lastindex != 1: if match.lastindex is None:
# No ifndef. Request manual addition with hints # No ifndef. Request manual addition with hints
self.fail(fpath, match.re, match.string, '', '', False) self.fail(fpath, match.re, match.string, '', '', False)
return False # failed
# Does the guard end with a '_H'? # Does the guard end with a '_H'?
running_guard = match.group(1) running_guard = match.group(1)
if not running_guard.endswith('_H'): if not running_guard.endswith('_H'):
fcontents = self.fail(fpath, match.re, match.string, match.group(1), fcontents = self.fail(fpath, match.re, match.string, match.group(1),
valid_guard, fix) valid_guard, fix)
save(fpath, fcontents) if fix: save(fpath, fcontents)
# Is it the expected one based on the file path? # Is it the expected one based on the file path?
if running_guard != valid_guard: if running_guard != valid_guard:
fcontents = self.fail(fpath, match.re, match.string, match.group(1), fcontents = self.fail(fpath, match.re, match.string, match.group(1),
valid_guard, fix) valid_guard, fix)
save(fpath, fcontents) if fix: save(fpath, fcontents)
# Is there a #define? Is it the same as the #ifndef one? # Is there a #define? Is it the same as the #ifndef one?
match = self.define_re.search(fcontents) match = self.define_re.search(fcontents)
if match.lastindex != 1: if match.lastindex is None:
# No define. Request manual addition with hints # No define. Request manual addition with hints
self.fail(fpath, match.re, match.string, '', '', False) self.fail(fpath, match.re, match.string, '', '', False)
return False # failed
# Is the #define guard the same as the #ifndef guard? # Is the #define guard the same as the #ifndef guard?
if match.group(1) != running_guard: if match.group(1) != running_guard:
fcontents = self.fail(fpath, match.re, match.string, match.group(1), fcontents = self.fail(fpath, match.re, match.string, match.group(1),
valid_guard, fix) valid_guard, fix)
save(fpath, fcontents) if fix: save(fpath, fcontents)
# Is there a properly commented #endif? # Is there a properly commented #endif?
endif_re = self.endif_cpp_re if cpp_header else self.endif_c_re endif_re = self.endif_cpp_re if cpp_header else self.endif_c_re
matches = endif_re.findall(fcontents) flines = fcontents.rstrip().splitlines()
if not matches: match = endif_re.search(flines[-1])
if not match:
# No endif. Check if we have the last line as just '#endif' and if so # No endif. Check if we have the last line as just '#endif' and if so
# replace it with a properly commented one. # replace it with a properly commented one.
flines = fcontents.splitlines()
if flines[-1] == '#endif': if flines[-1] == '#endif':
flines[-1] = ('#endif' + flines[-1] = ('#endif' +
(' // {}\n'.format(valid_guard) if cpp_header (' // {}\n'.format(valid_guard) if cpp_header
else ' /* {} */\n'.format(valid_guard))) else ' /* {} */\n'.format(valid_guard)))
fcontents = '\n'.join(flines) if fix:
save(fpath, fcontents) fcontents = '\n'.join(flines)
save(fpath, fcontents)
else: else:
# something else is wrong, bail out # something else is wrong, bail out
self.fail(fpath, endif_re, match.string, '', '', False) self.fail(fpath, endif_re, match.string, '', '', False)
elif matches[-1] != running_guard: elif match.group(1) != running_guard:
# Is the #endif guard the same as the #ifndef and #define guards? # Is the #endif guard the same as the #ifndef and #define guards?
fcontents = self.fail(fpath, endif_re, fcontents, matches[-1], fcontents = self.fail(fpath, endif_re, fcontents, matches[-1],
valid_guard, fix) valid_guard, fix)
save(fpath, fcontents) if fix: save(fpath, fcontents)
return not self.failed # Did the check succeed? (ie, not failed) return not self.failed # Did the check succeed? (ie, not failed)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment