nickdesaulniers created this revision.
Herald added a project: All.
nickdesaulniers requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Otherwise we observe false positive warnings claiming that an asm goto
cannot jump to another label in a different scope when that label isn't
even a target of the asm goto.

This was a bug introduced back in initial support for asm goto,
commit b8fee677bf8e ("Re-check in clang support gun asm goto after fixing 
tests.")


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155342

Files:
  clang/lib/Sema/JumpDiagnostics.cpp
  clang/test/Sema/asm-goto.cpp


Index: clang/test/Sema/asm-goto.cpp
===================================================================
--- clang/test/Sema/asm-goto.cpp
+++ clang/test/Sema/asm-goto.cpp
@@ -59,3 +59,13 @@
 loop:
   return 0;
 }
+
+void test4cleanup(int*);
+// No errors expected.
+void test4(void) {
+  asm goto(""::::l0);
+l0:;
+  int x __attribute__((cleanup(test4cleanup)));
+  asm goto(""::::l1);
+l1:;
+}
Index: clang/lib/Sema/JumpDiagnostics.cpp
===================================================================
--- clang/lib/Sema/JumpDiagnostics.cpp
+++ clang/lib/Sema/JumpDiagnostics.cpp
@@ -370,7 +370,7 @@
         // have it.  This makes the second scan not have to walk the AST again.
         LabelAndGotoScopes[S] = ParentScope;
         AsmJumps.push_back(GS);
-        for (auto *E : GS->labels())
+        for (AddrLabelExpr *E : GS->labels())
           AsmJumpTargets.push_back(E->getLabel());
       }
     break;
@@ -788,6 +788,17 @@
     // Walk through all the jump sites, checking that they can trivially
     // reach this label scope.
     for (auto [JumpScope, JumpStmt] : JumpScopes) {
+      // This unnecessary copy is because:
+      // warning: captured structured bindings are a C++20 extension
+      // [-Wc++20-extensions]
+      LabelDecl *TL = TargetLabel;
+      // Is TargetLabel one of the targets of the JumpStmt? If not, then skip
+      // it.
+      if (IsAsmGoto &&
+          llvm::none_of(cast<GCCAsmStmt>(JumpStmt)->labels(),
+                        [=](AddrLabelExpr *E) { return E->getLabel() == TL; }))
+        continue;
+
       unsigned Scope = JumpScope;
       // Walk out the "scope chain" for this scope, looking for a scope
       // we've marked reachable.  For well-formed code this amortizes


Index: clang/test/Sema/asm-goto.cpp
===================================================================
--- clang/test/Sema/asm-goto.cpp
+++ clang/test/Sema/asm-goto.cpp
@@ -59,3 +59,13 @@
 loop:
   return 0;
 }
+
+void test4cleanup(int*);
+// No errors expected.
+void test4(void) {
+  asm goto(""::::l0);
+l0:;
+  int x __attribute__((cleanup(test4cleanup)));
+  asm goto(""::::l1);
+l1:;
+}
Index: clang/lib/Sema/JumpDiagnostics.cpp
===================================================================
--- clang/lib/Sema/JumpDiagnostics.cpp
+++ clang/lib/Sema/JumpDiagnostics.cpp
@@ -370,7 +370,7 @@
         // have it.  This makes the second scan not have to walk the AST again.
         LabelAndGotoScopes[S] = ParentScope;
         AsmJumps.push_back(GS);
-        for (auto *E : GS->labels())
+        for (AddrLabelExpr *E : GS->labels())
           AsmJumpTargets.push_back(E->getLabel());
       }
     break;
@@ -788,6 +788,17 @@
     // Walk through all the jump sites, checking that they can trivially
     // reach this label scope.
     for (auto [JumpScope, JumpStmt] : JumpScopes) {
+      // This unnecessary copy is because:
+      // warning: captured structured bindings are a C++20 extension
+      // [-Wc++20-extensions]
+      LabelDecl *TL = TargetLabel;
+      // Is TargetLabel one of the targets of the JumpStmt? If not, then skip
+      // it.
+      if (IsAsmGoto &&
+          llvm::none_of(cast<GCCAsmStmt>(JumpStmt)->labels(),
+                        [=](AddrLabelExpr *E) { return E->getLabel() == TL; }))
+        continue;
+
       unsigned Scope = JumpScope;
       // Walk out the "scope chain" for this scope, looking for a scope
       // we've marked reachable.  For well-formed code this amortizes
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to