[clang] [clang] Non-object types are non-trivially relocatable (PR #69734)

Amirreza Ashouri via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 14 08:43:28 PST 2023


https://github.com/AMP999 updated https://github.com/llvm/llvm-project/pull/69734

>From ffc19ddcab61b99c9ad71cdf96de32fef6ff666b Mon Sep 17 00:00:00 2001
From: Amirreza Ashouri <ar.ashouri999 at gmail.com>
Date: Sat, 7 Oct 2023 15:18:55 +0330
Subject: [PATCH] [clang] Non-object types are non-trivially relocatable

Both active C++ proposals (P1144 and P2786) agree that
`is_trivially_relocatable_v<int&>` and `is_trivially_relocatable_v<int()>`
should be false, not true. Only complete object types
can be trivially relocatable.

Fixes #67498
---
 clang/docs/ReleaseNotes.rst                   |  9 ++++++---
 clang/lib/AST/Type.cpp                        |  2 ++
 clang/test/SemaCXX/type-traits-incomplete.cpp |  8 +++++++-
 clang/test/SemaCXX/type-traits-nonobject.cpp  | 16 ++++++++++++++++
 4 files changed, 31 insertions(+), 4 deletions(-)
 create mode 100644 clang/test/SemaCXX/type-traits-nonobject.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ed1a978b5382d71..501c8c302438ba0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -477,10 +477,10 @@ Bug Fixes in This Version
 - Clang now prints unnamed members in diagnostic messages instead of giving an
   empty ''. Fixes
   (`#63759 <https://github.com/llvm/llvm-project/issues/63759>`_)
-- Fix crash in __builtin_strncmp and related builtins when the size value
-  exceeded the maximum value representable by int64_t. Fixes
+- Fix crash in ``__builtin_strncmp`` and related builtins when the size value
+  exceeded the maximum value representable by ``int64_t``. Fixes
   (`#64876 <https://github.com/llvm/llvm-project/issues/64876>`_)
-- Fixed an assertion if a function has cleanups and fatal erors.
+- Fixed an assertion if a function has cleanups and fatal errors.
   (`#48974 <https://github.com/llvm/llvm-project/issues/48974>`_)
 - Clang now emits an error if it is not possible to deduce array size for a
   variable with incomplete array type.
@@ -557,6 +557,9 @@ Bug Fixes in This Version
   Fixes (`#67317 <https://github.com/llvm/llvm-project/issues/67317>`_)
 - Fixed an issue that a benign assertion might hit when instantiating a pack expansion
   inside a lambda. (`#61460 <https://github.com/llvm/llvm-project/issues/61460>`_)
+- ``__is_trivially_relocatable`` no longer returns true for non-object types
+  such as references and functions.
+  Fixes (`#67498 <https://github.com/llvm/llvm-project/issues/67498>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index c8e452e2feab0bf..160a725939ccd4c 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2649,6 +2649,8 @@ bool QualType::isTriviallyRelocatableType(const ASTContext &Context) const {
 
   if (BaseElementType->isIncompleteType()) {
     return false;
+  } else if (!BaseElementType->isObjectType()) {
+    return false;
   } else if (const auto *RD = BaseElementType->getAsRecordDecl()) {
     return RD->canPassInRegisters();
   } else {
diff --git a/clang/test/SemaCXX/type-traits-incomplete.cpp b/clang/test/SemaCXX/type-traits-incomplete.cpp
index c0a520e167698af..3e341d648244075 100644
--- a/clang/test/SemaCXX/type-traits-incomplete.cpp
+++ b/clang/test/SemaCXX/type-traits-incomplete.cpp
@@ -1,8 +1,14 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s 
 
-struct S; // expected-note 2 {{forward declaration of 'S'}}
+struct S; // expected-note 6 {{forward declaration of 'S'}}
 
 void f() {
   __is_pod(S); // expected-error{{incomplete type 'S' used in type trait expression}}
   __is_pod(S[]); // expected-error{{incomplete type 'S' used in type trait expression}}
+
+  __is_trivially_copyable(S); // expected-error{{incomplete type 'S' used in type trait expression}}
+  __is_trivially_copyable(S[]); // expected-error{{incomplete type 'S' used in type trait expression}}
+
+  __is_trivially_relocatable(S); // expected-error{{incomplete type 'S' used in type trait expression}}
+  __is_trivially_relocatable(S[]); // expected-error{{incomplete type 'S' used in type trait expression}}
 }
diff --git a/clang/test/SemaCXX/type-traits-nonobject.cpp b/clang/test/SemaCXX/type-traits-nonobject.cpp
new file mode 100644
index 000000000000000..c9e3c30e5533d48
--- /dev/null
+++ b/clang/test/SemaCXX/type-traits-nonobject.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
+
+// expected-no-diagnostics
+
+static_assert(!__is_pod(void), "");
+static_assert(!__is_pod(int&), "");
+static_assert(!__is_pod(int()), "");
+
+static_assert(!__is_trivially_copyable(void), "");
+static_assert(!__is_trivially_copyable(int&), "");
+static_assert(!__is_trivially_copyable(int()), "");
+
+static_assert(!__is_trivially_relocatable(void), "");
+static_assert(!__is_trivially_relocatable(int&), "");
+static_assert(!__is_trivially_relocatable(int()), "");



More information about the cfe-commits mailing list