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

via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 21 19:41:21 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: June Rhodes (hach-que)

<details>
<summary>Changes</summary>

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`.

---
Full diff: https://github.com/llvm/llvm-project/pull/86233.diff


2 Files Affected:

- (modified) clang/include/clang/ASTMatchers/ASTMatchers.h (+17) 
- (modified) clang/lib/ASTMatchers/Dynamic/Registry.cpp (+1) 


``````````diff
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);

``````````

</details>


https://github.com/llvm/llvm-project/pull/86233


More information about the cfe-commits mailing list