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)
| 3028 | return False |
| 3029 | |
| 3030 | def UpdatePreprocessor(self, line): |
| 3031 | """Update preprocessor stack. |
| 3032 | |
| 3033 | We need to handle preprocessors due to classes like this: |
| 3034 | #ifdef SWIG |
| 3035 | struct ResultDetailsPageElementExtensionPoint { |
| 3036 | #else |
| 3037 | struct ResultDetailsPageElementExtensionPoint : public Extension { |
| 3038 | #endif |
| 3039 | |
| 3040 | We make the following assumptions (good enough for most files): |
| 3041 | - Preprocessor condition evaluates to true from #if up to first |
| 3042 | #else/#elif/#endif. |
| 3043 | |
| 3044 | - Preprocessor condition evaluates to false from #else/#elif up |
| 3045 | to #endif. We still perform lint checks on these lines, but |
| 3046 | these do not affect nesting stack. |
| 3047 | |
| 3048 | Args: |
| 3049 | line: current line to check. |
| 3050 | """ |
| 3051 | if Match(r'^\s*#\s*(if|ifdef|ifndef)\b', line): |
| 3052 | # Beginning of #if block, save the nesting stack here. The saved |
| 3053 | # stack will allow us to restore the parsing state in the #else case. |
| 3054 | self.pp_stack.append(_PreprocessorInfo(copy.deepcopy(self.stack))) |
| 3055 | elif Match(r'^\s*#\s*(else|elif)\b', line): |
| 3056 | # Beginning of #else block |
| 3057 | if self.pp_stack: |
| 3058 | if not self.pp_stack[-1].seen_else: |
| 3059 | # This is the first #else or #elif block. Remember the |
| 3060 | # whole nesting stack up to this point. This is what we |
| 3061 | # keep after the #endif. |
| 3062 | self.pp_stack[-1].seen_else = True |
| 3063 | self.pp_stack[-1].stack_before_else = copy.deepcopy(self.stack) |
| 3064 | |
| 3065 | # Restore the stack to how it was before the #if |
| 3066 | self.stack = copy.deepcopy(self.pp_stack[-1].stack_before_if) |
| 3067 | else: |
| 3068 | # TODO(unknown): unexpected #else, issue warning? |
| 3069 | pass |
| 3070 | elif Match(r'^\s*#\s*endif\b', line): |
| 3071 | # End of #if or #else blocks. |
| 3072 | if self.pp_stack: |
| 3073 | # If we saw an #else, we will need to restore the nesting |
| 3074 | # stack to its former state before the #else, otherwise we |
| 3075 | # will just continue from where we left off. |
| 3076 | if self.pp_stack[-1].seen_else: |
| 3077 | # Here we can just use a shallow copy since we are the last |
| 3078 | # reference to it. |
| 3079 | self.stack = self.pp_stack[-1].stack_before_else |
| 3080 | # Drop the corresponding #if |
| 3081 | self.pp_stack.pop() |
| 3082 | else: |
| 3083 | # TODO(unknown): unexpected #endif, issue warning? |
| 3084 | pass |
| 3085 | |
| 3086 | # TODO(unknown): Update() is too long, but we will refactor later. |
| 3087 | def Update(self, filename, clean_lines, linenum, error): |
no test coverage detected