]> granicus.if.org Git - clang/commitdiff
Fix a couple of wrong self-comparison diagnostics.
authorRichard Smith <richard-llvm@metafoo.co.uk>
Sun, 7 Jan 2018 22:18:05 +0000 (22:18 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Sun, 7 Jan 2018 22:18:05 +0000 (22:18 +0000)
Check whether we are comparing the same entity, not merely the same
declaration, and don't assume that weak declarations resolve to distinct
entities.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@321976 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaExpr.cpp
test/Sema/self-comparison.c
test/SemaCXX/self-comparison.cpp

index 933289360b2830b78eb9e10dbca33375d51a786b..e9e65fc1fc5a146ee55a2ad15bc13eb8ad8be17b 100644 (file)
@@ -9627,7 +9627,8 @@ static void diagnoseTautologicalComparison(Sema &S, SourceLocation Loc,
   // 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 diagnoseTautologicalComparison(Sema &S, SourceLocation Loc,
                               << 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
index edb3a6a4c8a5fa169155f3ad6601e8621dabb287..bd7adcd9cdaa261a123ba8fcb624da50839dc126 100644 (file)
@@ -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;
+}
index fb15ec843061db56c2aa0abdee7d0ec2c29e7d2e..2af19abb30a53adc611d752e9a69bc20fde89e42 100644 (file)
@@ -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}}