wscript 4.3 KB

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