[PATCH] D92956: Fix https://bugs.llvm.org/show_bug.cgi?id=48011

2020-12-09 Thread z via Phabricator via cfe-commits
fanfuqiang created this revision.
fanfuqiang added a reviewer: dblaikie.
fanfuqiang requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

fix https://bugs.llvm.org/show_bug.cgi?id=48011


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D92956

Files:
  clang/lib/Sema/SemaStmt.cpp
  clang/test/SemaCXX/warn-range-loop-analysis.cpp


Index: clang/test/SemaCXX/warn-range-loop-analysis.cpp
===
--- clang/test/SemaCXX/warn-range-loop-analysis.cpp
+++ clang/test/SemaCXX/warn-range-loop-analysis.cpp
@@ -29,6 +29,12 @@
   operator int();
 };
 
+struct ID1 {
+  // Small trivally copy constructor, but no trivally copy assignment 
+  // operator.
+  ID1& operator=(ID1 const& other) { return *this; }
+};
+
 // Testing notes:
 // test0 checks that the full text of the warnings and notes is correct.  The
 //   rest of the tests checks a smaller portion of the text.
@@ -455,6 +461,23 @@
   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:17-[[@LINE-3]]:18}:" "
 }
 
+void test11() {
+  Container C;
+
+  for (const ID1 &x : C) {}
+  // No warning
+
+  for (const ID1& x : C) {}
+  // No warning
+
+  for (const ID1 & x : C) {}
+  // No warning
+
+  for (const ID1&x : C) {}
+  // No warning
+}
+
+
 template 
 void test_template_function() {
   // In a template instantiation the diagnostics should not be emitted for
Index: clang/lib/Sema/SemaStmt.cpp
===
--- clang/lib/Sema/SemaStmt.cpp
+++ clang/lib/Sema/SemaStmt.cpp
@@ -2855,8 +2855,14 @@
   // diagnostic for these instances. 64 bytes is a common size of a cache line.
   // (The function `getTypeSize` returns the size in bits.)
   ASTContext &Ctx = SemaRef.Context;
+  const CXXRecordDecl *ClassDecl = nullptr;
+  if (const auto *RT = VariableType->getAs()) {
+ClassDecl = dyn_cast(RT->getDecl());
+  }
   if (Ctx.getTypeSize(VariableType) <= 64 * 8 &&
   (VariableType.isTriviallyCopyableType(Ctx) ||
+   // Fix https://bugs.llvm.org/show_bug.cgi?id=48011
+   (ClassDecl && !ClassDecl->hasNonTrivialCopyConstructor()) ||
hasTrivialABIAttr(VariableType)))
 return;
 


Index: clang/test/SemaCXX/warn-range-loop-analysis.cpp
===
--- clang/test/SemaCXX/warn-range-loop-analysis.cpp
+++ clang/test/SemaCXX/warn-range-loop-analysis.cpp
@@ -29,6 +29,12 @@
   operator int();
 };
 
+struct ID1 {
+  // Small trivally copy constructor, but no trivally copy assignment 
+  // operator.
+  ID1& operator=(ID1 const& other) { return *this; }
+};
+
 // Testing notes:
 // test0 checks that the full text of the warnings and notes is correct.  The
 //   rest of the tests checks a smaller portion of the text.
@@ -455,6 +461,23 @@
   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:17-[[@LINE-3]]:18}:" "
 }
 
+void test11() {
+  Container C;
+
+  for (const ID1 &x : C) {}
+  // No warning
+
+  for (const ID1& x : C) {}
+  // No warning
+
+  for (const ID1 & x : C) {}
+  // No warning
+
+  for (const ID1&x : C) {}
+  // No warning
+}
+
+
 template 
 void test_template_function() {
   // In a template instantiation the diagnostics should not be emitted for
Index: clang/lib/Sema/SemaStmt.cpp
===
--- clang/lib/Sema/SemaStmt.cpp
+++ clang/lib/Sema/SemaStmt.cpp
@@ -2855,8 +2855,14 @@
   // diagnostic for these instances. 64 bytes is a common size of a cache line.
   // (The function `getTypeSize` returns the size in bits.)
   ASTContext &Ctx = SemaRef.Context;
+  const CXXRecordDecl *ClassDecl = nullptr;
+  if (const auto *RT = VariableType->getAs()) {
+ClassDecl = dyn_cast(RT->getDecl());
+  }
   if (Ctx.getTypeSize(VariableType) <= 64 * 8 &&
   (VariableType.isTriviallyCopyableType(Ctx) ||
+   // Fix https://bugs.llvm.org/show_bug.cgi?id=48011
+   (ClassDecl && !ClassDecl->hasNonTrivialCopyConstructor()) ||
hasTrivialABIAttr(VariableType)))
 return;
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D92956: Fix https://bugs.llvm.org/show_bug.cgi?id=48011

2020-12-10 Thread z via Phabricator via cfe-commits
fanfuqiang updated this revision to Diff 310847.
fanfuqiang added a comment.

update testcase take advise of reviewers.
fix conflict testcase fail,  less warnning only for 
`hasNonTrivialCopyConstructor` .


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D92956/new/

https://reviews.llvm.org/D92956

Files:
  clang/lib/Sema/SemaStmt.cpp
  clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp
  clang/test/SemaCXX/warn-range-loop-analysis.cpp


Index: clang/test/SemaCXX/warn-range-loop-analysis.cpp
===
--- clang/test/SemaCXX/warn-range-loop-analysis.cpp
+++ clang/test/SemaCXX/warn-range-loop-analysis.cpp
@@ -29,6 +29,13 @@
   operator int();
 };
 
