AshleyRoll updated this revision to Diff 434250. AshleyRoll added a comment.
Rebased to remove clangd test failure Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D126960/new/ https://reviews.llvm.org/D126960 Files: clang/lib/Sema/SemaChecking.cpp clang/test/Sema/compare.c Index: clang/test/Sema/compare.c =================================================================== --- clang/test/Sema/compare.c +++ clang/test/Sema/compare.c @@ -419,3 +419,25 @@ if (x == y) x = y; // no warning if (y == x) y = x; // no warning } + +int warn_on_different_sign_after_unary_operator(unsigned a, int b) { + return + // unary not promotes boolean to int + (a > ~(!b)) // expected-warning {{comparison of integers of different signs: 'unsigned int' and 'int'}} + && + (a > -(b)) // expected-warning {{comparison of integers of different signs: 'unsigned int' and 'int'}} + && + (a > ++b) // expected-warning {{comparison of integers of different signs: 'unsigned int' and 'int'}} + && + (a > --b) // expected-warning {{comparison of integers of different signs: 'unsigned int' and 'int'}} + && + // unary not promotes boolean to int + (b > ~(!a)) // no warning + && + (b > -(a)) // expected-warning {{comparison of integers of different signs: 'int' and 'unsigned int'}} + && + (b > ++a) // expected-warning {{comparison of integers of different signs: 'int' and 'unsigned int'}} + && + (b > --a) // expected-warning {{comparison of integers of different signs: 'int' and 'unsigned int'}} + ; +} Index: clang/lib/Sema/SemaChecking.cpp =================================================================== --- clang/lib/Sema/SemaChecking.cpp +++ clang/lib/Sema/SemaChecking.cpp @@ -12326,6 +12326,13 @@ case UO_AddrOf: // should be impossible return IntRange::forValueOfType(C, GetExprType(E)); + case UO_Not: + // unary not promotes boolean to integer + if (UO->getSubExpr()->isKnownToHaveBooleanValue()) + return IntRange(MaxWidth, false); + + LLVM_FALLTHROUGH; + default: return GetExprRange(C, UO->getSubExpr(), MaxWidth, InConstantContext, Approximate);
Index: clang/test/Sema/compare.c =================================================================== --- clang/test/Sema/compare.c +++ clang/test/Sema/compare.c @@ -419,3 +419,25 @@ if (x == y) x = y; // no warning if (y == x) y = x; // no warning } + +int warn_on_different_sign_after_unary_operator(unsigned a, int b) { + return + // unary not promotes boolean to int + (a > ~(!b)) // expected-warning {{comparison of integers of different signs: 'unsigned int' and 'int'}} + && + (a > -(b)) // expected-warning {{comparison of integers of different signs: 'unsigned int' and 'int'}} + && + (a > ++b) // expected-warning {{comparison of integers of different signs: 'unsigned int' and 'int'}} + && + (a > --b) // expected-warning {{comparison of integers of different signs: 'unsigned int' and 'int'}} + && + // unary not promotes boolean to int + (b > ~(!a)) // no warning + && + (b > -(a)) // expected-warning {{comparison of integers of different signs: 'int' and 'unsigned int'}} + && + (b > ++a) // expected-warning {{comparison of integers of different signs: 'int' and 'unsigned int'}} + && + (b > --a) // expected-warning {{comparison of integers of different signs: 'int' and 'unsigned int'}} + ; +} Index: clang/lib/Sema/SemaChecking.cpp =================================================================== --- clang/lib/Sema/SemaChecking.cpp +++ clang/lib/Sema/SemaChecking.cpp @@ -12326,6 +12326,13 @@ case UO_AddrOf: // should be impossible return IntRange::forValueOfType(C, GetExprType(E)); + case UO_Not: + // unary not promotes boolean to integer + if (UO->getSubExpr()->isKnownToHaveBooleanValue()) + return IntRange(MaxWidth, false); + + LLVM_FALLTHROUGH; + default: return GetExprRange(C, UO->getSubExpr(), MaxWidth, InConstantContext, Approximate);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits