aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergio Martins <smartins@kde.org>2019-05-12 15:51:41 +0100
committerSergio Martins <smartins@kde.org>2019-05-12 15:51:41 +0100
commite1c8faffca38256ad27cd13c2d8e33c92c092b79 (patch)
tree91e30caefa2834cac0162217b0b04b692b33adbc
parent2ea365b34208e6c7c7349cfd1b0063f75dd48af0 (diff)
Add TypeUtils::isSmallTrivial()
-rw-r--r--src/TypeUtils.cpp36
-rw-r--r--src/TypeUtils.h8
2 files changed, 44 insertions, 0 deletions
diff --git a/src/TypeUtils.cpp b/src/TypeUtils.cpp
index 78987794..887e6494 100644
--- a/src/TypeUtils.cpp
+++ b/src/TypeUtils.cpp
@@ -79,6 +79,42 @@ bool TypeUtils::classifyQualType(const ClazyContext *context, clang::QualType qu
return true;
}
+bool TypeUtils::isSmallTrivial(const ClazyContext *context, QualType qualType)
+{
+ if (qualType.isNull())
+ return false;
+
+ if (qualType->isPointerType())
+ qualType = qualType->getPointeeType();
+
+ if (qualType->isPointerType()) // We don't care about ** (We can change this whenever we have a use case)
+ return false;
+
+ QualType unrefQualType = TypeUtils::unrefQualType(qualType);
+ const Type *paramType = unrefQualType.getTypePtrOrNull();
+ if (!paramType || paramType->isIncompleteType())
+ return false;
+
+ if (isUndeducibleAuto(paramType))
+ return false;
+
+ if (qualType->isRValueReferenceType()) // && ref, nothing to do here
+ return false;
+
+ CXXRecordDecl *recordDecl = paramType->getAsCXXRecordDecl();
+ CXXMethodDecl *copyCtor = recordDecl ? Utils::copyCtor(recordDecl) : nullptr;
+ const bool hasDeletedCopyCtor = copyCtor && copyCtor->isDeleted();
+ const bool isTrivial = recordDecl && !recordDecl->hasNonTrivialCopyConstructor() && !recordDecl->hasNonTrivialDestructor() && !hasDeletedCopyCtor;
+
+ if (isTrivial) {
+ const auto typeSize = context->astContext.getTypeSize(unrefQualType) / 8;
+ const bool isSmall = typeSize <= 16;
+ return isSmall;
+ }
+
+ return false;
+}
+
void TypeUtils::heapOrStackAllocated(Expr *arg, const std::string &type,
const clang::LangOptions &lo,
bool &isStack, bool &isHeap)
diff --git a/src/TypeUtils.h b/src/TypeUtils.h
index f1f7d6b5..686283af 100644
--- a/src/TypeUtils.h
+++ b/src/TypeUtils.h
@@ -83,6 +83,14 @@ bool classifyQualType(const ClazyContext *context, clang::QualType qualType, con
clang::Stmt *body = nullptr);
/**
+ * @brief Lighter version of classifyQualType in case you just want to know if it's small and trivially copyable&destructible
+ * @param context The clazy context
+ * @param qualType The QualType we're testing.
+ * @return true if the type specified by QualType (or its pointee) are small and trivially copyable/destructible.
+ */
+bool isSmallTrivial(const ClazyContext *context, clang::QualType qualType);
+
+/**
* If qt is a reference, return it without a reference.
* If qt is not a reference, return qt.
*