Skip to content

Commit

Permalink
[clang-tidy] support unscoped enumerations in readability-static-acce…
Browse files Browse the repository at this point in the history
…ssed-through-instance

fixed [60810](#60810)
unscoped enumerations in class can also be checked by `readability-static-accessed-through-instance`
add matcher for `enumConstantDecl` to match format
```
struct {
    enum { E1 };
};
```
The filter of member expression and the fix hint should be same as other condition.

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D147315
  • Loading branch information
HerrCai0907 committed Apr 3, 2023
1 parent 4301178 commit edd6a33
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
Expand Up @@ -39,7 +39,8 @@ void StaticAccessedThroughInstanceCheck::storeOptions(
void StaticAccessedThroughInstanceCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
memberExpr(hasDeclaration(anyOf(cxxMethodDecl(isStaticStorageClass()),
varDecl(hasStaticStorageDuration()))))
varDecl(hasStaticStorageDuration()),
enumConstantDecl())))
.bind("memberExpression"),
this);
}
Expand All @@ -64,15 +65,15 @@ void StaticAccessedThroughInstanceCheck::check(
: BaseExpr->getType().getUnqualifiedType();

const ASTContext *AstContext = Result.Context;
PrintingPolicy PrintingPolicyWithSupressedTag(AstContext->getLangOpts());
PrintingPolicyWithSupressedTag.SuppressTagKeyword = true;
PrintingPolicyWithSupressedTag.SuppressUnwrittenScope = true;
PrintingPolicy PrintingPolicyWithSuppressedTag(AstContext->getLangOpts());
PrintingPolicyWithSuppressedTag.SuppressTagKeyword = true;
PrintingPolicyWithSuppressedTag.SuppressUnwrittenScope = true;

PrintingPolicyWithSupressedTag.PrintCanonicalTypes =
PrintingPolicyWithSuppressedTag.PrintCanonicalTypes =
!BaseExpr->getType()->isTypedefNameType();

std::string BaseTypeName =
BaseType.getAsString(PrintingPolicyWithSupressedTag);
BaseType.getAsString(PrintingPolicyWithSuppressedTag);

// Do not warn for CUDA built-in variables.
if (StringRef(BaseTypeName).startswith("__cuda_builtin_"))
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Expand Up @@ -254,6 +254,10 @@ Changes in existing checks
<clang-tidy/checks/readability/misleading-indentation>` check when warning would
be unnecessarily emitted for template dependent ``if constexpr``.

- Improved :doc:`readability-static-accessed-through-instance
<clang-tidy/checks/readability/static-accessed-through-instance>` check to
support unscoped enumerations through instances.

- Fixed a false positive in :doc:`cppcoreguidelines-slicing
<clang-tidy/checks/cppcoreguidelines/slicing>` check when warning would be
emitted in constructor for virtual base class initialization.
Expand Down
Expand Up @@ -15,11 +15,15 @@ The following code:
struct C {
static void foo();
static int x;
enum { E1 };
enum E { E2 };
};

C *c1 = new C();
c1->foo();
c1->x;
c1->E1;
c1->E2;
is changed to:

Expand All @@ -28,4 +32,6 @@ is changed to:
C *c1 = new C();
C::foo();
C::x;
C::E1;
C::E2;
@@ -1,10 +1,21 @@
// RUN: %check_clang_tidy %s readability-static-accessed-through-instance %t -- -- -isystem %S/Inputs/static-accessed-through-instance
#include <__clang_cuda_builtin_vars.h>

enum OutEnum {
E0,
};

struct C {
static void foo();
static int x;
int nsx;
enum {
Anonymous,
};
enum E {
E1,
};
using enum OutEnum;
void mf() {
(void)&x; // OK, x is accessed inside the struct.
(void)&C::x; // OK, x is accessed using a qualified-id.
Expand Down Expand Up @@ -144,6 +155,16 @@ void static_through_instance() {
c1->x; // 2
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
// CHECK-FIXES: {{^}} C::x; // 2{{$}}
c1->Anonymous; // 3
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
// CHECK-FIXES: {{^}} C::Anonymous; // 3{{$}}
c1->E1; // 4
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
// CHECK-FIXES: {{^}} C::E1; // 4{{$}}
c1->E0; // 5
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
// CHECK-FIXES: {{^}} C::E0; // 5{{$}}

c1->nsx; // OK, nsx is a non-static member.

const C *c2 = new C();
Expand Down

0 comments on commit edd6a33

Please sign in to comment.