| 1 | n/a | import re |
|---|
| 2 | n/a | import sys |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | def negate(condition): |
|---|
| 5 | n/a | """ |
|---|
| 6 | n/a | Returns a CPP conditional that is the opposite of the conditional passed in. |
|---|
| 7 | n/a | """ |
|---|
| 8 | n/a | if condition.startswith('!'): |
|---|
| 9 | n/a | return condition[1:] |
|---|
| 10 | n/a | return "!" + condition |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | class Monitor: |
|---|
| 13 | n/a | """ |
|---|
| 14 | n/a | A simple C preprocessor that scans C source and computes, line by line, |
|---|
| 15 | n/a | what the current C preprocessor #if state is. |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | Doesn't handle everything--for example, if you have /* inside a C string, |
|---|
| 18 | n/a | without a matching */ (also inside a C string), or with a */ inside a C |
|---|
| 19 | n/a | string but on another line and with preprocessor macros in between... |
|---|
| 20 | n/a | the parser will get lost. |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | Anyway this implementation seems to work well enough for the CPython sources. |
|---|
| 23 | n/a | """ |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | is_a_simple_defined = re.compile(r'^defined\s*\(\s*[A-Za-z0-9_]+\s*\)$').match |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | def __init__(self, filename=None, *, verbose=False): |
|---|
| 28 | n/a | self.stack = [] |
|---|
| 29 | n/a | self.in_comment = False |
|---|
| 30 | n/a | self.continuation = None |
|---|
| 31 | n/a | self.line_number = 0 |
|---|
| 32 | n/a | self.filename = filename |
|---|
| 33 | n/a | self.verbose = verbose |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | def __repr__(self): |
|---|
| 36 | n/a | return ''.join(( |
|---|
| 37 | n/a | '<Monitor ', |
|---|
| 38 | n/a | str(id(self)), |
|---|
| 39 | n/a | " line=", str(self.line_number), |
|---|
| 40 | n/a | " condition=", repr(self.condition()), |
|---|
| 41 | n/a | ">")) |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | def status(self): |
|---|
| 44 | n/a | return str(self.line_number).rjust(4) + ": " + self.condition() |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | def condition(self): |
|---|
| 47 | n/a | """ |
|---|
| 48 | n/a | Returns the current preprocessor state, as a single #if condition. |
|---|
| 49 | n/a | """ |
|---|
| 50 | n/a | return " && ".join(condition for token, condition in self.stack) |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | def fail(self, *a): |
|---|
| 53 | n/a | if self.filename: |
|---|
| 54 | n/a | filename = " " + self.filename |
|---|
| 55 | n/a | else: |
|---|
| 56 | n/a | filename = '' |
|---|
| 57 | n/a | print("Error at" + filename, "line", self.line_number, ":") |
|---|
| 58 | n/a | print(" ", ' '.join(str(x) for x in a)) |
|---|
| 59 | n/a | sys.exit(-1) |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | def close(self): |
|---|
| 62 | n/a | if self.stack: |
|---|
| 63 | n/a | self.fail("Ended file while still in a preprocessor conditional block!") |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | def write(self, s): |
|---|
| 66 | n/a | for line in s.split("\n"): |
|---|
| 67 | n/a | self.writeline(line) |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | def writeline(self, line): |
|---|
| 70 | n/a | self.line_number += 1 |
|---|
| 71 | n/a | line = line.strip() |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | def pop_stack(): |
|---|
| 74 | n/a | if not self.stack: |
|---|
| 75 | n/a | self.fail("#" + token + " without matching #if / #ifdef / #ifndef!") |
|---|
| 76 | n/a | return self.stack.pop() |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | if self.continuation: |
|---|
| 79 | n/a | line = self.continuation + line |
|---|
| 80 | n/a | self.continuation = None |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | if not line: |
|---|
| 83 | n/a | return |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | if line.endswith('\\'): |
|---|
| 86 | n/a | self.continuation = line[:-1].rstrip() + " " |
|---|
| 87 | n/a | return |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | # we have to ignore preprocessor commands inside comments |
|---|
| 90 | n/a | # |
|---|
| 91 | n/a | # we also have to handle this: |
|---|
| 92 | n/a | # /* start |
|---|
| 93 | n/a | # ... |
|---|
| 94 | n/a | # */ /* <-- tricky! |
|---|
| 95 | n/a | # ... |
|---|
| 96 | n/a | # */ |
|---|
| 97 | n/a | # and this: |
|---|
| 98 | n/a | # /* start |
|---|
| 99 | n/a | # ... |
|---|
| 100 | n/a | # */ /* also tricky! */ |
|---|
| 101 | n/a | if self.in_comment: |
|---|
| 102 | n/a | if '*/' in line: |
|---|
| 103 | n/a | # snip out the comment and continue |
|---|
| 104 | n/a | # |
|---|
| 105 | n/a | # GCC allows |
|---|
| 106 | n/a | # /* comment |
|---|
| 107 | n/a | # */ #include <stdio.h> |
|---|
| 108 | n/a | # maybe other compilers too? |
|---|
| 109 | n/a | _, _, line = line.partition('*/') |
|---|
| 110 | n/a | self.in_comment = False |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | while True: |
|---|
| 113 | n/a | if '/*' in line: |
|---|
| 114 | n/a | if self.in_comment: |
|---|
| 115 | n/a | self.fail("Nested block comment!") |
|---|
| 116 | n/a | |
|---|
| 117 | n/a | before, _, remainder = line.partition('/*') |
|---|
| 118 | n/a | comment, comment_ends, after = remainder.partition('*/') |
|---|
| 119 | n/a | if comment_ends: |
|---|
| 120 | n/a | # snip out the comment |
|---|
| 121 | n/a | line = before.rstrip() + ' ' + after.lstrip() |
|---|
| 122 | n/a | continue |
|---|
| 123 | n/a | # comment continues to eol |
|---|
| 124 | n/a | self.in_comment = True |
|---|
| 125 | n/a | line = before.rstrip() |
|---|
| 126 | n/a | break |
|---|
| 127 | n/a | |
|---|
| 128 | n/a | # we actually have some // comments |
|---|
| 129 | n/a | # (but block comments take precedence) |
|---|
| 130 | n/a | before, line_comment, comment = line.partition('//') |
|---|
| 131 | n/a | if line_comment: |
|---|
| 132 | n/a | line = before.rstrip() |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | if not line.startswith('#'): |
|---|
| 135 | n/a | return |
|---|
| 136 | n/a | |
|---|
| 137 | n/a | line = line[1:].lstrip() |
|---|
| 138 | n/a | assert line |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | fields = line.split() |
|---|
| 141 | n/a | token = fields[0].lower() |
|---|
| 142 | n/a | condition = ' '.join(fields[1:]).strip() |
|---|
| 143 | n/a | |
|---|
| 144 | n/a | if_tokens = {'if', 'ifdef', 'ifndef'} |
|---|
| 145 | n/a | all_tokens = if_tokens | {'elif', 'else', 'endif'} |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | if token not in all_tokens: |
|---|
| 148 | n/a | return |
|---|
| 149 | n/a | |
|---|
| 150 | n/a | # cheat a little here, to reuse the implementation of if |
|---|
| 151 | n/a | if token == 'elif': |
|---|
| 152 | n/a | pop_stack() |
|---|
| 153 | n/a | token = 'if' |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | if token in if_tokens: |
|---|
| 156 | n/a | if not condition: |
|---|
| 157 | n/a | self.fail("Invalid format for #" + token + " line: no argument!") |
|---|
| 158 | n/a | if token == 'if': |
|---|
| 159 | n/a | if not self.is_a_simple_defined(condition): |
|---|
| 160 | n/a | condition = "(" + condition + ")" |
|---|
| 161 | n/a | else: |
|---|
| 162 | n/a | fields = condition.split() |
|---|
| 163 | n/a | if len(fields) != 1: |
|---|
| 164 | n/a | self.fail("Invalid format for #" + token + " line: should be exactly one argument!") |
|---|
| 165 | n/a | symbol = fields[0] |
|---|
| 166 | n/a | condition = 'defined(' + symbol + ')' |
|---|
| 167 | n/a | if token == 'ifndef': |
|---|
| 168 | n/a | condition = '!' + condition |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | self.stack.append(("if", condition)) |
|---|
| 171 | n/a | if self.verbose: |
|---|
| 172 | n/a | print(self.status()) |
|---|
| 173 | n/a | return |
|---|
| 174 | n/a | |
|---|
| 175 | n/a | previous_token, previous_condition = pop_stack() |
|---|
| 176 | n/a | |
|---|
| 177 | n/a | if token == 'else': |
|---|
| 178 | n/a | self.stack.append(('else', negate(previous_condition))) |
|---|
| 179 | n/a | elif token == 'endif': |
|---|
| 180 | n/a | pass |
|---|
| 181 | n/a | if self.verbose: |
|---|
| 182 | n/a | print(self.status()) |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | if __name__ == '__main__': |
|---|
| 185 | n/a | for filename in sys.argv[1:]: |
|---|
| 186 | n/a | with open(filename, "rt") as f: |
|---|
| 187 | n/a | cpp = Monitor(filename, verbose=True) |
|---|
| 188 | n/a | print() |
|---|
| 189 | n/a | print(filename) |
|---|
| 190 | n/a | for line_number, line in enumerate(f.read().split('\n'), 1): |
|---|
| 191 | n/a | cpp.writeline(line) |
|---|