kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

6_8_06_01_goto_label_in_dead_branch.c (1290B)


      1 /* Regression: a goto label whose FIRST mention is inside a constant-false
      2  * (codegen-suppressed) `if` must still receive a real CG-label id, not the
      3  * suppression sentinel. Previously the sentinel aliased the function's first
      4  * real label — here the `switch` dispatch — and the -O0 native emitter aborted
      5  * with "MCEmitter: label N placed twice". Mirrors yyjson's check_str_len /
      6  * fail_alloc shape (`if ((sizeof(...) < 8) && rt) goto fail;`). */
      7 
      8 static int classify(int sel, unsigned long n) {
      9   switch (sel) { /* dispatch is the function's first real label */
     10     case 1:
     11       /* sizeof(char) is 1 on every target, so the && folds to constant
     12        * false: this `goto fail` is parsed suppressed and is the first
     13        * reference to `fail`. */
     14       if ((sizeof(char) > 1) && (n >= 1234)) goto fail;
     15       return 10;
     16     case 2:
     17       if (!n) goto fail; /* emit-enabled goto to the same label */
     18       return 20;
     19     default:
     20       goto fail;
     21   }
     22 fail:
     23   return -1;
     24 }
     25 
     26 int test_main(void) {
     27   int acc = 0;
     28   acc += classify(1, 5);                  /* normal case-1 path: 10 */
     29   acc += (classify(2, 0) == -1) ? 100 : 0; /* if(!n) goto fail: +100 */
     30   acc += (classify(9, 0) == -1) ? 1 : 0;   /* default -> fail: +1 */
     31   return acc;                              /* 111 */
     32 }