MCPcopy Create free account
hub / github.com/alibaba/graph-learn / UpdatePreprocessor

Method UpdatePreprocessor

dynamic_graph_service/ci/cpplint.py:2748–2802  ·  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

2746 return False
2747
2748 def UpdatePreprocessor(self, line):
2749 """Update preprocessor stack.
2750
2751 We need to handle preprocessors due to classes like this:
2752 #ifdef SWIG
2753 struct ResultDetailsPageElementExtensionPoint {
2754 #else
2755 struct ResultDetailsPageElementExtensionPoint : public Extension {
2756 #endif
2757
2758 We make the following assumptions (good enough for most files):
2759 - Preprocessor condition evaluates to true from #if up to first
2760 #else/#elif/#endif.
2761
2762 - Preprocessor condition evaluates to false from #else/#elif up
2763 to #endif. We still perform lint checks on these lines, but
2764 these do not affect nesting stack.
2765
2766 Args:
2767 line: current line to check.
2768 """
2769 if Match(r'^\s*#\s*(if|ifdef|ifndef)\b', line):
2770 # Beginning of #if block, save the nesting stack here. The saved
2771 # stack will allow us to restore the parsing state in the #else case.
2772 self.pp_stack.append(_PreprocessorInfo(copy.deepcopy(self.stack)))
2773 elif Match(r'^\s*#\s*(else|elif)\b', line):
2774 # Beginning of #else block
2775 if self.pp_stack:
2776 if not self.pp_stack[-1].seen_else:
2777 # This is the first #else or #elif block. Remember the
2778 # whole nesting stack up to this point. This is what we
2779 # keep after the #endif.
2780 self.pp_stack[-1].seen_else = True
2781 self.pp_stack[-1].stack_before_else = copy.deepcopy(self.stack)
2782
2783 # Restore the stack to how it was before the #if
2784 self.stack = copy.deepcopy(self.pp_stack[-1].stack_before_if)
2785 else:
2786 # TODO(unknown): unexpected #else, issue warning?
2787 pass
2788 elif Match(r'^\s*#\s*endif\b', line):
2789 # End of #if or #else blocks.
2790 if self.pp_stack:
2791 # If we saw an #else, we will need to restore the nesting
2792 # stack to its former state before the #else, otherwise we
2793 # will just continue from where we left off.
2794 if self.pp_stack[-1].seen_else:
2795 # Here we can just use a shallow copy since we are the last
2796 # reference to it.
2797 self.stack = self.pp_stack[-1].stack_before_else
2798 # Drop the corresponding #if
2799 self.pp_stack.pop()
2800 else:
2801 # TODO(unknown): unexpected #endif, issue warning?
2802 pass
2803
2804 # TODO(unknown): Update() is too long, but we will refactor later.
2805 def Update(self, filename, clean_lines, linenum, error):

Callers 1

UpdateMethod · 0.95

Calls 4

MatchFunction · 0.85
_PreprocessorInfoClass · 0.85
popMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected