[clang] [Clang] Require base element type of `__has_unique_object_representations` to be complete (PR #95432)

2024-06-13 Thread via cfe-commits

https://github.com/cor3ntin approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/95432
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Require base element type of `__has_unique_object_representations` to be complete (PR #95432)

2024-06-13 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Mital Ashok (MitalAshok)


Changes

Fixes #95311

Previous behaviour was that `false` was silently returned, templated classes 
were not instantiated and incomplete classes did not issue an error.

---
Full diff: https://github.com/llvm/llvm-project/pull/95432.diff


4 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+3) 
- (modified) clang/lib/AST/ASTContext.cpp (+6) 
- (modified) clang/lib/Sema/SemaExprCXX.cpp (+4-1) 
- (modified) clang/test/SemaCXX/type-traits.cpp (+11) 


``diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8c2f737836a9d..4359213286fa0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -660,6 +660,9 @@ Bug Fixes in This Version
 - Correctly reject declarations where a statement is required in C.
   Fixes #GH92775
 
+- ``__has_unique_object_representations`` correctly handles arrays of unknown 
bounds of
+  types by ensuring they are complete and instantiating them if needed. Fixes 
(#GH95311).
+
 Bug Fixes to Compiler Builtins
 ^^
 
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index aa22825602a40..f3b698b3f0c59 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -2791,6 +2791,12 @@ bool ASTContext::hasUniqueObjectRepresentations(
 return hasUniqueObjectRepresentations(getBaseElementType(Ty),
   CheckIfTriviallyCopyable);
 
+  if (Ty->isVoidType())
+return false;
+
+  assert(!Ty->isIncompleteType() && "hasUniqueObjectRepresentations should not 
"
+"be called with an incomplete type");
+
   // (9.1) - T is trivially copyable...
   if (CheckIfTriviallyCopyable && !Ty.isTriviallyCopyableType(*this))
 return false;
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index f3af8dee6b090..127621a31470d 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -5149,6 +5149,10 @@ static bool CheckUnaryTypeTraitTypeCompleteness(Sema , 
TypeTrait UTT,
   case UTT_HasTrivialCopy:
   case UTT_HasTrivialDestructor:
   case UTT_HasVirtualDestructor:
+  // has_unique_object_representations when T is an array is defined in 
terms
+  // of has_unique_object_representations>, so the base
+  // type needs to be complete even if the type is an incomplete array type.
+  case UTT_HasUniqueObjectRepresentations:
 ArgTy = QualType(ArgTy->getBaseElementTypeUnsafe(), 0);
 [[fallthrough]];
 
@@ -5157,7 +5161,6 @@ static bool CheckUnaryTypeTraitTypeCompleteness(Sema , 
TypeTrait UTT,
   case UTT_IsDestructible:
   case UTT_IsNothrowDestructible:
   case UTT_IsTriviallyDestructible:
-  case UTT_HasUniqueObjectRepresentations:
 if (ArgTy->isIncompleteArrayType() || ArgTy->isVoidType())
   return true;
 
diff --git a/clang/test/SemaCXX/type-traits.cpp 
b/clang/test/SemaCXX/type-traits.cpp
index d40605f56f1ed..b5cefb18fa13e 100644
--- a/clang/test/SemaCXX/type-traits.cpp
+++ b/clang/test/SemaCXX/type-traits.cpp
@@ -3505,6 +3505,17 @@ 
static_assert(__has_unique_object_representations(_BitInt(8)), "BitInt:");
 static_assert(!__has_unique_object_representations(_BitInt(127)), "BitInt:");
 static_assert(__has_unique_object_representations(_BitInt(128)), "BitInt:");
 
+namespace GH95311 {
+
+template 
+class Foo {
+  int x;
+};
+static_assert(__has_unique_object_representations(Foo<0>[]));
+class Bar; // expected-note {{forward declaration of 'GH95311::Bar'}}
+static_assert(__has_unique_object_representations(Bar[])); // expected-error 
{{incomplete type}}
+
+}
 
 namespace PR46209 {
   // Foo has both a trivial assignment operator and a non-trivial one.

``




https://github.com/llvm/llvm-project/pull/95432
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Require base element type of `__has_unique_object_representations` to be complete (PR #95432)

2024-06-13 Thread via cfe-commits


@@ -2791,6 +2791,12 @@ bool ASTContext::hasUniqueObjectRepresentations(
 return hasUniqueObjectRepresentations(getBaseElementType(Ty),
   CheckIfTriviallyCopyable);
 
+  if (Ty->isVoidType())

cor3ntin wrote:

I'd prefer that, I think

https://github.com/llvm/llvm-project/pull/95432
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Require base element type of `__has_unique_object_representations` to be complete (PR #95432)

2024-06-13 Thread Mital Ashok via cfe-commits

https://github.com/MitalAshok updated 
https://github.com/llvm/llvm-project/pull/95432

>From 3874b20e44c67e8ac0d2eb2665fb0ea9f09c6f5d Mon Sep 17 00:00:00 2001
From: Mital Ashok 
Date: Thu, 13 Jun 2024 17:26:50 +0100
Subject: [PATCH 1/2] [Clang] Require base element type of
 __has_unique_object_representations to be complete

---
 clang/docs/ReleaseNotes.rst|  3 +++
 clang/lib/AST/ASTContext.cpp   |  6 ++
 clang/lib/Sema/SemaExprCXX.cpp |  5 -
 clang/test/SemaCXX/type-traits.cpp | 11 +++
 4 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8c2f737836a9d..4359213286fa0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -660,6 +660,9 @@ Bug Fixes in This Version
 - Correctly reject declarations where a statement is required in C.
   Fixes #GH92775
 
+- ``__has_unique_object_representations`` correctly handles arrays of unknown 
bounds of
+  types by ensuring they are complete and instantiating them if needed. Fixes 
(#GH95311).
+
 Bug Fixes to Compiler Builtins
 ^^
 
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index aa22825602a40..f3b698b3f0c59 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -2791,6 +2791,12 @@ bool ASTContext::hasUniqueObjectRepresentations(
 return hasUniqueObjectRepresentations(getBaseElementType(Ty),
   CheckIfTriviallyCopyable);
 
+  if (Ty->isVoidType())
+return false;
+
+  assert(!Ty->isIncompleteType() && "hasUniqueObjectRepresentations should not 
"
+"be called with an incomplete type");
+
   // (9.1) - T is trivially copyable...
   if (CheckIfTriviallyCopyable && !Ty.isTriviallyCopyableType(*this))
 return false;
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index f3af8dee6b090..127621a31470d 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -5149,6 +5149,10 @@ static bool CheckUnaryTypeTraitTypeCompleteness(Sema , 
TypeTrait UTT,
   case UTT_HasTrivialCopy:
   case UTT_HasTrivialDestructor:
   case UTT_HasVirtualDestructor:
+  // has_unique_object_representations when T is an array is defined in 
terms
+  // of has_unique_object_representations>, so the base
+  // type needs to be complete even if the type is an incomplete array type.
+  case UTT_HasUniqueObjectRepresentations:
 ArgTy = QualType(ArgTy->getBaseElementTypeUnsafe(), 0);
 [[fallthrough]];
 
@@ -5157,7 +5161,6 @@ static bool CheckUnaryTypeTraitTypeCompleteness(Sema , 
TypeTrait UTT,
   case UTT_IsDestructible:
   case UTT_IsNothrowDestructible:
   case UTT_IsTriviallyDestructible:
-  case UTT_HasUniqueObjectRepresentations:
 if (ArgTy->isIncompleteArrayType() || ArgTy->isVoidType())
   return true;
 
diff --git a/clang/test/SemaCXX/type-traits.cpp 
b/clang/test/SemaCXX/type-traits.cpp
index d40605f56f1ed..b5cefb18fa13e 100644
--- a/clang/test/SemaCXX/type-traits.cpp
+++ b/clang/test/SemaCXX/type-traits.cpp
@@ -3505,6 +3505,17 @@ 
static_assert(__has_unique_object_representations(_BitInt(8)), "BitInt:");
 static_assert(!__has_unique_object_representations(_BitInt(127)), "BitInt:");
 static_assert(__has_unique_object_representations(_BitInt(128)), "BitInt:");
 
+namespace GH95311 {
+
+template 
+class Foo {
+  int x;
+};
+static_assert(__has_unique_object_representations(Foo<0>[]));
+class Bar; // expected-note {{forward declaration of 'GH95311::Bar'}}
+static_assert(__has_unique_object_representations(Bar[])); // expected-error 
{{incomplete type}}
+
+}
 
 namespace PR46209 {
   // Foo has both a trivial assignment operator and a non-trivial one.

>From 60a66efac75e872cf257b00058a53286a374a8ec Mon Sep 17 00:00:00 2001
From: Mital Ashok 
Date: Thu, 13 Jun 2024 19:46:55 +0100
Subject: [PATCH 2/2] Address feedback on assertion

---
 clang/lib/AST/ASTContext.cpp | 8 +++-
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index f3b698b3f0c59..d82936e991173 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -2791,11 +2791,9 @@ bool ASTContext::hasUniqueObjectRepresentations(
 return hasUniqueObjectRepresentations(getBaseElementType(Ty),
   CheckIfTriviallyCopyable);
 
-  if (Ty->isVoidType())
-return false;
-
-  assert(!Ty->isIncompleteType() && "hasUniqueObjectRepresentations should not 
"
-"be called with an incomplete type");
+  assert((Ty->isVoidType() || !Ty->isIncompleteType()) &&
+ "hasUniqueObjectRepresentations should not be called with an "
+ "incomplete type");
 
   // (9.1) - T is trivially copyable...
   if (CheckIfTriviallyCopyable && !Ty.isTriviallyCopyableType(*this))

___

[clang] [Clang] Require base element type of `__has_unique_object_representations` to be complete (PR #95432)

2024-06-13 Thread Shafik Yaghmour via cfe-commits


@@ -2791,6 +2791,12 @@ bool ASTContext::hasUniqueObjectRepresentations(
 return hasUniqueObjectRepresentations(getBaseElementType(Ty),
   CheckIfTriviallyCopyable);
 
+  if (Ty->isVoidType())

shafik wrote:

It is not clear why we need this change as well.

https://github.com/llvm/llvm-project/pull/95432
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Require base element type of `__has_unique_object_representations` to be complete (PR #95432)

2024-06-13 Thread Mital Ashok via cfe-commits

https://github.com/MitalAshok created 
https://github.com/llvm/llvm-project/pull/95432

Fixes #95311

Previous behaviour was that `false` was silently returned, templated classes 
were not instantiated and incomplete classes did not issue an error.

>From 3874b20e44c67e8ac0d2eb2665fb0ea9f09c6f5d Mon Sep 17 00:00:00 2001
From: Mital Ashok 
Date: Thu, 13 Jun 2024 17:26:50 +0100
Subject: [PATCH] [Clang] Require base element type of
 __has_unique_object_representations to be complete

---
 clang/docs/ReleaseNotes.rst|  3 +++
 clang/lib/AST/ASTContext.cpp   |  6 ++
 clang/lib/Sema/SemaExprCXX.cpp |  5 -
 clang/test/SemaCXX/type-traits.cpp | 11 +++
 4 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8c2f737836a9d..4359213286fa0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -660,6 +660,9 @@ Bug Fixes in This Version
 - Correctly reject declarations where a statement is required in C.
   Fixes #GH92775
 
+- ``__has_unique_object_representations`` correctly handles arrays of unknown 
bounds of
+  types by ensuring they are complete and instantiating them if needed. Fixes 
(#GH95311).
+
 Bug Fixes to Compiler Builtins
 ^^
 
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index aa22825602a40..f3b698b3f0c59 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -2791,6 +2791,12 @@ bool ASTContext::hasUniqueObjectRepresentations(
 return hasUniqueObjectRepresentations(getBaseElementType(Ty),
   CheckIfTriviallyCopyable);
 
+  if (Ty->isVoidType())
+return false;
+
+  assert(!Ty->isIncompleteType() && "hasUniqueObjectRepresentations should not 
"
+"be called with an incomplete type");
+
   // (9.1) - T is trivially copyable...
   if (CheckIfTriviallyCopyable && !Ty.isTriviallyCopyableType(*this))
 return false;
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index f3af8dee6b090..127621a31470d 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -5149,6 +5149,10 @@ static bool CheckUnaryTypeTraitTypeCompleteness(Sema , 
TypeTrait UTT,
   case UTT_HasTrivialCopy:
   case UTT_HasTrivialDestructor:
   case UTT_HasVirtualDestructor:
+  // has_unique_object_representations when T is an array is defined in 
terms
+  // of has_unique_object_representations>, so the base
+  // type needs to be complete even if the type is an incomplete array type.
+  case UTT_HasUniqueObjectRepresentations:
 ArgTy = QualType(ArgTy->getBaseElementTypeUnsafe(), 0);
 [[fallthrough]];
 
@@ -5157,7 +5161,6 @@ static bool CheckUnaryTypeTraitTypeCompleteness(Sema , 
TypeTrait UTT,
   case UTT_IsDestructible:
   case UTT_IsNothrowDestructible:
   case UTT_IsTriviallyDestructible:
-  case UTT_HasUniqueObjectRepresentations:
 if (ArgTy->isIncompleteArrayType() || ArgTy->isVoidType())
   return true;
 
diff --git a/clang/test/SemaCXX/type-traits.cpp 
b/clang/test/SemaCXX/type-traits.cpp
index d40605f56f1ed..b5cefb18fa13e 100644
--- a/clang/test/SemaCXX/type-traits.cpp
+++ b/clang/test/SemaCXX/type-traits.cpp
@@ -3505,6 +3505,17 @@ 
static_assert(__has_unique_object_representations(_BitInt(8)), "BitInt:");
 static_assert(!__has_unique_object_representations(_BitInt(127)), "BitInt:");
 static_assert(__has_unique_object_representations(_BitInt(128)), "BitInt:");
 
+namespace GH95311 {
+
+template 
+class Foo {
+  int x;
+};
+static_assert(__has_unique_object_representations(Foo<0>[]));
+class Bar; // expected-note {{forward declaration of 'GH95311::Bar'}}
+static_assert(__has_unique_object_representations(Bar[])); // expected-error 
{{incomplete type}}
+
+}
 
 namespace PR46209 {
   // Foo has both a trivial assignment operator and a non-trivial one.

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Require base element type of `__has_unique_object_representations` to be complete (PR #95432)

2024-06-13 Thread Mital Ashok via cfe-commits


@@ -2791,6 +2791,12 @@ bool ASTContext::hasUniqueObjectRepresentations(
 return hasUniqueObjectRepresentations(getBaseElementType(Ty),
   CheckIfTriviallyCopyable);
 
+  if (Ty->isVoidType())

MitalAshok wrote:

For the new `!Ty->isIncompleteType()` assertion below. Before `void` would go 
to the `!isTriviallyCopyable` path, this is just a more explicit way of 
handling `__has_unique_object_representations(void)`. The assertion could also 
be `assert(Ty->isVoidType() || !Ty->isIncompleteType())` to follow the spec 
better?


https://github.com/llvm/llvm-project/pull/95432
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits