10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
19 cxxRecordDecl(has(accessSpecDecl())).bind(
"redundant-access-specifiers"),
24 const MatchFinder::MatchResult &Result) {
25 const auto *MatchedDecl =
26 Result.Nodes.getNodeAs<CXXRecordDecl>(
"redundant-access-specifiers");
28 const AccessSpecDecl *LastASDecl =
nullptr;
29 for (DeclContext::specific_decl_iterator<AccessSpecDecl>
30 AS(MatchedDecl->decls_begin()),
31 ASEnd(MatchedDecl->decls_end());
33 const AccessSpecDecl *ASDecl = *AS;
36 if (ASDecl->getLocation().isMacroID()) {
41 if (LastASDecl ==
nullptr) {
45 if (CheckFirstDeclaration) {
46 AccessSpecifier DefaultSpecifier =
47 MatchedDecl->isClass() ? AS_private : AS_public;
48 if (ASDecl->getAccess() == DefaultSpecifier) {
49 diag(ASDecl->getLocation(),
50 "redundant access specifier has the same accessibility as the "
51 "implicit access specifier")
52 << FixItHint::CreateRemoval(ASDecl->getSourceRange());
59 if (LastASDecl->getAccess() == ASDecl->getAccess()) {
61 if (LastASDecl->getLocation().isMacroID()) {
66 diag(ASDecl->getLocation(),
67 "redundant access specifier has the same accessibility as the "
68 "previous access specifier")
69 << FixItHint::CreateRemoval(ASDecl->getSourceRange());
70 diag(LastASDecl->getLocation(),
"previously declared here",
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.