[llvm] [NFC] Fix dangling-else warning (PR #112817)

Jinsong Ji via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 18 06:48:50 PDT 2024


https://github.com/jsji updated https://github.com/llvm/llvm-project/pull/112817

>From ea33083c04b68dad30362568e780550ee1942310 Mon Sep 17 00:00:00 2001
From: Jinsong Ji <jinsong.ji at intel.com>
Date: Fri, 18 Oct 2024 05:12:05 +0200
Subject: [PATCH 1/3] [NFC] Fix dangling-else warning
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This is one of the many PRs to fix errors with LLVM_ENABLE_WERROR=on. Built by GCC 11.

Fix warnings:

llvm/unittests/ProfileData/CoverageMappingTest.cpp: In member function ‘virtual void {anonymous}::CoverageMappingTest_TVIdxBuilder_Test::TestBody()’:
llvm/unittests/ProfileData/CoverageMappingTest.cpp:1116:10: error: suggest explicit braces to avoid ambiguous ‘else’ [-Werror=dangling-else]
 1116 |       if (Node.NextIDs[C] < 0)
---
 llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp    |  3 ++-
 llvm/unittests/IR/AttributesTest.cpp               |  4 ++--
 llvm/unittests/IR/PatternMatch.cpp                 | 12 ++++++++----
 llvm/unittests/Object/ELFObjectFileTest.cpp        |  3 ++-
 llvm/unittests/ProfileData/CoverageMappingTest.cpp |  3 ++-
 5 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
index fe04cbbce12dcd..6b6ee1a3f2cbcf 100644
--- a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -2936,8 +2936,9 @@ TEST_F(OpenMPIRBuilderTest, CriticalDirective) {
   const DataLayout &DL = M->getDataLayout();
   const llvm::Align TypeAlign = DL.getABITypeAlign(CriticalNamePtrTy);
   const llvm::Align PtrAlign = DL.getPointerABIAlignment(GV->getAddressSpace());
-  if (const llvm::MaybeAlign Alignment = GV->getAlign())
+  if (const llvm::MaybeAlign Alignment = GV->getAlign()) {
     EXPECT_EQ(*Alignment, std::max(TypeAlign, PtrAlign));
+  }
 }
 
 TEST_F(OpenMPIRBuilderTest, OrderedDirectiveDependSource) {
diff --git a/llvm/unittests/IR/AttributesTest.cpp b/llvm/unittests/IR/AttributesTest.cpp
index f73f2b20e9fea5..97aaf42570d1fe 100644
--- a/llvm/unittests/IR/AttributesTest.cpp
+++ b/llvm/unittests/IR/AttributesTest.cpp
@@ -468,9 +468,9 @@ TEST(Attributes, SetIntersect) {
     AS0 = AttributeSet::get(C0, AB0);
     Res = AS0.intersectWith(C0, AS1);
     ASSERT_EQ(Res.has_value(), CanDrop);
-    if (CanDrop)
+    if (CanDrop) {
       ASSERT_FALSE(Res->hasAttributes());
-
+    }
     AS1 = AttributeSet::get(C1, AB0);
     Res = AS0.intersectWith(C0, AS1);
     ASSERT_TRUE(Res.has_value());
diff --git a/llvm/unittests/IR/PatternMatch.cpp b/llvm/unittests/IR/PatternMatch.cpp
index 367ba6ab52a596..e6a08fd713bdda 100644
--- a/llvm/unittests/IR/PatternMatch.cpp
+++ b/llvm/unittests/IR/PatternMatch.cpp
@@ -637,20 +637,23 @@ TEST_F(PatternMatchTest, CheckedInt) {
     CRes = nullptr;
     EXPECT_EQ(CheckUgt1(APVal), m_CheckedInt(CheckUgt1).match(C));
     EXPECT_EQ(CheckUgt1(APVal), m_CheckedInt(CRes, CheckUgt1).match(C));
-    if (CheckUgt1(APVal))
+    if (CheckUgt1(APVal)) {
       EXPECT_EQ(CRes, C);
+    }
 
     CRes = nullptr;
     EXPECT_EQ(CheckNonZero(APVal), m_CheckedInt(CheckNonZero).match(C));
     EXPECT_EQ(CheckNonZero(APVal), m_CheckedInt(CRes, CheckNonZero).match(C));
-    if (CheckNonZero(APVal))
+    if (CheckNonZero(APVal)) {
       EXPECT_EQ(CRes, C);
+    }
 
     CRes = nullptr;
     EXPECT_EQ(CheckPow2(APVal), m_CheckedInt(CheckPow2).match(C));
     EXPECT_EQ(CheckPow2(APVal), m_CheckedInt(CRes, CheckPow2).match(C));
-    if (CheckPow2(APVal))
+    if (CheckPow2(APVal)) {
       EXPECT_EQ(CRes, C);
+    }
 
   };
 
@@ -710,8 +713,9 @@ TEST_F(PatternMatchTest, CheckedInt) {
     EXPECT_EQ(Expec, m_CheckedInt(CRes, CheckFn).match(C));
     if (Expec) {
       EXPECT_NE(CRes, nullptr);
-      if (AllSame)
+      if (AllSame) {
         EXPECT_EQ(CRes, C);
+      }
     }
   };
   auto DoVecCheck = [&](ArrayRef<std::optional<int8_t>> Vals) {
diff --git a/llvm/unittests/Object/ELFObjectFileTest.cpp b/llvm/unittests/Object/ELFObjectFileTest.cpp
index c13dc0e3fab898..153ac560179985 100644
--- a/llvm/unittests/Object/ELFObjectFileTest.cpp
+++ b/llvm/unittests/Object/ELFObjectFileTest.cpp
@@ -1244,8 +1244,9 @@ TEST(ELFObjectFileTest, ReadPGOAnalysisMap) {
           }
           EXPECT_EQ(PGOAnalyses, *ExpectedPGO);
           for (auto &&[BB, PGO] : llvm::zip(*BBAddrMaps, PGOAnalyses)) {
-            if (PGO.FeatEnable.BBFreq || PGO.FeatEnable.BrProb)
+            if (PGO.FeatEnable.BBFreq || PGO.FeatEnable.BrProb) {
               EXPECT_EQ(BB.getNumBBEntries(), PGO.BBEntries.size());
+            }
           }
         }
       };
diff --git a/llvm/unittests/ProfileData/CoverageMappingTest.cpp b/llvm/unittests/ProfileData/CoverageMappingTest.cpp
index ef147674591c51..288bf8e254d498 100644
--- a/llvm/unittests/ProfileData/CoverageMappingTest.cpp
+++ b/llvm/unittests/ProfileData/CoverageMappingTest.cpp
@@ -1113,8 +1113,9 @@ TEST(CoverageMappingTest, TVIdxBuilder) {
     EXPECT_EQ(Node.Width, IndicesRefs[I].Width);
     for (int C = 0; C < 2; ++C) {
       auto Index = TheBuilder.Indices[I][C];
-      if (Node.NextIDs[C] < 0)
+      if (Node.NextIDs[C] < 0) {
         EXPECT_TRUE(Decisions.insert({Index, Node.Width}).second);
+      }
     }
 #endif
   }

>From 41b00378b9e4bbf5f3b921d750d21dfd26ca12e6 Mon Sep 17 00:00:00 2001
From: Jinsong Ji <jinsong.ji at intel.com>
Date: Fri, 18 Oct 2024 06:48:17 -0700
Subject: [PATCH 2/3] Revert "[NFC] Fix dangling-else warning"

This reverts commit ea33083c04b68dad30362568e780550ee1942310.
---
 llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp    |  3 +--
 llvm/unittests/IR/AttributesTest.cpp               |  4 ++--
 llvm/unittests/IR/PatternMatch.cpp                 | 12 ++++--------
 llvm/unittests/Object/ELFObjectFileTest.cpp        |  3 +--
 llvm/unittests/ProfileData/CoverageMappingTest.cpp |  3 +--
 5 files changed, 9 insertions(+), 16 deletions(-)

diff --git a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
index 6b6ee1a3f2cbcf..fe04cbbce12dcd 100644
--- a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -2936,9 +2936,8 @@ TEST_F(OpenMPIRBuilderTest, CriticalDirective) {
   const DataLayout &DL = M->getDataLayout();
   const llvm::Align TypeAlign = DL.getABITypeAlign(CriticalNamePtrTy);
   const llvm::Align PtrAlign = DL.getPointerABIAlignment(GV->getAddressSpace());
-  if (const llvm::MaybeAlign Alignment = GV->getAlign()) {
+  if (const llvm::MaybeAlign Alignment = GV->getAlign())
     EXPECT_EQ(*Alignment, std::max(TypeAlign, PtrAlign));
-  }
 }
 
 TEST_F(OpenMPIRBuilderTest, OrderedDirectiveDependSource) {
diff --git a/llvm/unittests/IR/AttributesTest.cpp b/llvm/unittests/IR/AttributesTest.cpp
index 97aaf42570d1fe..f73f2b20e9fea5 100644
--- a/llvm/unittests/IR/AttributesTest.cpp
+++ b/llvm/unittests/IR/AttributesTest.cpp
@@ -468,9 +468,9 @@ TEST(Attributes, SetIntersect) {
     AS0 = AttributeSet::get(C0, AB0);
     Res = AS0.intersectWith(C0, AS1);
     ASSERT_EQ(Res.has_value(), CanDrop);
-    if (CanDrop) {
+    if (CanDrop)
       ASSERT_FALSE(Res->hasAttributes());
-    }
+
     AS1 = AttributeSet::get(C1, AB0);
     Res = AS0.intersectWith(C0, AS1);
     ASSERT_TRUE(Res.has_value());
diff --git a/llvm/unittests/IR/PatternMatch.cpp b/llvm/unittests/IR/PatternMatch.cpp
index e6a08fd713bdda..367ba6ab52a596 100644
--- a/llvm/unittests/IR/PatternMatch.cpp
+++ b/llvm/unittests/IR/PatternMatch.cpp
@@ -637,23 +637,20 @@ TEST_F(PatternMatchTest, CheckedInt) {
     CRes = nullptr;
     EXPECT_EQ(CheckUgt1(APVal), m_CheckedInt(CheckUgt1).match(C));
     EXPECT_EQ(CheckUgt1(APVal), m_CheckedInt(CRes, CheckUgt1).match(C));
-    if (CheckUgt1(APVal)) {
+    if (CheckUgt1(APVal))
       EXPECT_EQ(CRes, C);
-    }
 
     CRes = nullptr;
     EXPECT_EQ(CheckNonZero(APVal), m_CheckedInt(CheckNonZero).match(C));
     EXPECT_EQ(CheckNonZero(APVal), m_CheckedInt(CRes, CheckNonZero).match(C));
-    if (CheckNonZero(APVal)) {
+    if (CheckNonZero(APVal))
       EXPECT_EQ(CRes, C);
-    }
 
     CRes = nullptr;
     EXPECT_EQ(CheckPow2(APVal), m_CheckedInt(CheckPow2).match(C));
     EXPECT_EQ(CheckPow2(APVal), m_CheckedInt(CRes, CheckPow2).match(C));
-    if (CheckPow2(APVal)) {
+    if (CheckPow2(APVal))
       EXPECT_EQ(CRes, C);
-    }
 
   };
 
@@ -713,9 +710,8 @@ TEST_F(PatternMatchTest, CheckedInt) {
     EXPECT_EQ(Expec, m_CheckedInt(CRes, CheckFn).match(C));
     if (Expec) {
       EXPECT_NE(CRes, nullptr);
-      if (AllSame) {
+      if (AllSame)
         EXPECT_EQ(CRes, C);
-      }
     }
   };
   auto DoVecCheck = [&](ArrayRef<std::optional<int8_t>> Vals) {
diff --git a/llvm/unittests/Object/ELFObjectFileTest.cpp b/llvm/unittests/Object/ELFObjectFileTest.cpp
index 153ac560179985..c13dc0e3fab898 100644
--- a/llvm/unittests/Object/ELFObjectFileTest.cpp
+++ b/llvm/unittests/Object/ELFObjectFileTest.cpp
@@ -1244,9 +1244,8 @@ TEST(ELFObjectFileTest, ReadPGOAnalysisMap) {
           }
           EXPECT_EQ(PGOAnalyses, *ExpectedPGO);
           for (auto &&[BB, PGO] : llvm::zip(*BBAddrMaps, PGOAnalyses)) {
-            if (PGO.FeatEnable.BBFreq || PGO.FeatEnable.BrProb) {
+            if (PGO.FeatEnable.BBFreq || PGO.FeatEnable.BrProb)
               EXPECT_EQ(BB.getNumBBEntries(), PGO.BBEntries.size());
-            }
           }
         }
       };
diff --git a/llvm/unittests/ProfileData/CoverageMappingTest.cpp b/llvm/unittests/ProfileData/CoverageMappingTest.cpp
index 288bf8e254d498..ef147674591c51 100644
--- a/llvm/unittests/ProfileData/CoverageMappingTest.cpp
+++ b/llvm/unittests/ProfileData/CoverageMappingTest.cpp
@@ -1113,9 +1113,8 @@ TEST(CoverageMappingTest, TVIdxBuilder) {
     EXPECT_EQ(Node.Width, IndicesRefs[I].Width);
     for (int C = 0; C < 2; ++C) {
       auto Index = TheBuilder.Indices[I][C];
-      if (Node.NextIDs[C] < 0) {
+      if (Node.NextIDs[C] < 0)
         EXPECT_TRUE(Decisions.insert({Index, Node.Width}).second);
-      }
     }
 #endif
   }

>From 376724180bc1de39a3020282c82921c8455fe37a Mon Sep 17 00:00:00 2001
From: Jinsong Ji <jinsong.ji at intel.com>
Date: Fri, 18 Oct 2024 06:47:20 -0700
Subject: [PATCH 3/3] disable dangling else for gcc

---
 llvm/unittests/CMakeLists.txt | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/llvm/unittests/CMakeLists.txt b/llvm/unittests/CMakeLists.txt
index 911ede701982f6..1056b7f60e0b00 100644
--- a/llvm/unittests/CMakeLists.txt
+++ b/llvm/unittests/CMakeLists.txt
@@ -14,6 +14,11 @@ function(add_llvm_target_unittest test_dir_name)
   add_llvm_unittest(${test_dir_name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
 endfunction()
 
+# GCC may emit false positive warnings against LLVM's style guide.
+if (CMAKE_COMPILER_IS_GNUCXX)
+  list(APPEND LLVM_COMPILE_FLAGS "-Wno-dangling-else")
+endif ()
+
 add_subdirectory(ADT)
 add_subdirectory(Analysis)
 add_subdirectory(AsmParser)



More information about the llvm-commits mailing list