[clang] 843390c - Apply proper source location to fallthrough switch cases.

Adrian Prantl via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 17 14:45:12 PDT 2021


Author: Adrian Prantl
Date: 2021-09-17T14:45:04-07:00
New Revision: 843390c58ae660fa356c534fdc8b19756f8d175c

URL: https://github.com/llvm/llvm-project/commit/843390c58ae660fa356c534fdc8b19756f8d175c
DIFF: https://github.com/llvm/llvm-project/commit/843390c58ae660fa356c534fdc8b19756f8d175c.diff

LOG: Apply proper source location to fallthrough switch cases.

This fixes a bug in clang where, when clang sees a switch with a
fallthrough to a default like this:

static void funcA(void) {}
static void funcB(void) {}

int main(int argc, char **argv) {

switch (argc) {
    case 0:
        funcA();
        break;
    case 10:
    default:
        funcB();
        break;
}
}

It does not add a proper debug location for that switch case, such as
case 10: above.

Patch by Shubham Rastogi!

Differential Revision: https://reviews.llvm.org/D109940

Added: 
    clang/test/CodeGen/debug-info-switch-fallthrough.c

Modified: 
    clang/lib/CodeGen/CGStmt.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index 08a4a6751083..373179349fb0 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -1521,6 +1521,12 @@ void CodeGenFunction::EmitCaseStmt(const CaseStmt &S,
     NextCase = dyn_cast<CaseStmt>(CurCase->getSubStmt());
   }
 
+  // Generate a stop point for debug info if the case statement is
+  // followed by a default statement. A fallthrough case before a
+  // default case gets its own branch target.
+  if (CurCase->getSubStmt()->getStmtClass() == Stmt::DefaultStmtClass)
+    EmitStopPoint(CurCase);
+
   // Normal default recursion for non-cases.
   EmitStmt(CurCase->getSubStmt());
 }

diff  --git a/clang/test/CodeGen/debug-info-switch-fallthrough.c b/clang/test/CodeGen/debug-info-switch-fallthrough.c
new file mode 100644
index 000000000000..36ac371d8270
--- /dev/null
+++ b/clang/test/CodeGen/debug-info-switch-fallthrough.c
@@ -0,0 +1,17 @@
+// RUN:  %clang_cc1 -triple x86_64-apple-macosx11.0.0 -debug-info-kind=standalone -emit-llvm %s -o - | FileCheck %s
+// CHECK: ], !dbg !{{[0-9]+}}
+// CHECK-EMPTY:
+// CHECK-NEXT: {{.+}}
+// CHECK-NEXT: br {{.+}}, !dbg !{{[0-9+]}}
+// CHECK-EMPTY:
+// CHECK-NEXT: {{.+}}
+// CHECK-NEXT: br {{.+}}, !dbg ![[LOC:[0-9]+]]
+void test(int num) {
+  switch (num) {
+  case 0:
+    break;
+  case 10: // CHECK: ![[LOC]] = !DILocation(line: [[@LINE]], column:{{.+}}, scope: {{.+}})
+  default:
+    break;
+  }
+}


        


More information about the cfe-commits mailing list