-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang][diagnostics] added warning for possible enum compare typo #168445
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
[clang][diagnostics] added warning for possible enum compare typo #168445
Conversation
|
@llvm/pr-subscribers-clang Author: None (GrumpyPigSkin) ChangesAdded diagnosis and fixit comment for possible accidental comparison operator in an enum. Closes: #168146 Full diff: https://github.com/llvm/llvm-project/pull/168445.diff 3 Files Affected:
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 3e864475f22a1..b4da42584c78f 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -13707,4 +13707,14 @@ def err_amdgcn_load_lds_size_invalid_value : Error<"invalid size value">;
def note_amdgcn_load_lds_size_valid_value : Note<"size must be %select{1, 2, or 4|1, 2, 4, 12 or 16}0">;
def err_amdgcn_coop_atomic_invalid_as : Error<"cooperative atomic requires a global or generic pointer">;
+
+def warn_comparison_in_enum_initializer
+ : Warning<"comparison operator '%0' in enumerator constant is likely a "
+ "typo for "
+ "the shift operator '%1'">,
+ InGroup<DiagGroup<"enum-compare-typo">>;
+
+def note_enum_compare_typo_suggest
+ : Note<"use '%0' to perform a bitwise shift">;
+
} // end of sema component.
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 086dd8ba1c670..ae3e6f2f49917 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -20129,6 +20129,30 @@ EnumConstantDecl *Sema::CheckEnumConstant(EnumDecl *Enum,
if (Val)
Val = DefaultLvalueConversion(Val).get();
+ if (Val) {
+ if (const BinaryOperator *BinOp =
+ dyn_cast<BinaryOperator>(Val->IgnoreParenImpCasts())) {
+ if (BinOp->getOpcode() == BO_LT || BinOp->getOpcode() == BO_GT) {
+ const Expr *LHS = BinOp->getLHS()->IgnoreParenImpCasts();
+ if (const auto *IntLiteral = dyn_cast<IntegerLiteral>(LHS)) {
+ if (IntLiteral->getValue() == 1) {
+ auto suggestedOp = (BinOp->getOpcode() == BO_LT)
+ ? BinaryOperator::getOpcodeStr(BO_Shl)
+ : BinaryOperator::getOpcodeStr(BO_Shr);
+ SourceLocation OperatorLoc = BinOp->getOperatorLoc();
+
+ Diag(OperatorLoc, diag::warn_comparison_in_enum_initializer)
+ << BinOp->getOpcodeStr() << suggestedOp;
+
+ Diag(OperatorLoc, diag::note_enum_compare_typo_suggest)
+ << suggestedOp
+ << FixItHint::CreateReplacement(OperatorLoc, suggestedOp);
+ }
+ }
+ }
+ }
+ }
+
if (Val) {
if (Enum->isDependentType() || Val->isTypeDependent() ||
Val->containsErrors())
diff --git a/clang/test/Sema/warn-enum-compare-typo.c b/clang/test/Sema/warn-enum-compare-typo.c
new file mode 100644
index 0000000000000..ee6ef0676b4b8
--- /dev/null
+++ b/clang/test/Sema/warn-enum-compare-typo.c
@@ -0,0 +1,38 @@
+// RUN: %clang_cc1 -fsyntax-only -Wenum-compare-typo -verify %s
+// RUN: %clang_cc1 -fsyntax-only -Wenum-compare-typo -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
+
+enum E {
+ kOptionGood1 = 1ull << 0,
+ kOptionGood2 = 1ull >> 0,
+ // expected-warning@+3 {{comparison operator '<' in enumerator constant is likely a typo for the shift operator '<<'}}
+ // expected-note@+2 {{use '<<' to perform a bitwise shift}}
+ // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:22-[[@LINE+1]]:23}:"<<"
+ kOptionBad1 = 1ull < 1,
+ // expected-warning@+3 {{comparison operator '>' in enumerator constant is likely a typo for the shift operator '>>'}}
+ // expected-note@+2 {{use '>>' to perform a bitwise shift}}
+ // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:19-[[@LINE+1]]:20}:">>"
+ kOptionBad2 = 1 > 3,
+ // expected-warning@+3 {{comparison operator '>' in enumerator constant is likely a typo for the shift operator '>>'}}
+ // expected-note@+2 {{use '>>' to perform a bitwise shift}}
+ // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:20-[[@LINE+1]]:21}:">>"
+ kOptionBad3 = (1 > 2)
+};
+
+// Ensure the warning does not fire on valid code
+enum F {
+ kSomeValue = 10,
+ kComparison = kSomeValue > 5, // No warning
+ kMaxEnum = 255,
+ kIsValid = kMaxEnum > 0, // No warning
+ kSum = 10 + 20, // No warning
+ kShift = 2 << 5 // No warning
+};
+
+// Ensure the diagnostic group works
+
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wenum-compare-typo"
+enum G {
+ kIgnored = 1 < 10 // No warning
+};
+#pragma clang diagnostic pop
|
|
@AaronBallman Please could you advise on who is best to review :) |
🐧 Linux x64 Test Results
|
ojhunt
left a comment
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.
Conceptually a good idea, but I think it could be re-worked such that it is more accurate - avoiding both false-positives and negatives.
Also needs test coverage for other common idioms as we don't want a warning that fires all the time on valid/idiomatic code.
| def err_amdgcn_coop_atomic_invalid_as : Error<"cooperative atomic requires a global or generic pointer">; | ||
|
|
||
| def warn_comparison_in_enum_initializer | ||
| : Warning<"comparison operator '%0' in enumerator constant is likely a " |
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.
My inclination is for "may" rather than "likely", there also needs to be a way to suppress this warning locally - having to put pragmas around blocks is error prone, and using a global -Wno- flag loses the warning for all code.
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.
I would say "is potentially a typo for a shift operator '%1'"
As for the suppression, I think an explicit cast to bool would be a reasonable marker of "I meant to do that".
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.
I have gone with "is potentially a typo for a shift operator '%1'".
I used any explicit cast to suppress the warning, is that fine?
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
AaronBallman
left a comment
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.
Thanks! You should also add a release note to clang/docs/ReleaseNotes.rst so users know about the improved diagnostic.
AaronBallman
left a comment
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!
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/166/builds/4081 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/27/builds/19207 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/134/builds/30168 Here is the relevant piece of the build log for the reference |
Added diagnosis and fixit comment for possible accidental comparison operator in an enum.
Closes: #168146