-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
@llvm/pr-subscribers-llvm-ir @llvm/pr-subscribers-flang-openmp Author: Jinsong Ji (jsji) ChangesThis 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()’: Full diff: https://github.com/llvm/llvm-project/pull/112817.diff 5 Files Affected:
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
}
|
@llvm/pr-subscribers-pgo Author: Jinsong Ji (jsji) ChangesThis 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()’: Full diff: https://github.com/llvm/llvm-project/pull/112817.diff 5 Files Affected:
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
}
|
There was a problem hiding this 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.
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? |
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)
This reverts commit ea33083.
I don't think macros inside gest will be become |
The problem here is because these macros, eg:
I don't think it is realistic to change gtest code for such warnings. So, either disable the warnings, or add braces. |
@nikic @teresajohnson Any further comments? Thanks. |
There was a problem hiding this 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.
llvm/unittests/CMakeLists.txt
Outdated
@@ -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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Done. Thanks @teresajohnson !
There was a problem hiding this 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.
# if (...) | ||
# EXPECT_TURE(...) | ||
# will be expanded into something like: | ||
# if(...) | ||
# switch (0) case 0: default: if (...) ; else return;; |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
…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.
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
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.