clang-tools 22.0.0git
TypeTraits.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "TypeTraits.h"
10#include "clang/AST/ASTContext.h"
11#include "clang/AST/DeclCXX.h"
12#include <optional>
13
15
16static bool classHasTrivialCopyAndDestroy(QualType Type) {
17 auto *Record = Type->getAsCXXRecordDecl();
18 return Record && Record->hasDefinition() &&
19 !Record->hasNonTrivialCopyConstructor() &&
20 !Record->hasNonTrivialDestructor();
21}
22
23static bool hasDeletedCopyConstructor(QualType Type) {
24 auto *Record = Type->getAsCXXRecordDecl();
25 if (!Record || !Record->hasDefinition())
26 return false;
27 return llvm::any_of(Record->ctors(), [](const auto *Constructor) {
28 return Constructor->isCopyConstructor() && Constructor->isDeleted();
29 });
30}
31
32std::optional<bool> isExpensiveToCopy(QualType Type,
33 const ASTContext &Context) {
34 if (Type->isDependentType() || Type->isIncompleteType())
35 return std::nullopt;
36 return !Type.isTriviallyCopyableType(Context) &&
38 !hasDeletedCopyConstructor(Type) && !Type->isObjCLifetimeType();
39}
40
41bool recordIsTriviallyDefaultConstructible(const RecordDecl &RecordDecl,
42 const ASTContext &Context) {
43 const auto *ClassDecl = dyn_cast<CXXRecordDecl>(&RecordDecl);
44 // Non-C++ records are always trivially constructible.
45 if (!ClassDecl)
46 return true;
47 // It is impossible to determine whether an ill-formed decl is trivially
48 // constructible.
49 if (RecordDecl.isInvalidDecl())
50 return false;
51 // A class with a user-provided default constructor is not trivially
52 // constructible.
53 if (ClassDecl->hasUserProvidedDefaultConstructor())
54 return false;
55 // A polymorphic class is not trivially constructible
56 if (ClassDecl->isPolymorphic())
57 return false;
58 // A class is trivially constructible if it has a trivial default constructor.
59 if (ClassDecl->hasTrivialDefaultConstructor())
60 return true;
61
62 // If all its fields are trivially constructible and have no default
63 // initializers.
64 for (const FieldDecl *Field : ClassDecl->fields()) {
65 if (Field->hasInClassInitializer())
66 return false;
67 if (!isTriviallyDefaultConstructible(Field->getType(), Context))
68 return false;
69 }
70 // If all its direct bases are trivially constructible.
71 return llvm::all_of(ClassDecl->bases(), [&](const CXXBaseSpecifier &Base) {
72 return isTriviallyDefaultConstructible(Base.getType(), Context) &&
73 !Base.isVirtual();
74 });
75}
76
77// Based on QualType::isTrivial.
78bool isTriviallyDefaultConstructible(QualType Type, const ASTContext &Context) {
79 if (Type.isNull())
80 return false;
81
82 if (Type->isArrayType())
83 return isTriviallyDefaultConstructible(Context.getBaseElementType(Type),
84 Context);
85
86 // Return false for incomplete types after skipping any incomplete array
87 // types which are expressly allowed by the standard and thus our API.
88 if (Type->isIncompleteType())
89 return false;
90
91 if (Context.getLangOpts().ObjCAutoRefCount) {
92 switch (Type.getObjCLifetime()) {
93 case Qualifiers::OCL_ExplicitNone:
94 return true;
95
96 case Qualifiers::OCL_Strong:
97 case Qualifiers::OCL_Weak:
98 case Qualifiers::OCL_Autoreleasing:
99 return false;
100
101 case Qualifiers::OCL_None:
102 if (Type->isObjCLifetimeType())
103 return false;
104 break;
105 }
106 }
107
108 const QualType CanonicalType = Type.getCanonicalType();
109 if (CanonicalType->isDependentType())
110 return false;
111
112 // As an extension, Clang treats vector types as Scalar types.
113 if (CanonicalType->isScalarType() || CanonicalType->isVectorType())
114 return true;
115
116 if (const auto *RD = CanonicalType->getAsRecordDecl()) {
117 return recordIsTriviallyDefaultConstructible(*RD, Context);
118 }
119
120 // No other types can match.
121 return false;
122}
123
124// Based on QualType::isDestructedType.
125bool isTriviallyDestructible(QualType Type) {
126 if (Type.isNull())
127 return false;
128
129 if (Type->isIncompleteType())
130 return false;
131
132 if (Type.getCanonicalType()->isDependentType())
133 return false;
134
135 return Type.isDestructedType() == QualType::DK_none;
136}
137
138bool hasNonTrivialMoveConstructor(QualType Type) {
139 auto *Record = Type->getAsCXXRecordDecl();
140 return Record && Record->hasDefinition() &&
141 Record->hasNonTrivialMoveConstructor();
142}
143
144bool hasNonTrivialMoveAssignment(QualType Type) {
145 auto *Record = Type->getAsCXXRecordDecl();
146 return Record && Record->hasDefinition() &&
147 Record->hasNonTrivialMoveAssignment();
148}
149
150} // namespace clang::tidy::utils::type_traits
bool isTriviallyDefaultConstructible(QualType Type, const ASTContext &Context)
Returns true if Type is trivially default constructible.
bool recordIsTriviallyDefaultConstructible(const RecordDecl &RecordDecl, const ASTContext &Context)
Returns true if RecordDecl is trivially default constructible.
std::optional< bool > isExpensiveToCopy(QualType Type, const ASTContext &Context)
Returns true if Type is expensive to copy.
static bool classHasTrivialCopyAndDestroy(QualType Type)
bool hasNonTrivialMoveAssignment(QualType Type)
Return true if Type has a non-trivial move assignment operator.
static bool hasDeletedCopyConstructor(QualType Type)
bool hasNonTrivialMoveConstructor(QualType Type)
Returns true if Type has a non-trivial move constructor.
bool isTriviallyDestructible(QualType Type)
Returns true if Type is trivially destructible.