wscript (4441B)
1 #! /usr/bin/env python 2 # encoding: utf-8 3 # Thomas Nagy, 2012 (ita) 4 5 VERSION='0.0.1' 6 APPNAME='preproc_test' 7 top = '.' 8 out = 'build' 9 10 from waflib import Utils 11 from waflib.Tools import c_preproc 12 from waflib.Tools.c_preproc import NUM, OP, IDENT 13 14 from waflib.Logs import pprint 15 16 def configure(conf): 17 pass 18 19 def build(bld): 20 21 bld.failure = 0 22 def disp(color, result): 23 pprint(color, result) 24 if color == 'RED': 25 bld.failure=1 26 def stop_status(bld): 27 if bld.failure: 28 bld.fatal('One or several test failed, check the outputs above') 29 bld.add_post_fun(stop_status) 30 31 defs = { 32 'm1' : "m1 9 + 9", 33 'fun0' : "fun0(x, y) x y", 34 'fun1' : "fun1(x, y) x ## y", 35 'fun2' : "fun2(x) #x", 36 'fun3' : "fun3(x, y) x * y", 37 'fun4' : "fun4(x) fun2(x)", 38 'fun5' : "fun5(x, y, z) x ## y ## z", 39 'fun6' : "fun6(x, y) <x.y>", 40 'fun7' : "fun() 7", 41 } 42 43 def test(x, result, fun=c_preproc.reduce_tokens): 44 toks = c_preproc.tokenize(x) 45 c_preproc.reduce_tokens(toks, defs, []) 46 ret = c_preproc.stringize(toks) 47 48 if ret == result: 49 color = "GREEN" 50 else: 51 color = "RED" 52 disp(color, "%s\t\t%r" % (ret, toks)) 53 54 test("1 + m1 + 1", "1+9+9+1") 55 test("1 + fun0(1, +) 1", "1+1+1") 56 test("fun2(mmm)", "mmm") 57 test("m1", "9+9") 58 test("fun2(m1)", "m1") 59 test("fun4(m1)", "9+9") 60 test("fun1(m, m)", "mm") 61 test("fun5(a, b, c)", "abc") 62 test("fun1(>, =)", ">=") 63 test("fun1(a, 12)", "a12") 64 test("fun5(a, _, 12)", "a_12") 65 test("fun6(math, h)", "<math.h>") 66 67 def test(x, result): 68 ret = c_preproc.extract_include(x, defs) 69 if ret == result: 70 color = "GREEN" 71 else: 72 color = "RED" 73 disp(color, "%s" % str(ret)) 74 75 test("fun6(math, h)", ("<", "math.h")) 76 77 def test(x, result): 78 toks = c_preproc.tokenize(x) 79 c_preproc.reduce_tokens(toks, defs, []) 80 (_, ret) = c_preproc.reduce_eval(toks) 81 if int(ret) == result: 82 color = "GREEN" 83 else: 84 color = "RED" 85 disp(color, "%s\t\t%r" % (ret, toks)) 86 87 test("1+1", 2) 88 test("1-1", 0) 89 test("1?77:0", 77) 90 test("0?0:88", 88) 91 test("1+2*3", 7) 92 test("1*2+3", 5) 93 94 test("7*m1*3", 90) 95 test("m1*3", 36) 96 test("defined m1", 1) 97 test("defined(m1)", 1) 98 test("defined inex", 0) 99 test("defined(inex)", 0) 100 test("fun7()", 7) 101 102 test("0&&2<3", 0) 103 test("(5>1)*6", 6) 104 test("1,2,3*9,9", 9) 105 106 test("0x52 > 02", 1) 107 108 # lazy evaluation 109 test("defined(foo) && foo > 2", 0) 110 test("defined(m1) && m1 > 20", 0) 111 test("defined(m1) || m1 > 20", 1) 112 113 # undefined macros -> 0 114 test("not_possibly_defined || another", 0) 115 116 test("1+2+((3+4)+5)+6==(6*7)/2==1*-1*-1", 1) 117 118 119 def add_defs(a, b, c, expected): 120 main = bld.path.find_resource('src/main.c') 121 bld.env.DEFINES = ['A=%s' % str(a), 'B=%s' % str(b), 'C=%s' % str(c)] 122 gruik = c_preproc.c_parser([main.parent]) 123 gruik.start(main, bld.env) 124 125 if len(gruik.nodes) == 1 and gruik.nodes[0].name == expected: 126 color = "GREEN" 127 else: 128 color = "RED" 129 disp(color, "%r %r %r -> header %s (got %r)" % (a, b, c, expected, gruik.nodes)) 130 131 add_defs(1, 1, 1, 'a.h') 132 add_defs(1, 1, 0, 'b.h') 133 add_defs(1, 0, 1, 'c.h') 134 add_defs(1, 0, 0, 'd.h') 135 add_defs(0, 1, 1, 'e.h') 136 add_defs(0, 1, 0, 'f.h') 137 add_defs(0, 0, 1, 'g.h') 138 add_defs(0, 0, 0, 'h.h') 139 140 141 defs = { 142 'a' : 'a 0', 143 'b' : 'b 1', 144 'c' : 'c 1', 145 'd' : 'd 0', 146 'e' : 'e a || b || c || d' 147 } 148 149 150 def test_pasting(): 151 main = bld.path.find_resource('src/pasting.c') 152 bld.env.DEFINES = ['PREFIX_VAL=', 'SUFFIX_VAL='] 153 gruik = c_preproc.c_parser([main.parent]) 154 gruik.start(main, bld.env) 155 if len(gruik.nodes) == 1 and gruik.nodes[0].name == 'a.h': 156 color = "GREEN" 157 else: 158 color = "RED" 159 disp(color, "token pasting -> %r (expected a.h)" % gruik.nodes) 160 161 test_pasting() 162 163 def test(x, result): 164 toks = c_preproc.tokenize(x) 165 c_preproc.reduce_tokens(toks, defs, []) 166 (_, ret) = c_preproc.reduce_eval(toks) 167 if int(ret) == result: 168 color = "GREEN" 169 else: 170 color = "RED" 171 disp(color, "%s\t\t%r" % (ret, toks)) 172 173 test('a||b||c||d', 1) 174 test('a&&b&&c&&d', 0) 175 test('e', 1) 176 177 def test_rec(defines, expected): 178 main = bld.path.find_resource('recursion/a.c') 179 bld.env.DEFINES = defines.split() 180 gruik = c_preproc.c_parser([main.parent]) 181 gruik.start(main, bld.env) 182 result = "".join([x.name[0] for x in gruik.nodes]) 183 if result == expected: 184 color = "GREEN" 185 else: 186 color = "RED" 187 disp(color, "%s\t\t%r" % (expected, gruik.nodes)) 188 189 test_rec("", "a") 190 test_rec("FOO=1", "ac") 191 test_rec("BAR=1", "abc") 192 test_rec("FOO=1 BAR=1", "ac") 193 194 return 195 test("1?1,(0?5:9):3,4", 0) # <- invalid expression 196 197 198