diff options
Diffstat (limited to 'clang-tools-extra/clang-tidy/readability')
7 files changed, 155 insertions, 4 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/AvoidReturnWithVoidValueCheck.h b/clang-tools-extra/clang-tidy/readability/AvoidReturnWithVoidValueCheck.h index 1533b9a..97b522a 100644 --- a/clang-tools-extra/clang-tidy/readability/AvoidReturnWithVoidValueCheck.h +++ b/clang-tools-extra/clang-tidy/readability/AvoidReturnWithVoidValueCheck.h @@ -34,7 +34,6 @@ private: } void storeOptions(ClangTidyOptions::OptionMap &Opts) override; -private: bool IgnoreMacros; bool StrictMode; }; diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt index 0d0641c..91e9354 100644 --- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt @@ -49,6 +49,7 @@ add_clang_library(clangTidyReadabilityModule STATIC RedundantSmartptrGetCheck.cpp RedundantStringCStrCheck.cpp RedundantStringInitCheck.cpp + RedundantTypenameCheck.cpp ReferenceToConstructedTemporaryCheck.cpp SimplifyBooleanExprCheck.cpp SimplifySubscriptExprCheck.cpp diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp index 5178bee..ef3eac8 100644 --- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp @@ -83,14 +83,18 @@ namespace readability { m(Member) \ m(ClassConstant) \ m(ClassMember) \ + m(ClassConstexpr) \ + m(GlobalConstexprVariable) \ m(GlobalConstant) \ m(GlobalConstantPointer) \ m(GlobalPointer) \ m(GlobalVariable) \ + m(LocalConstexprVariable) \ m(LocalConstant) \ m(LocalConstantPointer) \ m(LocalPointer) \ m(LocalVariable) \ + m(StaticConstexprVariable) \ m(StaticConstant) \ m(StaticVariable) \ m(Constant) \ @@ -1497,8 +1501,22 @@ StyleKind IdentifierNamingCheck::findStyleKindForField( StyleKind IdentifierNamingCheck::findStyleKindForVar( const VarDecl *Var, QualType Type, ArrayRef<std::optional<NamingStyle>> NamingStyles) const { - if (Var->isConstexpr() && NamingStyles[SK_ConstexprVariable]) - return SK_ConstexprVariable; + if (Var->isConstexpr()) { + if (Var->isStaticDataMember() && NamingStyles[SK_ClassConstexpr]) + return SK_ClassConstexpr; + + if (Var->isFileVarDecl() && NamingStyles[SK_GlobalConstexprVariable]) + return SK_GlobalConstexprVariable; + + if (Var->isStaticLocal() && NamingStyles[SK_StaticConstexprVariable]) + return SK_StaticConstexprVariable; + + if (Var->isLocalVarDecl() && NamingStyles[SK_LocalConstexprVariable]) + return SK_LocalConstexprVariable; + + if (NamingStyles[SK_ConstexprVariable]) + return SK_ConstexprVariable; + } if (!Type.isNull() && Type.isConstQualified()) { if (Var->isStaticDataMember() && NamingStyles[SK_ClassConstant]) diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h index 3db9d23..0b17af8 100644 --- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h +++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h @@ -33,7 +33,7 @@ enum StyleKind : int; class IdentifierNamingCheck final : public RenamerClangTidyCheck { public: IdentifierNamingCheck(StringRef Name, ClangTidyContext *Context); - ~IdentifierNamingCheck(); + ~IdentifierNamingCheck() override; void storeOptions(ClangTidyOptions::OptionMap &Opts) override; diff --git a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp index fcfac05..569302e 100644 --- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp @@ -52,6 +52,7 @@ #include "RedundantSmartptrGetCheck.h" #include "RedundantStringCStrCheck.h" #include "RedundantStringInitCheck.h" +#include "RedundantTypenameCheck.h" #include "ReferenceToConstructedTemporaryCheck.h" #include "SimplifyBooleanExprCheck.h" #include "SimplifySubscriptExprCheck.h" @@ -143,6 +144,8 @@ public: "readability-redundant-parentheses"); CheckFactories.registerCheck<RedundantPreprocessorCheck>( "readability-redundant-preprocessor"); + CheckFactories.registerCheck<RedundantTypenameCheck>( + "readability-redundant-typename"); CheckFactories.registerCheck<ReferenceToConstructedTemporaryCheck>( "readability-reference-to-constructed-temporary"); CheckFactories.registerCheck<SimplifySubscriptExprCheck>( diff --git a/clang-tools-extra/clang-tidy/readability/RedundantTypenameCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantTypenameCheck.cpp new file mode 100644 index 0000000..a4edd2b --- /dev/null +++ b/clang-tools-extra/clang-tidy/readability/RedundantTypenameCheck.cpp @@ -0,0 +1,94 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "RedundantTypenameCheck.h" +#include "clang/AST/TypeLoc.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +void RedundantTypenameCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher(typeLoc(unless(hasAncestor(decl(isInstantiated())))) + .bind("nonDependentTypeLoc"), + this); + + if (!getLangOpts().CPlusPlus20) + return; + + const auto InImplicitTypenameContext = anyOf( + hasParent(decl(anyOf( + typedefNameDecl(), templateTypeParmDecl(), nonTypeTemplateParmDecl(), + friendDecl(), fieldDecl(), + varDecl(hasDeclContext(anyOf(namespaceDecl(), translationUnitDecl())), + unless(parmVarDecl())), + parmVarDecl(hasParent(expr(requiresExpr()))), + parmVarDecl(hasParent(typeLoc(hasParent(decl( + anyOf(cxxMethodDecl(), hasParent(friendDecl()), + functionDecl(has(nestedNameSpecifier())), + cxxDeductionGuideDecl(hasDeclContext(recordDecl())))))))), + // Match return types. + functionDecl(unless(cxxConversionDecl()))))), + hasParent(expr(anyOf(cxxNamedCastExpr(), cxxNewExpr())))); + Finder->addMatcher( + typeLoc(InImplicitTypenameContext).bind("dependentTypeLoc"), this); +} + +void RedundantTypenameCheck::check(const MatchFinder::MatchResult &Result) { + const SourceLocation ElaboratedKeywordLoc = [&] { + if (const auto *NonDependentTypeLoc = + Result.Nodes.getNodeAs<TypeLoc>("nonDependentTypeLoc")) { + if (const auto TL = NonDependentTypeLoc->getAs<TypedefTypeLoc>()) + return TL.getElaboratedKeywordLoc(); + + if (const auto TL = NonDependentTypeLoc->getAs<TagTypeLoc>()) + return TL.getElaboratedKeywordLoc(); + + if (const auto TL = NonDependentTypeLoc + ->getAs<DeducedTemplateSpecializationTypeLoc>()) + return TL.getElaboratedKeywordLoc(); + + if (const auto TL = + NonDependentTypeLoc->getAs<TemplateSpecializationTypeLoc>()) + if (!TL.getType()->isDependentType()) + return TL.getElaboratedKeywordLoc(); + } else { + TypeLoc InnermostTypeLoc = + *Result.Nodes.getNodeAs<TypeLoc>("dependentTypeLoc"); + while (const TypeLoc Next = InnermostTypeLoc.getNextTypeLoc()) + InnermostTypeLoc = Next; + + if (const auto TL = InnermostTypeLoc.getAs<DependentNameTypeLoc>()) + return TL.getElaboratedKeywordLoc(); + + if (const auto TL = + InnermostTypeLoc.getAs<TemplateSpecializationTypeLoc>()) + return TL.getElaboratedKeywordLoc(); + } + + return SourceLocation(); + }(); + + if (ElaboratedKeywordLoc.isInvalid()) + return; + + if (Token ElaboratedKeyword; + Lexer::getRawToken(ElaboratedKeywordLoc, ElaboratedKeyword, + *Result.SourceManager, getLangOpts()) || + ElaboratedKeyword.getRawIdentifier() != "typename") + return; + + diag(ElaboratedKeywordLoc, "redundant 'typename'") + << FixItHint::CreateRemoval(ElaboratedKeywordLoc); +} + +} // namespace clang::tidy::readability diff --git a/clang-tools-extra/clang-tidy/readability/RedundantTypenameCheck.h b/clang-tools-extra/clang-tidy/readability/RedundantTypenameCheck.h new file mode 100644 index 0000000..8e86b0c --- /dev/null +++ b/clang-tools-extra/clang-tidy/readability/RedundantTypenameCheck.h @@ -0,0 +1,36 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_REDUNDANTTYPENAMECHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_REDUNDANTTYPENAMECHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang::tidy::readability { + +/// Finds redundant uses of the `typename` keyword. +/// +/// For the user-facing documentation see: +/// https://clang.llvm.org/extra/clang-tidy/checks/readability/redundant-typename.html +class RedundantTypenameCheck : public ClangTidyCheck { +public: + RedundantTypenameCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { + return LangOpts.CPlusPlus; + } + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + std::optional<TraversalKind> getCheckTraversalKind() const override { + return TK_IgnoreUnlessSpelledInSource; + } +}; + +} // namespace clang::tidy::readability + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_REDUNDANTTYPENAMECHECK_H |
