From c3c8524640ffce0427d81f78501f32c3c8a07427 Mon Sep 17 00:00:00 2001 From: Eugene Zelenko Date: Wed, 1 Nov 2017 23:09:49 +0000 Subject: [PATCH] [ASTMatchers] Fix some Clang-tidy modernize and Include What You Use warnings; other minor fixes (NFC). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@317137 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/ASTMatchers/ASTMatchers.h | 84 +++++++---- .../clang/ASTMatchers/ASTMatchersInternal.h | 131 +++++++++++------- include/clang/ASTMatchers/Dynamic/Parser.h | 31 +++-- include/clang/ASTMatchers/Dynamic/Registry.h | 16 +-- lib/ASTMatchers/ASTMatchersInternal.cpp | 52 +++++-- lib/ASTMatchers/Dynamic/Marshallers.h | 102 ++++++++++---- lib/ASTMatchers/Dynamic/Parser.cpp | 43 +++--- lib/ASTMatchers/Dynamic/Registry.cpp | 48 ++++--- 8 files changed, 327 insertions(+), 180 deletions(-) diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h index 766e95cdde..d1a582508a 100644 --- a/include/clang/ASTMatchers/ASTMatchers.h +++ b/include/clang/ASTMatchers/ASTMatchers.h @@ -1,4 +1,4 @@ -//===--- ASTMatchers.h - Structural query framework -------------*- C++ -*-===// +//===- ASTMatchers.h - Structural query framework ---------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -46,13 +46,48 @@ #define LLVM_CLANG_ASTMATCHERS_ASTMATCHERS_H #include "clang/AST/ASTContext.h" +#include "clang/AST/ASTTypeTraits.h" +#include "clang/AST/Attr.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" #include "clang/AST/DeclFriend.h" #include "clang/AST/DeclObjC.h" #include "clang/AST/DeclTemplate.h" +#include "clang/AST/Expr.h" +#include "clang/AST/ExprCXX.h" +#include "clang/AST/ExprObjC.h" +#include "clang/AST/NestedNameSpecifier.h" +#include "clang/AST/OperationKinds.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/StmtCXX.h" +#include "clang/AST/StmtObjC.h" +#include "clang/AST/TemplateBase.h" +#include "clang/AST/TemplateName.h" +#include "clang/AST/Type.h" +#include "clang/AST/TypeLoc.h" #include "clang/ASTMatchers/ASTMatchersInternal.h" #include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/AttrKinds.h" +#include "clang/Basic/ExceptionSpecificationType.h" +#include "clang/Basic/IdentifierTable.h" +#include "clang/Basic/LLVM.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Basic/Specifiers.h" +#include "clang/Basic/TypeTraits.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/Compiler.h" +#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/Regex.h" +#include +#include #include +#include +#include +#include +#include namespace clang { namespace ast_matchers { @@ -78,7 +113,7 @@ public: /// \brief Type of mapping from binding identifiers to bound nodes. This type /// is an associative container with a key type of \c std::string and a value /// type of \c clang::ast_type_traits::DynTypedNode - typedef internal::BoundNodesMap::IDToNodeMap IDToNodeMap; + using IDToNodeMap = internal::BoundNodesMap::IDToNodeMap; /// \brief Retrieve mapping from binding identifiers to bound nodes. const IDToNodeMap &getMap() const { @@ -86,13 +121,13 @@ public: } private: + friend class internal::BoundNodesTreeBuilder; + /// \brief Create BoundNodes from a pre-filled map of bindings. BoundNodes(internal::BoundNodesMap &MyBoundNodes) : MyBoundNodes(MyBoundNodes) {} internal::BoundNodesMap MyBoundNodes; - - friend class internal::BoundNodesTreeBuilder; }; /// \brief If the provided matcher matches a node, binds the node to \c ID. @@ -107,13 +142,13 @@ internal::Matcher id(StringRef ID, /// \brief Types of matchers for the top-level classes in the AST class /// hierarchy. /// @{ -typedef internal::Matcher DeclarationMatcher; -typedef internal::Matcher StatementMatcher; -typedef internal::Matcher TypeMatcher; -typedef internal::Matcher TypeLocMatcher; -typedef internal::Matcher NestedNameSpecifierMatcher; -typedef internal::Matcher NestedNameSpecifierLocMatcher; -typedef internal::Matcher CXXCtorInitializerMatcher; +using DeclarationMatcher = internal::Matcher; +using StatementMatcher = internal::Matcher; +using TypeMatcher = internal::Matcher; +using TypeLocMatcher = internal::Matcher; +using NestedNameSpecifierMatcher = internal::Matcher; +using NestedNameSpecifierLocMatcher = internal::Matcher; +using CXXCtorInitializerMatcher = internal::Matcher; /// @} /// \brief Matches any node. @@ -2186,23 +2221,23 @@ const internal::VariadicAllOfMatcher typeLoc; /// \c b. /// /// Usable as: Any Matcher -const internal::VariadicOperatorMatcherFunc<2, UINT_MAX> eachOf = { - internal::DynTypedMatcher::VO_EachOf -}; +const internal::VariadicOperatorMatcherFunc< + 2, std::numeric_limits::max()> + eachOf = {internal::DynTypedMatcher::VO_EachOf}; /// \brief Matches if any of the given matchers matches. /// /// Usable as: Any Matcher -const internal::VariadicOperatorMatcherFunc<2, UINT_MAX> anyOf = { - internal::DynTypedMatcher::VO_AnyOf -}; +const internal::VariadicOperatorMatcherFunc< + 2, std::numeric_limits::max()> + anyOf = {internal::DynTypedMatcher::VO_AnyOf}; /// \brief Matches if all given matchers match. /// /// Usable as: Any Matcher -const internal::VariadicOperatorMatcherFunc<2, UINT_MAX> allOf = { - internal::DynTypedMatcher::VO_AllOf -}; +const internal::VariadicOperatorMatcherFunc< + 2, std::numeric_limits::max()> + allOf = {internal::DynTypedMatcher::VO_AllOf}; /// \brief Matches sizeof (C99), alignof (C++11) and vec_step (OpenCL) /// @@ -4004,7 +4039,6 @@ AST_MATCHER_P(UnaryOperator, hasUnaryOperand, /// \code /// int a = b ?: 1; /// \endcode - AST_POLYMORPHIC_MATCHER_P(hasSourceExpression, AST_POLYMORPHIC_SUPPORTED_TYPES(CastExpr, OpaqueValueExpr), @@ -5677,7 +5711,6 @@ AST_MATCHER_P(ReturnStmt, hasReturnValue, internal::Matcher, return false; } - /// \brief Matches CUDA kernel call expression. /// /// Example matches, @@ -5688,7 +5721,6 @@ const internal::VariadicDynCastAllOfMatcher< Stmt, CUDAKernelCallExpr> cudaKernelCallExpr; - /// \brief Matches expressions that resolve to a null pointer constant, such as /// GNU's __null, C++11's nullptr, or C's NULL macro. /// @@ -5772,7 +5804,7 @@ AST_MATCHER(NamedDecl, hasExternalFormalLinkage) { return Node.hasExternalFormalLinkage(); } -} // end namespace ast_matchers -} // end namespace clang +} // namespace ast_matchers +} // namespace clang -#endif +#endif // LLVM_CLANG_ASTMATCHERS_ASTMATCHERS_H diff --git a/include/clang/ASTMatchers/ASTMatchersInternal.h b/include/clang/ASTMatchers/ASTMatchersInternal.h index cd59bbcaeb..203575e689 100644 --- a/include/clang/ASTMatchers/ASTMatchersInternal.h +++ b/include/clang/ASTMatchers/ASTMatchersInternal.h @@ -1,4 +1,4 @@ -//===--- ASTMatchersInternal.h - Structural query framework -----*- C++ -*-===// +//===- ASTMatchersInternal.h - Structural query framework -------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -38,23 +38,42 @@ #include "clang/AST/ASTTypeTraits.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclCXX.h" -#include "clang/AST/DeclObjC.h" #include "clang/AST/DeclTemplate.h" +#include "clang/AST/Expr.h" #include "clang/AST/ExprCXX.h" -#include "clang/AST/ExprObjC.h" +#include "clang/AST/NestedNameSpecifier.h" #include "clang/AST/Stmt.h" -#include "clang/AST/StmtCXX.h" -#include "clang/AST/StmtObjC.h" +#include "clang/AST/TemplateName.h" #include "clang/AST/Type.h" +#include "clang/AST/TypeLoc.h" +#include "clang/Basic/LLVM.h" +#include "clang/Basic/OperatorKinds.h" +#include "llvm/ADT/APFloat.h" #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/IntrusiveRefCntPtr.h" +#include "llvm/ADT/None.h" #include "llvm/ADT/Optional.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/iterator.h" +#include "llvm/Support/Casting.h" #include "llvm/Support/ManagedStatic.h" +#include +#include +#include +#include #include #include +#include +#include +#include #include namespace clang { + +class ASTContext; + namespace ast_matchers { class BoundNodes; @@ -158,7 +177,7 @@ public: /// Note that we're using std::map here, as for memoization: /// - we need a comparison operator /// - we need an assignment operator - typedef std::map IDToNodeMap; + using IDToNodeMap = std::map; const IDToNodeMap &getMap() const { return NodeMap; @@ -188,7 +207,7 @@ public: /// BoundNodesTree. class Visitor { public: - virtual ~Visitor() {} + virtual ~Visitor() = default; /// \brief Called multiple times during a single call to VisitMatches(...). /// @@ -248,7 +267,7 @@ class ASTMatchFinder; class DynMatcherInterface : public llvm::ThreadSafeRefCountedBase { public: - virtual ~DynMatcherInterface() {} + virtual ~DynMatcherInterface() = default; /// \brief Returns true if \p DynNode can be matched. /// @@ -317,26 +336,29 @@ public: /// \brief Takes ownership of the provided implementation pointer. template DynTypedMatcher(MatcherInterface *Implementation) - : AllowBind(false), - SupportedKind(ast_type_traits::ASTNodeKind::getFromNodeKind()), + : SupportedKind(ast_type_traits::ASTNodeKind::getFromNodeKind()), RestrictKind(SupportedKind), Implementation(Implementation) {} /// \brief Construct from a variadic function. enum VariadicOperator { /// \brief Matches nodes for which all provided matchers match. VO_AllOf, + /// \brief Matches nodes for which at least one of the provided matchers /// matches. VO_AnyOf, + /// \brief Matches nodes for which at least one of the provided matchers /// matches, but doesn't stop at the first match. VO_EachOf, + /// \brief Matches nodes that do not match the provided matcher. /// /// Uses the variadic matcher interface, but fails if /// InnerMatchers.size() != 1. VO_UnaryNot }; + static DynTypedMatcher constructVariadic(VariadicOperator Op, ast_type_traits::ASTNodeKind SupportedKind, @@ -382,7 +404,7 @@ public: /// include both in the ID to make it unique. /// /// \c MatcherIDType supports operator< and provides strict weak ordering. - typedef std::pair MatcherIDType; + using MatcherIDType = std::pair; MatcherIDType getID() const { /// FIXME: Document the requirements this imposes on matcher /// implementations (no new() implementation_ during a Matches()). @@ -428,13 +450,12 @@ private: DynTypedMatcher(ast_type_traits::ASTNodeKind SupportedKind, ast_type_traits::ASTNodeKind RestrictKind, IntrusiveRefCntPtr Implementation) - : AllowBind(false), - SupportedKind(SupportedKind), - RestrictKind(RestrictKind), + : SupportedKind(SupportedKind), RestrictKind(RestrictKind), Implementation(std::move(Implementation)) {} - bool AllowBind; + bool AllowBind = false; ast_type_traits::ASTNodeKind SupportedKind; + /// \brief A potentially stricter node kind. /// /// It allows to perform implicit and dynamic cast of matchers without @@ -545,6 +566,7 @@ public: private: // For Matcher <=> Matcher conversions. template friend class Matcher; + // For DynTypedMatcher::unconditionalConvertTo. friend class DynTypedMatcher; @@ -618,8 +640,8 @@ bool matchesFirstInPointerRange(const MatcherT &Matcher, IteratorT Start, // Metafunction to determine if type T has a member called getDecl. template class has_getDecl { - typedef char yes[1]; - typedef char no[2]; + using yes = char[1]; + using no = char[2]; template static yes& test(Inner *I, decltype(I->getDecl()) * = nullptr); @@ -741,7 +763,6 @@ private: /// matcher matches on it. bool matchesSpecialized(const Type &Node, ASTMatchFinder *Finder, BoundNodesTreeBuilder *Builder) const { - // DeducedType does not have declarations of its own, so // match the deduced type instead. const Type *EffectiveType = &Node; @@ -917,6 +938,7 @@ public: enum TraversalKind { /// Will traverse any child nodes. TK_AsIs, + /// Will not traverse implicit casts and parentheses. TK_IgnoreImplicitCastsAndParentheses }; @@ -925,6 +947,7 @@ public: enum BindKind { /// Stop at the first match and only bind the first match. BK_First, + /// Create results for all combinations of bindings that match. BK_All }; @@ -933,11 +956,12 @@ public: enum AncestorMatchMode { /// All ancestors. AMM_All, + /// Direct parent only. AMM_ParentOnly }; - virtual ~ASTMatchFinder() {} + virtual ~ASTMatchFinder() = default; /// \brief Returns true if the given class is directly or indirectly derived /// from a base type matching \c base. @@ -960,7 +984,7 @@ public: std::is_base_of::value || std::is_base_of::value, "unsupported type for recursive matching"); - return matchesChildOf(ast_type_traits::DynTypedNode::create(Node), + return matchesChildOf(ast_type_traits::DynTypedNode::create(Node), Matcher, Builder, Traverse, Bind); } @@ -1023,17 +1047,17 @@ template struct TypeList {}; // Empty sentinel type list. template struct TypeList { /// \brief The first type on the list. - typedef T1 head; + using head = T1; /// \brief A sublist with the tail. ie everything but the head. /// /// This type is used to do recursion. TypeList<>/EmptyTypeList indicates the /// end of the list. - typedef TypeList tail; + using tail = TypeList; }; /// \brief The empty type list. -typedef TypeList<> EmptyTypeList; +using EmptyTypeList = TypeList<>; /// \brief Helper meta-function to determine if some type \c T is present or /// a parent type in the list. @@ -1051,8 +1075,9 @@ struct TypeListContainsSuperOf { /// \brief A "type list" that contains all types. /// /// Useful for matchers like \c anything and \c unless. -typedef TypeList AllNodeBaseTypes; +using AllNodeBaseTypes = + TypeList; /// \brief Helper meta-function to extract the argument out of a function of /// type void(Arg). @@ -1060,21 +1085,22 @@ typedef TypeList struct ExtractFunctionArgMeta; template struct ExtractFunctionArgMeta { - typedef T type; + using type = T; }; /// \brief Default type lists for ArgumentAdaptingMatcher matchers. -typedef AllNodeBaseTypes AdaptativeDefaultFromTypes; -typedef TypeList AdaptativeDefaultToTypes; +using AdaptativeDefaultFromTypes = AllNodeBaseTypes; +using AdaptativeDefaultToTypes = + TypeList; /// \brief All types that are supported by HasDeclarationMatcher above. -typedef TypeList - HasDeclarationSupportedTypes; +using HasDeclarationSupportedTypes = + TypeList; /// \brief Converts a \c Matcher to a matcher of desired type \c To by /// "adapting" a \c To into a \c T. @@ -1098,7 +1124,7 @@ struct ArgumentAdaptingMatcherFunc { explicit Adaptor(const Matcher &InnerMatcher) : InnerMatcher(InnerMatcher) {} - typedef ToTypes ReturnTypes; + using ReturnTypes = ToTypes; template operator Matcher() const { return Matcher(new ArgumentAdapterT(InnerMatcher)); @@ -1135,7 +1161,8 @@ template