1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 __revision__ = "src/engine/SCons/cpp.py 2014/09/27 12:51:43 garyo"
25
26 __doc__ = """
27 SCons C Pre-Processor module
28 """
29
30 import SCons.compat
31
32 import os
33 import re
34
35
36
37
38
39
40
41
42
43
44
45
46 cpp_lines_dict = {
47
48
49 ('if', 'elif', 'ifdef', 'ifndef',)
50 : '\s+(.+)',
51
52
53
54 ('import', 'include', 'include_next',)
55 : '\s*(.+)',
56
57
58 ('else', 'endif',) : '',
59
60
61
62
63
64
65 ('define',) : '\s+([_A-Za-z][_A-Za-z0-9_]*)(\([^)]*\))?\s*(.*)',
66
67
68 ('undef',) : '\s+([_A-Za-z][A-Za-z0-9_]*)',
69 }
70
71
72
73
74 Table = {}
75 for op_list, expr in cpp_lines_dict.items():
76 e = re.compile(expr)
77 for op in op_list:
78 Table[op] = e
79 del e
80 del op
81 del op_list
82
83
84
85
86
87 override = {
88 'if' : 'if(?!def)',
89 }
90 l = [override.get(x, x) for x in Table.keys()]
91
92
93
94
95
96
97
98 e = '^\s*#\s*(' + '|'.join(l) + ')(.*)$'
99
100
101 CPP_Expression = re.compile(e, re.M)
102
103
104
105
106
107
108
109
110
111
112
113
114
115 CPP_to_Python_Ops_Dict = {
116 '!' : ' not ',
117 '!=' : ' != ',
118 '&&' : ' and ',
119 '||' : ' or ',
120 '?' : ' and ',
121 ':' : ' or ',
122 '\r' : '',
123 }
124
125 CPP_to_Python_Ops_Sub = lambda m: CPP_to_Python_Ops_Dict[m.group(0)]
126
127
128
129
130
131
132
133 l = sorted(CPP_to_Python_Ops_Dict.keys(), key=lambda a: len(a), reverse=True)
134
135
136
137 expr = '|'.join(map(re.escape, l))
138
139
140 CPP_to_Python_Ops_Expression = re.compile(expr)
141
142
143
144 CPP_to_Python_Eval_List = [
145 ['defined\s+(\w+)', '"\\1" in __dict__'],
146 ['defined\s*\((\w+)\)', '"\\1" in __dict__'],
147 ['/\*.*\*/', ''],
148 ['/\*.*', ''],
149 ['//.*', ''],
150 ['(0x[0-9A-Fa-f]*)[UL]+', '\\1'],
151 ]
152
153
154
155 for l in CPP_to_Python_Eval_List:
156 l[0] = re.compile(l[0])
157
158
160 """
161 Converts a C pre-processor expression into an equivalent
162 Python expression that can be evaluated.
163 """
164 s = CPP_to_Python_Ops_Expression.sub(CPP_to_Python_Ops_Sub, s)
165 for expr, repl in CPP_to_Python_Eval_List:
166 s = expr.sub(repl, s)
167 return s
168
169
170
171 del expr
172 del l
173 del override
174
175
176
178 """
179 Handles delayed evaluation of a #define function call.
180 """
181 - def __init__(self, name, args, expansion):
182 """
183 Squirrels away the arguments and expansion value of a #define
184 macro function for later evaluation when we must actually expand
185 a value that uses it.
186 """
187 self.name = name
188 self.args = function_arg_separator.split(args)
189 try:
190 expansion = expansion.split('##')
191 except AttributeError:
192 pass
193 self.expansion = expansion
195 """
196 Evaluates the expansion of a #define macro function called
197 with the specified values.
198 """
199 if len(self.args) != len(values):
200 raise ValueError("Incorrect number of arguments to `%s'" % self.name)
201
202
203
204
205 locals = {}
206 for k, v in zip(self.args, values):
207 locals[k] = v
208
209 parts = []
210 for s in self.expansion:
211 if not s in self.args:
212 s = repr(s)
213 parts.append(s)
214 statement = ' + '.join(parts)
215
216 return eval(statement, globals(), locals)
217
218
219
220
221 line_continuations = re.compile('\\\\\r?\n')
222
223
224
225
226 function_name = re.compile('(\S+)\(([^)]*)\)')
227
228
229
230 function_arg_separator = re.compile(',\s*')
231
232
233
235 """
236 The main workhorse class for handling C pre-processing.
237 """
238 - def __init__(self, current=os.curdir, cpppath=(), dict={}, all=0):
239 global Table
240
241 cpppath = tuple(cpppath)
242
243 self.searchpath = {
244 '"' : (current,) + cpppath,
245 '<' : cpppath + (current,),
246 }
247
248
249
250
251
252
253 self.cpp_namespace = dict.copy()
254 self.cpp_namespace['__dict__'] = self.cpp_namespace
255
256 if all:
257 self.do_include = self.all_include
258
259
260
261
262
263
264
265
266 d = {
267 'scons_current_file' : self.scons_current_file
268 }
269 for op in Table.keys():
270 d[op] = getattr(self, 'do_' + op)
271 self.default_table = d
272
273
274
276 """
277 Turns the contents of a file into a list of easily-processed
278 tuples describing the CPP lines in the file.
279
280 The first element of each tuple is the line's preprocessor
281 directive (#if, #include, #define, etc., minus the initial '#').
282 The remaining elements are specific to the type of directive, as
283 pulled apart by the regular expression.
284 """
285 global CPP_Expression, Table
286 contents = line_continuations.sub('', contents)
287 cpp_tuples = CPP_Expression.findall(contents)
288 return [(m[0],) + Table[m[0]].match(m[1]).groups() for m in cpp_tuples]
289
291 """
292 Pre-processes a file.
293
294 This is the main public entry point.
295 """
296 self.current_file = file
297 return self.process_contents(self.read_file(file), file)
298
299 - def process_contents(self, contents, fname=None):
300 """
301 Pre-processes a file contents.
302
303 This is the main internal entry point.
304 """
305 self.stack = []
306 self.dispatch_table = self.default_table.copy()
307 self.current_file = fname
308 self.tuples = self.tupleize(contents)
309
310 self.initialize_result(fname)
311 while self.tuples:
312 t = self.tuples.pop(0)
313
314
315
316 self.dispatch_table[t[0]](t)
317 return self.finalize_result(fname)
318
319
320
322 """
323 Pushes the current dispatch table on the stack and re-initializes
324 the current dispatch table to the default.
325 """
326 self.stack.append(self.dispatch_table)
327 self.dispatch_table = self.default_table.copy()
328
330 """
331 Pops the previous dispatch table off the stack and makes it the
332 current one.
333 """
334 try: self.dispatch_table = self.stack.pop()
335 except IndexError: pass
336
337
338
340 """
341 Null method for when we explicitly want the action for a
342 specific preprocessor directive to do nothing.
343 """
344 pass
345
347 self.current_file = t[1]
348
350 """
351 Evaluates a C preprocessor expression.
352
353 This is done by converting it to a Python equivalent and
354 eval()ing it in the C preprocessor namespace we use to
355 track #define values.
356 """
357 t = CPP_to_Python(' '.join(t[1:]))
358 try: return eval(t, self.cpp_namespace)
359 except (NameError, TypeError): return 0
360
363
366
368 """
369 Finds the #include file for a given preprocessor tuple.
370 """
371 fname = t[2]
372 for d in self.searchpath[t[1]]:
373 if d == os.curdir:
374 f = fname
375 else:
376 f = os.path.join(d, fname)
377 if os.path.isfile(f):
378 return f
379 return None
380
383
384
385
387 """
388 Causes the PreProcessor object to start processing #import,
389 #include and #include_next lines.
390
391 This method will be called when a #if, #ifdef, #ifndef or #elif
392 evaluates True, or when we reach the #else in a #if, #ifdef,
393 #ifndef or #elif block where a condition already evaluated
394 False.
395
396 """
397 d = self.dispatch_table
398 p = self.stack[-1] if self.stack else self.default_table
399
400 for k in ('import', 'include', 'include_next'):
401 d[k] = p[k]
402
404 """
405 Causes the PreProcessor object to stop processing #import,
406 #include and #include_next lines.
407
408 This method will be called when a #if, #ifdef, #ifndef or #elif
409 evaluates False, or when we reach the #else in a #if, #ifdef,
410 #ifndef or #elif block where a condition already evaluated True.
411 """
412 d = self.dispatch_table
413 d['import'] = self.do_nothing
414 d['include'] = self.do_nothing
415 d['include_next'] = self.do_nothing
416
417
418
419
420
436
438 """
439 Default handling of a #ifdef line.
440 """
441 self._do_if_else_condition(t[1] in self.cpp_namespace)
442
444 """
445 Default handling of a #ifndef line.
446 """
447 self._do_if_else_condition(t[1] not in self.cpp_namespace)
448
454
464
466 """
467 Default handling of a #else line.
468 """
469 pass
470
472 """
473 Default handling of a #endif line.
474 """
475 self.restore()
476
478 """
479 Default handling of a #define line.
480 """
481 _, name, args, expansion = t
482 try:
483 expansion = int(expansion)
484 except (TypeError, ValueError):
485 pass
486 if args:
487 evaluator = FunctionEvaluator(name, args[1:-1], expansion)
488 self.cpp_namespace[name] = evaluator
489 else:
490 self.cpp_namespace[name] = expansion
491
493 """
494 Default handling of a #undef line.
495 """
496 try: del self.cpp_namespace[t[1]]
497 except KeyError: pass
498
500 """
501 Default handling of a #import line.
502 """
503
504 pass
505
507 """
508 Default handling of a #include line.
509 """
510 t = self.resolve_include(t)
511 include_file = self.find_include_file(t)
512 if include_file:
513
514 self.result.append(include_file)
515 contents = self.read_file(include_file)
516 new_tuples = [('scons_current_file', include_file)] + \
517 self.tupleize(contents) + \
518 [('scons_current_file', self.current_file)]
519 self.tuples[:] = new_tuples + self.tuples
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536 do_include_next = do_include
537
538
539
541 """Resolve a tuple-ized #include line.
542
543 This handles recursive expansion of values without "" or <>
544 surrounding the name until an initial " or < is found, to handle
545 #include FILE
546 where FILE is a #define somewhere else.
547 """
548 s = t[1]
549 while not s[0] in '<"':
550
551 try:
552 s = self.cpp_namespace[s]
553 except KeyError:
554 m = function_name.search(s)
555 s = self.cpp_namespace[m.group(1)]
556 if callable(s):
557 args = function_arg_separator.split(m.group(2))
558 s = s(*args)
559 if not s:
560 return None
561 return (t[0], s[0], s[1:-1])
562
567
569 """A preprocessor that ignores all #if/#elif/#else/#endif directives
570 and just reports back *all* of the #include files (like the classic
571 SCons scanner did).
572
573 This is functionally equivalent to using a regular expression to
574 find all of the #include lines, only slower. It exists mainly as
575 an example of how the main PreProcessor class can be sub-classed
576 to tailor its behavior.
577 """
579 PreProcessor.__init__(self, *args, **kw)
580 d = self.default_table
581 for func in ['if', 'elif', 'else', 'endif', 'ifdef', 'ifndef']:
582 d[func] = d[func] = self.do_nothing
583
584 del __revision__
585
586
587
588
589
590
591