wmllint: fix bugs with ifdef_stack

This section assumed that "#ifdef" and "#endif" would come at the very start of a line. When an author would indent the #ifdef but not the #endif, ifdef_stack.pop() would kill the starting value of None, leaving an empty list. wmllint would then crash:

  File "wmllint", line 1138, in global_sanity_check
    recruit[ifdef_stack[-1]] = (i+1, map(lambda x: x.strip(), value.split(",")))

IndexError: list index out of range

Stripping the line not only stops the crashes, but allows wmllint to pick up #ifdefs that it wasn't before.

I then looked more closely at the pop(). #endif shouldn't just drop the last value in the stack, but reset the whole stack back to None. I realized that pop() was leading to wmllint occasionally assigning recruitment that wasn't inside an #ifdef to values from earlier #ifdef stacks, e.g.:

>>  starting value: [None]
#ifdef EASY  >>  [None, 'EASY']
..
#else  >>  [None, 'EASY', '!EASY']
..
#endif  >>  pop(): [None, 'EASY']
..
recruit=  >>  ifdef_stack[-1]: EASY
This commit is contained in:
groggydice 2013-07-19 01:00:59 -04:00
parent 0891bffafb
commit 6164804128

View file

@ -1084,20 +1084,21 @@ def global_sanity_check(filename, lines):
recruitment_pattern = {}
ifdef_stack = [None]
for i in range(len(lines)):
if lines[i].startswith("#ifdef") or lines[i].startswith("#ifhave") or lines[i].startswith("#ifver"):
ifdef_stack.append(lines[i].strip().split()[1])
line = lines[i].strip()
if line.startswith("#ifdef") or line.startswith("#ifhave") or line.startswith("#ifver"):
ifdef_stack.append(line.split()[1])
continue
if lines[i].startswith("#ifndef") or lines[i].startswith("#ifnhave") or lines[i].startswith("#ifnver"):
ifdef_stack.append("!" + lines[i].strip().split()[1])
if line.startswith("#ifndef") or line.startswith("#ifnhave") or line.startswith("#ifnver"):
ifdef_stack.append("!" + line.split()[1])
continue
if lines[i].startswith("#else"):
if line.startswith("#else"):
if ifdef_stack[-1].startswith("!"):
ifdef_stack.append(ifdef_stack[-1][1:])
else:
ifdef_stack.append("!" + ifdef_stack[-1])
continue
if lines[i].startswith("#endif"):
ifdef_stack.pop()
if line.startswith("#endif"):
ifdef_stack = [None]
continue
if "[generator]" in lines[i]:
in_generator = True