def warn_mixed_sign_comparison : Warning<
"comparison of integers of different signs: %0 and %1">,
InGroup<DiagGroup<"sign-compare">>;
+def warn_mixed_sign_conditional : Warning<
+ "operands of ? are integers of different signs: %0 and %1">,
+ InGroup<DiagGroup<"sign-compare">>;
def err_invalid_this_use : Error<
"invalid use of 'this' outside of a nonstatic member function">;
void DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc,
Expr **Args, unsigned NumArgs);
+ void CheckSignCompare(Expr *LHS, Expr *RHS, SourceLocation Loc,
+ const PartialDiagnostic &PD);
+
virtual ExpressionEvaluationContext
PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext);
if (getLangOptions().CPlusPlus)
return CXXCheckConditionalOperands(Cond, LHS, RHS, QuestionLoc);
+ CheckSignCompare(LHS, RHS, QuestionLoc, diag::warn_mixed_sign_conditional);
+
UsualUnaryConversions(Cond);
UsualUnaryConversions(LHS);
UsualUnaryConversions(RHS);
}
/// Implements -Wsign-compare.
-static void DiagnoseSignCompare(Sema &S, Expr *lex, Expr *rex,
- BinaryOperator::Opcode Opc, SourceLocation OpLoc) {
+void Sema::CheckSignCompare(Expr *lex, Expr *rex, SourceLocation OpLoc,
+ const PartialDiagnostic &PD) {
QualType lt = lex->getType(), rt = rex->getType();
// Only warn if both operands are integral.
// If the value is a non-negative integer constant, then the
// signed->unsigned conversion won't change it.
llvm::APSInt value;
- if (signedOperand->isIntegerConstantExpr(value, S.Context)) {
+ if (signedOperand->isIntegerConstantExpr(value, Context)) {
assert(value.isSigned() && "result of signed expression not signed");
if (value.isNonNegative())
return;
}
- S.Diag(OpLoc, diag::warn_mixed_sign_comparison)
+ Diag(OpLoc, PD)
<< lex->getType() << rex->getType()
<< lex->getSourceRange() << rex->getSourceRange();
}
if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
return CheckVectorCompareOperands(lex, rex, Loc, isRelational);
- DiagnoseSignCompare(*this, lex, rex, Opc, Loc);
+ CheckSignCompare(lex, rex, Loc, diag::warn_mixed_sign_comparison);
// C99 6.5.8p3 / C99 6.5.9p4
if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
if (LHS->isTypeDependent() || RHS->isTypeDependent())
return Context.DependentTy;
+ CheckSignCompare(LHS, RHS, QuestionLoc, diag::warn_mixed_sign_conditional);
+
// C++0x 5.16p2
// If either the second or the third operand has type (cv) void, ...
QualType LTy = LHS->getType();
((short)a == b) + // expected-warning {{comparison of integers of different signs}}
(a == (unsigned int) b) + // expected-warning {{comparison of integers of different signs}}
(a == (unsigned short) b); // expected-warning {{comparison of integers of different signs}}
+
+ enum Enum {B};
+ return (a == B) +
+ ((int)a == B) +
+ ((short)a == B) +
+ (a == (unsigned int) B) + // expected-warning {{comparison of integers of different signs}}
+ (a == (unsigned short) B); // expected-warning {{comparison of integers of different signs}}
+
+ // Should be able to prove all of these are non-negative.
+ return (b == (long) B) +
+ (b == (int) B) +
+ (b == (short) B);
}
int equal(char *a, const char *b) {
typedef void *asdf;
*(0 ? (asdf) 0 : &x) = 10;
+
+ unsigned long test0 = 5;
+ test0 = test0 ? (long) test0 : test0; // expected-warning {{operands of ? are integers of different signs}}
+ test0 = test0 ? (int) test0 : test0; // expected-warning {{operands of ? are integers of different signs}}
+ test0 = test0 ? (short) test0 : test0; // expected-warning {{operands of ? are integers of different signs}}
+ test0 = test0 ? test0 : (long) test0; // expected-warning {{operands of ? are integers of different signs}}
+ test0 = test0 ? test0 : (int) test0; // expected-warning {{operands of ? are integers of different signs}}
+ test0 = test0 ? test0 : (short) test0; // expected-warning {{operands of ? are integers of different signs}}
+ test0 = test0 ? test0 : (long) 10;
+ test0 = test0 ? test0 : (int) 10;
+ test0 = test0 ? test0 : (short) 10;
+ test0 = test0 ? (long) 10 : test0;
+ test0 = test0 ? (int) 10 : test0;
+ test0 = test0 ? (short) 10 : test0;
+
+ enum Enum { EVal };
+ test0 = test0 ? EVal : test0;
+ test0 = test0 ? EVal : (int) test0; // okay: EVal is an int
+ test0 = test0 ? (unsigned) EVal : (int) test0; // expected-warning {{operands of ? are integers of different signs}}
}
int Postgresql() {
--- /dev/null
+// RUN: clang-cc -fsyntax-only -pedantic -verify %s
+
+int test0(long a, unsigned long b) {
+ enum Enum {B};
+ return (a == B) + // expected-warning {{comparison of integers of different signs}}
+ ((int)a == B) + // expected-warning {{comparison of integers of different signs}}
+ ((short)a == B) + // expected-warning {{comparison of integers of different signs}}
+ (a == (unsigned int) B) + // expected-warning {{comparison of integers of different signs}}
+ (a == (unsigned short) B); // expected-warning {{comparison of integers of different signs}}
+
+ // Should be able to prove all of these are non-negative.
+ return (b == (long) B) +
+ (b == (int) B) +
+ (b == (short) B);
+}
i1 = i1 ? i1 : ir1;
int *pi1 = i1 ? &i1 : 0;
pi1 = i1 ? 0 : &i1;
- i1 = i1 ? i1 : EVal;
- i1 = i1 ? EVal : i1;
+ i1 = i1 ? i1 : EVal; // expected-warning {{operands of ? are integers of different signs}} ??
+ i1 = i1 ? EVal : i1; // expected-warning {{operands of ? are integers of different signs}} ??
d1 = i1 ? 'c' : 4.0;
d1 = i1 ? 4.0 : 'c';
Base *pb = i1 ? (Base*)0 : (Derived*)0;
(void)&(i1 ? flds.b1 : flds.i1); // expected-error {{address of bit-field requested}}
(void)&(i1 ? flds.i1 : flds.b1); // expected-error {{address of bit-field requested}}
+
+ unsigned long test0 = 5;
+ test0 = test0 ? (long) test0 : test0; // expected-warning {{operands of ? are integers of different signs}}
+ test0 = test0 ? (int) test0 : test0; // expected-warning {{operands of ? are integers of different signs}}
+ test0 = test0 ? (short) test0 : test0; // expected-warning {{operands of ? are integers of different signs}}
+ test0 = test0 ? test0 : (long) test0; // expected-warning {{operands of ? are integers of different signs}}
+ test0 = test0 ? test0 : (int) test0; // expected-warning {{operands of ? are integers of different signs}}
+ test0 = test0 ? test0 : (short) test0; // expected-warning {{operands of ? are integers of different signs}}
+ test0 = test0 ? test0 : (long) 10;
+ test0 = test0 ? test0 : (int) 10;
+ test0 = test0 ? test0 : (short) 10;
+ test0 = test0 ? (long) 10 : test0;
+ test0 = test0 ? (int) 10 : test0;
+ test0 = test0 ? (short) 10 : test0;
+
+ test0 = test0 ? EVal : test0;
+ test0 = test0 ? EVal : (int) test0; // expected-warning {{operands of ? are integers of different signs}}
+
// Note the thing that this does not test: since DR446, various situations
// *must* create a separate temporary copy of class objects. This can only
// be properly tested at runtime, though.