Skip to content

Commit

Permalink
[Clang-tidy] Check the existence of ElaboratedType's qualifiers
Browse files Browse the repository at this point in the history
The ElaboratedType can have no qualifiers, so we should check it before
use.

Fix #issue53874(#53874)

Differential Revision: https://reviews.llvm.org/D119949
  • Loading branch information
junaire committed Mar 1, 2022
1 parent 70ab0a9 commit ac616fb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
Expand Up @@ -19,14 +19,15 @@ namespace readability {

static unsigned getNameSpecifierNestingLevel(const QualType &QType) {
if (const ElaboratedType *ElType = QType->getAs<ElaboratedType>()) {
const NestedNameSpecifier *NestedSpecifiers = ElType->getQualifier();
unsigned NameSpecifierNestingLevel = 1;
do {
NameSpecifierNestingLevel++;
NestedSpecifiers = NestedSpecifiers->getPrefix();
} while (NestedSpecifiers);

return NameSpecifierNestingLevel;
if (const NestedNameSpecifier *NestedSpecifiers = ElType->getQualifier()) {
unsigned NameSpecifierNestingLevel = 1;
do {
NameSpecifierNestingLevel++;
NestedSpecifiers = NestedSpecifiers->getPrefix();
} while (NestedSpecifiers);

return NameSpecifierNestingLevel;
}
}
return 0;
}
Expand Down Expand Up @@ -68,6 +69,10 @@ void StaticAccessedThroughInstanceCheck::check(
PrintingPolicy PrintingPolicyWithSupressedTag(AstContext->getLangOpts());
PrintingPolicyWithSupressedTag.SuppressTagKeyword = true;
PrintingPolicyWithSupressedTag.SuppressUnwrittenScope = true;

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

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

Expand Down
Expand Up @@ -198,6 +198,28 @@ void static_through_instance() {
h<4>();
}

struct SP {
static int I;
} P;

void usep() {
P.I;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
// CHECK-FIXES: {{^}} SP::I;{{$}}
}

namespace NSP {
struct SP {
static int I;
} P;
} // namespace NSP

void usensp() {
NSP::P.I;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
// CHECK-FIXES: {{^}} NSP::SP::I;{{$}}
}

// Overloaded member access operator
struct Q {
static int K;
Expand Down Expand Up @@ -237,9 +259,9 @@ void use_anonymous() {

namespace Outer {
inline namespace Inline {
struct S {
static int I;
};
struct S {
static int I;
};
}
}

Expand Down

0 comments on commit ac616fb

Please sign in to comment.