[clang] [Clang] Improve testing for the flexible array member (PR #89462)

2024-04-24 Thread Bill Wendling via cfe-commits

https://github.com/bwendling closed 
https://github.com/llvm/llvm-project/pull/89462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Improve testing for the flexible array member (PR #89462)

2024-04-23 Thread Eli Friedman via cfe-commits

https://github.com/efriedma-quic approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/89462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Improve testing for the flexible array member (PR #89462)

2024-04-22 Thread Bill Wendling via cfe-commits

https://github.com/bwendling updated 
https://github.com/llvm/llvm-project/pull/89462

>From 2a6b3356a977132459bed84fb4e4add631e181cb Mon Sep 17 00:00:00 2001
From: Bill Wendling 
Date: Fri, 19 Apr 2024 15:06:34 -0700
Subject: [PATCH 1/2] [Clang][NFC] Improve testing for the flexible array
 member

Testing for the name of the flexible array member isn't as robust as
testing the FieldDecl pointers.
---
 clang/lib/CodeGen/CGBuiltin.cpp | 21 -
 clang/lib/CodeGen/CodeGenFunction.h | 12 ++--
 2 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 4ab844d206e48a..0d2b5e1b5120ae 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -822,8 +822,9 @@ CodeGenFunction::evaluateOrEmitBuiltinObjectSize(const Expr 
*E, unsigned Type,
   return ConstantInt::get(ResType, ObjectSize, /*isSigned=*/true);
 }
 
-const FieldDecl *CodeGenFunction::FindFlexibleArrayMemberField(
-ASTContext , const RecordDecl *RD, StringRef Name, uint64_t ) {
+const FieldDecl *CodeGenFunction::FindFlexibleArrayMemberFieldAndOffset(
+ASTContext , const RecordDecl *RD, const FieldDecl *FAMDecl,
+uint64_t ) {
   const LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel =
   getLangOpts().getStrictFlexArraysLevel();
   uint32_t FieldNo = 0;
@@ -832,7 +833,7 @@ const FieldDecl 
*CodeGenFunction::FindFlexibleArrayMemberField(
 return nullptr;
 
   for (const FieldDecl *FD : RD->fields()) {
-if ((Name.empty() || FD->getNameAsString() == Name) &&
+if ((!FAMDecl || FD == FAMDecl) &&
 Decl::isFlexibleArrayMemberLike(
 Ctx, FD, FD->getType(), StrictFlexArraysLevel,
 /*IgnoreTemplateOrMacroSubstitution=*/true)) {
@@ -843,8 +844,8 @@ const FieldDecl 
*CodeGenFunction::FindFlexibleArrayMemberField(
 
 QualType Ty = FD->getType();
 if (Ty->isRecordType()) {
-  if (const FieldDecl *Field = FindFlexibleArrayMemberField(
-  Ctx, Ty->getAsRecordDecl(), Name, Offset)) {
+  if (const FieldDecl *Field = FindFlexibleArrayMemberFieldAndOffset(
+  Ctx, Ty->getAsRecordDecl(), FAMDecl, Offset)) {
 const ASTRecordLayout  = Ctx.getASTRecordLayout(RD);
 Offset += Layout.getFieldOffset(FieldNo);
 return Field;
@@ -930,12 +931,12 @@ CodeGenFunction::emitFlexibleArrayMemberSize(const Expr 
*E, unsigned Type,
 
   // Get the flexible array member Decl.
   const RecordDecl *OuterRD = nullptr;
-  std::string FAMName;
+  const FieldDecl *FAMDecl = nullptr;
   if (const auto *ME = dyn_cast(Base)) {
 // Check if \p Base is referencing the FAM itself.
 const ValueDecl *VD = ME->getMemberDecl();
 OuterRD = VD->getDeclContext()->getOuterLexicalRecordContext();
-FAMName = VD->getNameAsString();
+FAMDecl = dyn_cast(VD);
   } else if (const auto *DRE = dyn_cast(Base)) {
 // Check if we're pointing to the whole struct.
 QualType Ty = DRE->getDecl()->getType();
@@ -974,9 +975,11 @@ CodeGenFunction::emitFlexibleArrayMemberSize(const Expr 
*E, unsigned Type,
   if (!OuterRD)
 return nullptr;
 
+  // We call FindFlexibleArrayMemberAndOffset even if FAMDecl is non-null to
+  // get its offset.
   uint64_t Offset = 0;
-  const FieldDecl *FAMDecl =
-  FindFlexibleArrayMemberField(Ctx, OuterRD, FAMName, Offset);
+  FAMDecl =
+  FindFlexibleArrayMemberFieldAndOffset(Ctx, OuterRD, FAMDecl, Offset);
   Offset = Ctx.toCharUnitsFromBits(Offset).getQuantity();
 
   if (!FAMDecl || !FAMDecl->getType()->isCountAttributedType())
diff --git a/clang/lib/CodeGen/CodeGenFunction.h 
b/clang/lib/CodeGen/CodeGenFunction.h
index ff1873325d409f..a751649cdb597a 100644
--- a/clang/lib/CodeGen/CodeGenFunction.h
+++ b/clang/lib/CodeGen/CodeGenFunction.h
@@ -3204,12 +3204,12 @@ class CodeGenFunction : public CodeGenTypeCache {
llvm::Value *Index, QualType IndexType,
QualType IndexedType, bool Accessed);
 
-  // Find a struct's flexible array member. It may be embedded inside multiple
-  // sub-structs, but must still be the last field.
-  const FieldDecl *FindFlexibleArrayMemberField(ASTContext ,
-const RecordDecl *RD,
-StringRef Name,
-uint64_t );
+  // Find a struct's flexible array member and get its offset. It may be
+  // embedded inside multiple sub-structs, but must still be the last field.
+  const FieldDecl *
+  FindFlexibleArrayMemberFieldAndOffset(ASTContext , const RecordDecl *RD,
+const FieldDecl *FAMDecl,
+uint64_t );
 
   /// Find the FieldDecl specified in a FAM's "counted_by" attribute. Returns
   /// \p nullptr if either the attribute or the field doesn't exist.

>From a12d8a360b00823fac57e43dd6a05dbe3ee91b53 Mon 

[clang] [Clang] Improve testing for the flexible array member (PR #89462)

2024-04-19 Thread Bill Wendling via cfe-commits


@@ -930,12 +931,12 @@ CodeGenFunction::emitFlexibleArrayMemberSize(const Expr 
*E, unsigned Type,
 
   // Get the flexible array member Decl.
   const RecordDecl *OuterRD = nullptr;
-  std::string FAMName;
+  const FieldDecl *FAMDecl = nullptr;
   if (const auto *ME = dyn_cast(Base)) {
 // Check if \p Base is referencing the FAM itself.
 const ValueDecl *VD = ME->getMemberDecl();
 OuterRD = VD->getDeclContext()->getOuterLexicalRecordContext();
-FAMName = VD->getNameAsString();
+FAMDecl = dyn_cast(VD);

bwendling wrote:

Wouldn't hurt. Done.

https://github.com/llvm/llvm-project/pull/89462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Improve testing for the flexible array member (PR #89462)

2024-04-19 Thread Bill Wendling via cfe-commits

https://github.com/bwendling updated 
https://github.com/llvm/llvm-project/pull/89462

>From 2a6b3356a977132459bed84fb4e4add631e181cb Mon Sep 17 00:00:00 2001
From: Bill Wendling 
Date: Fri, 19 Apr 2024 15:06:34 -0700
Subject: [PATCH 1/2] [Clang][NFC] Improve testing for the flexible array
 member

Testing for the name of the flexible array member isn't as robust as
testing the FieldDecl pointers.
---
 clang/lib/CodeGen/CGBuiltin.cpp | 21 -
 clang/lib/CodeGen/CodeGenFunction.h | 12 ++--
 2 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 4ab844d206e48a..0d2b5e1b5120ae 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -822,8 +822,9 @@ CodeGenFunction::evaluateOrEmitBuiltinObjectSize(const Expr 
*E, unsigned Type,
   return ConstantInt::get(ResType, ObjectSize, /*isSigned=*/true);
 }
 
-const FieldDecl *CodeGenFunction::FindFlexibleArrayMemberField(
-ASTContext , const RecordDecl *RD, StringRef Name, uint64_t ) {
+const FieldDecl *CodeGenFunction::FindFlexibleArrayMemberFieldAndOffset(
+ASTContext , const RecordDecl *RD, const FieldDecl *FAMDecl,
+uint64_t ) {
   const LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel =
   getLangOpts().getStrictFlexArraysLevel();
   uint32_t FieldNo = 0;
@@ -832,7 +833,7 @@ const FieldDecl 
*CodeGenFunction::FindFlexibleArrayMemberField(
 return nullptr;
 
   for (const FieldDecl *FD : RD->fields()) {
-if ((Name.empty() || FD->getNameAsString() == Name) &&
+if ((!FAMDecl || FD == FAMDecl) &&
 Decl::isFlexibleArrayMemberLike(
 Ctx, FD, FD->getType(), StrictFlexArraysLevel,
 /*IgnoreTemplateOrMacroSubstitution=*/true)) {
@@ -843,8 +844,8 @@ const FieldDecl 
*CodeGenFunction::FindFlexibleArrayMemberField(
 
 QualType Ty = FD->getType();
 if (Ty->isRecordType()) {
-  if (const FieldDecl *Field = FindFlexibleArrayMemberField(
-  Ctx, Ty->getAsRecordDecl(), Name, Offset)) {
+  if (const FieldDecl *Field = FindFlexibleArrayMemberFieldAndOffset(
+  Ctx, Ty->getAsRecordDecl(), FAMDecl, Offset)) {
 const ASTRecordLayout  = Ctx.getASTRecordLayout(RD);
 Offset += Layout.getFieldOffset(FieldNo);
 return Field;
@@ -930,12 +931,12 @@ CodeGenFunction::emitFlexibleArrayMemberSize(const Expr 
*E, unsigned Type,
 
   // Get the flexible array member Decl.
   const RecordDecl *OuterRD = nullptr;
-  std::string FAMName;
+  const FieldDecl *FAMDecl = nullptr;
   if (const auto *ME = dyn_cast(Base)) {
 // Check if \p Base is referencing the FAM itself.
 const ValueDecl *VD = ME->getMemberDecl();
 OuterRD = VD->getDeclContext()->getOuterLexicalRecordContext();
-FAMName = VD->getNameAsString();
+FAMDecl = dyn_cast(VD);
   } else if (const auto *DRE = dyn_cast(Base)) {
 // Check if we're pointing to the whole struct.
 QualType Ty = DRE->getDecl()->getType();
@@ -974,9 +975,11 @@ CodeGenFunction::emitFlexibleArrayMemberSize(const Expr 
*E, unsigned Type,
   if (!OuterRD)
 return nullptr;
 
+  // We call FindFlexibleArrayMemberAndOffset even if FAMDecl is non-null to
+  // get its offset.
   uint64_t Offset = 0;
-  const FieldDecl *FAMDecl =
-  FindFlexibleArrayMemberField(Ctx, OuterRD, FAMName, Offset);
+  FAMDecl =
+  FindFlexibleArrayMemberFieldAndOffset(Ctx, OuterRD, FAMDecl, Offset);
   Offset = Ctx.toCharUnitsFromBits(Offset).getQuantity();
 
   if (!FAMDecl || !FAMDecl->getType()->isCountAttributedType())
diff --git a/clang/lib/CodeGen/CodeGenFunction.h 
b/clang/lib/CodeGen/CodeGenFunction.h
index ff1873325d409f..a751649cdb597a 100644
--- a/clang/lib/CodeGen/CodeGenFunction.h
+++ b/clang/lib/CodeGen/CodeGenFunction.h
@@ -3204,12 +3204,12 @@ class CodeGenFunction : public CodeGenTypeCache {
llvm::Value *Index, QualType IndexType,
QualType IndexedType, bool Accessed);
 
-  // Find a struct's flexible array member. It may be embedded inside multiple
-  // sub-structs, but must still be the last field.
-  const FieldDecl *FindFlexibleArrayMemberField(ASTContext ,
-const RecordDecl *RD,
-StringRef Name,
-uint64_t );
+  // Find a struct's flexible array member and get its offset. It may be
+  // embedded inside multiple sub-structs, but must still be the last field.
+  const FieldDecl *
+  FindFlexibleArrayMemberFieldAndOffset(ASTContext , const RecordDecl *RD,
+const FieldDecl *FAMDecl,
+uint64_t );
 
   /// Find the FieldDecl specified in a FAM's "counted_by" attribute. Returns
   /// \p nullptr if either the attribute or the field doesn't exist.

>From a12d8a360b00823fac57e43dd6a05dbe3ee91b53 Mon 

[clang] [Clang] Improve testing for the flexible array member (PR #89462)

2024-04-19 Thread Bill Wendling via cfe-commits

bwendling wrote:

A quick look at `Decl` seems like it would require adding a 
`PrevInContextAndBits`, which would increase the size by `sizeof(Decl *)`. I 
might be able to massage a way to make it work, but it's probably not worth it.

https://github.com/llvm/llvm-project/pull/89462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Improve testing for the flexible array member (PR #89462)

2024-04-19 Thread Eli Friedman via cfe-commits


@@ -930,12 +931,12 @@ CodeGenFunction::emitFlexibleArrayMemberSize(const Expr 
*E, unsigned Type,
 
   // Get the flexible array member Decl.
   const RecordDecl *OuterRD = nullptr;
-  std::string FAMName;
+  const FieldDecl *FAMDecl = nullptr;
   if (const auto *ME = dyn_cast(Base)) {
 // Check if \p Base is referencing the FAM itself.
 const ValueDecl *VD = ME->getMemberDecl();
 OuterRD = VD->getDeclContext()->getOuterLexicalRecordContext();
-FAMName = VD->getNameAsString();
+FAMDecl = dyn_cast(VD);

efriedma-quic wrote:

If VD isn't a FieldDecl, should we return early here?

https://github.com/llvm/llvm-project/pull/89462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Improve testing for the flexible array member (PR #89462)

2024-04-19 Thread Eli Friedman via cfe-commits

efriedma-quic wrote:

If you can pull it off without increasing the size of Decl, sure; not sure it's 
worthwhile otherwise.

https://github.com/llvm/llvm-project/pull/89462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Improve testing for the flexible array member (PR #89462)

2024-04-19 Thread Bill Wendling via cfe-commits

bwendling wrote:

Would changing the `decl_iterator` into a bidirectional iterator be acceptable? 
That way I could grab the last `FieldDecl` to check if it's a FAM rather than 
iterating through unnecessary `Decls`...

https://github.com/llvm/llvm-project/pull/89462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Improve testing for the flexible array member (PR #89462)

2024-04-19 Thread Bill Wendling via cfe-commits

https://github.com/bwendling edited 
https://github.com/llvm/llvm-project/pull/89462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits