[clang-tools-extra] [clang-tidy] ignore uninitialized std::array in member init (PR #98134)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 9 02:06:36 PDT 2024


https://github.com/dsiroky created https://github.com/llvm/llvm-project/pull/98134

cppcoreguidelines-pro-type-member-init check has an option IgnoreArrays for ignoring uninitialized C arrays. This patch adds support for C++ std::array as well.

>From 4fa1f6b877b60777c48f973b896fc3a385bdaa8e Mon Sep 17 00:00:00 2001
From: David Siroky <david at siroky.cz>
Date: Tue, 9 Jul 2024 10:50:00 +0200
Subject: [PATCH] [clang-tidy] ignore uninitialized std::array in member init

cppcoreguidelines-pro-type-member-init check has an option IgnoreArrays
for ignoring uninitialized C arrays. This patch adds support for C++
std::array as well.
---
 .../ProTypeMemberInitCheck.cpp                | 24 ++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
index 9c3c7cc70c187..f5fa340cdb985 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
@@ -422,6 +422,28 @@ static const char *getInitializer(QualType QT, bool UseAssignment) {
   }
 }
 
+static bool isStdArray(QualType QT) {
+  const auto *RT = QT->getAs<RecordType>();
+  if (!RT)
+    return false;
+  const auto *RD = RT->getDecl();
+  if (!RD)
+    return false;
+
+  const IdentifierInfo *II = RD->getIdentifier();
+  if (!II)
+    return false;
+
+  if (II->getName() == "array") {
+    const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(RD->getDeclContext());
+    if (NS && NS->getName() == "std") {
+      return true;
+    }
+  }
+
+  return false;
+}
+
 void ProTypeMemberInitCheck::checkMissingMemberInitializer(
     ASTContext &Context, const CXXRecordDecl &ClassDecl,
     const CXXConstructorDecl *Ctor) {
@@ -435,7 +457,7 @@ void ProTypeMemberInitCheck::checkMissingMemberInitializer(
   bool AnyMemberHasInitPerUnion = false;
   forEachFieldWithFilter(ClassDecl, ClassDecl.fields(),
                          AnyMemberHasInitPerUnion, [&](const FieldDecl *F) {
-    if (IgnoreArrays && F->getType()->isArrayType())
+    if (IgnoreArrays && (F->getType()->isArrayType() || isStdArray(F->getType())))
       return;
     if (F->hasInClassInitializer() && F->getParent()->isUnion()) {
       AnyMemberHasInitPerUnion = true;



More information about the cfe-commits mailing list