//===--- UseAutoCheck.cpp - clang-tidy-------------------------------------===// // // 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 "UseAutoCheck.h" #include "clang/AST/ASTContext.h" #include "clang/AST/TypeLoc.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" #include "clang/Basic/CharInfo.h" #include "clang/Tooling/FixIt.h" #include "llvm/ADT/STLExtras.h" usingnamespaceclang; usingnamespaceclang::ast_matchers; usingnamespaceclang::ast_matchers::internal; namespace clang::tidy::modernize { namespace { const char IteratorDeclStmtId[] = …; const char DeclWithNewId[] = …; const char DeclWithCastId[] = …; const char DeclWithTemplateCastId[] = …; size_t getTypeNameLength(bool RemoveStars, StringRef Text) { … } /// Matches variable declarations that have explicit initializers that /// are not initializer lists. /// /// Given /// \code /// iterator I = Container.begin(); /// MyType A(42); /// MyType B{2}; /// MyType C; /// \endcode /// /// varDecl(hasWrittenNonListInitializer()) maches \c I and \c A but not \c B /// or \c C. AST_MATCHER(VarDecl, hasWrittenNonListInitializer) { … } /// Matches QualTypes that are type sugar for QualTypes that match \c /// SugarMatcher. /// /// Given /// \code /// class C {}; /// typedef C my_type; /// typedef my_type my_other_type; /// \endcode /// /// qualType(isSugarFor(recordType(hasDeclaration(namedDecl(hasName("C")))))) /// matches \c my_type and \c my_other_type. AST_MATCHER_P(QualType, isSugarFor, Matcher<QualType>, SugarMatcher) { … } /// Matches named declarations that have one of the standard iterator /// names: iterator, reverse_iterator, const_iterator, const_reverse_iterator. /// /// Given /// \code /// iterator I; /// const_iterator CI; /// \endcode /// /// namedDecl(hasStdIteratorName()) matches \c I and \c CI. Matcher<NamedDecl> hasStdIteratorName() { … } /// Matches named declarations that have one of the standard container /// names. /// /// Given /// \code /// class vector {}; /// class forward_list {}; /// class my_ver{}; /// \endcode /// /// recordDecl(hasStdContainerName()) matches \c vector and \c forward_list /// but not \c my_vec. Matcher<NamedDecl> hasStdContainerName() { … } /// Matches declaration reference or member expressions with explicit template /// arguments. AST_POLYMORPHIC_MATCHER(hasExplicitTemplateArgs, AST_POLYMORPHIC_SUPPORTED_TYPES(DeclRefExpr, MemberExpr)) { … } /// Returns a DeclarationMatcher that matches standard iterators nested /// inside records with a standard container name. DeclarationMatcher standardIterator() { … } /// Returns a TypeMatcher that matches typedefs for standard iterators /// inside records with a standard container name. TypeMatcher typedefIterator() { … } /// Returns a TypeMatcher that matches records named for standard /// iterators nested inside records named for standard containers. TypeMatcher nestedIterator() { … } /// Returns a TypeMatcher that matches types declared with using /// declarations and which name standard iterators for standard containers. TypeMatcher iteratorFromUsingDeclaration() { … } /// This matcher returns declaration statements that contain variable /// declarations with written non-list initializer for standard iterators. StatementMatcher makeIteratorDeclMatcher() { … } StatementMatcher makeDeclWithNewMatcher() { … } StatementMatcher makeDeclWithCastMatcher() { … } StatementMatcher makeDeclWithTemplateCastMatcher() { … } StatementMatcher makeCombinedMatcher() { … } } // namespace UseAutoCheck::UseAutoCheck(StringRef Name, ClangTidyContext *Context) : … { … } void UseAutoCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { … } void UseAutoCheck::registerMatchers(MatchFinder *Finder) { … } void UseAutoCheck::replaceIterators(const DeclStmt *D, ASTContext *Context) { … } static void ignoreTypeLocClasses( TypeLoc &Loc, std::initializer_list<TypeLoc::TypeLocClass> const &LocClasses) { … } static bool isMutliLevelPointerToTypeLocClasses( TypeLoc Loc, std::initializer_list<TypeLoc::TypeLocClass> const &LocClasses) { … } void UseAutoCheck::replaceExpr( const DeclStmt *D, ASTContext *Context, llvm::function_ref<QualType(const Expr *)> GetType, StringRef Message) { … } void UseAutoCheck::check(const MatchFinder::MatchResult &Result) { … } } // namespace clang::tidy::modernize