[clang-tools-extra] [clang-tidy] Treat fields in anonymous records as names in enclosing scope when checking name styles (PR #75701)

2023-12-16 Thread Sirui Mu via cfe-commits

https://github.com/Lancern created 
https://github.com/llvm/llvm-project/pull/75701

Currently, fields in anonymous records are treated as normal record members 
during naming style check. This can be undesirable in certain situations since 
these fields are used just like names in their enclosing scopes:

```c++
class Foo {
  union {
int iv_;// warning: invalid case style for public member 'iv_'
float fv_;  // warning: invalid case style for public member 'fv_'
  };
};
```

`iv_` and `fv_` are used in the code like private members of `Foo` but their 
naming style comes from rules for public members.

This PR changes this behavior. It adds a new option `CheckAnonFieldInParent` to 
`readability-identifier-naming`. When set to `true`, fields in anonymous 
records will be treated as names in their enclosing scopes when checking name 
styles. Specifically:

- If the anonymous record is defined within the file scope or in a namespace 
scope, treat its fields as global variables when checking name styles;
- If the anonymous record is defined within a function, treat its fields as 
local variables when checking name styles;
- If the anonymous record is defined within a non-anonymous record, treat its 
fields as non-static record members when checking name styles.

>From 5d04ca8091fc81fad8e33355a0afcce290bf34f0 Mon Sep 17 00:00:00 2001
From: Sirui Mu 
Date: Sat, 16 Dec 2023 21:55:24 +0800
Subject: [PATCH 1/2] [clang-tidy] Check anonymous record field naming in
 enclosing scopes

Currently, fields in anonymous records are regarded as normal fields when
checking their name styles. Naming rules for class/struct/union member apply to
these fields. This commit changes this behavior:

- If the anonymous record is defined within the file scope or in a namespace
  scope, treat its fields as global variables when checking name styles;
- If the anonymous record is defined within a function, treat its fields as
  local variables when checking name styles;
- If the anonymous record is defined within a non-anonymous record, treat its
  fields as non-static record members when checking name styles.
---
 .../readability/IdentifierNamingCheck.cpp | 196 ++
 .../readability/IdentifierNamingCheck.h   |  13 ++
 .../clang-tidy/utils/ASTUtils.cpp |  24 +++
 clang-tools-extra/clang-tidy/utils/ASTUtils.h |   5 +
 .../identifier-naming-anon-record-fields.cpp  | 184 
 5 files changed, 341 insertions(+), 81 deletions(-)
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-anon-record-fields.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
index 03dcfa5f811095..0e18712fd27564 100644
--- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
@@ -9,6 +9,7 @@
 #include "IdentifierNamingCheck.h"
 
 #include "../GlobList.h"
+#include "../utils/ASTUtils.h"
 #include "clang/AST/CXXInheritance.h"
 #include "clang/Lex/PPCallbacks.h"
 #include "clang/Lex/Preprocessor.h"
@@ -1185,29 +1186,12 @@ StyleKind IdentifierNamingCheck::findStyleKind(
   }
 
   if (const auto *Decl = dyn_cast(D)) {
-QualType Type = Decl->getType();
-
-if (!Type.isNull() && Type.isConstQualified()) {
-  if (NamingStyles[SK_ConstantMember])
-return SK_ConstantMember;
-
-  if (NamingStyles[SK_Constant])
-return SK_Constant;
+const RecordDecl *Record = Decl->getParent();
+if (Record->isAnonymousStructOrUnion()) {
+  return findStyleKindForAnonField(Decl, NamingStyles);
 }
 
-if (Decl->getAccess() == AS_private && NamingStyles[SK_PrivateMember])
-  return SK_PrivateMember;
-
-if (Decl->getAccess() == AS_protected && NamingStyles[SK_ProtectedMember])
-  return SK_ProtectedMember;
-
-if (Decl->getAccess() == AS_public && NamingStyles[SK_PublicMember])
-  return SK_PublicMember;
-
-if (NamingStyles[SK_Member])
-  return SK_Member;
-
-return SK_Invalid;
+return findStyleKindForField(Decl, Decl->getType(), NamingStyles);
   }
 
   if (const auto *Decl = dyn_cast(D)) {
@@ -1244,66 +1228,7 @@ StyleKind IdentifierNamingCheck::findStyleKind(
   }
 
   if (const auto *Decl = dyn_cast(D)) {
-QualType Type = Decl->getType();
-
-if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable])
-  return SK_ConstexprVariable;
-
-if (!Type.isNull() && Type.isConstQualified()) {
-  if (Decl->isStaticDataMember() && NamingStyles[SK_ClassConstant])
-return SK_ClassConstant;
-
-  if (Decl->isFileVarDecl() && Type.getTypePtr()->isAnyPointerType() &&
-  NamingStyles[SK_GlobalConstantPointer])
-return SK_GlobalConstantPointer;
-
-  if (Decl->isFileVarDecl() && NamingStyles[SK_GlobalConstant])
-return SK_GlobalConstant;
-
-  if (Decl->isStaticLocal() && NamingStyles[

[clang-tools-extra] [clang-tidy] Treat fields in anonymous records as names in enclosing scope when checking name styles (PR #75701)

2023-12-16 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-tidy

Author: Sirui Mu (Lancern)


Changes

Currently, fields in anonymous records are treated as normal record members 
during naming style check. This can be undesirable in certain situations since 
these fields are used just like names in their enclosing scopes:

```c++
class Foo {
  union {
int iv_;// warning: invalid case style for public member 'iv_'
float fv_;  // warning: invalid case style for public member 'fv_'
  };
};
```

`iv_` and `fv_` are used in the code like private members of `Foo` but their 
naming style comes from rules for public members.

This PR changes this behavior. It adds a new option `CheckAnonFieldInParent` to 
`readability-identifier-naming`. When set to `true`, fields in anonymous 
records will be treated as names in their enclosing scopes when checking name 
styles. Specifically:

- If the anonymous record is defined within the file scope or in a namespace 
scope, treat its fields as global variables when checking name styles;
- If the anonymous record is defined within a function, treat its fields as 
local variables when checking name styles;
- If the anonymous record is defined within a non-anonymous record, treat its 
fields as non-static record members when checking name styles.

---

Patch is 22.95 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/75701.diff


5 Files Affected:

- (modified) clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp 
(+130-89) 
- (modified) clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h 
(+23-3) 
- (modified) clang-tools-extra/clang-tidy/utils/ASTUtils.cpp (+24) 
- (modified) clang-tools-extra/clang-tidy/utils/ASTUtils.h (+5) 
- (added) 
clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-anon-record-fields.cpp
 (+185) 


``diff
diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
index 03dcfa5f811095..e6f44dd51b4596 100644
--- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
@@ -9,6 +9,7 @@
 #include "IdentifierNamingCheck.h"
 
 #include "../GlobList.h"
+#include "../utils/ASTUtils.h"
 #include "clang/AST/CXXInheritance.h"
 #include "clang/Lex/PPCallbacks.h"
 #include "clang/Lex/Preprocessor.h"
@@ -286,7 +287,9 @@ IdentifierNamingCheck::FileStyle 
IdentifierNamingCheck::getFileStyleFromOptions(
 HPTOpt.value_or(IdentifierNamingCheck::HPT_Off));
   }
   bool IgnoreMainLike = Options.get("IgnoreMainLikeFunctions", false);
-  return {std::move(Styles), std::move(HNOption), IgnoreMainLike};
+  bool CheckAnonFieldInParent = Options.get("CheckAnonFieldInParent", false);
+  return {std::move(Styles), std::move(HNOption), IgnoreMainLike,
+  CheckAnonFieldInParent};
 }
 
 std::string IdentifierNamingCheck::HungarianNotation::getDeclTypeName(
@@ -859,6 +862,8 @@ void 
IdentifierNamingCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
   Options.store(Opts, "IgnoreFailedSplit", IgnoreFailedSplit);
   Options.store(Opts, "IgnoreMainLikeFunctions",
 MainFileStyle->isIgnoringMainLikeFunction());
+  Options.store(Opts, "CheckAnonFieldInParent",
+MainFileStyle->isCheckingAnonFieldInParentScope());
 }
 
 bool IdentifierNamingCheck::matchesStyle(
@@ -,7 +1116,7 @@ std::string IdentifierNamingCheck::fixupWithStyle(
 StyleKind IdentifierNamingCheck::findStyleKind(
 const NamedDecl *D,
 ArrayRef> NamingStyles,
-bool IgnoreMainLikeFunctions) const {
+bool IgnoreMainLikeFunctions, bool CheckAnonFieldInParentScope) const {
   assert(D && D->getIdentifier() && !D->getName().empty() && !D->isImplicit() 
&&
  "Decl must be an explicit identifier with a name.");
 
@@ -1185,29 +1190,14 @@ StyleKind IdentifierNamingCheck::findStyleKind(
   }
 
   if (const auto *Decl = dyn_cast(D)) {
-QualType Type = Decl->getType();
-
-if (!Type.isNull() && Type.isConstQualified()) {
-  if (NamingStyles[SK_ConstantMember])
-return SK_ConstantMember;
-
-  if (NamingStyles[SK_Constant])
-return SK_Constant;
+if (CheckAnonFieldInParentScope) {
+  const RecordDecl *Record = Decl->getParent();
+  if (Record->isAnonymousStructOrUnion()) {
+return findStyleKindForAnonField(Decl, NamingStyles);
+  }
 }
 
-if (Decl->getAccess() == AS_private && NamingStyles[SK_PrivateMember])
-  return SK_PrivateMember;
-
-if (Decl->getAccess() == AS_protected && NamingStyles[SK_ProtectedMember])
-  return SK_ProtectedMember;
-
-if (Decl->getAccess() == AS_public && NamingStyles[SK_PublicMember])
-  return SK_PublicMember;
-
-if (NamingStyles[SK_Member])
-  return SK_Member;
-
-return SK_Invalid;
+return findStyleKindForField(Decl, Decl->getType(), NamingStyles);
   }
 

[clang-tools-extra] [clang-tidy] Treat fields in anonymous records as names in enclosing scope when checking name styles (PR #75701)

2023-12-16 Thread via cfe-commits

EugeneZelenko wrote:

Please mention changes in Release Notes.

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


[clang-tools-extra] [clang-tidy] Treat fields in anonymous records as names in enclosing scope when checking name styles (PR #75701)

2023-12-16 Thread Sirui Mu via cfe-commits

https://github.com/Lancern updated 
https://github.com/llvm/llvm-project/pull/75701

>From 5d04ca8091fc81fad8e33355a0afcce290bf34f0 Mon Sep 17 00:00:00 2001
From: Sirui Mu 
Date: Sat, 16 Dec 2023 21:55:24 +0800
Subject: [PATCH 1/3] [clang-tidy] Check anonymous record field naming in
 enclosing scopes

Currently, fields in anonymous records are regarded as normal fields when
checking their name styles. Naming rules for class/struct/union member apply to
these fields. This commit changes this behavior:

- If the anonymous record is defined within the file scope or in a namespace
  scope, treat its fields as global variables when checking name styles;
- If the anonymous record is defined within a function, treat its fields as
  local variables when checking name styles;
- If the anonymous record is defined within a non-anonymous record, treat its
  fields as non-static record members when checking name styles.
---
 .../readability/IdentifierNamingCheck.cpp | 196 ++
 .../readability/IdentifierNamingCheck.h   |  13 ++
 .../clang-tidy/utils/ASTUtils.cpp |  24 +++
 clang-tools-extra/clang-tidy/utils/ASTUtils.h |   5 +
 .../identifier-naming-anon-record-fields.cpp  | 184 
 5 files changed, 341 insertions(+), 81 deletions(-)
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-anon-record-fields.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
index 03dcfa5f811095..0e18712fd27564 100644
--- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
@@ -9,6 +9,7 @@
 #include "IdentifierNamingCheck.h"
 
 #include "../GlobList.h"
+#include "../utils/ASTUtils.h"
 #include "clang/AST/CXXInheritance.h"
 #include "clang/Lex/PPCallbacks.h"
 #include "clang/Lex/Preprocessor.h"
@@ -1185,29 +1186,12 @@ StyleKind IdentifierNamingCheck::findStyleKind(
   }
 
   if (const auto *Decl = dyn_cast(D)) {
-QualType Type = Decl->getType();
-
-if (!Type.isNull() && Type.isConstQualified()) {
-  if (NamingStyles[SK_ConstantMember])
-return SK_ConstantMember;
-
-  if (NamingStyles[SK_Constant])
-return SK_Constant;
+const RecordDecl *Record = Decl->getParent();
+if (Record->isAnonymousStructOrUnion()) {
+  return findStyleKindForAnonField(Decl, NamingStyles);
 }
 
-if (Decl->getAccess() == AS_private && NamingStyles[SK_PrivateMember])
-  return SK_PrivateMember;
-
-if (Decl->getAccess() == AS_protected && NamingStyles[SK_ProtectedMember])
-  return SK_ProtectedMember;
-
-if (Decl->getAccess() == AS_public && NamingStyles[SK_PublicMember])
-  return SK_PublicMember;
-
-if (NamingStyles[SK_Member])
-  return SK_Member;
-
-return SK_Invalid;
+return findStyleKindForField(Decl, Decl->getType(), NamingStyles);
   }
 
   if (const auto *Decl = dyn_cast(D)) {
@@ -1244,66 +1228,7 @@ StyleKind IdentifierNamingCheck::findStyleKind(
   }
 
   if (const auto *Decl = dyn_cast(D)) {
-QualType Type = Decl->getType();
-
-if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable])
-  return SK_ConstexprVariable;
-
-if (!Type.isNull() && Type.isConstQualified()) {
-  if (Decl->isStaticDataMember() && NamingStyles[SK_ClassConstant])
-return SK_ClassConstant;
-
-  if (Decl->isFileVarDecl() && Type.getTypePtr()->isAnyPointerType() &&
-  NamingStyles[SK_GlobalConstantPointer])
-return SK_GlobalConstantPointer;
-
-  if (Decl->isFileVarDecl() && NamingStyles[SK_GlobalConstant])
-return SK_GlobalConstant;
-
-  if (Decl->isStaticLocal() && NamingStyles[SK_StaticConstant])
-return SK_StaticConstant;
-
-  if (Decl->isLocalVarDecl() && Type.getTypePtr()->isAnyPointerType() &&
-  NamingStyles[SK_LocalConstantPointer])
-return SK_LocalConstantPointer;
-
-  if (Decl->isLocalVarDecl() && NamingStyles[SK_LocalConstant])
-return SK_LocalConstant;
-
-  if (Decl->isFunctionOrMethodVarDecl() && NamingStyles[SK_LocalConstant])
-return SK_LocalConstant;
-
-  if (NamingStyles[SK_Constant])
-return SK_Constant;
-}
-
-if (Decl->isStaticDataMember() && NamingStyles[SK_ClassMember])
-  return SK_ClassMember;
-
-if (Decl->isFileVarDecl() && Type.getTypePtr()->isAnyPointerType() &&
-NamingStyles[SK_GlobalPointer])
-  return SK_GlobalPointer;
-
-if (Decl->isFileVarDecl() && NamingStyles[SK_GlobalVariable])
-  return SK_GlobalVariable;
-
-if (Decl->isStaticLocal() && NamingStyles[SK_StaticVariable])
-  return SK_StaticVariable;
-
-if (Decl->isLocalVarDecl() && Type.getTypePtr()->isAnyPointerType() &&
-NamingStyles[SK_LocalPointer])
-  return SK_LocalPointer;
-
-if (Decl->isLocalVarDecl() && NamingStyles[SK_LocalVariable])
-  re

[clang-tools-extra] [clang-tidy] Treat fields in anonymous records as names in enclosing scope when checking name styles (PR #75701)

2023-12-16 Thread via cfe-commits


@@ -128,6 +128,11 @@ Improvements to clang-tidy
   as a value for `-export-fixes` to export individual yaml files for each
   compilation unit.
 
+- A new option `readability-identifier-naming.CheckAnonFieldInParent` is 
added. When set

EugeneZelenko wrote:

Please keep alphabetical order (by check name) in this section. If entry for 
`readability-identifier-naming` exists, append statement to it.

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


[clang-tools-extra] [clang-tidy] Treat fields in anonymous records as names in enclosing scope when checking name styles (PR #75701)

2023-12-16 Thread Sirui Mu via cfe-commits

https://github.com/Lancern updated 
https://github.com/llvm/llvm-project/pull/75701

>From 5d04ca8091fc81fad8e33355a0afcce290bf34f0 Mon Sep 17 00:00:00 2001
From: Sirui Mu 
Date: Sat, 16 Dec 2023 21:55:24 +0800
Subject: [PATCH 1/4] [clang-tidy] Check anonymous record field naming in
 enclosing scopes

Currently, fields in anonymous records are regarded as normal fields when
checking their name styles. Naming rules for class/struct/union member apply to
these fields. This commit changes this behavior:

- If the anonymous record is defined within the file scope or in a namespace
  scope, treat its fields as global variables when checking name styles;
- If the anonymous record is defined within a function, treat its fields as
  local variables when checking name styles;
- If the anonymous record is defined within a non-anonymous record, treat its
  fields as non-static record members when checking name styles.
---
 .../readability/IdentifierNamingCheck.cpp | 196 ++
 .../readability/IdentifierNamingCheck.h   |  13 ++
 .../clang-tidy/utils/ASTUtils.cpp |  24 +++
 clang-tools-extra/clang-tidy/utils/ASTUtils.h |   5 +
 .../identifier-naming-anon-record-fields.cpp  | 184 
 5 files changed, 341 insertions(+), 81 deletions(-)
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-anon-record-fields.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
index 03dcfa5f811095..0e18712fd27564 100644
--- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
@@ -9,6 +9,7 @@
 #include "IdentifierNamingCheck.h"
 
 #include "../GlobList.h"
+#include "../utils/ASTUtils.h"
 #include "clang/AST/CXXInheritance.h"
 #include "clang/Lex/PPCallbacks.h"
 #include "clang/Lex/Preprocessor.h"
@@ -1185,29 +1186,12 @@ StyleKind IdentifierNamingCheck::findStyleKind(
   }
 
   if (const auto *Decl = dyn_cast(D)) {
-QualType Type = Decl->getType();
-
-if (!Type.isNull() && Type.isConstQualified()) {
-  if (NamingStyles[SK_ConstantMember])
-return SK_ConstantMember;
-
-  if (NamingStyles[SK_Constant])
-return SK_Constant;
+const RecordDecl *Record = Decl->getParent();
+if (Record->isAnonymousStructOrUnion()) {
+  return findStyleKindForAnonField(Decl, NamingStyles);
 }
 
-if (Decl->getAccess() == AS_private && NamingStyles[SK_PrivateMember])
-  return SK_PrivateMember;
-
-if (Decl->getAccess() == AS_protected && NamingStyles[SK_ProtectedMember])
-  return SK_ProtectedMember;
-
-if (Decl->getAccess() == AS_public && NamingStyles[SK_PublicMember])
-  return SK_PublicMember;
-
-if (NamingStyles[SK_Member])
-  return SK_Member;
-
-return SK_Invalid;
+return findStyleKindForField(Decl, Decl->getType(), NamingStyles);
   }
 
   if (const auto *Decl = dyn_cast(D)) {
@@ -1244,66 +1228,7 @@ StyleKind IdentifierNamingCheck::findStyleKind(
   }
 
   if (const auto *Decl = dyn_cast(D)) {
-QualType Type = Decl->getType();
-
-if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable])
-  return SK_ConstexprVariable;
-
-if (!Type.isNull() && Type.isConstQualified()) {
-  if (Decl->isStaticDataMember() && NamingStyles[SK_ClassConstant])
-return SK_ClassConstant;
-
-  if (Decl->isFileVarDecl() && Type.getTypePtr()->isAnyPointerType() &&
-  NamingStyles[SK_GlobalConstantPointer])
-return SK_GlobalConstantPointer;
-
-  if (Decl->isFileVarDecl() && NamingStyles[SK_GlobalConstant])
-return SK_GlobalConstant;
-
-  if (Decl->isStaticLocal() && NamingStyles[SK_StaticConstant])
-return SK_StaticConstant;
-
-  if (Decl->isLocalVarDecl() && Type.getTypePtr()->isAnyPointerType() &&
-  NamingStyles[SK_LocalConstantPointer])
-return SK_LocalConstantPointer;
-
-  if (Decl->isLocalVarDecl() && NamingStyles[SK_LocalConstant])
-return SK_LocalConstant;
-
-  if (Decl->isFunctionOrMethodVarDecl() && NamingStyles[SK_LocalConstant])
-return SK_LocalConstant;
-
-  if (NamingStyles[SK_Constant])
-return SK_Constant;
-}
-
-if (Decl->isStaticDataMember() && NamingStyles[SK_ClassMember])
-  return SK_ClassMember;
-
-if (Decl->isFileVarDecl() && Type.getTypePtr()->isAnyPointerType() &&
-NamingStyles[SK_GlobalPointer])
-  return SK_GlobalPointer;
-
-if (Decl->isFileVarDecl() && NamingStyles[SK_GlobalVariable])
-  return SK_GlobalVariable;
-
-if (Decl->isStaticLocal() && NamingStyles[SK_StaticVariable])
-  return SK_StaticVariable;
-
-if (Decl->isLocalVarDecl() && Type.getTypePtr()->isAnyPointerType() &&
-NamingStyles[SK_LocalPointer])
-  return SK_LocalPointer;
-
-if (Decl->isLocalVarDecl() && NamingStyles[SK_LocalVariable])
-  re

[clang-tools-extra] [clang-tidy] Treat fields in anonymous records as names in enclosing scope when checking name styles (PR #75701)

2023-12-26 Thread Piotr Zegar via cfe-commits


@@ -207,6 +208,31 @@ After if AggressiveDependentMemberLookup is `true`:
   }
 };
 
+.. option:: CheckAnonFieldInParent
+
+When set to `true`, fields in anonymous records (i.e. anonymous
+unions and structs) will be treated as names in the enclosing scope
+rather than public members of the anonymous record for the purpose
+of name checking.
+
+For example:
+
+.. code-block:: c++
+
+class Foo {
+private:
+  union {
+int iv_;
+float fv_;
+  };
+};
+
+If CheckAnonFieldInParent is `false`, you may get warnings that ``iv_`` and
+``fv_`` are not coherent to public member names, because ``iv_`` and ``fv_``
+are public members of the anonymous union. When CheckAnonFieldInParent is

PiotrZSL wrote:

:option:`CheckAnonFieldInParent`

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


[clang-tools-extra] [clang-tidy] Treat fields in anonymous records as names in enclosing scope when checking name styles (PR #75701)

2023-12-26 Thread Piotr Zegar via cfe-commits


@@ -207,6 +208,31 @@ After if AggressiveDependentMemberLookup is `true`:
   }
 };
 
+.. option:: CheckAnonFieldInParent
+
+When set to `true`, fields in anonymous records (i.e. anonymous
+unions and structs) will be treated as names in the enclosing scope
+rather than public members of the anonymous record for the purpose
+of name checking.
+
+For example:
+
+.. code-block:: c++
+
+class Foo {
+private:
+  union {
+int iv_;
+float fv_;
+  };
+};
+
+If CheckAnonFieldInParent is `false`, you may get warnings that ``iv_`` and

PiotrZSL wrote:

:option:`CheckAnonFieldInParent `

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


[clang-tools-extra] [clang-tidy] Treat fields in anonymous records as names in enclosing scope when checking name styles (PR #75701)

2023-12-26 Thread Piotr Zegar via cfe-commits

https://github.com/PiotrZSL approved this pull request.

LGTM

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


[clang-tools-extra] [clang-tidy] Treat fields in anonymous records as names in enclosing scope when checking name styles (PR #75701)

2023-12-26 Thread Piotr Zegar via cfe-commits

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


[clang-tools-extra] [clang-tidy] Treat fields in anonymous records as names in enclosing scope when checking name styles (PR #75701)

2023-12-26 Thread Sirui Mu via cfe-commits

https://github.com/Lancern updated 
https://github.com/llvm/llvm-project/pull/75701

>From 5d04ca8091fc81fad8e33355a0afcce290bf34f0 Mon Sep 17 00:00:00 2001
From: Sirui Mu 
Date: Sat, 16 Dec 2023 21:55:24 +0800
Subject: [PATCH 1/5] [clang-tidy] Check anonymous record field naming in
 enclosing scopes

Currently, fields in anonymous records are regarded as normal fields when
checking their name styles. Naming rules for class/struct/union member apply to
these fields. This commit changes this behavior:

- If the anonymous record is defined within the file scope or in a namespace
  scope, treat its fields as global variables when checking name styles;
- If the anonymous record is defined within a function, treat its fields as
  local variables when checking name styles;
- If the anonymous record is defined within a non-anonymous record, treat its
  fields as non-static record members when checking name styles.
---
 .../readability/IdentifierNamingCheck.cpp | 196 ++
 .../readability/IdentifierNamingCheck.h   |  13 ++
 .../clang-tidy/utils/ASTUtils.cpp |  24 +++
 clang-tools-extra/clang-tidy/utils/ASTUtils.h |   5 +
 .../identifier-naming-anon-record-fields.cpp  | 184 
 5 files changed, 341 insertions(+), 81 deletions(-)
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-anon-record-fields.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
index 03dcfa5f811095..0e18712fd27564 100644
--- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
@@ -9,6 +9,7 @@
 #include "IdentifierNamingCheck.h"
 
 #include "../GlobList.h"
+#include "../utils/ASTUtils.h"
 #include "clang/AST/CXXInheritance.h"
 #include "clang/Lex/PPCallbacks.h"
 #include "clang/Lex/Preprocessor.h"
@@ -1185,29 +1186,12 @@ StyleKind IdentifierNamingCheck::findStyleKind(
   }
 
   if (const auto *Decl = dyn_cast(D)) {
-QualType Type = Decl->getType();
-
-if (!Type.isNull() && Type.isConstQualified()) {
-  if (NamingStyles[SK_ConstantMember])
-return SK_ConstantMember;
-
-  if (NamingStyles[SK_Constant])
-return SK_Constant;
+const RecordDecl *Record = Decl->getParent();
+if (Record->isAnonymousStructOrUnion()) {
+  return findStyleKindForAnonField(Decl, NamingStyles);
 }
 
-if (Decl->getAccess() == AS_private && NamingStyles[SK_PrivateMember])
-  return SK_PrivateMember;
-
-if (Decl->getAccess() == AS_protected && NamingStyles[SK_ProtectedMember])
-  return SK_ProtectedMember;
-
-if (Decl->getAccess() == AS_public && NamingStyles[SK_PublicMember])
-  return SK_PublicMember;
-
-if (NamingStyles[SK_Member])
-  return SK_Member;
-
-return SK_Invalid;
+return findStyleKindForField(Decl, Decl->getType(), NamingStyles);
   }
 
   if (const auto *Decl = dyn_cast(D)) {
@@ -1244,66 +1228,7 @@ StyleKind IdentifierNamingCheck::findStyleKind(
   }
 
   if (const auto *Decl = dyn_cast(D)) {
-QualType Type = Decl->getType();
-
-if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable])
-  return SK_ConstexprVariable;
-
-if (!Type.isNull() && Type.isConstQualified()) {
-  if (Decl->isStaticDataMember() && NamingStyles[SK_ClassConstant])
-return SK_ClassConstant;
-
-  if (Decl->isFileVarDecl() && Type.getTypePtr()->isAnyPointerType() &&
-  NamingStyles[SK_GlobalConstantPointer])
-return SK_GlobalConstantPointer;
-
-  if (Decl->isFileVarDecl() && NamingStyles[SK_GlobalConstant])
-return SK_GlobalConstant;
-
-  if (Decl->isStaticLocal() && NamingStyles[SK_StaticConstant])
-return SK_StaticConstant;
-
-  if (Decl->isLocalVarDecl() && Type.getTypePtr()->isAnyPointerType() &&
-  NamingStyles[SK_LocalConstantPointer])
-return SK_LocalConstantPointer;
-
-  if (Decl->isLocalVarDecl() && NamingStyles[SK_LocalConstant])
-return SK_LocalConstant;
-
-  if (Decl->isFunctionOrMethodVarDecl() && NamingStyles[SK_LocalConstant])
-return SK_LocalConstant;
-
-  if (NamingStyles[SK_Constant])
-return SK_Constant;
-}
-
-if (Decl->isStaticDataMember() && NamingStyles[SK_ClassMember])
-  return SK_ClassMember;
-
-if (Decl->isFileVarDecl() && Type.getTypePtr()->isAnyPointerType() &&
-NamingStyles[SK_GlobalPointer])
-  return SK_GlobalPointer;
-
-if (Decl->isFileVarDecl() && NamingStyles[SK_GlobalVariable])
-  return SK_GlobalVariable;
-
-if (Decl->isStaticLocal() && NamingStyles[SK_StaticVariable])
-  return SK_StaticVariable;
-
-if (Decl->isLocalVarDecl() && Type.getTypePtr()->isAnyPointerType() &&
-NamingStyles[SK_LocalPointer])
-  return SK_LocalPointer;
-
-if (Decl->isLocalVarDecl() && NamingStyles[SK_LocalVariable])
-  re

[clang-tools-extra] [clang-tidy] Treat fields in anonymous records as names in enclosing scope when checking name styles (PR #75701)

2023-12-26 Thread Piotr Zegar via cfe-commits

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