[clang] Add 'isPODType' AST matcher (PR #86233)

June Rhodes via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 21 19:40:34 PDT 2024


https://github.com/hach-que created https://github.com/llvm/llvm-project/pull/86233

This adds an `isPODType` AST matcher which matches if the matched type is a Plain Old Data (POD) type.

Given:

```cpp
class Y
{ 
public:
    int a;
    std::string b;
};
```

the matcher `fieldDecl(hasType(qualType(isPODType())))` would match `Y::a` but not `Y::b`.

>From 87d02c2278e023b9439be96c2d34e934bcb57a26 Mon Sep 17 00:00:00 2001
From: June Rhodes <jrhodes at redpoint.games>
Date: Fri, 22 Mar 2024 13:36:54 +1100
Subject: [PATCH] Add 'isPODType' AST matcher

---
 clang/include/clang/ASTMatchers/ASTMatchers.h | 17 +++++++++++++++++
 clang/lib/ASTMatchers/Dynamic/Registry.cpp    |  1 +
 2 files changed, 18 insertions(+)

diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index 2f71053d030f68..a9e803eec570c7 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -4087,6 +4087,23 @@ AST_POLYMORPHIC_MATCHER_P(
   return Inner.matches(source->getTypeLoc(), Finder, Builder);
 }
 
+/// Matches if the matched type is a Plain Old Data (POD) type.
+///
+/// Given
+/// \code
+///   class Y
+///   { 
+///   public:
+///       int a;
+///       std::string b;
+///   };
+/// \endcode
+/// fieldDecl(hasType(qualType(isPODType())))
+///   matches Y::a
+AST_MATCHER(QualType, isPODType) {
+  return Node.isPODType(Finder->getASTContext());
+}
+
 /// Matches if the matched type is represented by the given string.
 ///
 /// Given
diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
index 2c75e6beb74301..4f716d9f166495 100644
--- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -377,6 +377,7 @@ RegistryMaps::RegistryMaps() {
   REGISTER_MATCHER(hasTypeLoc);
   REGISTER_MATCHER(hasUnaryOperand);
   REGISTER_MATCHER(hasUnarySelector);
+  REGISTER_MATCHER(isPODType);
   REGISTER_MATCHER(hasUnderlyingDecl);
   REGISTER_MATCHER(hasUnderlyingType);
   REGISTER_MATCHER(hasUnqualifiedDesugaredType);



More information about the cfe-commits mailing list