Skip to content

Commit

Permalink
Add isExpensiveToCopy AST matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
hach-que committed Jul 4, 2024
1 parent e7fceb7 commit 1012fab
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
33 changes: 33 additions & 0 deletions clang/include/clang/ASTMatchers/ASTMatchers.Unreal.h
Original file line number Diff line number Diff line change
Expand Up @@ -468,4 +468,37 @@ AST_MATCHER(ElaboratedTypeLoc, hasRedundantNamespacing) {
return false;
}

inline bool classHasTrivialCopyAndDestroy(QualType Type) {
auto *Record = Type->getAsCXXRecordDecl();
return Record && Record->hasDefinition() &&
!Record->hasNonTrivialCopyConstructor() &&
!Record->hasNonTrivialDestructor();
}

inline bool hasDeletedCopyConstructor(QualType Type) {
auto *Record = Type->getAsCXXRecordDecl();
if (!Record || !Record->hasDefinition())
return false;
for (const auto *Constructor : Record->ctors()) {
if (Constructor->isCopyConstructor() && Constructor->isDeleted())
return true;
}
return false;
}

inline std::optional<bool> isExpensiveToCopyNode(QualType Type,
const ASTContext &Context) {
if (Type->isDependentType() || Type->isIncompleteType())
return std::nullopt;
return !Type.isTriviallyCopyableType(Context) &&
!classHasTrivialCopyAndDestroy(Type) &&
!hasDeletedCopyConstructor(Type) && !Type->isObjCLifetimeType();
}

AST_MATCHER(QualType, isExpensiveToCopy) {
std::optional<bool> IsExpensive =
isExpensiveToCopyNode(Node, Finder->getASTContext());
return IsExpensive && *IsExpensive;
}

// @unreal: END
21 changes: 21 additions & 0 deletions clang/lib/ASTMatchers/Dynamic/Registry.Unreal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @unreal: BEGIN
REGISTER_MATCHER(refersToPack);
REGISTER_MATCHER(forNone);
REGISTER_MATCHER(forNoDescendant);
REGISTER_MATCHER(isUClass);
REGISTER_MATCHER(isUStruct);
REGISTER_MATCHER(isUInterface);
REGISTER_MATCHER(isIInterface);
REGISTER_MATCHER(isUFunction);
REGISTER_MATCHER(isUProperty);
REGISTER_MATCHER(hasUSpecifier);
REGISTER_MATCHER(hasUSpecifierValue);
REGISTER_MATCHER(hasUMetadata);
REGISTER_MATCHER(hasUMetadataValue);
REGISTER_MATCHER(withIInterface);
REGISTER_MATCHER(withUInterface);
REGISTER_MATCHER(isMissingDllImportOrExport);
REGISTER_MATCHER(isPODType);
REGISTER_MATCHER(hasRedundantNamespacing);
REGISTER_MATCHER(isExpensiveToCopy);
// @unreal: END
18 changes: 2 additions & 16 deletions clang/lib/ASTMatchers/Dynamic/Registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,22 +380,6 @@ RegistryMaps::RegistryMaps() {
REGISTER_MATCHER(hasUnaryOperand);
REGISTER_MATCHER(hasUnarySelector);
REGISTER_MATCHER(isPODType);
// @unreal: BEGIN
REGISTER_MATCHER(isUClass);
REGISTER_MATCHER(isUStruct);
REGISTER_MATCHER(isUInterface);
REGISTER_MATCHER(isIInterface);
REGISTER_MATCHER(isUFunction);
REGISTER_MATCHER(isUProperty);
REGISTER_MATCHER(hasUSpecifier);
REGISTER_MATCHER(hasUSpecifierValue);
REGISTER_MATCHER(hasUMetadata);
REGISTER_MATCHER(hasUMetadataValue);
REGISTER_MATCHER(withIInterface);
REGISTER_MATCHER(withUInterface);
REGISTER_MATCHER(isMissingDllImportOrExport);
REGISTER_MATCHER(hasRedundantNamespacing);
// @unreal: END
REGISTER_MATCHER(hasUnderlyingDecl);
REGISTER_MATCHER(hasUnderlyingType);
REGISTER_MATCHER(hasUnqualifiedDesugaredType);
Expand Down Expand Up @@ -629,6 +613,8 @@ RegistryMaps::RegistryMaps() {
REGISTER_MATCHER(voidType);
REGISTER_MATCHER(whileStmt);
REGISTER_MATCHER(withInitializer);

#include "Registry.Unreal.h"
}

RegistryMaps::~RegistryMaps() = default;
Expand Down

0 comments on commit 1012fab

Please sign in to comment.