Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/ifdef.py @ 58765:2ba7677bfa95 legacy-trunk
testCopysign was defined twice in test_math; combine the definitions
| author | Mark Dickinson <dickinsm@gmail.com> |
|---|---|
| date | Sat, 06 Feb 2010 23:11:25 +0000 |
| parents | 4727f260f6f8 |
| children | 33b156e118c0 fcfe324663a6 |
| rev | line source |
|---|---|
|
4573
73758db2f464
/usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents:
1624
diff
changeset
|
1 #! /usr/bin/env python |
| 1624 | 2 |
| 3 # Selectively preprocess #ifdef / #ifndef statements. | |
| 4 # Usage: | |
| 5 # ifdef [-Dname] ... [-Uname] ... [file] ... | |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
6 # |
| 1624 | 7 # 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 | |
| 29 import sys | |
| 30 import getopt | |
| 31 | |
| 32 defs = [] | |
| 33 undefs = [] | |
| 34 | |
| 35 def main(): | |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
36 opts, args = getopt.getopt(sys.argv[1:], 'D:U:') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
37 for o, a in opts: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
38 if o == '-D': |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
39 defs.append(a) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
40 if o == '-U': |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
41 undefs.append(a) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
42 if not args: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
43 args = ['-'] |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
25219
diff
changeset
|
44 for filename in args: |
|
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
25219
diff
changeset
|
45 if filename == '-': |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
46 process(sys.stdin, sys.stdout) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
47 else: |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
25219
diff
changeset
|
48 f = open(filename, 'r') |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
49 process(f, sys.stdout) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
50 f.close() |
| 1624 | 51 |
| 52 def process(fpi, fpo): | |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
53 keywords = ('if', 'ifdef', 'ifndef', 'else', 'endif') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
54 ok = 1 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
55 stack = [] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
56 while 1: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
57 line = fpi.readline() |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
58 if not line: break |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
59 while line[-2:] == '\\\n': |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
60 nextline = fpi.readline() |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
61 if not nextline: break |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
62 line = line + nextline |
|
25219
8a7a2f1f88f0
Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents:
15930
diff
changeset
|
63 tmp = line.strip() |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
64 if tmp[:1] != '#': |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
65 if ok: fpo.write(line) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
66 continue |
|
25219
8a7a2f1f88f0
Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents:
15930
diff
changeset
|
67 tmp = tmp[1:].strip() |
|
8a7a2f1f88f0
Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents:
15930
diff
changeset
|
68 words = tmp.split() |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
69 keyword = words[0] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
70 if keyword not in keywords: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
71 if ok: fpo.write(line) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
72 continue |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
73 if keyword in ('ifdef', 'ifndef') and len(words) == 2: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
74 if keyword == 'ifdef': |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
75 ko = 1 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
76 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
77 ko = 0 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
78 word = words[1] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
79 if word in defs: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
80 stack.append((ok, ko, word)) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
81 if not ko: ok = 0 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
82 elif word in undefs: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
83 stack.append((ok, not ko, word)) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
84 if ko: ok = 0 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
85 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
86 stack.append((ok, -1, word)) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
87 if ok: fpo.write(line) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
88 elif keyword == 'if': |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
89 stack.append((ok, -1, '')) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
90 if ok: fpo.write(line) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
91 elif keyword == 'else' and stack: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
92 s_ok, s_ko, s_word = stack[-1] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
93 if s_ko < 0: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
94 if ok: fpo.write(line) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
95 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
96 s_ko = not s_ko |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
97 ok = s_ok |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
98 if not s_ko: ok = 0 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
99 stack[-1] = s_ok, s_ko, s_word |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
100 elif keyword == 'endif' and stack: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
101 s_ok, s_ko, s_word = stack[-1] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
102 if s_ko < 0: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
103 if ok: fpo.write(line) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
104 del stack[-1] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
105 ok = s_ok |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
106 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
107 sys.stderr.write('Unknown keyword %s\n' % keyword) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
108 if stack: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
109 sys.stderr.write('stack: %s\n' % stack) |
| 1624 | 110 |
|
33074
981fff7363b9
[Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents:
29107
diff
changeset
|
111 if __name__ == '__main__': |
|
981fff7363b9
[Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents:
29107
diff
changeset
|
112 main() |
