[clang] af66563 - [clang] fix -Wuninitialized for asm goto outputs on indirect edges.

Nick Desaulniers via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 16 18:04:08 PST 2023


Author: Nick Desaulniers
Date: 2023-02-16T17:58:35-08:00
New Revision: af6656338db365fde6c0b0e53db8f98aa8ed59d4

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

LOG: [clang] fix -Wuninitialized for asm goto outputs on indirect edges.

Now that we support outputs from asm goto along indirect edges, we can
remove/revert some code that was added to help warn about the previous
limitation that outputs were not supported along indirect edges.

Reverts some code added in:
commit 72aa619a7fe0 ("Warn of uninitialized variables on asm goto's indirect branch")
commit 3a604fdbcd5f ("[Clang][CFG] check children statements of asm goto")
But keeps+updates the tests.

Link: https://github.com/llvm/llvm-project/issues/53562

Reviewed By: void

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

Added: 
    

Modified: 
    clang/lib/Analysis/UninitializedValues.cpp
    clang/test/Analysis/uninit-asm-goto.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Analysis/UninitializedValues.cpp b/clang/lib/Analysis/UninitializedValues.cpp
index 2437095a22cf5..87765db0c5b2f 100644
--- a/clang/lib/Analysis/UninitializedValues.cpp
+++ b/clang/lib/Analysis/UninitializedValues.cpp
@@ -586,28 +586,6 @@ class TransferFunctions : public StmtVisitor<TransferFunctions> {
           continue;
         }
 
-        if (AtPredExit == MayUninitialized) {
-          // If the predecessor's terminator is an "asm goto" that initializes
-          // the variable, then don't count it as "initialized" on the indirect
-          // paths.
-          CFGTerminator term = Pred->getTerminator();
-          if (const auto *as = dyn_cast_or_null<GCCAsmStmt>(term.getStmt())) {
-            const CFGBlock *fallthrough = *Pred->succ_begin();
-            if (as->isAsmGoto() &&
-                llvm::any_of(as->outputs(), [&](const Expr *output) {
-                    return vd == findVar(output).getDecl() &&
-                        llvm::any_of(as->labels(),
-                                     [&](const AddrLabelExpr *label) {
-                          return label->getLabel()->getStmt() == B->Label &&
-                              B != fallthrough;
-                        });
-                })) {
-              Use.setUninitAfterDecl();
-              continue;
-            }
-          }
-        }
-
         unsigned &SV = SuccsVisited[Pred->getBlockID()];
         if (!SV) {
           // When visiting the first successor of a block, mark all NULL
@@ -820,7 +798,8 @@ void TransferFunctions::VisitGCCAsmStmt(GCCAsmStmt *as) {
     // it's used on an indirect path, where it's not guaranteed to be
     // defined.
     if (const VarDecl *VD = findVar(Ex).getDecl())
-      vals[VD] = MayUninitialized;
+      if (vals[VD] != Initialized)
+        vals[VD] = MayUninitialized;
   }
 }
 

diff  --git a/clang/test/Analysis/uninit-asm-goto.cpp b/clang/test/Analysis/uninit-asm-goto.cpp
index 1b9d1689b036f..be1a2b3b01dd4 100644
--- a/clang/test/Analysis/uninit-asm-goto.cpp
+++ b/clang/test/Analysis/uninit-asm-goto.cpp
@@ -9,9 +9,9 @@ int test1(int x) {
     return -1;
 }
 
+// test2: Expect no diagnostics
 int test2(int x) {
-  int y; // expected-warning {{variable 'y' is used uninitialized whenever its declaration is reached}}
-         // expected-note at -1 {{initialize the variable}}
+  int y;
   if (x < 42)
     asm goto("" : "+S"(x), "+D"(y) : "r"(x) :: indirect_1, indirect_2);
   else
@@ -20,29 +20,29 @@ int test2(int x) {
 indirect_1:
   return -42;
 indirect_2:
-  return y; // expected-note {{uninitialized use occurs here}}
+  return y;
 }
 
+// test3: Expect no diagnostics
 int test3(int x) {
-  int y; // expected-warning {{variable 'y' is used uninitialized whenever its declaration is reached}}
-         // expected-note at -1 {{initialize the variable}}
+  int y;
   asm goto("" : "=&r"(y) : "r"(x) : : fail);
 normal:
   y += x;
   return y;
   if (x) {
 fail:
-    return y; // expected-note {{uninitialized use occurs here}}
+    return y;
   }
   return 0;
 }
 
+// test4: Expect no diagnostics
 int test4(int x) {
-  int y; // expected-warning {{variable 'y' is used uninitialized whenever its declaration is reached}}
-         // expected-note at -1 {{initialize the variable}}
+  int y;
   goto forward;
 backward:
-  return y; // expected-note {{uninitialized use occurs here}}
+  return y;
 forward:
   asm goto("" : "=r"(y) : "r"(x) : : backward);
   return y;
@@ -70,23 +70,40 @@ int test6(unsigned int *x) {
   return -1;
 }
 
+// test7: Expect no diagnostics.
 int test7(int z) {
-    int x; // expected-warning {{variable 'x' is used uninitialized whenever its declaration is reached}}
-           // expected-note at -1 {{initialize the variable 'x' to silence this warning}}
+    int x;
     if (z)
       asm goto ("":"=r"(x):::A1,A2);
     return 0;
     A1:
     A2:
-    return x; // expected-note {{uninitialized use occurs here}}
+    return x;
 }
 
+// test8: Expect no diagnostics
 int test8() {
-    int x = 0; // expected-warning {{variable 'x' is used uninitialized whenever its declaration is reached}}
-               // expected-note at -1 {{variable 'x' is declared here}}
+    int x = 0;
     asm goto ("":"=r"(x):::A1,A2);
     return 0;
     A1:
     A2:
-    return x; // expected-note {{uninitialized use occurs here}}
+    return x;
+}
+
+// test9: Expect no diagnostics
+int test9 (int x) {
+    int y;
+    asm goto("": "=r"(y) :::out);
+    return 42;
+out:
+    return y;
+}
+
+int test10() {
+  int y; // expected-note {{initialize the variable 'y' to silence this warning}}
+  asm goto(""::::out);
+  return 42;
+out:
+  return y; // expected-warning {{variable 'y' is uninitialized when used here}}
 }


        


More information about the cfe-commits mailing list