Update preprocessor stack. We need to handle preprocessors due to classes like this: #ifdef SWIG struct ResultDetailsPageElementExtensionPoint { #else struct ResultDetailsPageElementExtensionPoint : public Extension { #endif We make
(self, line)
| 3410 | return False |
| 3411 | |
| 3412 | def UpdatePreprocessor(self, line): |
| 3413 | """Update preprocessor stack. |
| 3414 | |
| 3415 | We need to handle preprocessors due to classes like this: |
| 3416 | #ifdef SWIG |
| 3417 | struct ResultDetailsPageElementExtensionPoint { |
| 3418 | #else |
| 3419 | struct ResultDetailsPageElementExtensionPoint : public Extension { |
| 3420 | #endif |
| 3421 | |
| 3422 | We make the following assumptions (good enough for most files): |
| 3423 | - Preprocessor condition evaluates to true from #if up to first |
| 3424 | #else/#elif/#endif. |
| 3425 | |
| 3426 | - Preprocessor condition evaluates to false from #else/#elif up |
| 3427 | to #endif. We still perform lint checks on these lines, but |
| 3428 | these do not affect nesting stack. |
| 3429 | |
| 3430 | Args: |
| 3431 | line: current line to check. |
| 3432 | """ |
| 3433 | if re.match(r"^\s*#\s*(if|ifdef|ifndef)\b", line): |
| 3434 | # Beginning of #if block, save the nesting stack here. The saved |
| 3435 | # stack will allow us to restore the parsing state in the #else case. |
| 3436 | self.pp_stack.append(_PreprocessorInfo(copy.deepcopy(self.stack))) |
| 3437 | elif re.match(r"^\s*#\s*(else|elif)\b", line): |
| 3438 | # Beginning of #else block |
| 3439 | if self.pp_stack: |
| 3440 | if not self.pp_stack[-1].seen_else: |
| 3441 | # This is the first #else or #elif block. Remember the |
| 3442 | # whole nesting stack up to this point. This is what we |
| 3443 | # keep after the #endif. |
| 3444 | self.pp_stack[-1].seen_else = True |
| 3445 | self.pp_stack[-1].stack_before_else = copy.deepcopy(self.stack) |
| 3446 | |
| 3447 | # Restore the stack to how it was before the #if |
| 3448 | self.stack = copy.deepcopy(self.pp_stack[-1].stack_before_if) |
| 3449 | else: |
| 3450 | # TODO(google): unexpected #else, issue warning? |
| 3451 | pass |
| 3452 | elif re.match(r"^\s*#\s*endif\b", line): |
| 3453 | # End of #if or #else blocks. |
| 3454 | if self.pp_stack: |
| 3455 | # If we saw an #else, we will need to restore the nesting |
| 3456 | # stack to its former state before the #else, otherwise we |
| 3457 | # will just continue from where we left off. |
| 3458 | if self.pp_stack[-1].seen_else: |
| 3459 | # Here we can just use a shallow copy since we are the last |
| 3460 | # reference to it. |
| 3461 | self.stack = self.pp_stack[-1].stack_before_else |
| 3462 | # Drop the corresponding #if |
| 3463 | self.pp_stack.pop() |
| 3464 | else: |
| 3465 | # TODO(google): unexpected #endif, issue warning? |
| 3466 | pass |
| 3467 | |
| 3468 | def _Pop(self): |
| 3469 | """Pop the innermost state (top of the stack) and remember the popped item.""" |
no test coverage detected