MCPcopy Create free account
hub / github.com/apache/mesos / UpdatePreprocessor

Method UpdatePreprocessor

support/cpplint.py:2530–2584  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers 1

UpdateMethod · 0.95

Calls 3

MatchFunction · 0.85
_PreprocessorInfoClass · 0.85
appendMethod · 0.45

Tested by

no test coverage detected