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)
| 2428 | return False |
| 2429 | |
| 2430 | def UpdatePreprocessor(self, line): |
| 2431 | """Update preprocessor stack. |
| 2432 | |
| 2433 | We need to handle preprocessors due to classes like this: |
| 2434 | #ifdef SWIG |
| 2435 | struct ResultDetailsPageElementExtensionPoint { |
| 2436 | #else |
| 2437 | struct ResultDetailsPageElementExtensionPoint : public Extension { |
| 2438 | #endif |
| 2439 | |
| 2440 | We make the following assumptions (good enough for most files): |
| 2441 | - Preprocessor condition evaluates to true from #if up to first |
| 2442 | #else/#elif/#endif. |
| 2443 | |
| 2444 | - Preprocessor condition evaluates to false from #else/#elif up |
| 2445 | to #endif. We still perform lint checks on these lines, but |
| 2446 | these do not affect nesting stack. |
| 2447 | |
| 2448 | Args: |
| 2449 | line: current line to check. |
| 2450 | """ |
| 2451 | if Match(r'^\s*#\s*(if|ifdef|ifndef)\b', line): |
| 2452 | # Beginning of #if block, save the nesting stack here. The saved |
| 2453 | # stack will allow us to restore the parsing state in the #else case. |
| 2454 | self.pp_stack.append(_PreprocessorInfo(copy.deepcopy(self.stack))) |
| 2455 | elif Match(r'^\s*#\s*(else|elif)\b', line): |
| 2456 | # Beginning of #else block |
| 2457 | if self.pp_stack: |
| 2458 | if not self.pp_stack[-1].seen_else: |
| 2459 | # This is the first #else or #elif block. Remember the |
| 2460 | # whole nesting stack up to this point. This is what we |
| 2461 | # keep after the #endif. |
| 2462 | self.pp_stack[-1].seen_else = True |
| 2463 | self.pp_stack[-1].stack_before_else = copy.deepcopy(self.stack) |
| 2464 | |
| 2465 | # Restore the stack to how it was before the #if |
| 2466 | self.stack = copy.deepcopy(self.pp_stack[-1].stack_before_if) |
| 2467 | else: |
| 2468 | # TODO(unknown): unexpected #else, issue warning? |
| 2469 | pass |
| 2470 | elif Match(r'^\s*#\s*endif\b', line): |
| 2471 | # End of #if or #else blocks. |
| 2472 | if self.pp_stack: |
| 2473 | # If we saw an #else, we will need to restore the nesting |
| 2474 | # stack to its former state before the #else, otherwise we |
| 2475 | # will just continue from where we left off. |
| 2476 | if self.pp_stack[-1].seen_else: |
| 2477 | # Here we can just use a shallow copy since we are the last |
| 2478 | # reference to it. |
| 2479 | self.stack = self.pp_stack[-1].stack_before_else |
| 2480 | # Drop the corresponding #if |
| 2481 | self.pp_stack.pop() |
| 2482 | else: |
| 2483 | # TODO(unknown): unexpected #endif, issue warning? |
| 2484 | pass |
| 2485 | |
| 2486 | # TODO(unknown): Update() is too long, but we will refactor later. |
| 2487 | def Update(self, filename, clean_lines, linenum, error): |
no test coverage detected