[clang] Changed Checks from TriviallyCopyable to TriviallyCopyConstructible (PR #77194)
Bhuminjay Soni via cfe-commits
cfe-commits at lists.llvm.org
Sun Jan 7 02:52:55 PST 2024
https://github.com/11happy updated https://github.com/llvm/llvm-project/pull/77194
>From 3c8dfcf96e732f4546f8019c0111fd873b233162 Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Mon, 1 Jan 2024 19:53:45 +0530
Subject: [PATCH 01/28] Changed Checks from TriviallyCopyable to
TriviallyCopyConstructible
Signed-off-by: 11happy <soni5happy at gmail.com>
---
clang/include/clang/AST/DeclCXX.h | 3 +++
clang/include/clang/AST/Type.h | 3 +++
clang/lib/AST/DeclCXX.cpp | 13 ++++++++++
clang/lib/AST/Type.cpp | 43 +++++++++++++++++++++++++++++++
clang/lib/Sema/SemaStmt.cpp | 2 +-
5 files changed, 63 insertions(+), 1 deletion(-)
diff --git a/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h
index 432293583576b5..3ac0d6a9e083cd 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -1425,6 +1425,9 @@ class CXXRecordDecl : public RecordDecl {
/// (C++11 [class]p6).
bool isTriviallyCopyable() const;
+ /// Determine whether this class is considered trivially copyable per
+ bool isTriviallyCopyConstructible() const;
+
/// Determine whether this class is considered trivial.
///
/// C++11 [class]p6:
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 1afa693672860f..bce2256f96a828 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -917,6 +917,9 @@ class QualType {
/// Return true if this is a trivially copyable type (C++0x [basic.types]p9)
bool isTriviallyCopyableType(const ASTContext &Context) const;
+ /// Return true if this is a trivially copyable type
+ bool isTriviallyCopyConstructibleType(const ASTContext &Context) const;
+
/// Return true if this is a trivially relocatable type.
bool isTriviallyRelocatableType(const ASTContext &Context) const;
diff --git a/clang/lib/AST/DeclCXX.cpp b/clang/lib/AST/DeclCXX.cpp
index c944862fcefeee..98b0a6dc28ea2f 100644
--- a/clang/lib/AST/DeclCXX.cpp
+++ b/clang/lib/AST/DeclCXX.cpp
@@ -587,6 +587,19 @@ bool CXXRecordDecl::isTriviallyCopyable() const {
return true;
}
+bool CXXRecordDecl::isTriviallyCopyConstructible() const {
+
+ // A trivially copy constructible class is a class that:
+ // -- has no non-trivial copy constructors,
+ if (hasNonTrivialCopyConstructor())
+ return false;
+ // -- has a trivial destructor.
+ if (!hasTrivialDestructor())
+ return false;
+
+ return true;
+}
+
void CXXRecordDecl::markedVirtualFunctionPure() {
// C++ [class.abstract]p2:
// A class is abstract if it has at least one pure virtual function.
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 160a725939ccd4..9c8b25798a0a95 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2644,6 +2644,49 @@ bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
return false;
}
+bool QualType::isTriviallyCopyConstructibleType(
+ const ASTContext &Context) const {
+ if ((*this)->isArrayType())
+ return Context.getBaseElementType(*this).isTriviallyCopyConstructibleType(
+ Context);
+
+ if (hasNonTrivialObjCLifetime())
+ return false;
+
+ // C++11 [basic.types]p9 - See Core 2094
+ // Scalar types, trivially copyable class types, arrays of such types, and
+ // cv-qualified versions of these types are collectively
+ // called trivially copy constructible types.
+
+ QualType CanonicalType = getCanonicalType();
+ if (CanonicalType->isDependentType())
+ return false;
+
+ if (CanonicalType->isSizelessBuiltinType())
+ return true;
+
+ // Return false for incomplete types after skipping any incomplete array types
+ // which are expressly allowed by the standard and thus our API.
+ if (CanonicalType->isIncompleteType())
+ return false;
+
+ // As an extension, Clang treats vector types as Scalar types.
+ if (CanonicalType->isScalarType() || CanonicalType->isVectorType())
+ return true;
+
+ if (const auto *RT = CanonicalType->getAs<RecordType>()) {
+ if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
+ if (!ClassDecl->isTriviallyCopyConstructible())
+ return false;
+ }
+
+ return true;
+ }
+
+ // No other types can match.
+ return false;
+}
+
bool QualType::isTriviallyRelocatableType(const ASTContext &Context) const {
QualType BaseElementType = Context.getBaseElementType(*this);
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index f0b03db690843a..21efe25ed84a3d 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -3200,7 +3200,7 @@ static void DiagnoseForRangeConstVariableCopies(Sema &SemaRef,
// (The function `getTypeSize` returns the size in bits.)
ASTContext &Ctx = SemaRef.Context;
if (Ctx.getTypeSize(VariableType) <= 64 * 8 &&
- (VariableType.isTriviallyCopyableType(Ctx) ||
+ (VariableType.isTriviallyCopyConstructibleType(Ctx) ||
hasTrivialABIAttr(VariableType)))
return;
>From 57b50cb0fc242d994558ddcb8226c3a785763e87 Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Mon, 1 Jan 2024 21:46:17 +0530
Subject: [PATCH 02/28] Added tests
Signed-off-by: 11happy <soni5happy at gmail.com>
---
...range-loop-analysis-trivially-copyable.cpp | 25 +++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp b/clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp
index e345ef40aed9ca..8fa387908c3658 100644
--- a/clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp
+++ b/clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp
@@ -33,6 +33,17 @@ void test_TriviallyCopyable_64_bytes() {
for (const auto r : records)
(void)r;
}
+void test_TriviallyCopyConstructible_64_bytes() {
+ struct Record {
+ char a[64];
+ Record& operator=(Record const& other){return *this;};
+
+ };
+
+ Record records[8];
+ for (const auto r : records)
+ (void)r;
+}
void test_TriviallyCopyable_65_bytes() {
struct Record {
@@ -47,6 +58,19 @@ void test_TriviallyCopyable_65_bytes() {
(void)r;
}
+void test_TriviallyCopyConstructible_65_bytes() {
+ struct Record {
+ char a[65];
+ Record& operator=(Record const& other){return *this;};
+
+ };
+ // expected-warning at +3 {{loop variable 'r' creates a copy from type 'const Record'}}
+ // expected-note at +2 {{use reference type 'const Record &' to prevent copying}}
+ Record records[8];
+ for (const auto r : records)
+ (void)r;
+}
+
void test_NonTriviallyCopyable() {
struct Record {
Record() {}
@@ -87,3 +111,4 @@ void test_TrivialABI_65_bytes() {
for (const auto r : records)
(void)r;
}
+
>From c346904caff1bf97605d84bdd0120cefe1ca35f4 Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Thu, 4 Jan 2024 20:10:04 +0530
Subject: [PATCH 03/28] implemented static non member
Signed-off-by: 11happy <soni5happy at gmail.com>
---
clang/include/clang/AST/Type.h | 3 ++
clang/lib/AST/Type.cpp | 59 ++++++++--------------------------
2 files changed, 16 insertions(+), 46 deletions(-)
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index bce2256f96a828..4532cfe63ebb1b 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -766,6 +766,8 @@ class QualType {
bool UseExcessPrecision(const ASTContext &Ctx);
+ static bool isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &Context,bool copy_constructible);
+
/// Retrieves a pointer to the underlying (unqualified) type.
///
/// This function requires that the type not be NULL. If the type might be
@@ -911,6 +913,7 @@ class QualType {
/// CXXRecordDecl::isCXX11StandardLayout, this takes DRs into account.
bool isCXX11PODType(const ASTContext &Context) const;
+
/// Return true if this is a trivial type per (C++0x [basic.types]p9)
bool isTrivialType(const ASTContext &Context) const;
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 9c8b25798a0a95..964df0bd1c97bf 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2605,52 +2605,18 @@ bool QualType::isTrivialType(const ASTContext &Context) const {
}
bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
- if ((*this)->isArrayType())
- return Context.getBaseElementType(*this).isTriviallyCopyableType(Context);
-
- if (hasNonTrivialObjCLifetime())
- return false;
-
- // C++11 [basic.types]p9 - See Core 2094
- // Scalar types, trivially copyable class types, arrays of such types, and
- // cv-qualified versions of these types are collectively
- // called trivially copyable types.
-
- QualType CanonicalType = getCanonicalType();
- if (CanonicalType->isDependentType())
- return false;
-
- if (CanonicalType->isSizelessBuiltinType())
- return true;
-
- // Return false for incomplete types after skipping any incomplete array types
- // which are expressly allowed by the standard and thus our API.
- if (CanonicalType->isIncompleteType())
- return false;
-
- // As an extension, Clang treats vector types as Scalar types.
- if (CanonicalType->isScalarType() || CanonicalType->isVectorType())
- return true;
-
- if (const auto *RT = CanonicalType->getAs<RecordType>()) {
- if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
- if (!ClassDecl->isTriviallyCopyable()) return false;
- }
-
- return true;
- }
+ return isTriviallyCopyableTypeImpl(*this,Context,false);
+}
- // No other types can match.
- return false;
+bool QualType::isTriviallyCopyConstructibleType(const ASTContext &Context) const {
+ return isTriviallyCopyableTypeImpl(*this,Context,true);
}
-bool QualType::isTriviallyCopyConstructibleType(
- const ASTContext &Context) const {
- if ((*this)->isArrayType())
- return Context.getBaseElementType(*this).isTriviallyCopyConstructibleType(
- Context);
+bool QualType::isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &Context,bool copy_constructible){
+ if (type->isArrayType())
+ return Context.getBaseElementType(type).isTriviallyCopyableTypeImpl(type,Context,copy_constructible);
- if (hasNonTrivialObjCLifetime())
+ if (type.hasNonTrivialObjCLifetime())
return false;
// C++11 [basic.types]p9 - See Core 2094
@@ -2658,7 +2624,7 @@ bool QualType::isTriviallyCopyConstructibleType(
// cv-qualified versions of these types are collectively
// called trivially copy constructible types.
- QualType CanonicalType = getCanonicalType();
+ QualType CanonicalType = type.getCanonicalType();
if (CanonicalType->isDependentType())
return false;
@@ -2676,13 +2642,14 @@ bool QualType::isTriviallyCopyConstructibleType(
if (const auto *RT = CanonicalType->getAs<RecordType>()) {
if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
- if (!ClassDecl->isTriviallyCopyConstructible())
- return false;
+ if (copy_constructible)
+ return ClassDecl->isTriviallyCopyConstructible();
+ else
+ return ClassDecl->isTriviallyCopyable();
}
return true;
}
-
// No other types can match.
return false;
}
>From 81daac9700f4a5eb63e57999e3b63a8839a0e2c3 Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Thu, 4 Jan 2024 20:25:39 +0530
Subject: [PATCH 04/28] Committing local changes before revert
---
clang/include/clang/AST/Type.h | 5 +++--
clang/lib/AST/Type.cpp | 14 +++++++++-----
2 files changed, 12 insertions(+), 7 deletions(-)
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 4532cfe63ebb1b..e4a3f7671e1076 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -766,7 +766,9 @@ class QualType {
bool UseExcessPrecision(const ASTContext &Ctx);
- static bool isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &Context,bool copy_constructible);
+ static bool isTriviallyCopyableTypeImpl(const QualType &type,
+ const ASTContext &Context,
+ bool copy_constructible);
/// Retrieves a pointer to the underlying (unqualified) type.
///
@@ -913,7 +915,6 @@ class QualType {
/// CXXRecordDecl::isCXX11StandardLayout, this takes DRs into account.
bool isCXX11PODType(const ASTContext &Context) const;
-
/// Return true if this is a trivial type per (C++0x [basic.types]p9)
bool isTrivialType(const ASTContext &Context) const;
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 964df0bd1c97bf..61661f73e1ea71 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2605,16 +2605,20 @@ bool QualType::isTrivialType(const ASTContext &Context) const {
}
bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
- return isTriviallyCopyableTypeImpl(*this,Context,false);
+ return isTriviallyCopyableTypeImpl(*this, Context, false);
}
-bool QualType::isTriviallyCopyConstructibleType(const ASTContext &Context) const {
- return isTriviallyCopyableTypeImpl(*this,Context,true);
+bool QualType::isTriviallyCopyConstructibleType(
+ const ASTContext &Context) const {
+ return isTriviallyCopyableTypeImpl(*this, Context, true);
}
-bool QualType::isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &Context,bool copy_constructible){
+bool QualType::isTriviallyCopyableTypeImpl(const QualType &type,
+ const ASTContext &Context,
+ bool copy_constructible) {
if (type->isArrayType())
- return Context.getBaseElementType(type).isTriviallyCopyableTypeImpl(type,Context,copy_constructible);
+ return Context.getBaseElementType(type).isTriviallyCopyableTypeImpl(
+ type, Context, copy_constructible);
if (type.hasNonTrivialObjCLifetime())
return false;
>From 723b9fd1716b0dead9541b82333ddf26ef68752c Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Thu, 4 Jan 2024 20:27:50 +0530
Subject: [PATCH 05/28] Revert "implemented static non member"
This reverts commit c346904caff1bf97605d84bdd0120cefe1ca35f4.
---
clang/include/clang/AST/Type.h | 4 +---
clang/lib/AST/Type.cpp | 25 ++++++++++---------------
2 files changed, 11 insertions(+), 18 deletions(-)
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index e4a3f7671e1076..7ccad032561810 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -766,9 +766,7 @@ class QualType {
bool UseExcessPrecision(const ASTContext &Ctx);
- static bool isTriviallyCopyableTypeImpl(const QualType &type,
- const ASTContext &Context,
- bool copy_constructible);
+ static bool isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &Context,bool copy_constructible);
/// Retrieves a pointer to the underlying (unqualified) type.
///
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 61661f73e1ea71..e0bbe6292704e1 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2605,22 +2605,18 @@ bool QualType::isTrivialType(const ASTContext &Context) const {
}
bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
- return isTriviallyCopyableTypeImpl(*this, Context, false);
+ return isTriviallyCopyableTypeImpl(*this,Context,false);
}
-bool QualType::isTriviallyCopyConstructibleType(
- const ASTContext &Context) const {
- return isTriviallyCopyableTypeImpl(*this, Context, true);
+bool QualType::isTriviallyCopyConstructibleType(const ASTContext &Context) const {
+ return isTriviallyCopyableTypeImpl(*this,Context,true);
}
-bool QualType::isTriviallyCopyableTypeImpl(const QualType &type,
- const ASTContext &Context,
- bool copy_constructible) {
+bool QualType::isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &Context,bool copy_constructible){
if (type->isArrayType())
- return Context.getBaseElementType(type).isTriviallyCopyableTypeImpl(
- type, Context, copy_constructible);
+ return Context.getBaseElementType(type).isTriviallyCopyableTypeImpl(type,Context,copy_constructible);
- if (type.hasNonTrivialObjCLifetime())
+ if (hasNonTrivialObjCLifetime())
return false;
// C++11 [basic.types]p9 - See Core 2094
@@ -2628,7 +2624,7 @@ bool QualType::isTriviallyCopyableTypeImpl(const QualType &type,
// cv-qualified versions of these types are collectively
// called trivially copy constructible types.
- QualType CanonicalType = type.getCanonicalType();
+ QualType CanonicalType = getCanonicalType();
if (CanonicalType->isDependentType())
return false;
@@ -2646,14 +2642,13 @@ bool QualType::isTriviallyCopyableTypeImpl(const QualType &type,
if (const auto *RT = CanonicalType->getAs<RecordType>()) {
if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
- if (copy_constructible)
- return ClassDecl->isTriviallyCopyConstructible();
- else
- return ClassDecl->isTriviallyCopyable();
+ if (!ClassDecl->isTriviallyCopyConstructible())
+ return false;
}
return true;
}
+
// No other types can match.
return false;
}
>From d3483b5d33ab087e2f5220cdc0f0c689fd6aba19 Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Thu, 4 Jan 2024 21:05:56 +0530
Subject: [PATCH 06/28] Revert "Committing local changes before revert"
This reverts commit 81daac9700f4a5eb63e57999e3b63a8839a0e2c3.
---
clang/include/clang/AST/Type.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 7ccad032561810..4532cfe63ebb1b 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -913,6 +913,7 @@ class QualType {
/// CXXRecordDecl::isCXX11StandardLayout, this takes DRs into account.
bool isCXX11PODType(const ASTContext &Context) const;
+
/// Return true if this is a trivial type per (C++0x [basic.types]p9)
bool isTrivialType(const ASTContext &Context) const;
>From 6c0936cfc9e7eb4be1a0d8b7b8bbaae0c05355bc Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Thu, 4 Jan 2024 22:08:42 +0530
Subject: [PATCH 07/28] Internal Static member
Signed-off-by: 11happy <soni5happy at gmail.com>
---
clang/include/clang/AST/Type.h | 2 --
clang/lib/AST/Type.cpp | 42 +++++++++++++++++++---------------
2 files changed, 24 insertions(+), 20 deletions(-)
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 4532cfe63ebb1b..bbcf3182b9f8f9 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -766,8 +766,6 @@ class QualType {
bool UseExcessPrecision(const ASTContext &Ctx);
- static bool isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &Context,bool copy_constructible);
-
/// Retrieves a pointer to the underlying (unqualified) type.
///
/// This function requires that the type not be NULL. If the type might be
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index e0bbe6292704e1..953bd8042a39db 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2604,19 +2604,11 @@ bool QualType::isTrivialType(const ASTContext &Context) const {
return false;
}
-bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
- return isTriviallyCopyableTypeImpl(*this,Context,false);
-}
-
-bool QualType::isTriviallyCopyConstructibleType(const ASTContext &Context) const {
- return isTriviallyCopyableTypeImpl(*this,Context,true);
-}
-
-bool QualType::isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &Context,bool copy_constructible){
+static bool isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &Context,bool copy_constructible){
if (type->isArrayType())
- return Context.getBaseElementType(type).isTriviallyCopyableTypeImpl(type,Context,copy_constructible);
+ return isTriviallyCopyableTypeImpl(Context.getBaseElementType(type),Context,copy_constructible);
- if (hasNonTrivialObjCLifetime())
+ if (type.hasNonTrivialObjCLifetime())
return false;
// C++11 [basic.types]p9 - See Core 2094
@@ -2624,7 +2616,7 @@ bool QualType::isTriviallyCopyableTypeImpl(const QualType &type, const ASTContex
// cv-qualified versions of these types are collectively
// called trivially copy constructible types.
- QualType CanonicalType = getCanonicalType();
+ QualType CanonicalType = type.getCanonicalType();
if (CanonicalType->isDependentType())
return false;
@@ -2642,17 +2634,31 @@ bool QualType::isTriviallyCopyableTypeImpl(const QualType &type, const ASTContex
if (const auto *RT = CanonicalType->getAs<RecordType>()) {
if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
- if (!ClassDecl->isTriviallyCopyConstructible())
- return false;
- }
+ if(copy_constructible){
+ return ClassDecl->isTriviallyCopyConstructible();
+ }
+ else{
+ return ClassDecl->isTriviallyCopyable();
- return true;
+ }
+ }
+ return true;
+
}
+// No other types can match.
+ return false;
+}
- // No other types can match.
- return false;
+bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
+ return isTriviallyCopyableTypeImpl(*this,Context,/*IsCopyConstructible=*/false);
+}
+
+bool QualType::isTriviallyCopyConstructibleType(const ASTContext &Context) const {
+ return isTriviallyCopyableTypeImpl(*this,Context,/*IsCopyConstructible=*/true);
}
+
+
bool QualType::isTriviallyRelocatableType(const ASTContext &Context) const {
QualType BaseElementType = Context.getBaseElementType(*this);
>From 41cfb3275a4f8918a33b54b16b1b06f268cfe40e Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Thu, 4 Jan 2024 22:10:25 +0530
Subject: [PATCH 08/28] Internal static member
Signed-off-by: 11happy <soni5happy at gmail.com>
---
clang/lib/AST/Type.cpp | 33 +++++++++++++++++----------------
1 file changed, 17 insertions(+), 16 deletions(-)
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 953bd8042a39db..77e58f97f4eaec 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2604,9 +2604,12 @@ bool QualType::isTrivialType(const ASTContext &Context) const {
return false;
}
-static bool isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &Context,bool copy_constructible){
+static bool isTriviallyCopyableTypeImpl(const QualType &type,
+ const ASTContext &Context,
+ bool copy_constructible) {
if (type->isArrayType())
- return isTriviallyCopyableTypeImpl(Context.getBaseElementType(type),Context,copy_constructible);
+ return isTriviallyCopyableTypeImpl(Context.getBaseElementType(type),
+ Context, copy_constructible);
if (type.hasNonTrivialObjCLifetime())
return false;
@@ -2634,31 +2637,29 @@ static bool isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &
if (const auto *RT = CanonicalType->getAs<RecordType>()) {
if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
- if(copy_constructible){
+ if (copy_constructible) {
return ClassDecl->isTriviallyCopyConstructible();
+ } else {
+ return ClassDecl->isTriviallyCopyable();
}
- else{
- return ClassDecl->isTriviallyCopyable();
-
- }
}
- return true;
-
+ return true;
}
-// No other types can match.
- return false;
+ // No other types can match.
+ return false;
}
bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
- return isTriviallyCopyableTypeImpl(*this,Context,/*IsCopyConstructible=*/false);
+ return isTriviallyCopyableTypeImpl(*this, Context,
+ /*IsCopyConstructible=*/false);
}
-bool QualType::isTriviallyCopyConstructibleType(const ASTContext &Context) const {
- return isTriviallyCopyableTypeImpl(*this,Context,/*IsCopyConstructible=*/true);
+bool QualType::isTriviallyCopyConstructibleType(
+ const ASTContext &Context) const {
+ return isTriviallyCopyableTypeImpl(*this, Context,
+ /*IsCopyConstructible=*/true);
}
-
-
bool QualType::isTriviallyRelocatableType(const ASTContext &Context) const {
QualType BaseElementType = Context.getBaseElementType(*this);
>From a2b208c908b96819f309dc5b733ec272b874b9ce Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Thu, 4 Jan 2024 22:16:06 +0530
Subject: [PATCH 09/28] Formatted the code
Signed-off-by: 11happy <soni5happy at gmail.com>
---
clang/include/clang/AST/Type.h | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index bbcf3182b9f8f9..bce2256f96a828 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -911,7 +911,6 @@ class QualType {
/// CXXRecordDecl::isCXX11StandardLayout, this takes DRs into account.
bool isCXX11PODType(const ASTContext &Context) const;
-
/// Return true if this is a trivial type per (C++0x [basic.types]p9)
bool isTrivialType(const ASTContext &Context) const;
>From c5847f70e79855ec43d8dbfebbcb12787fc0122d Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Fri, 5 Jan 2024 20:33:03 +0530
Subject: [PATCH 10/28] IsCopyConstructible
Signed-off-by: 11happy <soni5happy at gmail.com>
---
clang/lib/AST/Type.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 77e58f97f4eaec..0d9fdfa8c66883 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2606,10 +2606,10 @@ bool QualType::isTrivialType(const ASTContext &Context) const {
static bool isTriviallyCopyableTypeImpl(const QualType &type,
const ASTContext &Context,
- bool copy_constructible) {
+ bool IsCopyConstructible) {
if (type->isArrayType())
return isTriviallyCopyableTypeImpl(Context.getBaseElementType(type),
- Context, copy_constructible);
+ Context, IsCopyConstructible);
if (type.hasNonTrivialObjCLifetime())
return false;
@@ -2637,7 +2637,7 @@ static bool isTriviallyCopyableTypeImpl(const QualType &type,
if (const auto *RT = CanonicalType->getAs<RecordType>()) {
if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
- if (copy_constructible) {
+ if (IsCopyConstructible) {
return ClassDecl->isTriviallyCopyConstructible();
} else {
return ClassDecl->isTriviallyCopyable();
@@ -2651,7 +2651,7 @@ static bool isTriviallyCopyableTypeImpl(const QualType &type,
bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
return isTriviallyCopyableTypeImpl(*this, Context,
- /*IsCopyConstructible=*/false);
+ /*IsCopyConstructible*/false);
}
bool QualType::isTriviallyCopyConstructibleType(
>From 5a0309668a8cb6281f42d13fae2aaa96ff0ba280 Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Fri, 5 Jan 2024 20:51:48 +0530
Subject: [PATCH 11/28] Formatted the code
Signed-off-by: 11happy <soni5happy at gmail.com>
---
clang/lib/AST/Type.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 0d9fdfa8c66883..38ba1e377042c8 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2651,7 +2651,7 @@ static bool isTriviallyCopyableTypeImpl(const QualType &type,
bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
return isTriviallyCopyableTypeImpl(*this, Context,
- /*IsCopyConstructible*/false);
+ /*IsCopyConstructible*/ false);
}
bool QualType::isTriviallyCopyConstructibleType(
>From 7a44b585e42bf54316a70c0f189bfadbd0124a2f Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Fri, 5 Jan 2024 22:03:50 +0530
Subject: [PATCH 12/28] Added Release notes
Signed-off-by: 11happy <soni5happy at gmail.com>
---
clang/docs/ReleaseNotes.rst | 3 +++
1 file changed, 3 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 0c8fec691bf3c9..c66d55bc71ae4f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -856,6 +856,9 @@ Bug Fixes to AST Handling
- Fixed a bug where RecursiveASTVisitor fails to visit the
initializer of a bitfield.
`Issue 64916 <https://github.com/llvm/llvm-project/issues/64916>`_
+- Fixed a bug where range-loop-analysis checks for trivial copyability,
+ rather than trivial copy-constructibility
+ `Issue 47355 <https://github.com/llvm/llvm-project/issues/47355>`
Miscellaneous Bug Fixes
^^^^^^^^^^^^^^^^^^^^^^^
>From 73823769393451c9e886d3997047a6641f14ee0c Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Sat, 6 Jan 2024 16:48:50 +0530
Subject: [PATCH 13/28] Minor Fix
Signed-off-by: 11happy <soni5happy at gmail.com>
---
clang/lib/AST/Type.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 38ba1e377042c8..314bb8569e3033 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2651,7 +2651,7 @@ static bool isTriviallyCopyableTypeImpl(const QualType &type,
bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
return isTriviallyCopyableTypeImpl(*this, Context,
- /*IsCopyConstructible*/ false);
+ /*IsCopyConstructible=*/false);
}
bool QualType::isTriviallyCopyConstructibleType(
>From 658ea09da28dff2cd78179b73eaaa0e458b19252 Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Mon, 1 Jan 2024 19:53:45 +0530
Subject: [PATCH 14/28] Changed Checks from TriviallyCopyable to
TriviallyCopyConstructible
Signed-off-by: 11happy <soni5happy at gmail.com>
---
clang/include/clang/AST/DeclCXX.h | 3 +++
clang/include/clang/AST/Type.h | 3 +++
clang/lib/AST/DeclCXX.cpp | 13 ++++++++++
clang/lib/AST/Type.cpp | 43 +++++++++++++++++++++++++++++++
clang/lib/Sema/SemaStmt.cpp | 2 +-
5 files changed, 63 insertions(+), 1 deletion(-)
diff --git a/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h
index 984a4d8bab5e77..648f5f94640870 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -1425,6 +1425,9 @@ class CXXRecordDecl : public RecordDecl {
/// (C++11 [class]p6).
bool isTriviallyCopyable() const;
+ /// Determine whether this class is considered trivially copyable per
+ bool isTriviallyCopyConstructible() const;
+
/// Determine whether this class is considered trivial.
///
/// C++11 [class]p6:
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 9e9f896ebef7a6..d4e5310fb3abc6 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -917,6 +917,9 @@ class QualType {
/// Return true if this is a trivially copyable type (C++0x [basic.types]p9)
bool isTriviallyCopyableType(const ASTContext &Context) const;
+ /// Return true if this is a trivially copyable type
+ bool isTriviallyCopyConstructibleType(const ASTContext &Context) const;
+
/// Return true if this is a trivially relocatable type.
bool isTriviallyRelocatableType(const ASTContext &Context) const;
diff --git a/clang/lib/AST/DeclCXX.cpp b/clang/lib/AST/DeclCXX.cpp
index c944862fcefeee..98b0a6dc28ea2f 100644
--- a/clang/lib/AST/DeclCXX.cpp
+++ b/clang/lib/AST/DeclCXX.cpp
@@ -587,6 +587,19 @@ bool CXXRecordDecl::isTriviallyCopyable() const {
return true;
}
+bool CXXRecordDecl::isTriviallyCopyConstructible() const {
+
+ // A trivially copy constructible class is a class that:
+ // -- has no non-trivial copy constructors,
+ if (hasNonTrivialCopyConstructor())
+ return false;
+ // -- has a trivial destructor.
+ if (!hasTrivialDestructor())
+ return false;
+
+ return true;
+}
+
void CXXRecordDecl::markedVirtualFunctionPure() {
// C++ [class.abstract]p2:
// A class is abstract if it has at least one pure virtual function.
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index a894d3289eb185..7cf973bd6c2bb8 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2644,6 +2644,49 @@ bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
return false;
}
+bool QualType::isTriviallyCopyConstructibleType(
+ const ASTContext &Context) const {
+ if ((*this)->isArrayType())
+ return Context.getBaseElementType(*this).isTriviallyCopyConstructibleType(
+ Context);
+
+ if (hasNonTrivialObjCLifetime())
+ return false;
+
+ // C++11 [basic.types]p9 - See Core 2094
+ // Scalar types, trivially copyable class types, arrays of such types, and
+ // cv-qualified versions of these types are collectively
+ // called trivially copy constructible types.
+
+ QualType CanonicalType = getCanonicalType();
+ if (CanonicalType->isDependentType())
+ return false;
+
+ if (CanonicalType->isSizelessBuiltinType())
+ return true;
+
+ // Return false for incomplete types after skipping any incomplete array types
+ // which are expressly allowed by the standard and thus our API.
+ if (CanonicalType->isIncompleteType())
+ return false;
+
+ // As an extension, Clang treats vector types as Scalar types.
+ if (CanonicalType->isScalarType() || CanonicalType->isVectorType())
+ return true;
+
+ if (const auto *RT = CanonicalType->getAs<RecordType>()) {
+ if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
+ if (!ClassDecl->isTriviallyCopyConstructible())
+ return false;
+ }
+
+ return true;
+ }
+
+ // No other types can match.
+ return false;
+}
+
bool QualType::isTriviallyRelocatableType(const ASTContext &Context) const {
QualType BaseElementType = Context.getBaseElementType(*this);
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index f0b03db690843a..21efe25ed84a3d 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -3200,7 +3200,7 @@ static void DiagnoseForRangeConstVariableCopies(Sema &SemaRef,
// (The function `getTypeSize` returns the size in bits.)
ASTContext &Ctx = SemaRef.Context;
if (Ctx.getTypeSize(VariableType) <= 64 * 8 &&
- (VariableType.isTriviallyCopyableType(Ctx) ||
+ (VariableType.isTriviallyCopyConstructibleType(Ctx) ||
hasTrivialABIAttr(VariableType)))
return;
>From 31d1de11cbd3b386cd0fbc711019e6afef7878ca Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Mon, 1 Jan 2024 21:46:17 +0530
Subject: [PATCH 15/28] Added tests
Signed-off-by: 11happy <soni5happy at gmail.com>
---
...range-loop-analysis-trivially-copyable.cpp | 25 +++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp b/clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp
index e345ef40aed9ca..8fa387908c3658 100644
--- a/clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp
+++ b/clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp
@@ -33,6 +33,17 @@ void test_TriviallyCopyable_64_bytes() {
for (const auto r : records)
(void)r;
}
+void test_TriviallyCopyConstructible_64_bytes() {
+ struct Record {
+ char a[64];
+ Record& operator=(Record const& other){return *this;};
+
+ };
+
+ Record records[8];
+ for (const auto r : records)
+ (void)r;
+}
void test_TriviallyCopyable_65_bytes() {
struct Record {
@@ -47,6 +58,19 @@ void test_TriviallyCopyable_65_bytes() {
(void)r;
}
+void test_TriviallyCopyConstructible_65_bytes() {
+ struct Record {
+ char a[65];
+ Record& operator=(Record const& other){return *this;};
+
+ };
+ // expected-warning at +3 {{loop variable 'r' creates a copy from type 'const Record'}}
+ // expected-note at +2 {{use reference type 'const Record &' to prevent copying}}
+ Record records[8];
+ for (const auto r : records)
+ (void)r;
+}
+
void test_NonTriviallyCopyable() {
struct Record {
Record() {}
@@ -87,3 +111,4 @@ void test_TrivialABI_65_bytes() {
for (const auto r : records)
(void)r;
}
+
>From c9170598e4445881e7fe8af83c28a334f417020f Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Thu, 4 Jan 2024 20:10:04 +0530
Subject: [PATCH 16/28] implemented static non member
Signed-off-by: 11happy <soni5happy at gmail.com>
---
clang/include/clang/AST/Type.h | 3 ++
clang/lib/AST/Type.cpp | 59 ++++++++--------------------------
2 files changed, 16 insertions(+), 46 deletions(-)
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index d4e5310fb3abc6..12b8079999ca90 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -766,6 +766,8 @@ class QualType {
bool UseExcessPrecision(const ASTContext &Ctx);
+ static bool isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &Context,bool copy_constructible);
+
/// Retrieves a pointer to the underlying (unqualified) type.
///
/// This function requires that the type not be NULL. If the type might be
@@ -911,6 +913,7 @@ class QualType {
/// CXXRecordDecl::isCXX11StandardLayout, this takes DRs into account.
bool isCXX11PODType(const ASTContext &Context) const;
+
/// Return true if this is a trivial type per (C++0x [basic.types]p9)
bool isTrivialType(const ASTContext &Context) const;
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 7cf973bd6c2bb8..8382a301cae427 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2605,52 +2605,18 @@ bool QualType::isTrivialType(const ASTContext &Context) const {
}
bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
- if ((*this)->isArrayType())
- return Context.getBaseElementType(*this).isTriviallyCopyableType(Context);
-
- if (hasNonTrivialObjCLifetime())
- return false;
-
- // C++11 [basic.types]p9 - See Core 2094
- // Scalar types, trivially copyable class types, arrays of such types, and
- // cv-qualified versions of these types are collectively
- // called trivially copyable types.
-
- QualType CanonicalType = getCanonicalType();
- if (CanonicalType->isDependentType())
- return false;
-
- if (CanonicalType->isSizelessBuiltinType())
- return true;
-
- // Return false for incomplete types after skipping any incomplete array types
- // which are expressly allowed by the standard and thus our API.
- if (CanonicalType->isIncompleteType())
- return false;
-
- // As an extension, Clang treats vector types as Scalar types.
- if (CanonicalType->isScalarType() || CanonicalType->isVectorType())
- return true;
-
- if (const auto *RT = CanonicalType->getAs<RecordType>()) {
- if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
- if (!ClassDecl->isTriviallyCopyable()) return false;
- }
-
- return true;
- }
+ return isTriviallyCopyableTypeImpl(*this,Context,false);
+}
- // No other types can match.
- return false;
+bool QualType::isTriviallyCopyConstructibleType(const ASTContext &Context) const {
+ return isTriviallyCopyableTypeImpl(*this,Context,true);
}
-bool QualType::isTriviallyCopyConstructibleType(
- const ASTContext &Context) const {
- if ((*this)->isArrayType())
- return Context.getBaseElementType(*this).isTriviallyCopyConstructibleType(
- Context);
+bool QualType::isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &Context,bool copy_constructible){
+ if (type->isArrayType())
+ return Context.getBaseElementType(type).isTriviallyCopyableTypeImpl(type,Context,copy_constructible);
- if (hasNonTrivialObjCLifetime())
+ if (type.hasNonTrivialObjCLifetime())
return false;
// C++11 [basic.types]p9 - See Core 2094
@@ -2658,7 +2624,7 @@ bool QualType::isTriviallyCopyConstructibleType(
// cv-qualified versions of these types are collectively
// called trivially copy constructible types.
- QualType CanonicalType = getCanonicalType();
+ QualType CanonicalType = type.getCanonicalType();
if (CanonicalType->isDependentType())
return false;
@@ -2676,13 +2642,14 @@ bool QualType::isTriviallyCopyConstructibleType(
if (const auto *RT = CanonicalType->getAs<RecordType>()) {
if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
- if (!ClassDecl->isTriviallyCopyConstructible())
- return false;
+ if (copy_constructible)
+ return ClassDecl->isTriviallyCopyConstructible();
+ else
+ return ClassDecl->isTriviallyCopyable();
}
return true;
}
-
// No other types can match.
return false;
}
>From bd4b4490ae4338d4d5934b98a4a64f55713ab27f Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Thu, 4 Jan 2024 20:25:39 +0530
Subject: [PATCH 17/28] Committing local changes before revert
---
clang/include/clang/AST/Type.h | 5 +++--
clang/lib/AST/Type.cpp | 14 +++++++++-----
2 files changed, 12 insertions(+), 7 deletions(-)
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 12b8079999ca90..d36dff03c0467b 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -766,7 +766,9 @@ class QualType {
bool UseExcessPrecision(const ASTContext &Ctx);
- static bool isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &Context,bool copy_constructible);
+ static bool isTriviallyCopyableTypeImpl(const QualType &type,
+ const ASTContext &Context,
+ bool copy_constructible);
/// Retrieves a pointer to the underlying (unqualified) type.
///
@@ -913,7 +915,6 @@ class QualType {
/// CXXRecordDecl::isCXX11StandardLayout, this takes DRs into account.
bool isCXX11PODType(const ASTContext &Context) const;
-
/// Return true if this is a trivial type per (C++0x [basic.types]p9)
bool isTrivialType(const ASTContext &Context) const;
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 8382a301cae427..768a9ad3fd824b 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2605,16 +2605,20 @@ bool QualType::isTrivialType(const ASTContext &Context) const {
}
bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
- return isTriviallyCopyableTypeImpl(*this,Context,false);
+ return isTriviallyCopyableTypeImpl(*this, Context, false);
}
-bool QualType::isTriviallyCopyConstructibleType(const ASTContext &Context) const {
- return isTriviallyCopyableTypeImpl(*this,Context,true);
+bool QualType::isTriviallyCopyConstructibleType(
+ const ASTContext &Context) const {
+ return isTriviallyCopyableTypeImpl(*this, Context, true);
}
-bool QualType::isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &Context,bool copy_constructible){
+bool QualType::isTriviallyCopyableTypeImpl(const QualType &type,
+ const ASTContext &Context,
+ bool copy_constructible) {
if (type->isArrayType())
- return Context.getBaseElementType(type).isTriviallyCopyableTypeImpl(type,Context,copy_constructible);
+ return Context.getBaseElementType(type).isTriviallyCopyableTypeImpl(
+ type, Context, copy_constructible);
if (type.hasNonTrivialObjCLifetime())
return false;
>From a7ac8955929668927d9e71eaa5db13c36073e021 Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Thu, 4 Jan 2024 20:27:50 +0530
Subject: [PATCH 18/28] Revert "implemented static non member"
This reverts commit c346904caff1bf97605d84bdd0120cefe1ca35f4.
---
clang/include/clang/AST/Type.h | 4 +---
clang/lib/AST/Type.cpp | 25 ++++++++++---------------
2 files changed, 11 insertions(+), 18 deletions(-)
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index d36dff03c0467b..d0860dcdf38dc0 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -766,9 +766,7 @@ class QualType {
bool UseExcessPrecision(const ASTContext &Ctx);
- static bool isTriviallyCopyableTypeImpl(const QualType &type,
- const ASTContext &Context,
- bool copy_constructible);
+ static bool isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &Context,bool copy_constructible);
/// Retrieves a pointer to the underlying (unqualified) type.
///
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 768a9ad3fd824b..2f74026c2b8420 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2605,22 +2605,18 @@ bool QualType::isTrivialType(const ASTContext &Context) const {
}
bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
- return isTriviallyCopyableTypeImpl(*this, Context, false);
+ return isTriviallyCopyableTypeImpl(*this,Context,false);
}
-bool QualType::isTriviallyCopyConstructibleType(
- const ASTContext &Context) const {
- return isTriviallyCopyableTypeImpl(*this, Context, true);
+bool QualType::isTriviallyCopyConstructibleType(const ASTContext &Context) const {
+ return isTriviallyCopyableTypeImpl(*this,Context,true);
}
-bool QualType::isTriviallyCopyableTypeImpl(const QualType &type,
- const ASTContext &Context,
- bool copy_constructible) {
+bool QualType::isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &Context,bool copy_constructible){
if (type->isArrayType())
- return Context.getBaseElementType(type).isTriviallyCopyableTypeImpl(
- type, Context, copy_constructible);
+ return Context.getBaseElementType(type).isTriviallyCopyableTypeImpl(type,Context,copy_constructible);
- if (type.hasNonTrivialObjCLifetime())
+ if (hasNonTrivialObjCLifetime())
return false;
// C++11 [basic.types]p9 - See Core 2094
@@ -2628,7 +2624,7 @@ bool QualType::isTriviallyCopyableTypeImpl(const QualType &type,
// cv-qualified versions of these types are collectively
// called trivially copy constructible types.
- QualType CanonicalType = type.getCanonicalType();
+ QualType CanonicalType = getCanonicalType();
if (CanonicalType->isDependentType())
return false;
@@ -2646,14 +2642,13 @@ bool QualType::isTriviallyCopyableTypeImpl(const QualType &type,
if (const auto *RT = CanonicalType->getAs<RecordType>()) {
if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
- if (copy_constructible)
- return ClassDecl->isTriviallyCopyConstructible();
- else
- return ClassDecl->isTriviallyCopyable();
+ if (!ClassDecl->isTriviallyCopyConstructible())
+ return false;
}
return true;
}
+
// No other types can match.
return false;
}
>From 51659f680efc29227471c311c8c95a74e18dd203 Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Thu, 4 Jan 2024 21:05:56 +0530
Subject: [PATCH 19/28] Revert "Committing local changes before revert"
This reverts commit 81daac9700f4a5eb63e57999e3b63a8839a0e2c3.
---
clang/include/clang/AST/Type.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index d0860dcdf38dc0..12b8079999ca90 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -913,6 +913,7 @@ class QualType {
/// CXXRecordDecl::isCXX11StandardLayout, this takes DRs into account.
bool isCXX11PODType(const ASTContext &Context) const;
+
/// Return true if this is a trivial type per (C++0x [basic.types]p9)
bool isTrivialType(const ASTContext &Context) const;
>From b8416479097c3fb5028892fbe97a14b0aa9986a7 Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Thu, 4 Jan 2024 22:08:42 +0530
Subject: [PATCH 20/28] Internal Static member
Signed-off-by: 11happy <soni5happy at gmail.com>
---
clang/include/clang/AST/Type.h | 2 --
clang/lib/AST/Type.cpp | 42 +++++++++++++++++++---------------
2 files changed, 24 insertions(+), 20 deletions(-)
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 12b8079999ca90..9776850fe60348 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -766,8 +766,6 @@ class QualType {
bool UseExcessPrecision(const ASTContext &Ctx);
- static bool isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &Context,bool copy_constructible);
-
/// Retrieves a pointer to the underlying (unqualified) type.
///
/// This function requires that the type not be NULL. If the type might be
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 2f74026c2b8420..202e457fc83a63 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2604,19 +2604,11 @@ bool QualType::isTrivialType(const ASTContext &Context) const {
return false;
}
-bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
- return isTriviallyCopyableTypeImpl(*this,Context,false);
-}
-
-bool QualType::isTriviallyCopyConstructibleType(const ASTContext &Context) const {
- return isTriviallyCopyableTypeImpl(*this,Context,true);
-}
-
-bool QualType::isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &Context,bool copy_constructible){
+static bool isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &Context,bool copy_constructible){
if (type->isArrayType())
- return Context.getBaseElementType(type).isTriviallyCopyableTypeImpl(type,Context,copy_constructible);
+ return isTriviallyCopyableTypeImpl(Context.getBaseElementType(type),Context,copy_constructible);
- if (hasNonTrivialObjCLifetime())
+ if (type.hasNonTrivialObjCLifetime())
return false;
// C++11 [basic.types]p9 - See Core 2094
@@ -2624,7 +2616,7 @@ bool QualType::isTriviallyCopyableTypeImpl(const QualType &type, const ASTContex
// cv-qualified versions of these types are collectively
// called trivially copy constructible types.
- QualType CanonicalType = getCanonicalType();
+ QualType CanonicalType = type.getCanonicalType();
if (CanonicalType->isDependentType())
return false;
@@ -2642,17 +2634,31 @@ bool QualType::isTriviallyCopyableTypeImpl(const QualType &type, const ASTContex
if (const auto *RT = CanonicalType->getAs<RecordType>()) {
if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
- if (!ClassDecl->isTriviallyCopyConstructible())
- return false;
- }
+ if(copy_constructible){
+ return ClassDecl->isTriviallyCopyConstructible();
+ }
+ else{
+ return ClassDecl->isTriviallyCopyable();
- return true;
+ }
+ }
+ return true;
+
}
+// No other types can match.
+ return false;
+}
- // No other types can match.
- return false;
+bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
+ return isTriviallyCopyableTypeImpl(*this,Context,/*IsCopyConstructible=*/false);
+}
+
+bool QualType::isTriviallyCopyConstructibleType(const ASTContext &Context) const {
+ return isTriviallyCopyableTypeImpl(*this,Context,/*IsCopyConstructible=*/true);
}
+
+
bool QualType::isTriviallyRelocatableType(const ASTContext &Context) const {
QualType BaseElementType = Context.getBaseElementType(*this);
>From 8c3f1e14fcd93486140cbf0193eb9e4f11be7a86 Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Thu, 4 Jan 2024 22:10:25 +0530
Subject: [PATCH 21/28] Internal static member
Signed-off-by: 11happy <soni5happy at gmail.com>
---
clang/lib/AST/Type.cpp | 33 +++++++++++++++++----------------
1 file changed, 17 insertions(+), 16 deletions(-)
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 202e457fc83a63..43791e5f3d5ce2 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2604,9 +2604,12 @@ bool QualType::isTrivialType(const ASTContext &Context) const {
return false;
}
-static bool isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &Context,bool copy_constructible){
+static bool isTriviallyCopyableTypeImpl(const QualType &type,
+ const ASTContext &Context,
+ bool copy_constructible) {
if (type->isArrayType())
- return isTriviallyCopyableTypeImpl(Context.getBaseElementType(type),Context,copy_constructible);
+ return isTriviallyCopyableTypeImpl(Context.getBaseElementType(type),
+ Context, copy_constructible);
if (type.hasNonTrivialObjCLifetime())
return false;
@@ -2634,31 +2637,29 @@ static bool isTriviallyCopyableTypeImpl(const QualType &type, const ASTContext &
if (const auto *RT = CanonicalType->getAs<RecordType>()) {
if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
- if(copy_constructible){
+ if (copy_constructible) {
return ClassDecl->isTriviallyCopyConstructible();
+ } else {
+ return ClassDecl->isTriviallyCopyable();
}
- else{
- return ClassDecl->isTriviallyCopyable();
-
- }
}
- return true;
-
+ return true;
}
-// No other types can match.
- return false;
+ // No other types can match.
+ return false;
}
bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
- return isTriviallyCopyableTypeImpl(*this,Context,/*IsCopyConstructible=*/false);
+ return isTriviallyCopyableTypeImpl(*this, Context,
+ /*IsCopyConstructible=*/false);
}
-bool QualType::isTriviallyCopyConstructibleType(const ASTContext &Context) const {
- return isTriviallyCopyableTypeImpl(*this,Context,/*IsCopyConstructible=*/true);
+bool QualType::isTriviallyCopyConstructibleType(
+ const ASTContext &Context) const {
+ return isTriviallyCopyableTypeImpl(*this, Context,
+ /*IsCopyConstructible=*/true);
}
-
-
bool QualType::isTriviallyRelocatableType(const ASTContext &Context) const {
QualType BaseElementType = Context.getBaseElementType(*this);
>From 50054dad25bcd38bf3e18df81c8db854c5e99433 Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Thu, 4 Jan 2024 22:16:06 +0530
Subject: [PATCH 22/28] Formatted the code
Signed-off-by: 11happy <soni5happy at gmail.com>
---
clang/include/clang/AST/Type.h | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 9776850fe60348..d4e5310fb3abc6 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -911,7 +911,6 @@ class QualType {
/// CXXRecordDecl::isCXX11StandardLayout, this takes DRs into account.
bool isCXX11PODType(const ASTContext &Context) const;
-
/// Return true if this is a trivial type per (C++0x [basic.types]p9)
bool isTrivialType(const ASTContext &Context) const;
>From cbf03e254b340734daf61240da79029a03cbb6a9 Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Fri, 5 Jan 2024 20:33:03 +0530
Subject: [PATCH 23/28] IsCopyConstructible
Signed-off-by: 11happy <soni5happy at gmail.com>
---
clang/lib/AST/Type.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 43791e5f3d5ce2..651861a14e60d5 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2606,10 +2606,10 @@ bool QualType::isTrivialType(const ASTContext &Context) const {
static bool isTriviallyCopyableTypeImpl(const QualType &type,
const ASTContext &Context,
- bool copy_constructible) {
+ bool IsCopyConstructible) {
if (type->isArrayType())
return isTriviallyCopyableTypeImpl(Context.getBaseElementType(type),
- Context, copy_constructible);
+ Context, IsCopyConstructible);
if (type.hasNonTrivialObjCLifetime())
return false;
@@ -2637,7 +2637,7 @@ static bool isTriviallyCopyableTypeImpl(const QualType &type,
if (const auto *RT = CanonicalType->getAs<RecordType>()) {
if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
- if (copy_constructible) {
+ if (IsCopyConstructible) {
return ClassDecl->isTriviallyCopyConstructible();
} else {
return ClassDecl->isTriviallyCopyable();
@@ -2651,7 +2651,7 @@ static bool isTriviallyCopyableTypeImpl(const QualType &type,
bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
return isTriviallyCopyableTypeImpl(*this, Context,
- /*IsCopyConstructible=*/false);
+ /*IsCopyConstructible*/false);
}
bool QualType::isTriviallyCopyConstructibleType(
>From 158d239171186d7bcf063b2b0ed627a800817042 Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Fri, 5 Jan 2024 20:51:48 +0530
Subject: [PATCH 24/28] Formatted the code
Signed-off-by: 11happy <soni5happy at gmail.com>
---
clang/lib/AST/Type.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 651861a14e60d5..1c72e2ac0f74a8 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2651,7 +2651,7 @@ static bool isTriviallyCopyableTypeImpl(const QualType &type,
bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
return isTriviallyCopyableTypeImpl(*this, Context,
- /*IsCopyConstructible*/false);
+ /*IsCopyConstructible*/ false);
}
bool QualType::isTriviallyCopyConstructibleType(
>From 07d8bbf784c6dd7a087d2a227e7a93174ccaa285 Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Fri, 5 Jan 2024 22:03:50 +0530
Subject: [PATCH 25/28] Added Release notes
Signed-off-by: 11happy <soni5happy at gmail.com>
---
clang/docs/ReleaseNotes.rst | 3 ---
1 file changed, 3 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9b6e00b231216b..68a293e3b64815 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -870,9 +870,6 @@ Bug Fixes to AST Handling
- Fixed a bug where RecursiveASTVisitor fails to visit the
initializer of a bitfield.
`Issue 64916 <https://github.com/llvm/llvm-project/issues/64916>`_
-- Fixed a bug where Template Instantiation failed to handle Lambda Expressions
- with certain types of Attributes.
- (`#76521 <https://github.com/llvm/llvm-project/issues/76521>`_)
Miscellaneous Bug Fixes
^^^^^^^^^^^^^^^^^^^^^^^
>From d4b3eba3be99da9d51a8958a0ce1df43245fb92c Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Sat, 6 Jan 2024 16:48:50 +0530
Subject: [PATCH 26/28] Minor Fix
Signed-off-by: 11happy <soni5happy at gmail.com>
---
clang/lib/AST/Type.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 1c72e2ac0f74a8..b419fc8836b032 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2651,7 +2651,7 @@ static bool isTriviallyCopyableTypeImpl(const QualType &type,
bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
return isTriviallyCopyableTypeImpl(*this, Context,
- /*IsCopyConstructible*/ false);
+ /*IsCopyConstructible=*/false);
}
bool QualType::isTriviallyCopyConstructibleType(
>From 878c9b19fb3cc3135368a836bd03b7bb94cbacf5 Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Sat, 6 Jan 2024 17:45:13 +0530
Subject: [PATCH 27/28] Rebased
Signed-off-by: 11happy <soni5happy at gmail.com>
---
clang/docs/ReleaseNotes.rst | 3 +++
1 file changed, 3 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 68a293e3b64815..be9d17b255318e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -870,6 +870,9 @@ Bug Fixes to AST Handling
- Fixed a bug where RecursiveASTVisitor fails to visit the
initializer of a bitfield.
`Issue 64916 <https://github.com/llvm/llvm-project/issues/64916>`_
+- Fixed a bug where range-loop-analysis checks for trivial copyability,
+ rather than trivial copy-constructibility
+ `Issue 47355 <https://github.com/llvm/llvm-project/issues/47355>`_
Miscellaneous Bug Fixes
^^^^^^^^^^^^^^^^^^^^^^^
>From a5938afa8e6a7784ffe744a11fc0c85b98fb60a7 Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Sun, 7 Jan 2024 16:22:08 +0530
Subject: [PATCH 28/28] Corrected Release Notes
Signed-off-by: 11happy <soni5happy at gmail.com>
---
clang/docs/ReleaseNotes.rst | 3 +++
1 file changed, 3 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index be9d17b255318e..c5290804fd63b3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -873,6 +873,9 @@ Bug Fixes to AST Handling
- Fixed a bug where range-loop-analysis checks for trivial copyability,
rather than trivial copy-constructibility
`Issue 47355 <https://github.com/llvm/llvm-project/issues/47355>`_
+- Fixed a bug where Template Instantiation failed to handle Lambda Expressions
+ with certain types of Attributes.
+ (`#76521 <https://github.com/llvm/llvm-project/issues/76521>`_)
Miscellaneous Bug Fixes
^^^^^^^^^^^^^^^^^^^^^^^
More information about the cfe-commits
mailing list