This is a reduced version of the original patch for PR12463 that handles only string comparisons (I don't care about arrays enough to deal with the details and there the problem is unlikely to happen anyway, but the problem is more likely with strings if e.g. refactoring).
-- Lubos Lunak
From aa0ab874f8052d33aa256840d898b71e983f6388 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubo=C5=A1=20Lu=C5=88=C3=A1k?= <[email protected]> Date: Wed, 27 Nov 2013 17:36:30 +0100 Subject: [PATCH] warn about string literal comparisons even in macros (pr12463) They are unspecified even in macros. --- lib/Sema/SemaExpr.cpp | 12 +++++++----- test/Sema/exprs.c | 8 ++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 7894682..dbcc690 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -7783,22 +7783,24 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, if (!LHSType->hasFloatingRepresentation() && !(LHSType->isBlockPointerType() && IsRelational) && - !LHS.get()->getLocStart().isMacroID() && - !RHS.get()->getLocStart().isMacroID() && ActiveTemplateInstantiations.empty()) { // For non-floating point types, check for self-comparisons of the form // x == x, x != x, x < x, etc. These always evaluate to a constant, and // often indicate logic errors in the program. // // NOTE: Don't warn about comparison expressions resulting from macro - // expansion. Also don't warn about comparisons which are only self + // expansion, unless they do not make any sense at all. + // Also don't warn about comparisons which are only self // comparisons within a template specialization. The warnings should catch // obvious cases in the definition of the template anyways. The idea is to // warn when the typed comparison operator will always evaluate to the same // result. + bool InMacro = LHS.get()->getLocStart().isMacroID() || + RHS.get()->getLocStart().isMacroID(); ValueDecl *DL = getCompareDecl(LHSStripped); ValueDecl *DR = getCompareDecl(RHSStripped); - if (DL && DR && DL == DR && !IsWithinTemplateSpecialization(DL)) { + if (DL && DR && DL == DR && !IsWithinTemplateSpecialization(DL) && + !InMacro) { DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) << 0 // self- << (Opc == BO_EQ @@ -7806,7 +7808,7 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, || Opc == BO_GE)); } else if (DL && DR && LHSType->isArrayType() && RHSType->isArrayType() && !DL->getType()->isReferenceType() && - !DR->getType()->isReferenceType()) { + !DR->getType()->isReferenceType() && !InMacro) { // what is it always going to eval to? char always_evals_to; switch(Opc) { diff --git a/test/Sema/exprs.c b/test/Sema/exprs.c index 2fb17e4..3e90146 100644 --- a/test/Sema/exprs.c +++ b/test/Sema/exprs.c @@ -125,6 +125,14 @@ int test12b(const char *X) { return sizeof(X == "foo"); // no-warning } +// PR12463 +#define FOO_LITERAL "foo" +#define STRING_LITERAL_COMPARE "a" == "b" +int test12c(const char *X) { + return X == FOO_LITERAL; // expected-warning {{comparison against a string literal is unspecified (use strncmp instead)}} + return STRING_LITERAL_COMPARE; // expected-warning {{comparison against a string literal is unspecified (use strncmp instead)}} +} + // rdar://6719156 void test13( void (^P)()) { // expected-error {{blocks support disabled - compile with -fblocks}} -- 1.8.4.5
_______________________________________________ cfe-commits mailing list [email protected] http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
