Debian Codegraph @

Tools/clinic/libclinic/cpp.py

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