1 //===--- TypeTraits.cpp - clang-tidy---------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "TypeTraits.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/DeclCXX.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
18 namespace type_traits {
22 bool classHasTrivialCopyAndDestroy(QualType Type) {
23 auto *Record = Type->getAsCXXRecordDecl();
24 return Record && Record->hasDefinition() &&
25 !Record->hasNonTrivialCopyConstructor() &&
26 !Record->hasNonTrivialDestructor();
29 bool hasDeletedCopyConstructor(QualType Type) {
30 auto *Record = Type->getAsCXXRecordDecl();
31 if (!Record || !Record->hasDefinition())
33 for (const auto *Constructor : Record->ctors()) {
34 if (Constructor->isCopyConstructor() && Constructor->isDeleted())
42 llvm::Optional<bool> isExpensiveToCopy(QualType Type,
43 const ASTContext &Context) {
44 if (Type->isDependentType() || Type->isIncompleteType())
46 return !Type.isTriviallyCopyableType(Context) &&
47 !classHasTrivialCopyAndDestroy(Type) &&
48 !hasDeletedCopyConstructor(Type) &&
49 !Type->isObjCLifetimeType();
52 bool recordIsTriviallyDefaultConstructible(const RecordDecl &RecordDecl,
53 const ASTContext &Context) {
54 const auto *ClassDecl = dyn_cast<CXXRecordDecl>(&RecordDecl);
55 // Non-C++ records are always trivially constructible.
58 // A class with a user-provided default constructor is not trivially
60 if (ClassDecl->hasUserProvidedDefaultConstructor())
62 // A polymorphic class is not trivially constructible
63 if (ClassDecl->isPolymorphic())
65 // A class is trivially constructible if it has a trivial default constructor.
66 if (ClassDecl->hasTrivialDefaultConstructor())
69 // If all its fields are trivially constructible and have no default
71 for (const FieldDecl *Field : ClassDecl->fields()) {
72 if (Field->hasInClassInitializer())
74 if (!isTriviallyDefaultConstructible(Field->getType(), Context))
77 // If all its direct bases are trivially constructible.
78 for (const CXXBaseSpecifier &Base : ClassDecl->bases()) {
79 if (!isTriviallyDefaultConstructible(Base.getType(), Context))
88 // Based on QualType::isTrivial.
89 bool isTriviallyDefaultConstructible(QualType Type, const ASTContext &Context) {
93 if (Type->isArrayType())
94 return isTriviallyDefaultConstructible(Context.getBaseElementType(Type),
97 // Return false for incomplete types after skipping any incomplete array
98 // types which are expressly allowed by the standard and thus our API.
99 if (Type->isIncompleteType())
102 if (Context.getLangOpts().ObjCAutoRefCount) {
103 switch (Type.getObjCLifetime()) {
104 case Qualifiers::OCL_ExplicitNone:
107 case Qualifiers::OCL_Strong:
108 case Qualifiers::OCL_Weak:
109 case Qualifiers::OCL_Autoreleasing:
112 case Qualifiers::OCL_None:
113 if (Type->isObjCLifetimeType())
119 QualType CanonicalType = Type.getCanonicalType();
120 if (CanonicalType->isDependentType())
123 // As an extension, Clang treats vector types as Scalar types.
124 if (CanonicalType->isScalarType() || CanonicalType->isVectorType())
127 if (const auto *RT = CanonicalType->getAs<RecordType>()) {
128 return recordIsTriviallyDefaultConstructible(*RT->getDecl(), Context);
131 // No other types can match.
135 bool hasNonTrivialMoveConstructor(QualType Type) {
136 auto *Record = Type->getAsCXXRecordDecl();
137 return Record && Record->hasDefinition() &&
138 Record->hasNonTrivialMoveConstructor();
141 bool hasNonTrivialMoveAssignment(QualType Type) {
142 auto *Record = Type->getAsCXXRecordDecl();
143 return Record && Record->hasDefinition() &&
144 Record->hasNonTrivialMoveAssignment();
147 } // namespace type_traits