Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import pprint |
| 4 | import re |
Eli Bendersky | d097378 | 2012-01-19 08:09:33 +0200 | [diff] [blame] | 5 | import os, sys |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 6 | import unittest |
| 7 | |
| 8 | sys.path.insert(0, '..') |
| 9 | |
| 10 | from pycparser import c_parser |
| 11 | from pycparser.c_ast import * |
| 12 | from pycparser.c_parser import CParser, Coord, ParseError |
| 13 | |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 14 | _c_parser = c_parser.CParser( |
| 15 | lex_optimize=False, |
| 16 | yacc_debug=True, |
| 17 | yacc_optimize=False, |
| 18 | yacctab='yacctab') |
| 19 | |
| 20 | |
| 21 | def expand_decl(decl): |
| 22 | """ Converts the declaration into a nested list. |
| 23 | """ |
| 24 | typ = type(decl) |
| 25 | |
| 26 | if typ == TypeDecl: |
| 27 | return ['TypeDecl', expand_decl(decl.type)] |
| 28 | elif typ == IdentifierType: |
| 29 | return ['IdentifierType', decl.names] |
| 30 | elif typ == ID: |
| 31 | return ['ID', decl.name] |
| 32 | elif typ in [Struct, Union]: |
| 33 | decls = [expand_decl(d) for d in decl.decls or []] |
| 34 | return [typ.__name__, decl.name, decls] |
| 35 | else: |
| 36 | nested = expand_decl(decl.type) |
| 37 | |
| 38 | if typ == Decl: |
| 39 | if decl.quals: |
| 40 | return ['Decl', decl.quals, decl.name, nested] |
| 41 | else: |
| 42 | return ['Decl', decl.name, nested] |
| 43 | elif typ == Typename: # for function parameters |
| 44 | if decl.quals: |
| 45 | return ['Typename', decl.quals, nested] |
| 46 | else: |
| 47 | return ['Typename', nested] |
| 48 | elif typ == ArrayDecl: |
| 49 | dimval = decl.dim.value if decl.dim else '' |
| 50 | return ['ArrayDecl', dimval, nested] |
| 51 | elif typ == PtrDecl: |
| 52 | return ['PtrDecl', nested] |
| 53 | elif typ == Typedef: |
| 54 | return ['Typedef', decl.name, nested] |
| 55 | elif typ == FuncDecl: |
| 56 | if decl.args: |
| 57 | params = [expand_decl(param) for param in decl.args.params] |
| 58 | else: |
| 59 | params = [] |
| 60 | return ['FuncDecl', params, nested] |
| 61 | |
| 62 | |
| 63 | def expand_init(init): |
| 64 | """ Converts an initialization into a nested list |
| 65 | """ |
| 66 | typ = type(init) |
| 67 | |
eli.bendersky | f890a86 | 2010-10-30 12:13:23 +0200 | [diff] [blame] | 68 | if typ == NamedInitializer: |
| 69 | des = [expand_init(dp) for dp in init.name] |
| 70 | return (des, expand_init(init.expr)) |
| 71 | elif typ == ExprList: |
| 72 | return [expand_init(expr) for expr in init.exprs] |
| 73 | elif typ == Constant: |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 74 | return ['Constant', init.type, init.value] |
| 75 | elif typ == ID: |
| 76 | return ['ID', init.name] |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 77 | |
| 78 | |
eli.bendersky | 85d2e73 | 2011-05-20 19:47:26 +0300 | [diff] [blame] | 79 | class TestCParser_base(unittest.TestCase): |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 80 | def parse(self, txt, filename=''): |
| 81 | return self.cparser.parse(txt, filename) |
| 82 | |
| 83 | def setUp(self): |
| 84 | self.cparser = _c_parser |
| 85 | |
eli.bendersky | 85d2e73 | 2011-05-20 19:47:26 +0300 | [diff] [blame] | 86 | |
| 87 | class TestCParser_fundamentals(TestCParser_base): |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 88 | def get_decl(self, txt, index=0): |
| 89 | """ Given a source and an index returns the expanded |
| 90 | declaration at that index. |
| 91 | |
| 92 | FileAST holds a list of 'external declarations'. |
| 93 | index is the offset of the desired declaration in that |
| 94 | list. |
| 95 | """ |
| 96 | t = self.parse(txt).ext[index] |
| 97 | return expand_decl(t) |
| 98 | |
| 99 | def get_decl_init(self, txt, index=0): |
| 100 | """ Returns the expanded initializer of the declaration |
| 101 | at index. |
| 102 | """ |
| 103 | t = self.parse(txt).ext[index] |
| 104 | return expand_init(t.init) |
| 105 | |
| 106 | def test_FileAST(self): |
| 107 | t = self.parse('int a; char c;') |
| 108 | self.failUnless(isinstance(t, FileAST)) |
| 109 | self.assertEqual(len(t.ext), 2) |
eli.bendersky | 43cf0b2 | 2011-10-19 05:56:15 +0200 | [diff] [blame] | 110 | |
| 111 | # empty file |
| 112 | t2 = self.parse('') |
| 113 | self.failUnless(isinstance(t2, FileAST)) |
| 114 | self.assertEqual(len(t2.ext), 0) |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 115 | |
eli.bendersky | 38165b7 | 2011-02-04 08:13:39 +0200 | [diff] [blame] | 116 | def test_empty_toplevel_decl(self): |
| 117 | code = 'int foo;;' |
| 118 | t = self.parse(code) |
| 119 | self.failUnless(isinstance(t, FileAST)) |
| 120 | self.assertEqual(len(t.ext), 1) |
| 121 | self.assertEqual(self.get_decl(code), |
| 122 | ['Decl', 'foo', |
| 123 | ['TypeDecl', ['IdentifierType', ['int']]]]) |
| 124 | |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 125 | def assert_coord(self, node, line, file=None): |
| 126 | self.assertEqual(node.coord.line, line) |
| 127 | if file: |
| 128 | self.assertEqual(node.coord.file, file) |
| 129 | |
| 130 | def test_coords(self): |
eli.bendersky | 38165b7 | 2011-02-04 08:13:39 +0200 | [diff] [blame] | 131 | """ Tests the "coordinates" of parsed elements - file |
| 132 | name and line numbers, with modification insterted by |
| 133 | #line directives. |
| 134 | """ |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 135 | self.assert_coord(self.parse('int a;').ext[0], 1) |
| 136 | |
| 137 | t1 = """ |
| 138 | int a; |
| 139 | int b;\n\n |
| 140 | int c; |
| 141 | """ |
| 142 | f1 = self.parse(t1, filename='test.c') |
| 143 | self.assert_coord(f1.ext[0], 2, 'test.c') |
| 144 | self.assert_coord(f1.ext[1], 3, 'test.c') |
| 145 | self.assert_coord(f1.ext[2], 6, 'test.c') |
| 146 | |
| 147 | t1_1 = ''' |
| 148 | int main() { |
| 149 | k = p; |
| 150 | printf("%d", b); |
| 151 | return 0; |
| 152 | }''' |
| 153 | f1_1 = self.parse(t1_1, filename='test.c') |
eli.bendersky | ef29ff9 | 2010-10-29 16:25:43 +0200 | [diff] [blame] | 154 | self.assert_coord(f1_1.ext[0].body.block_items[0], 3, 'test.c') |
| 155 | self.assert_coord(f1_1.ext[0].body.block_items[1], 4, 'test.c') |
eli.bendersky | fc96e5e | 2011-03-04 09:51:23 +0200 | [diff] [blame] | 156 | |
| 157 | t1_2 = ''' |
| 158 | int main () { |
| 159 | int p = (int) k; |
| 160 | }''' |
| 161 | f1_2 = self.parse(t1_2, filename='test.c') |
| 162 | # make sure that the Cast has a coord (issue 23) |
| 163 | self.assert_coord(f1_2.ext[0].body.block_items[0].init, 3, 'test.c') |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 164 | |
| 165 | t2 = """ |
| 166 | #line 99 |
| 167 | int c; |
| 168 | """ |
| 169 | self.assert_coord(self.parse(t2).ext[0], 99) |
| 170 | |
| 171 | t3 = """ |
| 172 | int dsf; |
| 173 | char p; |
| 174 | #line 3000 "in.h" |
| 175 | char d; |
| 176 | """ |
| 177 | f3 = self.parse(t3, filename='test.c') |
| 178 | self.assert_coord(f3.ext[0], 2, 'test.c') |
| 179 | self.assert_coord(f3.ext[1], 3, 'test.c') |
| 180 | self.assert_coord(f3.ext[2], 3000, 'in.h') |
| 181 | |
| 182 | t4 = """ |
| 183 | #line 20 "restore.h" |
| 184 | int maydler(char); |
| 185 | |
| 186 | #line 30 "includes/daween.ph" |
| 187 | long j, k; |
| 188 | |
| 189 | #line 50000 |
| 190 | char* ro; |
| 191 | """ |
| 192 | f4 = self.parse(t4, filename='myb.c') |
| 193 | self.assert_coord(f4.ext[0], 20, 'restore.h') |
| 194 | self.assert_coord(f4.ext[1], 30, 'includes/daween.ph') |
| 195 | self.assert_coord(f4.ext[2], 30, 'includes/daween.ph') |
| 196 | self.assert_coord(f4.ext[3], 50000, 'includes/daween.ph') |
| 197 | |
| 198 | t5 = """ |
| 199 | int |
| 200 | #line 99 |
| 201 | c; |
| 202 | """ |
| 203 | self.assert_coord(self.parse(t5).ext[0], 99) |
| 204 | |
Eli Bendersky | 203b967 | 2012-06-15 10:11:24 +0300 | [diff] [blame] | 205 | # coord for ellipsis |
| 206 | t6 = """ |
| 207 | int foo(int j, |
| 208 | ...) { |
| 209 | }""" |
| 210 | f6 = self.parse(t6, filename='z.c') |
| 211 | self.assert_coord(self.parse(t6).ext[0].decl.type.args.params[1], 3) |
| 212 | |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 213 | def test_simple_decls(self): |
| 214 | self.assertEqual(self.get_decl('int a;'), |
| 215 | ['Decl', 'a', ['TypeDecl', ['IdentifierType', ['int']]]]) |
| 216 | |
| 217 | self.assertEqual(self.get_decl('unsigned int a;'), |
Eli Bendersky | 68497c2 | 2012-01-19 06:04:58 +0200 | [diff] [blame] | 218 | ['Decl', 'a', ['TypeDecl', ['IdentifierType', ['unsigned', 'int']]]]) |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 219 | |
eli.bendersky | affe032 | 2011-09-24 06:16:55 +0300 | [diff] [blame] | 220 | self.assertEqual(self.get_decl('_Bool a;'), |
| 221 | ['Decl', 'a', ['TypeDecl', ['IdentifierType', ['_Bool']]]]) |
| 222 | |
Eli Bendersky | f4d7346 | 2012-01-19 05:56:27 +0200 | [diff] [blame] | 223 | self.assertEqual(self.get_decl('float _Complex fcc;'), |
Eli Bendersky | 68497c2 | 2012-01-19 06:04:58 +0200 | [diff] [blame] | 224 | ['Decl', 'fcc', ['TypeDecl', ['IdentifierType', ['float', '_Complex']]]]) |
Eli Bendersky | f4d7346 | 2012-01-19 05:56:27 +0200 | [diff] [blame] | 225 | |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 226 | self.assertEqual(self.get_decl('char* string;'), |
| 227 | ['Decl', 'string', |
| 228 | ['PtrDecl', ['TypeDecl', ['IdentifierType', ['char']]]]]) |
| 229 | |
| 230 | self.assertEqual(self.get_decl('long ar[15];'), |
| 231 | ['Decl', 'ar', |
| 232 | ['ArrayDecl', '15', |
| 233 | ['TypeDecl', ['IdentifierType', ['long']]]]]) |
eli.bendersky | 98f4537 | 2010-10-30 09:46:29 +0200 | [diff] [blame] | 234 | |
| 235 | self.assertEqual(self.get_decl('long long ar[15];'), |
| 236 | ['Decl', 'ar', |
| 237 | ['ArrayDecl', '15', |
eli.bendersky | f890a86 | 2010-10-30 12:13:23 +0200 | [diff] [blame] | 238 | ['TypeDecl', ['IdentifierType', ['long', 'long']]]]]) |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 239 | |
| 240 | self.assertEqual(self.get_decl('unsigned ar[];'), |
| 241 | ['Decl', 'ar', |
| 242 | ['ArrayDecl', '', |
| 243 | ['TypeDecl', ['IdentifierType', ['unsigned']]]]]) |
| 244 | |
| 245 | self.assertEqual(self.get_decl('int strlen(char* s);'), |
| 246 | ['Decl', 'strlen', |
| 247 | ['FuncDecl', |
| 248 | [['Decl', 's', |
| 249 | ['PtrDecl', |
| 250 | ['TypeDecl', ['IdentifierType', ['char']]]]]], |
| 251 | ['TypeDecl', ['IdentifierType', ['int']]]]]) |
| 252 | |
| 253 | self.assertEqual(self.get_decl('int strcmp(char* s1, char* s2);'), |
| 254 | ['Decl', 'strcmp', |
| 255 | ['FuncDecl', |
| 256 | [ ['Decl', 's1', |
| 257 | ['PtrDecl', ['TypeDecl', ['IdentifierType', ['char']]]]], |
| 258 | ['Decl', 's2', |
| 259 | ['PtrDecl', ['TypeDecl', ['IdentifierType', ['char']]]]] |
| 260 | ], |
| 261 | ['TypeDecl', ['IdentifierType', ['int']]]]]) |
| 262 | |
| 263 | def test_nested_decls(self): # the fun begins |
| 264 | self.assertEqual(self.get_decl('char** ar2D;'), |
| 265 | ['Decl', 'ar2D', |
| 266 | ['PtrDecl', ['PtrDecl', |
| 267 | ['TypeDecl', ['IdentifierType', ['char']]]]]]) |
| 268 | |
| 269 | self.assertEqual(self.get_decl('int (*a)[1][2];'), |
| 270 | ['Decl', 'a', |
| 271 | ['PtrDecl', |
| 272 | ['ArrayDecl', '1', |
| 273 | ['ArrayDecl', '2', |
| 274 | ['TypeDecl', ['IdentifierType', ['int']]]]]]]) |
| 275 | |
| 276 | self.assertEqual(self.get_decl('int *a[1][2];'), |
| 277 | ['Decl', 'a', |
| 278 | ['ArrayDecl', '1', |
| 279 | ['ArrayDecl', '2', |
| 280 | ['PtrDecl', ['TypeDecl', ['IdentifierType', ['int']]]]]]]) |
| 281 | |
| 282 | self.assertEqual(self.get_decl('char ***ar3D[40];'), |
| 283 | ['Decl', 'ar3D', |
| 284 | ['ArrayDecl', '40', |
| 285 | ['PtrDecl', ['PtrDecl', ['PtrDecl', |
| 286 | ['TypeDecl', ['IdentifierType', ['char']]]]]]]]) |
| 287 | |
| 288 | self.assertEqual(self.get_decl('char (***ar3D)[40];'), |
| 289 | ['Decl', 'ar3D', |
| 290 | ['PtrDecl', ['PtrDecl', ['PtrDecl', |
| 291 | ['ArrayDecl', '40', ['TypeDecl', ['IdentifierType', ['char']]]]]]]]) |
| 292 | |
| 293 | self.assertEqual(self.get_decl('int (*x[4])(char, int);'), |
| 294 | ['Decl', 'x', |
| 295 | ['ArrayDecl', '4', |
| 296 | ['PtrDecl', |
| 297 | ['FuncDecl', |
| 298 | [ ['Typename', ['TypeDecl', ['IdentifierType', ['char']]]], |
| 299 | ['Typename', ['TypeDecl', ['IdentifierType', ['int']]]]], |
| 300 | ['TypeDecl', ['IdentifierType', ['int']]]]]]]) |
| 301 | |
| 302 | self.assertEqual(self.get_decl('char *(*(**foo [][8])())[];'), |
| 303 | ['Decl', 'foo', |
| 304 | ['ArrayDecl', '', |
| 305 | ['ArrayDecl', '8', |
| 306 | ['PtrDecl', ['PtrDecl', |
| 307 | ['FuncDecl', |
| 308 | [], |
| 309 | ['PtrDecl', |
| 310 | ['ArrayDecl', '', |
| 311 | ['PtrDecl', |
| 312 | ['TypeDecl', |
| 313 | ['IdentifierType', ['char']]]]]]]]]]]]) |
| 314 | |
| 315 | # explore named and unnamed function pointer parameters, |
| 316 | # with and without qualifiers |
| 317 | # |
| 318 | |
| 319 | # unnamed w/o quals |
| 320 | self.assertEqual(self.get_decl('int (*k)(int);'), |
| 321 | ['Decl', 'k', |
| 322 | ['PtrDecl', |
| 323 | ['FuncDecl', |
| 324 | [['Typename', ['TypeDecl', ['IdentifierType', ['int']]]]], |
| 325 | ['TypeDecl', ['IdentifierType', ['int']]]]]]) |
| 326 | |
| 327 | # unnamed w/ quals |
| 328 | self.assertEqual(self.get_decl('int (*k)(const int);'), |
| 329 | ['Decl', 'k', |
| 330 | ['PtrDecl', |
| 331 | ['FuncDecl', |
| 332 | [['Typename', ['const'], ['TypeDecl', ['IdentifierType', ['int']]]]], |
| 333 | ['TypeDecl', ['IdentifierType', ['int']]]]]]) |
| 334 | |
| 335 | # named w/o quals |
| 336 | self.assertEqual(self.get_decl('int (*k)(int q);'), |
| 337 | ['Decl', 'k', |
| 338 | ['PtrDecl', |
| 339 | ['FuncDecl', |
| 340 | [['Decl', 'q', ['TypeDecl', ['IdentifierType', ['int']]]]], |
| 341 | ['TypeDecl', ['IdentifierType', ['int']]]]]]) |
| 342 | |
| 343 | # named w/ quals |
| 344 | self.assertEqual(self.get_decl('int (*k)(const volatile int q);'), |
| 345 | ['Decl', 'k', |
| 346 | ['PtrDecl', |
| 347 | ['FuncDecl', |
Eli Bendersky | 68497c2 | 2012-01-19 06:04:58 +0200 | [diff] [blame] | 348 | [['Decl', ['const', 'volatile'], 'q', |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 349 | ['TypeDecl', ['IdentifierType', ['int']]]]], |
| 350 | ['TypeDecl', ['IdentifierType', ['int']]]]]]) |
| 351 | |
eli.bendersky | 79d5cf6 | 2010-10-29 13:33:52 +0200 | [diff] [blame] | 352 | # restrict qualifier |
| 353 | self.assertEqual(self.get_decl('int (*k)(restrict int* q);'), |
| 354 | ['Decl', 'k', |
| 355 | ['PtrDecl', |
| 356 | ['FuncDecl', |
| 357 | [['Decl', ['restrict'], 'q', |
| 358 | ['PtrDecl', |
| 359 | ['TypeDecl', ['IdentifierType', ['int']]]]]], |
| 360 | ['TypeDecl', ['IdentifierType', ['int']]]]]]) |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 361 | |
| 362 | def test_qualifiers_storage_specifiers(self): |
| 363 | def assert_qs(txt, index, quals, storage): |
| 364 | d = self.parse(txt).ext[index] |
| 365 | self.assertEqual(d.quals, quals) |
| 366 | self.assertEqual(d.storage, storage) |
| 367 | |
| 368 | assert_qs("extern int p;", 0, [], ['extern']) |
| 369 | assert_qs("const long p = 6;", 0, ['const'], []) |
| 370 | |
| 371 | d1 = "static const int p, q, r;" |
| 372 | for i in range(3): |
| 373 | assert_qs(d1, i, ['const'], ['static']) |
| 374 | |
| 375 | d2 = "static char * const p;" |
| 376 | assert_qs(d2, 0, [], ['static']) |
| 377 | pdecl = self.parse(d2).ext[0].type |
| 378 | self.failUnless(isinstance(pdecl, PtrDecl)) |
| 379 | self.assertEqual(pdecl.quals, ['const']) |
| 380 | |
| 381 | def test_sizeof(self): |
| 382 | e = """ |
| 383 | void foo() |
| 384 | { |
| 385 | int a = sizeof k; |
| 386 | int b = sizeof(int); |
eli.bendersky | c51e1d3 | 2011-02-04 08:52:33 +0200 | [diff] [blame] | 387 | int c = sizeof(int**);; |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 388 | |
| 389 | char* p = "just to make sure this parses w/o error..."; |
| 390 | int d = sizeof(int()); |
| 391 | } |
| 392 | """ |
| 393 | compound = self.parse(e).ext[0].body |
| 394 | |
eli.bendersky | ef29ff9 | 2010-10-29 16:25:43 +0200 | [diff] [blame] | 395 | s1 = compound.block_items[0].init |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 396 | self.assertTrue(isinstance(s1, UnaryOp)) |
| 397 | self.assertEqual(s1.op, 'sizeof') |
| 398 | self.assertTrue(isinstance(s1.expr, ID)) |
| 399 | self.assertEqual(s1.expr.name, 'k') |
| 400 | |
eli.bendersky | ef29ff9 | 2010-10-29 16:25:43 +0200 | [diff] [blame] | 401 | s2 = compound.block_items[1].init |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 402 | self.assertEqual(expand_decl(s2.expr), |
| 403 | ['Typename', ['TypeDecl', ['IdentifierType', ['int']]]]) |
| 404 | |
eli.bendersky | ef29ff9 | 2010-10-29 16:25:43 +0200 | [diff] [blame] | 405 | s3 = compound.block_items[2].init |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 406 | self.assertEqual(expand_decl(s3.expr), |
| 407 | ['Typename', |
| 408 | ['PtrDecl', |
| 409 | ['PtrDecl', |
| 410 | ['TypeDecl', |
| 411 | ['IdentifierType', ['int']]]]]]) |
| 412 | |
eli.bendersky | 9f48156 | 2010-10-30 15:50:47 +0200 | [diff] [blame] | 413 | # The C99 compound literal feature |
| 414 | # |
eli.bendersky | f890a86 | 2010-10-30 12:13:23 +0200 | [diff] [blame] | 415 | def test_compound_literals(self): |
eli.bendersky | 9f48156 | 2010-10-30 15:50:47 +0200 | [diff] [blame] | 416 | ps1 = self.parse(r''' |
eli.bendersky | f890a86 | 2010-10-30 12:13:23 +0200 | [diff] [blame] | 417 | void foo() { |
eli.bendersky | 9f48156 | 2010-10-30 15:50:47 +0200 | [diff] [blame] | 418 | p = (long long){k}; |
| 419 | tc = (struct jk){.a = {1, 2}, .b[0] = t}; |
| 420 | }''') |
eli.bendersky | f890a86 | 2010-10-30 12:13:23 +0200 | [diff] [blame] | 421 | |
eli.bendersky | 9f48156 | 2010-10-30 15:50:47 +0200 | [diff] [blame] | 422 | compound = ps1.ext[0].body.block_items[0].rvalue |
| 423 | self.assertEqual(expand_decl(compound.type), |
| 424 | ['Typename', ['TypeDecl', ['IdentifierType', ['long', 'long']]]]) |
| 425 | self.assertEqual(expand_init(compound.init), |
| 426 | [['ID', 'k']]) |
| 427 | |
| 428 | compound = ps1.ext[0].body.block_items[1].rvalue |
| 429 | self.assertEqual(expand_decl(compound.type), |
| 430 | ['Typename', ['TypeDecl', ['Struct', 'jk', []]]]) |
| 431 | self.assertEqual(expand_init(compound.init), |
| 432 | [ |
| 433 | ([['ID', 'a']], [['Constant', 'int', '1'], ['Constant', 'int', '2']]), |
| 434 | ([['ID', 'b'], ['Constant', 'int', '0']], ['ID', 't'])]) |
eli.bendersky | f890a86 | 2010-10-30 12:13:23 +0200 | [diff] [blame] | 435 | |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 436 | def test_enums(self): |
| 437 | e1 = "enum mycolor op;" |
| 438 | e1_type = self.parse(e1).ext[0].type.type |
| 439 | |
| 440 | self.assertTrue(isinstance(e1_type, Enum)) |
| 441 | self.assertEqual(e1_type.name, 'mycolor') |
| 442 | self.assertEqual(e1_type.values, None) |
| 443 | |
| 444 | e2 = "enum mysize {large=20, small, medium} shoes;" |
| 445 | e2_type = self.parse(e2).ext[0].type.type |
| 446 | |
| 447 | self.assertTrue(isinstance(e2_type, Enum)) |
| 448 | self.assertEqual(e2_type.name, 'mysize') |
| 449 | |
| 450 | e2_elist = e2_type.values |
| 451 | self.assertTrue(isinstance(e2_elist, EnumeratorList)) |
| 452 | |
| 453 | for e2_eval in e2_elist.enumerators: |
| 454 | self.assertTrue(isinstance(e2_eval, Enumerator)) |
| 455 | |
| 456 | self.assertEqual(e2_elist.enumerators[0].name, 'large') |
| 457 | self.assertEqual(e2_elist.enumerators[0].value.value, '20') |
| 458 | self.assertEqual(e2_elist.enumerators[2].name, 'medium') |
| 459 | self.assertEqual(e2_elist.enumerators[2].value, None) |
| 460 | |
| 461 | # enum with trailing comma (C99 feature) |
| 462 | e3 = """ |
| 463 | enum |
| 464 | { |
| 465 | red, |
| 466 | blue, |
| 467 | green, |
| 468 | } color; |
| 469 | """ |
| 470 | |
| 471 | e3_type = self.parse(e3).ext[0].type.type |
| 472 | self.assertTrue(isinstance(e3_type, Enum)) |
| 473 | e3_elist = e3_type.values |
| 474 | self.assertTrue(isinstance(e3_elist, EnumeratorList)) |
| 475 | |
| 476 | for e3_eval in e3_elist.enumerators: |
| 477 | self.assertTrue(isinstance(e3_eval, Enumerator)) |
| 478 | |
| 479 | self.assertEqual(e3_elist.enumerators[0].name, 'red') |
| 480 | self.assertEqual(e3_elist.enumerators[0].value, None) |
| 481 | self.assertEqual(e3_elist.enumerators[1].name, 'blue') |
| 482 | self.assertEqual(e3_elist.enumerators[2].name, 'green') |
| 483 | |
| 484 | def test_typedef(self): |
| 485 | # without typedef, error |
| 486 | s1 = """ |
| 487 | node k; |
| 488 | """ |
| 489 | self.assertRaises(ParseError, self.parse, s1) |
| 490 | |
| 491 | # now with typedef, works |
| 492 | s2 = """ |
| 493 | typedef void* node; |
| 494 | node k; |
| 495 | """ |
| 496 | ps2 = self.parse(s2) |
| 497 | self.assertEqual(expand_decl(ps2.ext[0]), |
| 498 | ['Typedef', 'node', |
| 499 | ['PtrDecl', |
| 500 | ['TypeDecl', ['IdentifierType', ['void']]]]]) |
| 501 | |
| 502 | self.assertEqual(expand_decl(ps2.ext[1]), |
| 503 | ['Decl', 'k', |
| 504 | ['TypeDecl', ['IdentifierType', ['node']]]]) |
| 505 | |
| 506 | s3 = """ |
| 507 | typedef int T; |
| 508 | typedef T *pT; |
| 509 | |
| 510 | pT aa, bb; |
| 511 | """ |
| 512 | ps3 = self.parse(s3) |
| 513 | self.assertEqual(expand_decl(ps3.ext[3]), |
| 514 | ['Decl', 'bb', |
| 515 | ['TypeDecl', ['IdentifierType', ['pT']]]]) |
| 516 | |
| 517 | s4 = ''' |
| 518 | typedef char* __builtin_va_list; |
| 519 | typedef __builtin_va_list __gnuc_va_list; |
| 520 | ''' |
| 521 | ps4 = self.parse(s4) |
| 522 | self.assertEqual(expand_decl(ps4.ext[1]), |
| 523 | ['Typedef', '__gnuc_va_list', |
| 524 | ['TypeDecl', |
| 525 | ['IdentifierType', ['__builtin_va_list']]]]) |
| 526 | |
| 527 | s5 = '''typedef struct tagHash Hash;''' |
| 528 | ps5 = self.parse(s5) |
| 529 | self.assertEqual(expand_decl(ps5.ext[0]), |
| 530 | ['Typedef', 'Hash', ['TypeDecl', ['Struct', 'tagHash', []]]]) |
| 531 | |
| 532 | def test_struct_union(self): |
| 533 | s1 = """ |
| 534 | struct { |
| 535 | int id; |
| 536 | char* name; |
| 537 | } joe; |
| 538 | """ |
| 539 | |
| 540 | self.assertEqual(expand_decl(self.parse(s1).ext[0]), |
| 541 | ['Decl', 'joe', |
| 542 | ['TypeDecl', ['Struct', None, |
| 543 | [ ['Decl', 'id', |
| 544 | ['TypeDecl', |
| 545 | ['IdentifierType', ['int']]]], |
| 546 | ['Decl', 'name', |
| 547 | ['PtrDecl', |
| 548 | ['TypeDecl', |
| 549 | ['IdentifierType', ['char']]]]]]]]]) |
| 550 | |
| 551 | s2 = """ |
| 552 | struct node p; |
| 553 | """ |
| 554 | self.assertEqual(expand_decl(self.parse(s2).ext[0]), |
| 555 | ['Decl', 'p', |
| 556 | ['TypeDecl', ['Struct', 'node', []]]]) |
| 557 | |
| 558 | s21 = """ |
| 559 | union pri ra; |
| 560 | """ |
| 561 | self.assertEqual(expand_decl(self.parse(s21).ext[0]), |
| 562 | ['Decl', 'ra', |
| 563 | ['TypeDecl', ['Union', 'pri', []]]]) |
| 564 | |
| 565 | s3 = """ |
| 566 | struct node* p; |
| 567 | """ |
| 568 | self.assertEqual(expand_decl(self.parse(s3).ext[0]), |
| 569 | ['Decl', 'p', |
| 570 | ['PtrDecl', |
| 571 | ['TypeDecl', ['Struct', 'node', []]]]]) |
| 572 | |
| 573 | s4 = """ |
| 574 | struct node; |
| 575 | """ |
| 576 | self.assertEqual(expand_decl(self.parse(s4).ext[0]), |
| 577 | ['Decl', None, |
| 578 | ['Struct', 'node', []]]) |
| 579 | |
| 580 | s5 = """ |
| 581 | union |
| 582 | { |
| 583 | struct |
| 584 | { |
| 585 | int type; |
| 586 | } n; |
| 587 | |
| 588 | struct |
| 589 | { |
| 590 | int type; |
| 591 | int intnode; |
| 592 | } ni; |
| 593 | } u; |
| 594 | """ |
| 595 | self.assertEqual(expand_decl(self.parse(s5).ext[0]), |
| 596 | ['Decl', 'u', |
| 597 | ['TypeDecl', |
| 598 | ['Union', None, |
| 599 | [['Decl', 'n', |
| 600 | ['TypeDecl', |
| 601 | ['Struct', None, |
| 602 | [['Decl', 'type', |
| 603 | ['TypeDecl', ['IdentifierType', ['int']]]]]]]], |
| 604 | ['Decl', 'ni', |
| 605 | ['TypeDecl', |
| 606 | ['Struct', None, |
| 607 | [['Decl', 'type', |
| 608 | ['TypeDecl', ['IdentifierType', ['int']]]], |
| 609 | ['Decl', 'intnode', |
| 610 | ['TypeDecl', ['IdentifierType', ['int']]]]]]]]]]]]) |
| 611 | |
| 612 | s6 = """ |
| 613 | typedef struct foo_tag |
| 614 | { |
| 615 | void* data; |
| 616 | } foo, *pfoo; |
| 617 | """ |
| 618 | s6_ast = self.parse(s6) |
| 619 | |
| 620 | self.assertEqual(expand_decl(s6_ast.ext[0]), |
| 621 | ['Typedef', 'foo', |
| 622 | ['TypeDecl', |
| 623 | ['Struct', 'foo_tag', |
| 624 | [['Decl', 'data', |
| 625 | ['PtrDecl', ['TypeDecl', ['IdentifierType', ['void']]]]]]]]]) |
| 626 | |
| 627 | self.assertEqual(expand_decl(s6_ast.ext[1]), |
| 628 | ['Typedef', 'pfoo', |
| 629 | ['PtrDecl', |
| 630 | ['TypeDecl', |
| 631 | ['Struct', 'foo_tag', |
| 632 | [['Decl', 'data', |
| 633 | ['PtrDecl', ['TypeDecl', ['IdentifierType', ['void']]]]]]]]]]) |
| 634 | |
| 635 | s7 = r""" |
| 636 | struct _on_exit_args { |
| 637 | void * _fnargs[32]; |
| 638 | void * _dso_handle[32]; |
| 639 | |
| 640 | long _fntypes; |
| 641 | #line 77 "D:\eli\cpp_stuff\libc_include/sys/reent.h" |
| 642 | |
| 643 | long _is_cxa; |
| 644 | }; |
| 645 | """ |
| 646 | |
| 647 | s7_ast = self.parse(s7, filename='test.c') |
| 648 | self.assert_coord(s7_ast.ext[0].type.decls[2], 6, 'test.c') |
| 649 | self.assert_coord(s7_ast.ext[0].type.decls[3], 78, |
| 650 | r'D:\eli\cpp_stuff\libc_include/sys/reent.h') |
| 651 | |
| 652 | s8 = """ |
| 653 | typedef enum tagReturnCode {SUCCESS, FAIL} ReturnCode; |
| 654 | |
| 655 | typedef struct tagEntry |
| 656 | { |
| 657 | char* key; |
| 658 | char* value; |
| 659 | } Entry; |
| 660 | |
| 661 | |
| 662 | typedef struct tagNode |
| 663 | { |
| 664 | Entry* entry; |
| 665 | |
| 666 | struct tagNode* next; |
| 667 | } Node; |
| 668 | |
| 669 | typedef struct tagHash |
| 670 | { |
| 671 | unsigned int table_size; |
| 672 | |
| 673 | Node** heads; |
| 674 | |
| 675 | } Hash; |
| 676 | """ |
| 677 | s8_ast = self.parse(s8) |
| 678 | self.assertEqual(expand_decl(s8_ast.ext[3]), |
| 679 | ['Typedef', 'Hash', |
| 680 | ['TypeDecl', ['Struct', 'tagHash', |
| 681 | [['Decl', 'table_size', |
Eli Bendersky | 68497c2 | 2012-01-19 06:04:58 +0200 | [diff] [blame] | 682 | ['TypeDecl', ['IdentifierType', ['unsigned', 'int']]]], |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 683 | ['Decl', 'heads', |
| 684 | ['PtrDecl', ['PtrDecl', ['TypeDecl', ['IdentifierType', ['Node']]]]]]]]]]) |
| 685 | |
eli.bendersky | 697ecc5 | 2011-02-10 07:05:13 +0200 | [diff] [blame] | 686 | def test_anonymous_struct_union(self): |
| 687 | s1 = """ |
| 688 | union |
| 689 | { |
| 690 | union |
| 691 | { |
| 692 | int i; |
| 693 | long l; |
| 694 | }; |
| 695 | |
| 696 | struct |
| 697 | { |
| 698 | int type; |
| 699 | int intnode; |
| 700 | }; |
| 701 | } u; |
| 702 | """ |
| 703 | |
| 704 | self.assertEqual(expand_decl(self.parse(s1).ext[0]), |
| 705 | ['Decl', 'u', |
| 706 | ['TypeDecl', |
| 707 | ['Union', None, |
| 708 | [['Decl', None, |
| 709 | ['Union', None, |
| 710 | [['Decl', 'i', |
| 711 | ['TypeDecl', |
| 712 | ['IdentifierType', ['int']]]], |
| 713 | ['Decl', 'l', |
| 714 | ['TypeDecl', |
| 715 | ['IdentifierType', ['long']]]]]]], |
| 716 | ['Decl', None, |
| 717 | ['Struct', None, |
| 718 | [['Decl', 'type', |
| 719 | ['TypeDecl', |
| 720 | ['IdentifierType', ['int']]]], |
| 721 | ['Decl', 'intnode', |
| 722 | ['TypeDecl', |
| 723 | ['IdentifierType', ['int']]]]]]]]]]]) |
| 724 | |
| 725 | s2 = """ |
| 726 | struct |
| 727 | { |
| 728 | int i; |
| 729 | union |
| 730 | { |
| 731 | int id; |
| 732 | char* name; |
| 733 | }; |
| 734 | float f; |
| 735 | } joe; |
| 736 | """ |
| 737 | |
| 738 | self.assertEqual(expand_decl(self.parse(s2).ext[0]), |
| 739 | ['Decl', 'joe', |
| 740 | ['TypeDecl', |
| 741 | ['Struct', None, |
| 742 | [['Decl', 'i', |
| 743 | ['TypeDecl', |
| 744 | ['IdentifierType', ['int']]]], |
| 745 | ['Decl', None, |
| 746 | ['Union', None, |
| 747 | [['Decl', 'id', |
| 748 | ['TypeDecl', |
| 749 | ['IdentifierType', ['int']]]], |
| 750 | ['Decl', 'name', |
| 751 | ['PtrDecl', |
| 752 | ['TypeDecl', |
| 753 | ['IdentifierType', ['char']]]]]]]], |
| 754 | ['Decl', 'f', |
| 755 | ['TypeDecl', |
| 756 | ['IdentifierType', ['float']]]]]]]]) |
| 757 | |
| 758 | # ISO/IEC 9899:201x Commitee Draft 2010-11-16, N1539 |
| 759 | # section 6.7.2.1, par. 19, example 1 |
| 760 | s3 = """ |
| 761 | struct v { |
| 762 | union { |
| 763 | struct { int i, j; }; |
| 764 | struct { long k, l; } w; |
| 765 | }; |
| 766 | int m; |
| 767 | } v1; |
| 768 | """ |
| 769 | |
| 770 | self.assertEqual(expand_decl(self.parse(s3).ext[0]), |
| 771 | ['Decl', 'v1', |
| 772 | ['TypeDecl', |
| 773 | ['Struct', 'v', |
| 774 | [['Decl', None, |
| 775 | ['Union', None, |
| 776 | [['Decl', None, |
| 777 | ['Struct', None, |
| 778 | [['Decl', 'i', |
| 779 | ['TypeDecl', |
| 780 | ['IdentifierType', ['int']]]], |
| 781 | ['Decl', 'j', |
| 782 | ['TypeDecl', |
| 783 | ['IdentifierType', ['int']]]]]]], |
| 784 | ['Decl', 'w', |
| 785 | ['TypeDecl', |
| 786 | ['Struct', None, |
| 787 | [['Decl', 'k', |
| 788 | ['TypeDecl', |
| 789 | ['IdentifierType', ['long']]]], |
| 790 | ['Decl', 'l', |
| 791 | ['TypeDecl', |
| 792 | ['IdentifierType', ['long']]]]]]]]]]], |
| 793 | ['Decl', 'm', |
| 794 | ['TypeDecl', |
| 795 | ['IdentifierType', ['int']]]]]]]]) |
| 796 | |
eli.bendersky | dce29a0 | 2011-02-10 07:55:00 +0200 | [diff] [blame] | 797 | s4 = """ |
| 798 | struct v { |
| 799 | int i; |
| 800 | float; |
| 801 | } v2;""" |
eli.bendersky | 6b01179 | 2011-06-22 17:50:56 +0300 | [diff] [blame] | 802 | # just make sure this doesn't raise ParseError |
| 803 | self.parse(s4) |
eli.bendersky | dce29a0 | 2011-02-10 07:55:00 +0200 | [diff] [blame] | 804 | |
eli.bendersky | 0e0a71f | 2010-10-09 08:32:00 +0200 | [diff] [blame] | 805 | def test_struct_bitfields(self): |
eli.bendersky | 38ed9a9 | 2010-10-09 09:29:59 +0200 | [diff] [blame] | 806 | # a struct with two bitfields, one unnamed |
eli.bendersky | 0e0a71f | 2010-10-09 08:32:00 +0200 | [diff] [blame] | 807 | s1 = """ |
| 808 | struct { |
| 809 | int k:6; |
| 810 | int :2; |
| 811 | } joe; |
| 812 | """ |
| 813 | |
| 814 | parsed_struct = self.parse(s1).ext[0] |
| 815 | |
eli.bendersky | 38ed9a9 | 2010-10-09 09:29:59 +0200 | [diff] [blame] | 816 | # We can see here the name of the decl for the unnamed bitfield is |
eli.bendersky | 0e0a71f | 2010-10-09 08:32:00 +0200 | [diff] [blame] | 817 | # None, but expand_decl doesn't show bitfield widths |
| 818 | # ... |
| 819 | self.assertEqual(expand_decl(parsed_struct), |
| 820 | ['Decl', 'joe', |
| 821 | ['TypeDecl', ['Struct', None, |
| 822 | [ ['Decl', 'k', |
| 823 | ['TypeDecl', |
| 824 | ['IdentifierType', ['int']]]], |
| 825 | ['Decl', None, |
| 826 | ['TypeDecl', |
| 827 | ['IdentifierType', ['int']]]]]]]]) |
| 828 | |
| 829 | # ... |
| 830 | # so we test them manually |
| 831 | self.assertEqual(parsed_struct.type.type.decls[0].bitsize.value, '6') |
| 832 | self.assertEqual(parsed_struct.type.type.decls[1].bitsize.value, '2') |
| 833 | |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 834 | def test_tags_namespace(self): |
| 835 | """ Tests that the tags of structs/unions/enums reside in a separate namespace and |
| 836 | can be named after existing types. |
| 837 | """ |
| 838 | s1 = """ |
| 839 | typedef int tagEntry; |
| 840 | |
| 841 | struct tagEntry |
| 842 | { |
| 843 | char* key; |
| 844 | char* value; |
| 845 | } Entry; |
| 846 | """ |
| 847 | |
| 848 | s1_ast = self.parse(s1) |
| 849 | self.assertEqual(expand_decl(s1_ast.ext[1]), |
| 850 | ['Decl', 'Entry', |
| 851 | ['TypeDecl', ['Struct', 'tagEntry', |
| 852 | [['Decl', 'key', |
| 853 | ['PtrDecl', ['TypeDecl', ['IdentifierType', ['char']]]]], |
| 854 | ['Decl', 'value', |
| 855 | ['PtrDecl', ['TypeDecl', ['IdentifierType', ['char']]]]]]]]]) |
| 856 | |
| 857 | s2 = """ |
| 858 | struct tagEntry; |
| 859 | |
| 860 | typedef struct tagEntry tagEntry; |
| 861 | |
| 862 | struct tagEntry |
| 863 | { |
| 864 | char* key; |
| 865 | char* value; |
| 866 | } Entry; |
| 867 | """ |
| 868 | |
| 869 | s2_ast = self.parse(s2) |
| 870 | self.assertEqual(expand_decl(s2_ast.ext[2]), |
| 871 | ['Decl', 'Entry', |
| 872 | ['TypeDecl', ['Struct', 'tagEntry', |
| 873 | [['Decl', 'key', |
| 874 | ['PtrDecl', ['TypeDecl', ['IdentifierType', ['char']]]]], |
| 875 | ['Decl', 'value', |
| 876 | ['PtrDecl', ['TypeDecl', ['IdentifierType', ['char']]]]]]]]]) |
| 877 | |
| 878 | s3 = """ |
| 879 | typedef int mytag; |
| 880 | |
| 881 | enum mytag {ABC, CDE}; |
| 882 | enum mytag joe; |
| 883 | """ |
| 884 | |
| 885 | s3_type = self.parse(s3).ext[1].type |
| 886 | |
| 887 | self.assertTrue(isinstance(s3_type, Enum)) |
| 888 | self.assertEqual(s3_type.name, 'mytag') |
| 889 | |
| 890 | def test_multi_decls(self): |
| 891 | d1 = 'int a, b;' |
| 892 | |
| 893 | self.assertEqual(self.get_decl(d1, 0), |
| 894 | ['Decl', 'a', ['TypeDecl', ['IdentifierType', ['int']]]]) |
| 895 | self.assertEqual(self.get_decl(d1, 1), |
| 896 | ['Decl', 'b', ['TypeDecl', ['IdentifierType', ['int']]]]) |
| 897 | |
| 898 | d2 = 'char* p, notp, ar[4];' |
| 899 | self.assertEqual(self.get_decl(d2, 0), |
| 900 | ['Decl', 'p', |
| 901 | ['PtrDecl', |
| 902 | ['TypeDecl', ['IdentifierType', ['char']]]]]) |
| 903 | self.assertEqual(self.get_decl(d2, 1), |
| 904 | ['Decl', 'notp', ['TypeDecl', ['IdentifierType', ['char']]]]) |
| 905 | self.assertEqual(self.get_decl(d2, 2), |
| 906 | ['Decl', 'ar', |
| 907 | ['ArrayDecl', '4', |
| 908 | ['TypeDecl', ['IdentifierType', ['char']]]]]) |
| 909 | |
| 910 | def test_invalid_multiple_types_error(self): |
| 911 | bad = [ |
| 912 | 'int enum {ab, cd} fubr;', |
| 913 | 'enum kid char brbr;'] |
| 914 | |
| 915 | for b in bad: |
| 916 | self.assertRaises(ParseError, self.parse, b) |
| 917 | |
| 918 | def test_decl_inits(self): |
| 919 | d1 = 'int a = 16;' |
eli.bendersky | f890a86 | 2010-10-30 12:13:23 +0200 | [diff] [blame] | 920 | #~ self.parse(d1).show() |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 921 | self.assertEqual(self.get_decl(d1), |
| 922 | ['Decl', 'a', ['TypeDecl', ['IdentifierType', ['int']]]]) |
| 923 | self.assertEqual(self.get_decl_init(d1), |
| 924 | ['Constant', 'int', '16']) |
| 925 | |
Eli Bendersky | 3b1b08d | 2012-06-15 12:37:54 +0300 | [diff] [blame^] | 926 | d1_1 = 'float f = 0xEF.56p1;' |
| 927 | self.assertEqual(self.get_decl_init(d1_1), |
| 928 | ['Constant', 'float', '0xEF.56p1']) |
| 929 | |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 930 | d2 = 'long ar[] = {7, 8, 9};' |
eli.bendersky | f890a86 | 2010-10-30 12:13:23 +0200 | [diff] [blame] | 931 | #~ self.parse(d2).show() |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 932 | self.assertEqual(self.get_decl(d2), |
| 933 | ['Decl', 'ar', |
| 934 | ['ArrayDecl', '', |
| 935 | ['TypeDecl', ['IdentifierType', ['long']]]]]) |
| 936 | self.assertEqual(self.get_decl_init(d2), |
| 937 | [ ['Constant', 'int', '7'], |
| 938 | ['Constant', 'int', '8'], |
eli.bendersky | f890a86 | 2010-10-30 12:13:23 +0200 | [diff] [blame] | 939 | ['Constant', 'int', '9']]) |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 940 | |
| 941 | d3 = 'char p = j;' |
| 942 | self.assertEqual(self.get_decl(d3), |
| 943 | ['Decl', 'p', ['TypeDecl', ['IdentifierType', ['char']]]]) |
| 944 | self.assertEqual(self.get_decl_init(d3), |
| 945 | ['ID', 'j']) |
| 946 | |
| 947 | d4 = "char x = 'c', *p = {0, 1, 2, {4, 5}, 6};" |
| 948 | self.assertEqual(self.get_decl(d4, 0), |
| 949 | ['Decl', 'x', ['TypeDecl', ['IdentifierType', ['char']]]]) |
| 950 | self.assertEqual(self.get_decl_init(d4, 0), |
| 951 | ['Constant', 'char', "'c'"]) |
| 952 | self.assertEqual(self.get_decl(d4, 1), |
| 953 | ['Decl', 'p', |
| 954 | ['PtrDecl', |
| 955 | ['TypeDecl', ['IdentifierType', ['char']]]]]) |
eli.bendersky | f890a86 | 2010-10-30 12:13:23 +0200 | [diff] [blame] | 956 | |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 957 | self.assertEqual(self.get_decl_init(d4, 1), |
| 958 | [ ['Constant', 'int', '0'], |
| 959 | ['Constant', 'int', '1'], |
| 960 | ['Constant', 'int', '2'], |
eli.bendersky | f890a86 | 2010-10-30 12:13:23 +0200 | [diff] [blame] | 961 | [['Constant', 'int', '4'], |
| 962 | ['Constant', 'int', '5']], |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 963 | ['Constant', 'int', '6']]) |
eli.bendersky | f890a86 | 2010-10-30 12:13:23 +0200 | [diff] [blame] | 964 | |
| 965 | def test_decl_named_inits(self): |
| 966 | d1 = 'int a = {.k = 16};' |
| 967 | self.assertEqual(self.get_decl_init(d1), |
| 968 | [( [['ID', 'k']], |
| 969 | ['Constant', 'int', '16'])]) |
| 970 | |
| 971 | d2 = 'int a = { [0].a = {1}, [1].a[0] = 2 };' |
| 972 | self.assertEqual(self.get_decl_init(d2), |
| 973 | [ |
| 974 | ([['Constant', 'int', '0'], ['ID', 'a']], |
| 975 | [['Constant', 'int', '1']]), |
| 976 | ([['Constant', 'int', '1'], ['ID', 'a'], ['Constant', 'int', '0']], |
| 977 | ['Constant', 'int', '2'])]) |
| 978 | |
| 979 | d3 = 'int a = { .a = 1, .c = 3, 4, .b = 5};' |
| 980 | self.assertEqual(self.get_decl_init(d3), |
| 981 | [ |
| 982 | ([['ID', 'a']], ['Constant', 'int', '1']), |
| 983 | ([['ID', 'c']], ['Constant', 'int', '3']), |
| 984 | ['Constant', 'int', '4'], |
| 985 | ([['ID', 'b']], ['Constant', 'int', '5'])]) |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 986 | |
| 987 | def test_function_definitions(self): |
| 988 | def parse_fdef(str): |
| 989 | return self.parse(str).ext[0] |
| 990 | |
| 991 | def fdef_decl(fdef): |
| 992 | return expand_decl(fdef.decl) |
| 993 | |
| 994 | f1 = parse_fdef(''' |
| 995 | int factorial(int p) |
| 996 | { |
| 997 | return 3; |
| 998 | } |
| 999 | ''') |
| 1000 | |
| 1001 | self.assertEqual(fdef_decl(f1), |
| 1002 | ['Decl', 'factorial', |
| 1003 | ['FuncDecl', |
| 1004 | [['Decl', 'p', ['TypeDecl', ['IdentifierType', ['int']]]]], |
| 1005 | ['TypeDecl', ['IdentifierType', ['int']]]]]) |
| 1006 | |
eli.bendersky | ef29ff9 | 2010-10-29 16:25:43 +0200 | [diff] [blame] | 1007 | self.assertEqual(type(f1.body.block_items[0]), Return) |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 1008 | |
| 1009 | f2 = parse_fdef(''' |
| 1010 | char* zzz(int p, char* c) |
| 1011 | { |
| 1012 | int a; |
| 1013 | char b; |
| 1014 | |
| 1015 | a = b + 2; |
| 1016 | return 3; |
| 1017 | } |
| 1018 | ''') |
| 1019 | |
| 1020 | self.assertEqual(fdef_decl(f2), |
| 1021 | ['Decl', 'zzz', |
| 1022 | ['FuncDecl', |
| 1023 | [ ['Decl', 'p', ['TypeDecl', ['IdentifierType', ['int']]]], |
| 1024 | ['Decl', 'c', ['PtrDecl', |
| 1025 | ['TypeDecl', ['IdentifierType', ['char']]]]]], |
| 1026 | ['PtrDecl', ['TypeDecl', ['IdentifierType', ['char']]]]]]) |
| 1027 | |
eli.bendersky | ef29ff9 | 2010-10-29 16:25:43 +0200 | [diff] [blame] | 1028 | self.assertEqual(list(map(type, f2.body.block_items)), |
| 1029 | [Decl, Decl, Assignment, Return]) |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 1030 | |
| 1031 | f3 = parse_fdef(''' |
| 1032 | char* zzz(p, c) |
| 1033 | long p, *c; |
| 1034 | { |
| 1035 | int a; |
| 1036 | char b; |
| 1037 | |
| 1038 | a = b + 2; |
| 1039 | return 3; |
| 1040 | } |
| 1041 | ''') |
| 1042 | |
| 1043 | self.assertEqual(fdef_decl(f3), |
| 1044 | ['Decl', 'zzz', |
| 1045 | ['FuncDecl', |
| 1046 | [ ['ID', 'p'], |
| 1047 | ['ID', 'c']], |
| 1048 | ['PtrDecl', ['TypeDecl', ['IdentifierType', ['char']]]]]]) |
| 1049 | |
eli.bendersky | ef29ff9 | 2010-10-29 16:25:43 +0200 | [diff] [blame] | 1050 | self.assertEqual(list(map(type, f3.body.block_items)), |
| 1051 | [Decl, Decl, Assignment, Return]) |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 1052 | |
| 1053 | self.assertEqual(expand_decl(f3.param_decls[0]), |
| 1054 | ['Decl', 'p', ['TypeDecl', ['IdentifierType', ['long']]]]) |
| 1055 | self.assertEqual(expand_decl(f3.param_decls[1]), |
| 1056 | ['Decl', 'c', ['PtrDecl', ['TypeDecl', ['IdentifierType', ['long']]]]]) |
| 1057 | |
eli.bendersky | 7154066 | 2010-07-03 12:58:52 +0200 | [diff] [blame] | 1058 | def test_unified_string_literals(self): |
| 1059 | # simple string, for reference |
| 1060 | d1 = self.get_decl_init('char* s = "hello";') |
| 1061 | self.assertEqual(d1, ['Constant', 'string', '"hello"']) |
| 1062 | |
| 1063 | d2 = self.get_decl_init('char* s = "hello" " world";') |
| 1064 | self.assertEqual(d2, ['Constant', 'string', '"hello world"']) |
| 1065 | |
| 1066 | # the test case from issue 6 |
| 1067 | d3 = self.parse(r''' |
| 1068 | int main() { |
| 1069 | fprintf(stderr, |
| 1070 | "Wrong Params?\n" |
| 1071 | "Usage:\n" |
| 1072 | "%s <binary_file_path>\n", |
| 1073 | argv[0] |
| 1074 | ); |
| 1075 | } |
| 1076 | ''') |
| 1077 | |
| 1078 | self.assertEqual( |
eli.bendersky | ef29ff9 | 2010-10-29 16:25:43 +0200 | [diff] [blame] | 1079 | d3.ext[0].body.block_items[0].args.exprs[1].value, |
eli.bendersky | 7154066 | 2010-07-03 12:58:52 +0200 | [diff] [blame] | 1080 | r'"Wrong Params?\nUsage:\n%s <binary_file_path>\n"') |
eli.bendersky | 4a89f11 | 2010-07-05 06:02:03 +0200 | [diff] [blame] | 1081 | |
| 1082 | d4 = self.get_decl_init('char* s = "" "foobar";') |
| 1083 | self.assertEqual(d4, ['Constant', 'string', '"foobar"']) |
| 1084 | |
| 1085 | d5 = self.get_decl_init(r'char* s = "foo\"" "bar";') |
| 1086 | self.assertEqual(d5, ['Constant', 'string', r'"foo\"bar"']) |
eli.bendersky | 7154066 | 2010-07-03 12:58:52 +0200 | [diff] [blame] | 1087 | |
eli.bendersky | 79d5cf6 | 2010-10-29 13:33:52 +0200 | [diff] [blame] | 1088 | def test_inline_specifier(self): |
| 1089 | ps2 = self.parse('static inline void inlinefoo(void);') |
| 1090 | self.assertEqual(ps2.ext[0].funcspec, ['inline']) |
eli.bendersky | 2e907fa | 2010-10-29 15:51:07 +0200 | [diff] [blame] | 1091 | |
| 1092 | # variable length array |
| 1093 | def test_vla(self): |
| 1094 | ps2 = self.parse(r''' |
| 1095 | int main() { |
| 1096 | int size; |
| 1097 | int var[size = 5]; |
| 1098 | |
| 1099 | int var2[*]; |
| 1100 | } |
| 1101 | ''') |
eli.bendersky | ef29ff9 | 2010-10-29 16:25:43 +0200 | [diff] [blame] | 1102 | self.failUnless(isinstance(ps2.ext[0].body.block_items[1].type.dim, Assignment)) |
| 1103 | self.failUnless(isinstance(ps2.ext[0].body.block_items[2].type.dim, ID)) |
eli.bendersky | 79d5cf6 | 2010-10-29 13:33:52 +0200 | [diff] [blame] | 1104 | |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 1105 | |
eli.bendersky | 85d2e73 | 2011-05-20 19:47:26 +0300 | [diff] [blame] | 1106 | class TestCParser_whole_code(TestCParser_base): |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 1107 | """ Testing of parsing whole chunks of code. |
| 1108 | |
| 1109 | Since I don't want to rely on the structure of ASTs too |
| 1110 | much, most of these tests are implemented with visitors. |
| 1111 | """ |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 1112 | # A simple helper visitor that lists the values of all the |
| 1113 | # Constant nodes it sees. |
| 1114 | # |
| 1115 | class ConstantVisitor(NodeVisitor): |
| 1116 | def __init__(self): |
| 1117 | self.values = [] |
| 1118 | |
| 1119 | def visit_Constant(self, node): |
| 1120 | self.values.append(node.value) |
| 1121 | |
| 1122 | # This visitor counts the amount of references to the ID |
| 1123 | # with the name provided to it in the constructor. |
| 1124 | # |
| 1125 | class IDNameCounter(NodeVisitor): |
| 1126 | def __init__(self, name): |
| 1127 | self.name = name |
| 1128 | self.nrefs = 0 |
| 1129 | |
| 1130 | def visit_ID(self, node): |
| 1131 | if node.name == self.name: |
| 1132 | self.nrefs += 1 |
| 1133 | |
| 1134 | # Counts the amount of nodes of a given class |
| 1135 | # |
| 1136 | class NodeKlassCounter(NodeVisitor): |
| 1137 | def __init__(self, node_klass): |
| 1138 | self.klass = node_klass |
| 1139 | self.n = 0 |
| 1140 | |
| 1141 | def generic_visit(self, node): |
| 1142 | if node.__class__ == self.klass: |
| 1143 | self.n += 1 |
| 1144 | |
| 1145 | NodeVisitor.generic_visit(self, node) |
| 1146 | |
| 1147 | def assert_all_Constants(self, code, constants): |
| 1148 | """ Asserts that the list of all Constant values (by |
| 1149 | 'preorder' appearance) in the chunk of code is as |
| 1150 | given. |
| 1151 | """ |
eli.bendersky | ed89049 | 2010-06-25 08:25:55 +0300 | [diff] [blame] | 1152 | if isinstance(code, str): |
| 1153 | parsed = self.parse(code) |
| 1154 | else: |
| 1155 | parsed = code |
| 1156 | |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 1157 | cv = self.ConstantVisitor() |
| 1158 | cv.visit(parsed) |
| 1159 | self.assertEqual(cv.values, constants) |
| 1160 | |
| 1161 | def assert_num_ID_refs(self, code, name, num): |
| 1162 | """ Asserts the number of references to the ID with |
| 1163 | the given name. |
| 1164 | """ |
| 1165 | if isinstance(code, str): |
| 1166 | parsed = self.parse(code) |
| 1167 | else: |
| 1168 | parsed = code |
| 1169 | |
| 1170 | iv = self.IDNameCounter(name) |
| 1171 | iv.visit(parsed) |
| 1172 | self.assertEqual(iv.nrefs, num) |
| 1173 | |
| 1174 | def assert_num_klass_nodes(self, code, klass, num): |
| 1175 | """ Asserts the amount of klass nodes in the code. |
| 1176 | """ |
| 1177 | if isinstance(code, str): |
| 1178 | parsed = self.parse(code) |
| 1179 | else: |
| 1180 | parsed = code |
| 1181 | |
| 1182 | cv = self.NodeKlassCounter(klass) |
| 1183 | cv.visit(parsed) |
| 1184 | self.assertEqual(cv.n, num) |
| 1185 | |
| 1186 | def test_expressions(self): |
| 1187 | e1 = '''int k = (r + 10.0) >> 6 + 8 << (3 & 0x14);''' |
| 1188 | self.assert_all_Constants(e1, ['10.0', '6', '8', '3', '0x14']) |
| 1189 | |
| 1190 | e2 = r'''char n = '\n', *prefix = "st_";''' |
| 1191 | self.assert_all_Constants(e2, [r"'\n'", '"st_"']) |
| 1192 | |
| 1193 | def test_statements(self): |
| 1194 | s1 = r''' |
| 1195 | void foo(){ |
| 1196 | if (sp == 1) |
| 1197 | if (optind >= argc || |
| 1198 | argv[optind][0] != '-' || argv[optind][1] == '\0') |
| 1199 | return -1; |
| 1200 | else if (strcmp(argv[optind], "--") == 0) { |
| 1201 | optind++; |
| 1202 | return -1; |
| 1203 | } |
| 1204 | } |
| 1205 | ''' |
| 1206 | |
| 1207 | self.assert_all_Constants(s1, |
| 1208 | ['1', '0', r"'-'", '1', r"'\0'", '1', r'"--"', '0', '1']) |
| 1209 | |
| 1210 | ps1 = self.parse(s1) |
| 1211 | self.assert_num_ID_refs(ps1, 'argv', 3) |
| 1212 | self.assert_num_ID_refs(ps1, 'optind', 5) |
| 1213 | |
| 1214 | self.assert_num_klass_nodes(ps1, If, 3) |
| 1215 | self.assert_num_klass_nodes(ps1, Return, 2) |
| 1216 | self.assert_num_klass_nodes(ps1, FuncCall, 1) # strcmp |
| 1217 | self.assert_num_klass_nodes(ps1, BinaryOp, 7) |
| 1218 | |
| 1219 | # In the following code, Hash and Node were defined as |
| 1220 | # int to pacify the parser that sees they're used as |
| 1221 | # types |
| 1222 | # |
| 1223 | s2 = r''' |
| 1224 | typedef int Hash, Node; |
| 1225 | |
| 1226 | void HashDestroy(Hash* hash) |
| 1227 | { |
| 1228 | unsigned int i; |
| 1229 | |
| 1230 | if (hash == NULL) |
| 1231 | return; |
| 1232 | |
| 1233 | for (i = 0; i < hash->table_size; ++i) |
| 1234 | { |
| 1235 | Node* temp = hash->heads[i]; |
| 1236 | |
| 1237 | while (temp != NULL) |
| 1238 | { |
| 1239 | Node* temp2 = temp; |
| 1240 | |
| 1241 | free(temp->entry->key); |
| 1242 | free(temp->entry->value); |
| 1243 | free(temp->entry); |
| 1244 | |
| 1245 | temp = temp->next; |
| 1246 | |
| 1247 | free(temp2); |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | free(hash->heads); |
| 1252 | hash->heads = NULL; |
| 1253 | |
| 1254 | free(hash); |
| 1255 | } |
| 1256 | ''' |
| 1257 | |
| 1258 | ps2 = self.parse(s2) |
| 1259 | self.assert_num_klass_nodes(ps2, FuncCall, 6) |
| 1260 | self.assert_num_klass_nodes(ps2, FuncDef, 1) |
| 1261 | self.assert_num_klass_nodes(ps2, For, 1) |
| 1262 | self.assert_num_klass_nodes(ps2, While, 1) |
| 1263 | self.assert_num_klass_nodes(ps2, StructRef, 10) |
| 1264 | |
| 1265 | # declarations don't count |
| 1266 | self.assert_num_ID_refs(ps2, 'hash', 6) |
| 1267 | self.assert_num_ID_refs(ps2, 'i', 4) |
eli.bendersky | ed89049 | 2010-06-25 08:25:55 +0300 | [diff] [blame] | 1268 | |
| 1269 | s3 = r''' |
| 1270 | void x(void) { |
| 1271 | int a, b; |
| 1272 | if (a < b) |
| 1273 | do { |
| 1274 | a = 0; |
| 1275 | } while (0); |
| 1276 | else if (a == b) { |
| 1277 | a = 1; |
| 1278 | } |
| 1279 | } |
| 1280 | ''' |
| 1281 | |
| 1282 | ps3 = self.parse(s3) |
| 1283 | self.assert_num_klass_nodes(ps3, DoWhile, 1) |
| 1284 | self.assert_num_ID_refs(ps3, 'a', 4) |
| 1285 | self.assert_all_Constants(ps3, ['0', '0', '1']) |
eli.bendersky | 91c0aa3 | 2011-10-16 05:50:43 +0200 | [diff] [blame] | 1286 | |
| 1287 | def test_empty_statement(self): |
| 1288 | s1 = r''' |
| 1289 | void foo(void){ |
| 1290 | ; |
| 1291 | return; |
| 1292 | } |
| 1293 | ''' |
| 1294 | ps1 = self.parse(s1) |
| 1295 | self.assert_num_klass_nodes(ps1, EmptyStatement, 1) |
| 1296 | self.assert_num_klass_nodes(ps1, Return, 1) |
eli.bendersky | 145890d | 2010-10-29 12:02:32 +0200 | [diff] [blame] | 1297 | |
Ben | 5cd3fd6 | 2012-02-03 06:02:40 +0200 | [diff] [blame] | 1298 | def test_switch_statement(self): |
Eli Bendersky | 12f0c9d | 2012-02-03 11:22:25 +0200 | [diff] [blame] | 1299 | def assert_case_node(node, const_value): |
| 1300 | self.failUnless(isinstance(node, Case)) |
| 1301 | self.failUnless(isinstance(node.expr, Constant)) |
| 1302 | self.assertEqual(node.expr.value, const_value) |
| 1303 | |
| 1304 | def assert_default_node(node): |
| 1305 | self.failUnless(isinstance(node, Default)) |
| 1306 | |
Ben | 5cd3fd6 | 2012-02-03 06:02:40 +0200 | [diff] [blame] | 1307 | s1 = r''' |
| 1308 | int foo(void) { |
| 1309 | switch (myvar) { |
| 1310 | case 10: |
| 1311 | k = 10; |
| 1312 | p = k + 1; |
| 1313 | return 10; |
| 1314 | case 20: |
| 1315 | case 30: |
| 1316 | return 20; |
| 1317 | default: |
| 1318 | break; |
| 1319 | } |
| 1320 | return 0; |
| 1321 | } |
| 1322 | ''' |
| 1323 | ps1 = self.parse(s1) |
Eli Bendersky | 12f0c9d | 2012-02-03 11:22:25 +0200 | [diff] [blame] | 1324 | switch = ps1.ext[0].body.block_items[0] |
| 1325 | |
| 1326 | block = switch.stmt.block_items |
| 1327 | assert_case_node(block[0], '10') |
| 1328 | self.assertEqual(len(block[0].stmts), 3) |
| 1329 | assert_case_node(block[1], '20') |
| 1330 | self.assertEqual(len(block[1].stmts), 0) |
| 1331 | assert_case_node(block[2], '30') |
| 1332 | self.assertEqual(len(block[2].stmts), 1) |
| 1333 | assert_default_node(block[3]) |
| 1334 | |
| 1335 | s2 = r''' |
| 1336 | int foo(void) { |
| 1337 | switch (myvar) { |
| 1338 | default: |
| 1339 | joe = moe; |
| 1340 | return 10; |
| 1341 | case 10: |
| 1342 | case 20: |
| 1343 | case 30: |
| 1344 | case 40: |
| 1345 | break; |
| 1346 | } |
| 1347 | return 0; |
| 1348 | } |
| 1349 | ''' |
| 1350 | ps2 = self.parse(s2) |
| 1351 | switch = ps2.ext[0].body.block_items[0] |
| 1352 | |
| 1353 | block = switch.stmt.block_items |
| 1354 | assert_default_node(block[0]) |
| 1355 | self.assertEqual(len(block[0].stmts), 2) |
| 1356 | assert_case_node(block[1], '10') |
| 1357 | self.assertEqual(len(block[1].stmts), 0) |
| 1358 | assert_case_node(block[2], '20') |
| 1359 | self.assertEqual(len(block[1].stmts), 0) |
| 1360 | assert_case_node(block[3], '30') |
| 1361 | self.assertEqual(len(block[1].stmts), 0) |
| 1362 | assert_case_node(block[4], '40') |
| 1363 | self.assertEqual(len(block[4].stmts), 1) |
Ben | 5cd3fd6 | 2012-02-03 06:02:40 +0200 | [diff] [blame] | 1364 | |
eli.bendersky | 145890d | 2010-10-29 12:02:32 +0200 | [diff] [blame] | 1365 | def test_for_statement(self): |
| 1366 | s2 = r''' |
| 1367 | void x(void) |
| 1368 | { |
| 1369 | int i; |
| 1370 | for (i = 0; i < 5; ++i) { |
| 1371 | x = 50; |
| 1372 | } |
| 1373 | } |
| 1374 | ''' |
| 1375 | ps2 = self.parse(s2) |
| 1376 | self.assert_num_klass_nodes(ps2, For, 1) |
| 1377 | # here there are 3 refs to 'i' since the declaration doesn't count as |
| 1378 | # a ref in the visitor |
| 1379 | # |
| 1380 | self.assert_num_ID_refs(ps2, 'i', 3) |
eli.bendersky | ed89049 | 2010-06-25 08:25:55 +0300 | [diff] [blame] | 1381 | |
eli.bendersky | 145890d | 2010-10-29 12:02:32 +0200 | [diff] [blame] | 1382 | s3 = r''' |
| 1383 | void x(void) |
| 1384 | { |
| 1385 | for (int i = 0; i < 5; ++i) { |
| 1386 | x = 50; |
| 1387 | } |
| 1388 | } |
| 1389 | ''' |
| 1390 | ps3 = self.parse(s3) |
eli.bendersky | 145890d | 2010-10-29 12:02:32 +0200 | [diff] [blame] | 1391 | self.assert_num_klass_nodes(ps3, For, 1) |
| 1392 | # here there are 2 refs to 'i' since the declaration doesn't count as |
| 1393 | # a ref in the visitor |
| 1394 | # |
| 1395 | self.assert_num_ID_refs(ps3, 'i', 2) |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 1396 | |
Eli Bendersky | d097378 | 2012-01-19 08:09:33 +0200 | [diff] [blame] | 1397 | def _open_c_file(self, name): |
| 1398 | """ Find a c file by name, taking into account the current dir can be |
| 1399 | in a couple of typical places |
| 1400 | """ |
| 1401 | fullnames = [ |
| 1402 | os.path.join('c_files', name), |
| 1403 | os.path.join('tests', 'c_files', name)] |
| 1404 | for fullname in fullnames: |
| 1405 | if os.path.exists(fullname): |
| 1406 | return open(fullname, 'rU') |
| 1407 | assert False, "Unreachable" |
| 1408 | |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 1409 | def test_whole_file(self): |
| 1410 | # See how pycparser handles a whole, real C file. |
| 1411 | # |
Eli Bendersky | d097378 | 2012-01-19 08:09:33 +0200 | [diff] [blame] | 1412 | code = self._open_c_file('memmgr_with_h.c').read() |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 1413 | p = self.parse(code) |
| 1414 | |
| 1415 | self.assert_num_klass_nodes(p, FuncDef, 5) |
| 1416 | |
| 1417 | # each FuncDef also has a FuncDecl. 4 declarations |
| 1418 | # + 5 definitions, overall 9 |
| 1419 | self.assert_num_klass_nodes(p, FuncDecl, 9) |
| 1420 | |
| 1421 | self.assert_num_klass_nodes(p, Typedef, 4) |
| 1422 | |
| 1423 | self.assertEqual(p.ext[4].coord.line, 88) |
| 1424 | self.assertEqual(p.ext[4].coord.file, "./memmgr.h") |
| 1425 | |
| 1426 | self.assertEqual(p.ext[6].coord.line, 10) |
| 1427 | self.assertEqual(p.ext[6].coord.file, "memmgr.c") |
| 1428 | |
| 1429 | def test_whole_file_with_stdio(self): |
| 1430 | # Parse a whole file with stdio.h included by cpp |
| 1431 | # |
Eli Bendersky | d097378 | 2012-01-19 08:09:33 +0200 | [diff] [blame] | 1432 | code = self._open_c_file('cppd_with_stdio_h.c').read() |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 1433 | p = self.parse(code) |
| 1434 | |
| 1435 | self.failUnless(isinstance(p.ext[0], Typedef)) |
| 1436 | self.assertEqual(p.ext[0].coord.line, 213) |
| 1437 | self.assertEqual(p.ext[0].coord.file, "D:\eli\cpp_stuff\libc_include/stddef.h") |
| 1438 | |
| 1439 | self.failUnless(isinstance(p.ext[-1], FuncDef)) |
| 1440 | self.assertEqual(p.ext[-1].coord.line, 15) |
| 1441 | self.assertEqual(p.ext[-1].coord.file, "example_c_file.c") |
| 1442 | |
| 1443 | self.failUnless(isinstance(p.ext[-8], Typedef)) |
| 1444 | self.failUnless(isinstance(p.ext[-8].type, TypeDecl)) |
| 1445 | self.assertEqual(p.ext[-8].name, 'cookie_io_functions_t') |
eli.bendersky | 85d2e73 | 2011-05-20 19:47:26 +0300 | [diff] [blame] | 1446 | |
| 1447 | |
| 1448 | class TestCParser_typenames(TestCParser_base): |
| 1449 | """ Test issues related to the typedef-name problem. |
| 1450 | """ |
| 1451 | def test_innerscope_typedef(self): |
| 1452 | # should fail since TT is not a type in bar |
| 1453 | s1 = r''' |
| 1454 | void foo() { |
| 1455 | typedef char TT; |
| 1456 | TT x; |
| 1457 | } |
| 1458 | void bar() { |
| 1459 | TT y; |
| 1460 | } |
| 1461 | ''' |
| 1462 | self.assertRaises(ParseError, self.parse, s1) |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 1463 | |
eli.bendersky | 85d2e73 | 2011-05-20 19:47:26 +0300 | [diff] [blame] | 1464 | # should succeed since TT is not a type in bar |
| 1465 | s2 = r''' |
| 1466 | void foo() { |
| 1467 | typedef char TT; |
| 1468 | TT x; |
| 1469 | } |
| 1470 | void bar() { |
| 1471 | unsigned TT; |
| 1472 | } |
| 1473 | ''' |
| 1474 | self.failUnless(isinstance(self.parse(s2), FileAST)) |
| 1475 | |
| 1476 | |
Eli Bendersky | 3921e8e | 2010-05-21 09:05:39 +0300 | [diff] [blame] | 1477 | |
| 1478 | if __name__ == '__main__': |
| 1479 | #~ suite = unittest.TestLoader().loadTestsFromNames( |
| 1480 | #~ ['test_c_parser.TestCParser_fundamentals.test_typedef']) |
| 1481 | |
| 1482 | #~ suite = unittest.TestLoader().loadTestsFromNames( |
| 1483 | #~ ['test_c_parser.TestCParser_whole_code.test_whole_file_with_stdio']) |
| 1484 | |
| 1485 | #~ suite = unittest.TestLoader().loadTestsFromTestCase( |
| 1486 | #~ TestCParser_whole_code) |
| 1487 | |
| 1488 | #~ unittest.TextTestRunner(verbosity=2).run(suite) |
| 1489 | unittest.main() |