[51587aa] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
[23b6f4d7] | 7 | // MLEMutator.cc -- |
---|
[51587aa] | 8 | // |
---|
[843054c2] | 9 | // Author : Rodolfo G. Esteves |
---|
[51587aa] | 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[adcc065] | 11 | // Last Modified By : Peter A. Buhr |
---|
[720a007] | 12 | // Last Modified On : Thu Mar 8 17:08:25 2018 |
---|
| 13 | // Update Count : 219 |
---|
[51587aa] | 14 | // |
---|
[a08ba92] | 15 | |
---|
[adcc065] | 16 | // NOTE: There are two known subtle differences from the code that uC++ generates for the same input |
---|
| 17 | // -CFA puts the break label inside at the end of a switch, uC++ puts it after |
---|
| 18 | // -CFA puts the break label after a block, uC++ puts it inside at the end |
---|
| 19 | // It is unclear if these differences are important, but if they are, then the fix would go in this file, since this is |
---|
| 20 | // where these labels are generated. |
---|
[994ec2c] | 21 | |
---|
[d180746] | 22 | #include <ext/alloc_traits.h> // for __alloc_traits<>::value_type |
---|
| 23 | #include <algorithm> // for find, find_if |
---|
| 24 | #include <cassert> // for assert, assertf |
---|
| 25 | #include <memory> // for allocator_traits<>::value_... |
---|
[51b7345] | 26 | |
---|
[d180746] | 27 | #include "Common/utility.h" // for toString, operator+ |
---|
| 28 | #include "ControlStruct/LabelGenerator.h" // for LabelGenerator |
---|
[51b7345] | 29 | #include "MLEMutator.h" |
---|
[d180746] | 30 | #include "SynTree/Attribute.h" // for Attribute |
---|
| 31 | #include "SynTree/Expression.h" // for Expression |
---|
| 32 | #include "SynTree/Statement.h" // for BranchStmt, CompoundStmt |
---|
[51b7345] | 33 | |
---|
| 34 | namespace ControlStruct { |
---|
[a08ba92] | 35 | MLEMutator::~MLEMutator() { |
---|
| 36 | delete targetTable; |
---|
| 37 | targetTable = 0; |
---|
| 38 | } |
---|
[e39aa0f] | 39 | namespace { |
---|
[720a007] | 40 | bool isLoop( const MLEMutator::Entry & e ) { return dynamic_cast< WhileStmt * >( e.get_controlStructure() ) || dynamic_cast< ForStmt * >( e.get_controlStructure() ); } |
---|
| 41 | bool isSwitch( const MLEMutator::Entry & e ) { return dynamic_cast< SwitchStmt *>( e.get_controlStructure() ); } |
---|
| 42 | |
---|
| 43 | bool isBreakTarget( const MLEMutator::Entry & e ) { return isLoop( e ) || isSwitch( e ) || dynamic_cast< CompoundStmt *>( e.get_controlStructure() ); } |
---|
| 44 | bool isContinueTarget( const MLEMutator::Entry & e ) { return isLoop( e ); } |
---|
| 45 | bool isFallthroughTarget( const MLEMutator::Entry & e ) { return dynamic_cast< CaseStmt *>( e.get_controlStructure() );; } |
---|
| 46 | bool isFallthroughDefaultTarget( const MLEMutator::Entry & e ) { return isSwitch( e ); } |
---|
| 47 | } // namespace |
---|
[a08ba92] | 48 | |
---|
[adcc065] | 49 | // break labels have to come after the statement they break out of, so mutate a statement, then if they inform us |
---|
| 50 | // through the breakLabel field tha they need a place to jump to on a break statement, add the break label to the |
---|
| 51 | // body of statements |
---|
[720a007] | 52 | void MLEMutator::fixBlock( std::list< Statement * > &kids, bool caseClause ) { |
---|
| 53 | SemanticErrorException errors; |
---|
| 54 | |
---|
[a08ba92] | 55 | for ( std::list< Statement * >::iterator k = kids.begin(); k != kids.end(); k++ ) { |
---|
[720a007] | 56 | if ( caseClause ) { |
---|
| 57 | // once a label is seen, it's no longer a valid fallthrough target |
---|
| 58 | for ( Label & l : (*k)->labels ) { |
---|
| 59 | fallthroughLabels.erase( l ); |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | // aggregate errors since the PassVisitor mutate loop was unrollled |
---|
| 64 | try { |
---|
| 65 | *k = (*k)->acceptMutator(*visitor); |
---|
| 66 | } catch( SemanticErrorException &e ) { |
---|
| 67 | errors.append( e ); |
---|
| 68 | } |
---|
[a08ba92] | 69 | |
---|
| 70 | if ( ! get_breakLabel().empty() ) { |
---|
[d939274] | 71 | std::list< Statement * >::iterator next = k+1; |
---|
[e39aa0f] | 72 | std::list<Label> ls; ls.push_back( get_breakLabel() ); |
---|
| 73 | kids.insert( next, new NullStmt( ls ) ); |
---|
[a08ba92] | 74 | set_breakLabel(""); |
---|
| 75 | } // if |
---|
| 76 | } // for |
---|
[720a007] | 77 | |
---|
| 78 | if ( ! errors.isEmpty() ) { |
---|
| 79 | throw errors; |
---|
| 80 | } |
---|
[27de955] | 81 | } |
---|
| 82 | |
---|
[94e025a2] | 83 | void MLEMutator::premutate( CompoundStmt *cmpndStmt ) { |
---|
| 84 | visit_children = false; |
---|
| 85 | bool labeledBlock = !(cmpndStmt->labels.empty()); |
---|
[27de955] | 86 | if ( labeledBlock ) { |
---|
[af68f0a] | 87 | Label brkLabel = generator->newLabel("blockBreak", cmpndStmt); |
---|
[e39aa0f] | 88 | enclosingControlStructures.push_back( Entry( cmpndStmt, brkLabel ) ); |
---|
[720a007] | 89 | GuardAction( [this]() { enclosingControlStructures.pop_back(); } ); |
---|
[27de955] | 90 | } // if |
---|
| 91 | |
---|
[adcc065] | 92 | // a child statement may set the break label - if they do, attach it to the next statement |
---|
[94e025a2] | 93 | std::list< Statement * > &kids = cmpndStmt->kids; |
---|
[27de955] | 94 | fixBlock( kids ); |
---|
[a08ba92] | 95 | |
---|
| 96 | if ( labeledBlock ) { |
---|
[e39aa0f] | 97 | assert( ! enclosingControlStructures.empty() ); |
---|
| 98 | if ( ! enclosingControlStructures.back().useBreakExit().empty() ) { |
---|
| 99 | set_breakLabel( enclosingControlStructures.back().useBreakExit() ); |
---|
[adcc065] | 100 | } // if |
---|
[a08ba92] | 101 | } // if |
---|
[adcc065] | 102 | } |
---|
| 103 | |
---|
[a08ba92] | 104 | |
---|
[33c4b81] | 105 | void addUnused( Statement * stmt, const Label & originalTarget ) { |
---|
| 106 | // break/continue without a label doesn't need unused attribute |
---|
| 107 | if ( originalTarget == "" ) return; |
---|
| 108 | // add unused attribute to the originalTarget of a labelled break/continue |
---|
| 109 | for ( Label & l : stmt->get_labels() ) { |
---|
| 110 | // find the label to add unused attribute to |
---|
| 111 | if ( l == originalTarget ) { |
---|
| 112 | for ( Attribute * attr : l.get_attributes() ) { |
---|
| 113 | // ensure attribute isn't added twice |
---|
| 114 | if ( attr->get_name() == "unused" ) return; |
---|
| 115 | } |
---|
| 116 | l.get_attributes().push_back( new Attribute( "unused" ) ); |
---|
| 117 | return; |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | assertf( false, "Could not find label '%s' on statement %s", originalTarget.get_name().c_str(), toString( stmt ).c_str() ); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | |
---|
[a16764a6] | 124 | Statement *MLEMutator::postmutate( BranchStmt *branchStmt ) throw ( SemanticErrorException ) { |
---|
[94e025a2] | 125 | std::string originalTarget = branchStmt->originalTarget; |
---|
[be5aa1b] | 126 | |
---|
[e39aa0f] | 127 | std::list< Entry >::reverse_iterator targetEntry; |
---|
[af68f0a] | 128 | switch ( branchStmt->get_type() ) { |
---|
| 129 | case BranchStmt::Goto: |
---|
| 130 | return branchStmt; |
---|
| 131 | case BranchStmt::Continue: |
---|
| 132 | case BranchStmt::Break: { |
---|
| 133 | bool isContinue = branchStmt->get_type() == BranchStmt::Continue; |
---|
| 134 | // unlabeled break/continue |
---|
| 135 | if ( branchStmt->get_target() == "" ) { |
---|
| 136 | if ( isContinue ) { |
---|
| 137 | // continue target is outermost loop |
---|
[720a007] | 138 | targetEntry = std::find_if( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), isContinueTarget ); |
---|
[af68f0a] | 139 | } else { |
---|
[720a007] | 140 | // break target is outermost loop, switch, or block control structure |
---|
| 141 | if ( enclosingControlStructures.empty() ) SemanticError( branchStmt->location, "'break' outside a loop, 'switch', or labelled block" ); |
---|
| 142 | targetEntry = std::find_if( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), isBreakTarget ); |
---|
[af68f0a] | 143 | } // if |
---|
| 144 | } else { |
---|
| 145 | // labeled break/continue - lookup label in table to find attached control structure |
---|
| 146 | targetEntry = std::find( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), (*targetTable)[branchStmt->get_target()] ); |
---|
| 147 | } // if |
---|
| 148 | // ensure that selected target is valid |
---|
[720a007] | 149 | if ( targetEntry == enclosingControlStructures.rend() || (isContinue && ! isContinueTarget( *targetEntry ) ) ) { |
---|
[a16764a6] | 150 | SemanticError( branchStmt->location, toString( (isContinue ? "'continue'" : "'break'"), " target must be an enclosing ", (isContinue ? "loop: " : "control structure: "), originalTarget ) ); |
---|
[af68f0a] | 151 | } // if |
---|
| 152 | break; |
---|
| 153 | } |
---|
[720a007] | 154 | case BranchStmt::FallThrough: |
---|
| 155 | targetEntry = std::find_if( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), isFallthroughTarget ); |
---|
| 156 | // ensure that selected target is valid |
---|
| 157 | if ( targetEntry == enclosingControlStructures.rend() ) { |
---|
| 158 | SemanticError( branchStmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" ); |
---|
| 159 | } // if |
---|
| 160 | if ( branchStmt->get_target() != "" ) { |
---|
| 161 | // labelled fallthrough |
---|
| 162 | // target must be in the set of valid fallthrough labels |
---|
| 163 | if ( ! fallthroughLabels.count( branchStmt->get_target() ) ) { |
---|
| 164 | SemanticError( branchStmt->location, toString( "'fallthrough' target must be a later case statement: ", originalTarget ) ); |
---|
| 165 | } |
---|
| 166 | return new BranchStmt( originalTarget, BranchStmt::Goto ); |
---|
| 167 | } |
---|
| 168 | break; |
---|
| 169 | case BranchStmt::FallThroughDefault: { |
---|
| 170 | // fallthrough default |
---|
| 171 | targetEntry = std::find_if( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), isFallthroughDefaultTarget ); |
---|
| 172 | |
---|
| 173 | // ensure that fallthrough is within a switch or choose |
---|
| 174 | if ( targetEntry == enclosingControlStructures.rend() ) { |
---|
| 175 | SemanticError( branchStmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" ); |
---|
| 176 | } // if |
---|
| 177 | |
---|
| 178 | // ensure that switch or choose has a default clause |
---|
| 179 | SwitchStmt * switchStmt = strict_dynamic_cast< SwitchStmt * >( targetEntry->get_controlStructure() ); |
---|
| 180 | bool foundDefault = false; |
---|
| 181 | for ( Statement * stmt : switchStmt->statements ) { |
---|
| 182 | CaseStmt * caseStmt = strict_dynamic_cast< CaseStmt * >( stmt ); |
---|
| 183 | if ( caseStmt->isDefault() ) { |
---|
| 184 | foundDefault = true; |
---|
| 185 | } // if |
---|
| 186 | } // for |
---|
| 187 | if ( ! foundDefault ) { |
---|
| 188 | SemanticError( branchStmt->location, "'fallthrough default' must be enclosed in a 'switch' or 'choose' control structure with a 'default' clause" ); |
---|
| 189 | } |
---|
| 190 | break; |
---|
| 191 | } |
---|
| 192 | |
---|
[af68f0a] | 193 | default: |
---|
| 194 | assert( false ); |
---|
| 195 | } // switch |
---|
[a08ba92] | 196 | |
---|
[27de955] | 197 | // branch error checks, get the appropriate label name and create a goto |
---|
| 198 | Label exitLabel; |
---|
[94e025a2] | 199 | switch ( branchStmt->type ) { |
---|
[a08ba92] | 200 | case BranchStmt::Break: |
---|
[e39aa0f] | 201 | assert( targetEntry->useBreakExit() != ""); |
---|
| 202 | exitLabel = targetEntry->useBreakExit(); |
---|
[be5aa1b] | 203 | break; |
---|
[a08ba92] | 204 | case BranchStmt::Continue: |
---|
[e39aa0f] | 205 | assert( targetEntry->useContExit() != ""); |
---|
| 206 | exitLabel = targetEntry->useContExit(); |
---|
[be5aa1b] | 207 | break; |
---|
[720a007] | 208 | case BranchStmt::FallThrough: |
---|
| 209 | assert( targetEntry->useFallExit() != ""); |
---|
| 210 | exitLabel = targetEntry->useFallExit(); |
---|
| 211 | break; |
---|
| 212 | case BranchStmt::FallThroughDefault: |
---|
| 213 | assert( targetEntry->useFallDefaultExit() != ""); |
---|
| 214 | exitLabel = targetEntry->useFallDefaultExit(); |
---|
| 215 | // check that fallthrough default comes before the default clause |
---|
| 216 | if ( ! targetEntry->isFallDefaultValid() ) { |
---|
| 217 | SemanticError( branchStmt->location, "'fallthrough default' must precede the 'default' clause" ); |
---|
| 218 | } |
---|
| 219 | break; |
---|
[a08ba92] | 220 | default: |
---|
[27de955] | 221 | assert(0); // shouldn't be here |
---|
[a08ba92] | 222 | } // switch |
---|
| 223 | |
---|
[33c4b81] | 224 | // add unused attribute to label to silence warnings |
---|
[94e025a2] | 225 | addUnused( targetEntry->get_controlStructure(), branchStmt->originalTarget ); |
---|
[33c4b81] | 226 | |
---|
[af68f0a] | 227 | // transform break/continue statements into goto to simplify later handling of branches |
---|
[ba3706f] | 228 | return new BranchStmt( exitLabel, BranchStmt::Goto ); |
---|
[a08ba92] | 229 | } |
---|
| 230 | |
---|
| 231 | Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) { |
---|
[d939274] | 232 | // ensure loop body is a block |
---|
[a08ba92] | 233 | CompoundStmt *newBody; |
---|
| 234 | if ( ! (newBody = dynamic_cast<CompoundStmt *>( bodyLoop )) ) { |
---|
[ba3706f] | 235 | newBody = new CompoundStmt(); |
---|
[a08ba92] | 236 | newBody->get_kids().push_back( bodyLoop ); |
---|
| 237 | } // if |
---|
| 238 | |
---|
[27de955] | 239 | // only generate these when needed |
---|
[a08ba92] | 240 | |
---|
[27de955] | 241 | if ( e.isContUsed() ) { |
---|
| 242 | // continue label goes in the body as the last statement |
---|
| 243 | std::list< Label > labels; labels.push_back( e.useContExit() ); |
---|
[e39aa0f] | 244 | newBody->get_kids().push_back( new NullStmt( labels ) ); |
---|
[adcc065] | 245 | } // if |
---|
[27de955] | 246 | |
---|
| 247 | if ( e.isBreakUsed() ) { |
---|
[adcc065] | 248 | // break label goes after the loop -- it'll get set by the outer mutator if we do this |
---|
[23b6f4d7] | 249 | set_breakLabel( e.useBreakExit() ); |
---|
[adcc065] | 250 | } // if |
---|
[a08ba92] | 251 | |
---|
| 252 | return newBody; |
---|
| 253 | } |
---|
| 254 | |
---|
[94e025a2] | 255 | template< typename LoopClass > |
---|
| 256 | void MLEMutator::prehandleLoopStmt( LoopClass * loopStmt ) { |
---|
| 257 | // remember this as the most recent enclosing loop, then mutate the body of the loop -- this will determine |
---|
| 258 | // whether brkLabel and contLabel are used with branch statements and will recursively do the same to nested |
---|
| 259 | // loops |
---|
| 260 | Label brkLabel = generator->newLabel("loopBreak", loopStmt); |
---|
| 261 | Label contLabel = generator->newLabel("loopContinue", loopStmt); |
---|
| 262 | enclosingControlStructures.push_back( Entry( loopStmt, brkLabel, contLabel ) ); |
---|
[720a007] | 263 | GuardAction( [this]() { enclosingControlStructures.pop_back(); } ); |
---|
[94e025a2] | 264 | } |
---|
| 265 | |
---|
| 266 | template< typename LoopClass > |
---|
| 267 | Statement * MLEMutator::posthandleLoopStmt( LoopClass * loopStmt ) { |
---|
| 268 | assert( ! enclosingControlStructures.empty() ); |
---|
| 269 | Entry &e = enclosingControlStructures.back(); |
---|
| 270 | // sanity check that the enclosing loops have been popped correctly |
---|
| 271 | assert ( e == loopStmt ); |
---|
| 272 | |
---|
| 273 | // this will take the necessary steps to add definitions of the previous two labels, if they are used. |
---|
[720a007] | 274 | loopStmt->body = mutateLoop( loopStmt->get_body(), e ); |
---|
[94e025a2] | 275 | return loopStmt; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | void MLEMutator::premutate( WhileStmt * whileStmt ) { |
---|
| 279 | return prehandleLoopStmt( whileStmt ); |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | void MLEMutator::premutate( ForStmt * forStmt ) { |
---|
| 283 | return prehandleLoopStmt( forStmt ); |
---|
[27de955] | 284 | } |
---|
| 285 | |
---|
[94e025a2] | 286 | Statement * MLEMutator::postmutate( WhileStmt * whileStmt ) { |
---|
| 287 | return posthandleLoopStmt( whileStmt ); |
---|
[27de955] | 288 | } |
---|
| 289 | |
---|
[94e025a2] | 290 | Statement * MLEMutator::postmutate( ForStmt * forStmt ) { |
---|
| 291 | return posthandleLoopStmt( forStmt ); |
---|
[adcc065] | 292 | } |
---|
| 293 | |
---|
[94e025a2] | 294 | void MLEMutator::premutate( IfStmt * ifStmt ) { |
---|
| 295 | // generate a label for breaking out of a labeled if |
---|
| 296 | bool labeledBlock = !(ifStmt->get_labels().empty()); |
---|
| 297 | if ( labeledBlock ) { |
---|
| 298 | Label brkLabel = generator->newLabel("blockBreak", ifStmt); |
---|
| 299 | enclosingControlStructures.push_back( Entry( ifStmt, brkLabel ) ); |
---|
[720a007] | 300 | GuardAction( [this]() { enclosingControlStructures.pop_back(); } ); |
---|
[94e025a2] | 301 | } // if |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | Statement * MLEMutator::postmutate( IfStmt * ifStmt ) { |
---|
| 305 | bool labeledBlock = !(ifStmt->get_labels().empty()); |
---|
| 306 | if ( labeledBlock ) { |
---|
| 307 | if ( ! enclosingControlStructures.back().useBreakExit().empty() ) { |
---|
| 308 | set_breakLabel( enclosingControlStructures.back().useBreakExit() ); |
---|
| 309 | } // if |
---|
| 310 | } // if |
---|
| 311 | return ifStmt; |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | void MLEMutator::premutate( CaseStmt *caseStmt ) { |
---|
| 315 | visit_children = false; |
---|
[720a007] | 316 | |
---|
| 317 | // mark default as seen before visiting its statements to catch default loops |
---|
| 318 | if ( caseStmt->isDefault() ) { |
---|
| 319 | enclosingControlStructures.back().seenDefault(); |
---|
| 320 | } // if |
---|
| 321 | |
---|
[94e025a2] | 322 | caseStmt->condition = maybeMutate( caseStmt->condition, *visitor ); |
---|
[720a007] | 323 | Label fallLabel = generator->newLabel( "fallThrough", caseStmt ); |
---|
| 324 | { |
---|
| 325 | // ensure that stack isn't corrupted by exceptions in fixBlock |
---|
| 326 | auto guard = makeFuncGuard( [&]() { enclosingControlStructures.push_back( Entry( caseStmt, fallLabel ) ); }, [this]() { enclosingControlStructures.pop_back(); } ); |
---|
| 327 | |
---|
| 328 | // empty case statement |
---|
| 329 | if( ! caseStmt->stmts.empty() ) { |
---|
| 330 | // the parser ensures that all statements in a case are grouped into a block |
---|
| 331 | CompoundStmt * block = strict_dynamic_cast< CompoundStmt * >( caseStmt->stmts.front() ); |
---|
| 332 | fixBlock( block->kids, true ); |
---|
| 333 | |
---|
| 334 | // add fallthrough label if necessary |
---|
| 335 | assert( ! enclosingControlStructures.empty() ); |
---|
| 336 | if ( enclosingControlStructures.back().isFallUsed() ) { |
---|
| 337 | std::list<Label> ls{ enclosingControlStructures.back().useFallExit() }; |
---|
| 338 | caseStmt->stmts.push_back( new NullStmt( ls ) ); |
---|
| 339 | } // if |
---|
| 340 | } // if |
---|
| 341 | } |
---|
| 342 | assert( ! enclosingControlStructures.empty() ); |
---|
| 343 | assertf( dynamic_cast<SwitchStmt *>( enclosingControlStructures.back().get_controlStructure() ), "Control structure enclosing a case clause must be a switch, but is: %s", toCString( enclosingControlStructures.back().get_controlStructure() ) ); |
---|
| 344 | if ( caseStmt->isDefault() ) { |
---|
| 345 | if ( enclosingControlStructures.back().isFallDefaultUsed() ) { |
---|
| 346 | // add fallthrough default label if necessary |
---|
| 347 | std::list<Label> ls{ enclosingControlStructures.back().useFallDefaultExit() }; |
---|
| 348 | caseStmt->stmts.push_front( new NullStmt( ls ) ); |
---|
| 349 | } // if |
---|
| 350 | } // if |
---|
[94e025a2] | 351 | } |
---|
| 352 | |
---|
| 353 | void MLEMutator::premutate( SwitchStmt *switchStmt ) { |
---|
| 354 | // generate a label for breaking out of a labeled switch |
---|
| 355 | Label brkLabel = generator->newLabel("switchBreak", switchStmt); |
---|
[720a007] | 356 | auto it = std::find_if( switchStmt->statements.rbegin(), switchStmt->statements.rend(), [](Statement * stmt) { |
---|
| 357 | CaseStmt * caseStmt = strict_dynamic_cast< CaseStmt * >( stmt ); |
---|
| 358 | return caseStmt->isDefault(); |
---|
| 359 | }); |
---|
| 360 | CaseStmt * defaultCase = it != switchStmt->statements.rend() ? strict_dynamic_cast<CaseStmt *>( *it ) : nullptr; |
---|
| 361 | Label fallDefaultLabel = defaultCase ? generator->newLabel( "fallThroughDefault", defaultCase ) : ""; |
---|
| 362 | enclosingControlStructures.push_back( Entry(switchStmt, brkLabel, fallDefaultLabel) ); |
---|
| 363 | GuardAction( [this]() { enclosingControlStructures.pop_back(); } ); |
---|
| 364 | |
---|
| 365 | // Collect valid labels for fallthrough. This is initially all labels at the same level as a case statement. |
---|
| 366 | // As labels are seen during traversal, they are removed, since fallthrough is not allowed to jump backwards. |
---|
| 367 | for ( Statement * stmt : switchStmt->statements ) { |
---|
| 368 | CaseStmt * caseStmt = strict_dynamic_cast< CaseStmt * >( stmt ); |
---|
| 369 | if ( caseStmt->stmts.empty() ) continue; |
---|
| 370 | CompoundStmt * block = dynamic_cast< CompoundStmt * >( caseStmt->stmts.front() ); |
---|
| 371 | for ( Statement * stmt : block->kids ) { |
---|
| 372 | for ( Label & l : stmt->labels ) { |
---|
| 373 | fallthroughLabels.insert( l ); |
---|
| 374 | } |
---|
| 375 | } |
---|
| 376 | } |
---|
[94e025a2] | 377 | } |
---|
| 378 | |
---|
| 379 | Statement * MLEMutator::postmutate( SwitchStmt * switchStmt ) { |
---|
| 380 | Entry &e = enclosingControlStructures.back(); |
---|
| 381 | assert ( e == switchStmt ); |
---|
| 382 | |
---|
| 383 | // only generate break label if labeled break is used |
---|
| 384 | if ( e.isBreakUsed() ) { |
---|
| 385 | // for the purposes of keeping switch statements uniform (i.e. all statements that are direct children of a |
---|
| 386 | // switch should be CastStmts), append the exit label + break to the last case statement; create a default |
---|
| 387 | // case if there are no cases |
---|
| 388 | std::list< Statement * > &statements = switchStmt->statements; |
---|
| 389 | if ( statements.empty() ) { |
---|
| 390 | statements.push_back( CaseStmt::makeDefault() ); |
---|
| 391 | } // if |
---|
| 392 | |
---|
| 393 | if ( CaseStmt * c = dynamic_cast< CaseStmt * >( statements.back() ) ) { |
---|
| 394 | Statement * stmt = new BranchStmt( Label("brkLabel"), BranchStmt::Break ); |
---|
| 395 | stmt->labels.push_back( e.useBreakExit() ); |
---|
| 396 | c->stmts.push_back( stmt ); |
---|
| 397 | } else assert(0); // as of this point, all statements of a switch are still CaseStmts |
---|
| 398 | } // if |
---|
| 399 | |
---|
| 400 | assert ( enclosingControlStructures.back() == switchStmt ); |
---|
| 401 | return switchStmt; |
---|
[a08ba92] | 402 | } |
---|
[51b7345] | 403 | } // namespace ControlStruct |
---|
[a08ba92] | 404 | |
---|
[51587aa] | 405 | // Local Variables: // |
---|
| 406 | // tab-width: 4 // |
---|
| 407 | // mode: c++ // |
---|
| 408 | // compile-command: "make install" // |
---|
| 409 | // End: // |
---|