r321976 - Fix a couple of wrong self-comparison diagnostics.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 7 14:18:05 PST 2018


Author: rsmith
Date: Sun Jan  7 14:18:05 2018
New Revision: 321976

URL: http://llvm.org/viewvc/llvm-project?rev=321976&view=rev
Log:
Fix a couple of wrong self-comparison diagnostics.

Check whether we are comparing the same entity, not merely the same
declaration, and don't assume that weak declarations resolve to distinct
entities.

Modified:
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/Sema/self-comparison.c
    cfe/trunk/test/SemaCXX/self-comparison.cpp

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=321976&r1=321975&r2=321976&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Sun Jan  7 14:18:05 2018
@@ -9627,7 +9627,8 @@ static void diagnoseTautologicalComparis
   // result.
   ValueDecl *DL = getCompareDecl(LHSStripped);
   ValueDecl *DR = getCompareDecl(RHSStripped);
-  if (DL && DR && DL == DR && !IsWithinTemplateSpecialization(DL)) {
+  if (DL && DR && declaresSameEntity(DL, DR) &&
+      !IsWithinTemplateSpecialization(DL)) {
     StringRef Result;
     switch (Opc) {
     case BO_EQ: case BO_LE: case BO_GE:
@@ -9648,10 +9649,9 @@ static void diagnoseTautologicalComparis
                               << Result);
   } else if (DL && DR && LHSType->isArrayType() && RHSType->isArrayType() &&
              !DL->getType()->isReferenceType() &&
-             !DR->getType()->isReferenceType()) {
+             !DR->getType()->isReferenceType() &&
+             !DL->isWeak() && !DR->isWeak()) {
     // What is it always going to evaluate to?
-    // FIXME: This is wrong if DL and DR are different Decls for the same
-    // entity. It's also wrong if DL and/or DR are weak declarations.
     StringRef Result;
     switch(Opc) {
     case BO_EQ: // e.g. array1 == array2

Modified: cfe/trunk/test/Sema/self-comparison.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/self-comparison.c?rev=321976&r1=321975&r2=321976&view=diff
==============================================================================
--- cfe/trunk/test/Sema/self-comparison.c (original)
+++ cfe/trunk/test/Sema/self-comparison.c Sun Jan  7 14:18:05 2018
@@ -86,3 +86,8 @@ int R8435950(int i) {
   return 1;
 }
 
+__attribute__((weak)) int weak_1[3];
+__attribute__((weak)) int weak_2[3];
+_Bool compare_weak() {
+  return weak_1 == weak_2;
+}

Modified: cfe/trunk/test/SemaCXX/self-comparison.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/self-comparison.cpp?rev=321976&r1=321975&r2=321976&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/self-comparison.cpp (original)
+++ cfe/trunk/test/SemaCXX/self-comparison.cpp Sun Jan  7 14:18:05 2018
@@ -21,3 +21,7 @@ struct A {
     return a == c; // expected-warning {{array comparison always evaluates to false}}
   }
 };
+
+namespace NA { extern "C" int x[3]; }
+namespace NB { extern "C" int x[3]; }
+bool k = NA::x == NB::x; // expected-warning {{self-comparison always evaluates to true}}




More information about the cfe-commits mailing list