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 the following assumptions (
(self, line)
| 2523 | return False |
| 2524 | |
| 2525 | def UpdatePreprocessor(self, line): |
| 2526 | """Update preprocessor stack. |
| 2527 | |
| 2528 | We need to handle preprocessors due to classes like this: |
| 2529 | #ifdef SWIG |
| 2530 | struct ResultDetailsPageElementExtensionPoint { |
| 2531 | #else |
| 2532 | struct ResultDetailsPageElementExtensionPoint : public Extension { |
| 2533 | #endif |
| 2534 | |
| 2535 | We make the following assumptions (good enough for most files): |
| 2536 | - Preprocessor condition evaluates to true from #if up to first |
| 2537 | #else/#elif/#endif. |
| 2538 | |
| 2539 | - Preprocessor condition evaluates to false from #else/#elif up |
| 2540 | to #endif. We still perform lint checks on these lines, but |
| 2541 | these do not affect nesting stack. |
| 2542 | |
| 2543 | Args: |
| 2544 | line: current line to check. |
| 2545 | """ |
| 2546 | if Match(r'^\s*#\s*(if|ifdef|ifndef)\b', line): |
| 2547 | # Beginning of #if block, save the nesting stack here. The saved |
| 2548 | # stack will allow us to restore the parsing state in the #else case. |
| 2549 | self.pp_stack.append(_PreprocessorInfo(copy.deepcopy(self.stack))) |
| 2550 | elif Match(r'^\s*#\s*(else|elif)\b', line): |
| 2551 | # Beginning of #else block |
| 2552 | if self.pp_stack: |
| 2553 | if not self.pp_stack[-1].seen_else: |
| 2554 | # This is the first #else or #elif block. Remember the |
| 2555 | # whole nesting stack up to this point. This is what we |
| 2556 | # keep after the #endif. |
| 2557 | self.pp_stack[-1].seen_else = True |
| 2558 | self.pp_stack[-1].stack_before_else = copy.deepcopy(self.stack) |
| 2559 | |
| 2560 | # Restore the stack to how it was before the #if |
| 2561 | self.stack = copy.deepcopy(self.pp_stack[-1].stack_before_if) |
| 2562 | else: |
| 2563 | # TODO(unknown): unexpected #else, issue warning? |
| 2564 | pass |
| 2565 | elif Match(r'^\s*#\s*endif\b', line): |
| 2566 | # End of #if or #else blocks. |
| 2567 | if self.pp_stack: |
| 2568 | # If we saw an #else, we will need to restore the nesting |
| 2569 | # stack to its former state before the #else, otherwise we |
| 2570 | # will just continue from where we left off. |
| 2571 | if self.pp_stack[-1].seen_else: |
| 2572 | # Here we can just use a shallow copy since we are the last |
| 2573 | # reference to it. |
| 2574 | self.stack = self.pp_stack[-1].stack_before_else |
| 2575 | # Drop the corresponding #if |
| 2576 | self.pp_stack.pop() |
| 2577 | else: |
| 2578 | # TODO(unknown): unexpected #endif, issue warning? |
| 2579 | pass |
| 2580 | |
| 2581 | # TODO(unknown): Update() is too long, but we will refactor later. |
| 2582 | def Update(self, filename, clean_lines, linenum, error): |
no test coverage detected