+struct ID1 {
+  // Small trivally copy constructor, but no trivally copy assignment 
+  // operator.
+  ID1(ID1 const&) = default;
+  ID1& operator=(ID1 const& other) { return *this; }
+};
+
 // Testing notes:
 // test0 checks that the full text of the warnings and notes is correct.  The
 //   rest of the tests checks a smaller portion of the text.
@@ -455,6 +462,13 @@
   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:17-[[@LINE-3]]:18}:" "
 }
 
+void test11() {
+  Container C;
+
+  for (const ID1 x : C) {}
+  // No warning
+}
+
 template 
 void test_template_function() {
   // In a template instantiation the diagnostics should not be emitted for
Index: clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp
===
--- clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp
+++ clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp
@@ -55,8 +55,6 @@
 int b;
   };
 
-  // expected-warning@+3 {{loop variable 'r' creates a copy from type 'const 
Record'}}
-  // expected-note@+2 {{use reference type 'const Record &' to prevent 
copying}}
   Record records[8];
   for (const auto r : records)
 (void)r;
Index: clang/lib/Sema/SemaStmt.cpp
===
--- clang/lib/Sema/SemaStmt.cpp
+++ clang/lib/Sema/SemaStmt.cpp
@@ -2855,8 +2855,14 @@
   // diagnostic for these instances. 64 bytes is a common size of a cache line.
   // (The function `getTypeSize` returns the size in bits.)
   ASTContext &Ctx = SemaRef.Context;
+  const CXXRecordDecl *ClassDecl = nullptr;
+  if (const auto *RT = VariableType->getAs()) {
+ClassDecl = dyn_cast(RT->getDecl());
+  }
   if (Ctx.getTypeSize(VariableType) <= 64 * 8 &&
   (VariableType.isTriviallyCopyableType(Ctx) ||
+   // Fix https://bugs.llvm.org/show_bug.cgi?id=48011
+   (ClassDecl && !ClassDecl->hasNonTrivialCopyConstructor()) ||
hasTrivialABIAttr(VariableType)))
 return;
 


Index: clang/test/SemaCXX/warn-range-loop-analysis.cpp
===
--- clang/test/SemaCXX/warn-range-loop-analysis.cpp
+++ clang/test/SemaCXX/warn-range-loop-analysis.cpp
@@ -29,6 +29,13 @@
   operator int();
 };
 
+struct ID1 {
+  // Small trivally copy constructor, but no trivally copy assignment 
+  // operator.
+  ID1(ID1 const&) = default;
+  ID1& operator=(ID1 const& other) { return *this; }
+};
+
 // Testing notes:
 // test0 checks that the full text of the warnings and notes is correct.  The
 //   rest of the tests checks a smaller portion of the text.
@@ -455,6 +462,13 @@
   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:17-[[@LINE-3]]:18}:" "
 }
 
+void test11() {
+  Container C;
+
+  for (const ID1 x : C) {}
+  // No warning
+}
+
 template 
 void test_template_function() {
   // In a template instantiation the diagnostics should not be emitted for
Index: clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp
===
--- clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp
+++ clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp
@@ -55,8 +55,6 @@
 int b;
   };
 
-  // expected-warning@+3 {{loop variable 'r' creates a copy from type 'const Record'}}
-  // expected-note@+2 {{use reference type 'const Record &' to prevent copying}}
   Record records[8];
   for (const auto r : records)
 (void)r;
Index: clang/lib/Sema/SemaStmt.cpp
===
--- clang/lib/Sema/SemaStmt.cpp
+++ clang/lib/Sema/SemaStmt.cpp
@@ -2855,8 +2855,14 @@
   // diagnostic for these instances. 64 bytes is a common size of a cache line.
   // (The function `getTypeSize` returns the size in bits.)
   ASTContext &Ctx = SemaRef.Context;
