MCPcopy Create free account
hub / github.com/BVLC/caffe / UpdatePreprocessor

Method UpdatePreprocessor

scripts/cpp_lint.py:1952–2006  ·  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

1950 return self.stack and isinstance(self.stack[-1], _NamespaceInfo)
1951
1952 def UpdatePreprocessor(self, line):
1953 """Update preprocessor stack.
1954
1955 We need to handle preprocessors due to classes like this:
1956 #ifdef SWIG
1957 struct ResultDetailsPageElementExtensionPoint {
1958 #else
1959 struct ResultDetailsPageElementExtensionPoint : public Extension {
1960 #endif
1961
1962 We make the following assumptions (good enough for most files):
1963 - Preprocessor condition evaluates to true from #if up to first
1964 #else/#elif/#endif.
1965
1966 - Preprocessor condition evaluates to false from #else/#elif up
1967 to #endif. We still perform lint checks on these lines, but
1968 these do not affect nesting stack.
1969
1970 Args:
1971 line: current line to check.
1972 """
1973 if Match(r'^\s*#\s*(if|ifdef|ifndef)\b', line):
1974 # Beginning of #if block, save the nesting stack here. The saved
1975 # stack will allow us to restore the parsing state in the #else case.
1976 self.pp_stack.append(_PreprocessorInfo(copy.deepcopy(self.stack)))
1977 elif Match(r'^\s*#\s*(else|elif)\b', line):
1978 # Beginning of #else block
1979 if self.pp_stack:
1980 if not self.pp_stack[-1].seen_else:
1981 # This is the first #else or #elif block. Remember the
1982 # whole nesting stack up to this point. This is what we
1983 # keep after the #endif.
1984 self.pp_stack[-1].seen_else = True
1985 self.pp_stack[-1].stack_before_else = copy.deepcopy(self.stack)
1986
1987 # Restore the stack to how it was before the #if
1988 self.stack = copy.deepcopy(self.pp_stack[-1].stack_before_if)
1989 else:
1990 # TODO(unknown): unexpected #else, issue warning?
1991 pass
1992 elif Match(r'^\s*#\s*endif\b', line):
1993 # End of #if or #else blocks.
1994 if self.pp_stack:
1995 # If we saw an #else, we will need to restore the nesting
1996 # stack to its former state before the #else, otherwise we
1997 # will just continue from where we left off.
1998 if self.pp_stack[-1].seen_else:
1999 # Here we can just use a shallow copy since we are the last
2000 # reference to it.
2001 self.stack = self.pp_stack[-1].stack_before_else
2002 # Drop the corresponding #if
2003 self.pp_stack.pop()
2004 else:
2005 # TODO(unknown): unexpected #endif, issue warning?
2006 pass
2007
2008 def Update(self, filename, clean_lines, linenum, error):
2009 """Update nesting state with current line.

Callers 1

UpdateMethod · 0.95

Calls 3

MatchFunction · 0.85
_PreprocessorInfoClass · 0.85
popMethod · 0.80

Tested by

no test coverage detected