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
(self, line)
| 3342 | return False |
| 3343 | |
| 3344 | def UpdatePreprocessor(self, line): |
| 3345 | """Update preprocessor stack. |
| 3346 | |
| 3347 | We need to handle preprocessors due to classes like this: |
| 3348 | #ifdef SWIG |
| 3349 | struct ResultDetailsPageElementExtensionPoint { |
| 3350 | #else |
| 3351 | struct ResultDetailsPageElementExtensionPoint : public Extension { |
| 3352 | #endif |
| 3353 | |
| 3354 | We make the following assumptions (good enough for most files): |
| 3355 | - Preprocessor condition evaluates to true from #if up to first |
| 3356 | #else/#elif/#endif. |
| 3357 | |
| 3358 | - Preprocessor condition evaluates to false from #else/#elif up |
| 3359 | to #endif. We still perform lint checks on these lines, but |
| 3360 | these do not affect nesting stack. |
| 3361 | |
| 3362 | Args: |
| 3363 | line: current line to check. |
| 3364 | """ |
| 3365 | if re.match(r"^\s*#\s*(if|ifdef|ifndef)\b", line): |
| 3366 | # Beginning of #if block, save the nesting stack here. The saved |
| 3367 | # stack will allow us to restore the parsing state in the #else case. |
| 3368 | self.pp_stack.append(_PreprocessorInfo(copy.deepcopy(self.stack))) |
| 3369 | elif re.match(r"^\s*#\s*(else|elif)\b", line): |
| 3370 | # Beginning of #else block |
| 3371 | if self.pp_stack: |
| 3372 | if not self.pp_stack[-1].seen_else: |
| 3373 | # This is the first #else or #elif block. Remember the |
| 3374 | # whole nesting stack up to this point. This is what we |
| 3375 | # keep after the #endif. |
| 3376 | self.pp_stack[-1].seen_else = True |
| 3377 | self.pp_stack[-1].stack_before_else = copy.deepcopy(self.stack) |
| 3378 | |
| 3379 | # Restore the stack to how it was before the #if |
| 3380 | self.stack = copy.deepcopy(self.pp_stack[-1].stack_before_if) |
| 3381 | else: |
| 3382 | # TODO(google): unexpected #else, issue warning? |
| 3383 | pass |
| 3384 | elif re.match(r"^\s*#\s*endif\b", line): |
| 3385 | # End of #if or #else blocks. |
| 3386 | if self.pp_stack: |
| 3387 | # If we saw an #else, we will need to restore the nesting |
| 3388 | # stack to its former state before the #else, otherwise we |
| 3389 | # will just continue from where we left off. |
| 3390 | if self.pp_stack[-1].seen_else: |
| 3391 | # Here we can just use a shallow copy since we are the last |
| 3392 | # reference to it. |
| 3393 | self.stack = self.pp_stack[-1].stack_before_else |
| 3394 | # Drop the corresponding #if |
| 3395 | self.pp_stack.pop() |
| 3396 | else: |
| 3397 | # TODO(google): unexpected #endif, issue warning? |
| 3398 | pass |
| 3399 | |
| 3400 | def _Pop(self): |
| 3401 | """Pop the innermost state (top of the stack) and remember the popped item.""" |
no test coverage detected