blob: c0c14595733a1deb95ab3404788605acd8966416 [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossum7faf67c1994-05-09 14:52:41 +00002
3# Selectively preprocess #ifdef / #ifndef statements.
4# Usage:
5# ifdef [-Dname] ... [-Uname] ... [file] ...
Tim Peters70c43782001-01-17 08:48:39 +00006#
Guido van Rossum7faf67c1994-05-09 14:52:41 +00007# This scans the file(s), looking for #ifdef and #ifndef preprocessor
8# commands that test for one of the names mentioned in the -D and -U
9# options. On standard output it writes a copy of the input file(s)
10# minus those code sections that are suppressed by the selected
11# combination of defined/undefined symbols. The #if(n)def/#else/#else
12# lines themselfs (if the #if(n)def tests for one of the mentioned
13# names) are removed as well.
14
15# Features: Arbitrary nesting of recognized and unrecognized
16# preprocesor statements works correctly. Unrecognized #if* commands
17# are left in place, so it will never remove too much, only too
18# little. It does accept whitespace around the '#' character.
19
20# Restrictions: There should be no comments or other symbols on the
21# #if(n)def lines. The effect of #define/#undef commands in the input
22# file or in included files is not taken into account. Tests using
23# #if and the defined() pseudo function are not recognized. The #elif
24# command is not recognized. Improperly nesting is not detected.
25# Lines that look like preprocessor commands but which are actually
26# part of comments or string literals will be mistaken for
27# preprocessor commands.
28
29import sys
30import regex
31import getopt
Guido van Rossum7faf67c1994-05-09 14:52:41 +000032
33defs = []
34undefs = []
35
36def main():
Tim Peters70c43782001-01-17 08:48:39 +000037 opts, args = getopt.getopt(sys.argv[1:], 'D:U:')
38 for o, a in opts:
39 if o == '-D':
40 defs.append(a)
41 if o == '-U':
42 undefs.append(a)
43 if not args:
44 args = ['-']
45 for file in args:
46 if file == '-':
47 process(sys.stdin, sys.stdout)
48 else:
49 f = open(file, 'r')
50 process(f, sys.stdout)
51 f.close()
Guido van Rossum7faf67c1994-05-09 14:52:41 +000052
53def process(fpi, fpo):
Tim Peters70c43782001-01-17 08:48:39 +000054 keywords = ('if', 'ifdef', 'ifndef', 'else', 'endif')
55 ok = 1
56 stack = []
57 while 1:
58 line = fpi.readline()
59 if not line: break
60 while line[-2:] == '\\\n':
61 nextline = fpi.readline()
62 if not nextline: break
63 line = line + nextline
Walter Dörwaldaaab30e2002-09-11 20:36:02 +000064 tmp = line.strip()
Tim Peters70c43782001-01-17 08:48:39 +000065 if tmp[:1] != '#':
66 if ok: fpo.write(line)
67 continue
Walter Dörwaldaaab30e2002-09-11 20:36:02 +000068 tmp = tmp[1:].strip()
69 words = tmp.split()
Tim Peters70c43782001-01-17 08:48:39 +000070 keyword = words[0]
71 if keyword not in keywords:
72 if ok: fpo.write(line)
73 continue
74 if keyword in ('ifdef', 'ifndef') and len(words) == 2:
75 if keyword == 'ifdef':
76 ko = 1
77 else:
78 ko = 0
79 word = words[1]
80 if word in defs:
81 stack.append((ok, ko, word))
82 if not ko: ok = 0
83 elif word in undefs:
84 stack.append((ok, not ko, word))
85 if ko: ok = 0
86 else:
87 stack.append((ok, -1, word))
88 if ok: fpo.write(line)
89 elif keyword == 'if':
90 stack.append((ok, -1, ''))
91 if ok: fpo.write(line)
92 elif keyword == 'else' and stack:
93 s_ok, s_ko, s_word = stack[-1]
94 if s_ko < 0:
95 if ok: fpo.write(line)
96 else:
97 s_ko = not s_ko
98 ok = s_ok
99 if not s_ko: ok = 0
100 stack[-1] = s_ok, s_ko, s_word
101 elif keyword == 'endif' and stack:
102 s_ok, s_ko, s_word = stack[-1]
103 if s_ko < 0:
104 if ok: fpo.write(line)
105 del stack[-1]
106 ok = s_ok
107 else:
108 sys.stderr.write('Unknown keyword %s\n' % keyword)
109 if stack:
110 sys.stderr.write('stack: %s\n' % stack)
Guido van Rossum7faf67c1994-05-09 14:52:41 +0000111
112main()