Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Copy link
Contributor

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.

Copy link
Collaborator

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".

Copy link
Contributor Author

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?

"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.
24 changes: 24 additions & 0 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
38 changes: 38 additions & 0 deletions clang/test/Sema/warn-enum-compare-typo.c
Original file line number Diff line number Diff line change
@@ -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
Loading