Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/ifdef.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 14a27acc755a |
| children | 8a7a2f1f88f0 |
| 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 regex | |
| 31 import getopt | |
| 32 import string | |
| 33 | |
| 34 defs = [] | |
| 35 undefs = [] | |
| 36 | |
| 37 def main(): | |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
38 opts, args = getopt.getopt(sys.argv[1:], 'D:U:') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
39 for o, a in opts: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
40 if o == '-D': |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
41 defs.append(a) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
42 if o == '-U': |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
43 undefs.append(a) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
44 if not args: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
45 args = ['-'] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
46 for file in args: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
47 if file == '-': |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
48 process(sys.stdin, sys.stdout) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
49 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
50 f = open(file, 'r') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
51 process(f, sys.stdout) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
52 f.close() |
| 1624 | 53 |
| 54 def process(fpi, fpo): | |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
55 keywords = ('if', 'ifdef', 'ifndef', 'else', 'endif') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
56 ok = 1 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
57 stack = [] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
58 while 1: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
59 line = fpi.readline() |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
60 if not line: break |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
61 while line[-2:] == '\\\n': |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
62 nextline = fpi.readline() |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
63 if not nextline: break |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
64 line = line + nextline |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
65 tmp = string.strip(line) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
66 if tmp[:1] != '#': |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
67 if ok: fpo.write(line) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
68 continue |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
69 tmp = string.strip(tmp[1:]) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
70 words = string.split(tmp) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
71 keyword = words[0] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
72 if keyword not in keywords: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
73 if ok: fpo.write(line) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
74 continue |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
75 if keyword in ('ifdef', 'ifndef') and len(words) == 2: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
76 if keyword == 'ifdef': |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
77 ko = 1 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
78 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
79 ko = 0 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
80 word = words[1] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
81 if word in defs: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
82 stack.append((ok, ko, word)) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
83 if not ko: ok = 0 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
84 elif word in undefs: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
85 stack.append((ok, not ko, word)) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
86 if ko: ok = 0 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
87 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
88 stack.append((ok, -1, word)) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
89 if ok: fpo.write(line) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
90 elif keyword == 'if': |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
91 stack.append((ok, -1, '')) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
92 if ok: fpo.write(line) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
93 elif keyword == 'else' and stack: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
94 s_ok, s_ko, s_word = stack[-1] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
95 if s_ko < 0: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
96 if ok: fpo.write(line) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
97 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
98 s_ko = not s_ko |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
99 ok = s_ok |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
100 if not s_ko: ok = 0 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
101 stack[-1] = s_ok, s_ko, s_word |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
102 elif keyword == 'endif' and stack: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
103 s_ok, s_ko, s_word = stack[-1] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
104 if s_ko < 0: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
105 if ok: fpo.write(line) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
106 del stack[-1] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
107 ok = s_ok |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
108 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
109 sys.stderr.write('Unknown keyword %s\n' % keyword) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
110 if stack: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9309
diff
changeset
|
111 sys.stderr.write('stack: %s\n' % stack) |
| 1624 | 112 |
| 113 main() |