+  const CXXRecordDecl *ClassDecl = nullptr;
+  if (const auto *RT = VariableType->getAs()) {
+ClassDecl = dyn_cast(RT->getDecl());
+  }
   if (Ctx.getTypeSize(VariableType) <= 64 * 8 &&
   (VariableType.isTriviallyCopyableType(Ctx) ||
+   // Fix https://bugs.llvm.org/show_bug.cgi?id=48011
+   (ClassDecl && !ClassDecl->hasNonTrivialCopyConstructor()) ||
hasTrivialABIAttr(Varia

[PATCH] D92956: Fix range-loop-analysis checks for trivial copyability

2020-12-10 Thread z via Phabricator via cfe-commits
fanfuqiang updated this revision to Diff 310865.
fanfuqiang marked 2 inline comments as done.
fanfuqiang added a comment.

update testcase take advise of reviewers.
fix conflict testcase fail, less warnning only for hasNonTrivialCopyConstructor 
.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D92956/new/

https://reviews.llvm.org/D92956

Files:
  clang/lib/Sema/SemaStmt.cpp
  clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp
  clang/test/SemaCXX/warn-range-loop-analysis.cpp


Index: clang/test/SemaCXX/warn-range-loop-analysis.cpp
===
--- clang/test/SemaCXX/warn-range-loop-analysis.cpp
+++ clang/test/SemaCXX/warn-range-loop-analysis.cpp
@@ -29,6 +29,13 @@
   operator int();
 };
 
+struct ID1 {
+  // Small trivally copy constructor, but no trivally copy assignment 
+  // operator.
+  ID1(ID1 const&) = default;
+  ID1& operator=(ID1 const& other) { return *this; }
+};
+
 // Testing notes:
 // test0 checks that the full text of the warnings and notes is correct.  The
 //   rest of the tests checks a smaller portion of the text.
@@ -455,6 +462,14 @@
   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:17-[[@LINE-3]]:18}:" "
 }
 
+void test11() {
+  Container C;
+
+  for (const ID1 x : C) {}
+  // No warning
+}
+
+
 template 
 void test_template_function() {
   // In a template instantiation the diagnostics should not be emitted for
Index: clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp
===
--- clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp
+++ clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp
@@ -55,8 +55,6 @@
 int b;
   };
 
-  // expected-warning@+3 {{loop variable 'r' creates a copy from type 'const 
Record'}}
-  // expected-note@+2 {{use reference type 'const Record &' to prevent 
copying}}
   Record records[8];
   for (const auto r : records)
 (void)r;
Index: clang/lib/Sema/SemaStmt.cpp
===
--- clang/lib/Sema/SemaStmt.cpp
+++ clang/lib/Sema/SemaStmt.cpp
@@ -2855,8 +2855,14 @@
   // diagnostic for these instances. 64 bytes is a common size of a cache line.
   // (The function `getTypeSize` returns the size in bits.)
   ASTContext &Ctx = SemaRef.Context;
+  const CXXRecordDecl *ClassDecl = nullptr;
+  if (const auto *RT = VariableType->getAs()) {
+ClassDecl = dyn_cast(RT->getDecl());
+  }
   if (Ctx.getTypeSize(VariableType) <= 64 * 8 &&
   (VariableType.isTriviallyCopyableType(Ctx) ||
+   // Fix https://bugs.llvm.org/show_bug.cgi?id=48011
+   (ClassDecl && !ClassDecl->hasNonTrivialCopyConstructor()) ||
hasTrivialABIAttr(VariableType)))
 return;
 


Index: clang/test/SemaCXX/warn-range-loop-analysis.cpp
===
--- clang/test/SemaCXX/warn-range-loop-analysis.cpp
+++ clang/test/SemaCXX/warn-range-loop-analysis.cpp
@@ -29,6 +29,13 @@
   operator int();
 };
 
+struct ID1 {
+  // Small trivally copy constructor, but no trivally copy assignment 
+  // operator.
+  ID1(ID1 const&) = default;
+  ID1& operator=(ID1 const& other) { return *this; }
+};
+
 // Testing notes:
 // test0 checks that the full text of the warnings and notes is correct.  The
 //   rest of the tests checks a smaller portion of the text.
@@ -455,6 +462,14 @@
   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:17-[[@LINE-3]]:18}:" "
 }
 
+void test11() {
+  Container C;
+
+  for (const ID1 x : C) {}
+  // No warning
+}
+
+
 template 
 void test_template_function() {
   // In a template instantiation the diagnostics should not be emitted for
Index: clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp
===
--- clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp
+++ clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp
@@ -55,8 +55,6 @@
 int b;
   };
 
-  // expected-warning@+3 {{loop variable 'r' creates a copy from type 'const Record'}}
-  // expected-note@+2 {{use reference type 'const Record &' to prevent copying}}
   Record records[8];
   for (const auto r : records)
 (void)r;
Index: clang/lib/Sema/SemaStmt.cpp
===
--- clang/lib/Sema/SemaStmt.cpp
+++ clang/lib/Sema/SemaStmt.cpp
@@ -2855,8 +2855,14 @@
   // diagnostic for these instances. 64 bytes is a common size of a cache line.
   // (The function `getTypeSize` returns the size in bits.)
   ASTContext &Ctx = SemaRef.Context;
+  const CXXRecordDecl *ClassDecl = nullptr;
+  if (const auto *RT = VariableType->getAs()) {
+ClassDecl = dyn_cast(RT->getDecl());
+  }
   if (Ctx.getTypeSize(VariableType) <= 64 * 8 &&
   (VariableType.isTriviallyCopyableType(Ctx) ||
+   // Fix https://bugs.llvm.org/show_bug.cgi?id=48011
+   (ClassD