Skip to content

[NFC] [CMake] Add -Wno-dangling-else for GCC built unittests #112817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 26, 2024

Conversation

jsji
Copy link
Member

@jsji jsji commented Oct 18, 2024

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)

The problem here is because these macros, eg: EXPECT_TRUE are expanded to a single line multi-statement code with if/else, which is indeed ambiguous after pre-processing. a simple example would be like: https://godbolt.org/z/4zjn56qrP

if(x)
 switch (0) case 0: default: if (...) ; else return;;

Given that omit braces in such cases is part of LLVM's style guide, and it is hard to add braces in gtest just for GCC's warning, add -Wno-dangling-else for GCC instead.

@llvmbot llvmbot added PGO Profile Guided Optimizations flang:openmp llvm:ir clang:openmp OpenMP related changes to Clang labels Oct 18, 2024
@jsji jsji requested review from goldsteinn, jh7370 and nikic October 18, 2024 03:13
@jsji jsji self-assigned this Oct 18, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 18, 2024

@llvm/pr-subscribers-llvm-ir

@llvm/pr-subscribers-flang-openmp

Author: Jinsong Ji (jsji)

Changes

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)


Full diff: https://github.com/llvm/llvm-project/pull/112817.diff

5 Files Affected:

  • (modified) llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp (+2-1)
  • (modified) llvm/unittests/IR/AttributesTest.cpp (+2-2)
  • (modified) llvm/unittests/IR/PatternMatch.cpp (+8-4)
  • (modified) llvm/unittests/Object/ELFObjectFileTest.cpp (+2-1)
  • (modified) llvm/unittests/ProfileData/CoverageMappingTest.cpp (+2-1)
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
   }

@llvmbot
Copy link
Member

llvmbot commented Oct 18, 2024

@llvm/pr-subscribers-pgo

Author: Jinsong Ji (jsji)

Changes

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)


Full diff: https://github.com/llvm/llvm-project/pull/112817.diff

5 Files Affected:

  • (modified) llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp (+2-1)
  • (modified) llvm/unittests/IR/AttributesTest.cpp (+2-2)
  • (modified) llvm/unittests/IR/PatternMatch.cpp (+8-4)
  • (modified) llvm/unittests/Object/ELFObjectFileTest.cpp (+2-1)
  • (modified) llvm/unittests/ProfileData/CoverageMappingTest.cpp (+2-1)
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
   }

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that omit braces in such cases is part of LLVM's style guide, I wonder whether it may be better to use -Wno-dangling-else when compiling gtest based code using GCC?

As Clang doesn't have this false positive, this warning will creep back in over time.

@teresajohnson
Copy link
Contributor

Given that omit braces in such cases is part of LLVM's style guide

Reference: https://llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-statement-bodies-of-if-else-loop-statements

I wonder whether it may be better to use -Wno-dangling-else when compiling gtest based code using GCC?

As Clang doesn't have this false positive, this warning will creep back in over time.

Agree. But where is the else in this case? I assume it is inside the gtest macros being invoked in the if statement bodies - might be better to add fixes there?

@jsji jsji requested a review from nikic October 18, 2024 13:48
jsji added 3 commits October 19, 2024 09:26
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)
@jsji
Copy link
Member Author

jsji commented Oct 21, 2024

But where is the else in this case? I assume it is inside the gtest macros being invoked in the if statement bodies - might be better to add fixes there?

I don't think macros inside gest will be become else of the if statement.

@jsji
Copy link
Member Author

jsji commented Oct 21, 2024

Given that omit braces in such cases is part of LLVM's style guide

Reference: https://llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-statement-bodies-of-if-else-loop-statements

I wonder whether it may be better to use -Wno-dangling-else when compiling gtest based code using GCC?
As Clang doesn't have this false positive, this warning will creep back in over time.

Agree. But where is the else in this case? I assume it is inside the gtest macros being invoked in the if statement bodies - might be better to add fixes there?

The problem here is because these macros, eg: EXPECT_TRUE are expanded to a single line multi-statement code with if/else, which is indeed ambiguous after pre-processing. a simple example would be like: https://godbolt.org/z/4zjn56qrP

    if(x)
     switch (0) case 0: default: if (x > 3) ; else return;;

I don't think it is realistic to change gtest code for such warnings. So, either disable the warnings, or add braces.

@jsji
Copy link
Member Author

jsji commented Oct 22, 2024

@nikic @teresajohnson Any further comments? Thanks.

Copy link
Contributor

@teresajohnson teresajohnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm but please update the title and description to reflect the new approach, and I suggested a more verbose comment below.

@@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you expand the comment to note that this is because of the gtest macro expansion.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Done. Thanks @teresajohnson !

@jsji jsji changed the title [NFC] Fix dangling-else warning [NFC] [CMake] Add -Wno-dangling-else for GCC built unittests Oct 26, 2024
@jsji jsji merged commit d72e711 into llvm:main Oct 26, 2024
4 of 7 checks passed
Copy link
Collaborator

@jh7370 jh7370 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address the typos I've highlighted in another commit.

Comment on lines +19 to +23
# if (...)
# EXPECT_TURE(...)
# will be expanded into something like:
# if(...)
# switch (0) case 0: default: if (...) ; else return;;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EXPECT_TURE -> EXPECT_TRUE
if(...) -> if (...)

# switch (0) case 0: default: if (...) ; else return;;
# GCC may emit false positive dangling-else warnings for such code.
# However, such warnings are actually against LLVM's style guide.
# disable the warning for GCC so that one can enbable Werror.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

disable -> Disable
enbable -> enable

I'd also do Werror -> -Werror to make it clear that this is an option.

NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
…2817)

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)

The problem here is because these macros, eg: EXPECT_TRUE are expanded
to a single line multi-statement code with if/else, which is indeed
ambiguous after pre-processing. a simple example would be like:
https://godbolt.org/z/4zjn56qrP

    if(x)
     switch (0) case 0: default: if (...) ; else return;;

Given that omit braces in such cases is part of LLVM's style guide, and
it is hard to add braces in gtest just for GCC's warning, add
-Wno-dangling-else for GCC instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:openmp OpenMP related changes to Clang flang:openmp llvm:ir PGO Profile Guided Optimizations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants