| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
from . import c_ast |
| 11 |
|
| 12 |
|
| 13 |
def fix_switch_cases(switch_node): |
| 14 |
""" The 'case' statements in a 'switch' come out of parsing with one |
| 15 |
child node, so subsequent statements are just tucked to the parent |
| 16 |
Compound. Additionally, consecutive (fall-through) case statements |
| 17 |
come out messy. This is a peculiarity of the C grammar. The following: |
| 18 |
|
| 19 |
switch (myvar) { |
| 20 |
case 10: |
| 21 |
k = 10; |
| 22 |
p = k + 1; |
| 23 |
return 10; |
| 24 |
case 20: |
| 25 |
case 30: |
| 26 |
return 20; |
| 27 |
default: |
| 28 |
break; |
| 29 |
} |
| 30 |
|
| 31 |
Creates this tree (pseudo-dump): |
| 32 |
|
| 33 |
Switch |
| 34 |
ID: myvar |
| 35 |
Compound: |
| 36 |
Case 10: |
| 37 |
k = 10 |
| 38 |
p = k + 1 |
| 39 |
return 10 |
| 40 |
Case 20: |
| 41 |
Case 30: |
| 42 |
return 20 |
| 43 |
Default: |
| 44 |
break |
| 45 |
|
| 46 |
The goal of this transform is to fix this mess, turning it into the |
| 47 |
following: |
| 48 |
|
| 49 |
Switch |
| 50 |
ID: myvar |
| 51 |
Compound: |
| 52 |
Case 10: |
| 53 |
k = 10 |
| 54 |
p = k + 1 |
| 55 |
return 10 |
| 56 |
Case 20: |
| 57 |
Case 30: |
| 58 |
return 20 |
| 59 |
Default: |
| 60 |
break |
| 61 |
|
| 62 |
A fixed AST node is returned. The argument may be modified. |
| 63 |
""" |
| 64 |
assert isinstance(switch_node, c_ast.Switch) |
| 65 |
if not isinstance(switch_node.stmt, c_ast.Compound): |
| 66 |
return switch_node |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
new_compound = c_ast.Compound([], switch_node.stmt.coord) |
| 71 |
|
| 72 |
|
| 73 |
last_case = None |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
for child in (switch_node.stmt.block_items or []): |
| 79 |
if isinstance(child, (c_ast.Case, c_ast.Default)): |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
new_compound.block_items.append(child) |
| 85 |
_extract_nested_case(child, new_compound.block_items) |
| 86 |
last_case = new_compound.block_items[-1] |
| 87 |
else: |
| 88 |
|
| 89 |
|
| 90 |
if last_case is None: |
| 91 |
new_compound.block_items.append(child) |
| 92 |
else: |
| 93 |
last_case.stmts.append(child) |
| 94 |
|
| 95 |
switch_node.stmt = new_compound |
| 96 |
return switch_node |
| 97 |
|
| 98 |
|
| 99 |
def _extract_nested_case(case_node, stmts_list): |
| 100 |
""" Recursively extract consecutive Case statements that are made nested |
| 101 |
by the parser and add them to the stmts_list. |
| 102 |
""" |
| 103 |
if isinstance(case_node.stmts[0], (c_ast.Case, c_ast.Default)): |
| 104 |
stmts_list.append(case_node.stmts.pop()) |
| 105 |
_extract_nested_case(stmts_list[-1], stmts_list) |
| 106 |
|
| 107 |
|
| 108 |
def fix_atomic_specifiers(decl): |
| 109 |
""" Atomic specifiers like _Atomic(type) are unusually structured, |
| 110 |
conferring a qualifier upon the contained type. |
| 111 |
|
| 112 |
This function fixes a decl with atomic specifiers to have a sane AST |
| 113 |
structure, by removing spurious Typename->TypeDecl pairs and attaching |
| 114 |
the _Atomic qualifier in the right place. |
| 115 |
""" |
| 116 |
|
| 117 |
|
| 118 |
while True: |
| 119 |
decl, found = _fix_atomic_specifiers_once(decl) |
| 120 |
if not found: |
| 121 |
break |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
typ = decl |
| 127 |
while not isinstance(typ, c_ast.TypeDecl): |
| 128 |
try: |
| 129 |
typ = typ.type |
| 130 |
except AttributeError: |
| 131 |
return decl |
| 132 |
if '_Atomic' in typ.quals and '_Atomic' not in decl.quals: |
| 133 |
decl.quals.append('_Atomic') |
| 134 |
if typ.declname is None: |
| 135 |
typ.declname = decl.name |
| 136 |
|
| 137 |
return decl |
| 138 |
|
| 139 |
|
| 140 |
def _fix_atomic_specifiers_once(decl): |
| 141 |
""" Performs one 'fix' round of atomic specifiers. |
| 142 |
Returns (modified_decl, found) where found is True iff a fix was made. |
| 143 |
""" |
| 144 |
parent = decl |
| 145 |
grandparent = None |
| 146 |
node = decl.type |
| 147 |
while node is not None: |
| 148 |
if isinstance(node, c_ast.Typename) and '_Atomic' in node.quals: |
| 149 |
break |
| 150 |
try: |
| 151 |
grandparent = parent |
| 152 |
parent = node |
| 153 |
node = node.type |
| 154 |
except AttributeError: |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
return decl, False |
| 159 |
|
| 160 |
assert isinstance(parent, c_ast.TypeDecl) |
| 161 |
grandparent.type = node.type |
| 162 |
if '_Atomic' not in node.type.quals: |
| 163 |
node.type.quals.append('_Atomic') |
| 164 |
return decl, True |