[all-commits] [llvm/llvm-project] aca372: [clang-tidy] Treat fields in anonymous records as ...

Sirui Mu via All-commits all-commits at lists.llvm.org
Tue Dec 26 06:49:56 PST 2023


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: aca3727e97bced3e98f8e0719de95c93034a97d2
      https://github.com/llvm/llvm-project/commit/aca3727e97bced3e98f8e0719de95c93034a97d2
  Author: Sirui Mu <msrlancern at gmail.com>
  Date:   2023-12-26 (Tue, 26 Dec 2023)

  Changed paths:
    M clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
    M clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h
    M clang-tools-extra/clang-tidy/utils/ASTUtils.cpp
    M clang-tools-extra/clang-tidy/utils/ASTUtils.h
    M clang-tools-extra/docs/ReleaseNotes.rst
    M clang-tools-extra/docs/clang-tidy/checks/readability/identifier-naming.rst
    A clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-anon-record-fields.cpp

  Log Message:
  -----------
  [clang-tidy] Treat fields in anonymous records as names in enclosing scope when checking name styles (#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.




More information about the All-commits mailing list