[PATCH] D101572: Make `hasTypeLoc` matcher support more node types.

Stephen Kelly via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 5 12:56:39 PDT 2021


steveire added a comment.

Please run `llvm-project/clang/docs/tools/dump_ast_matchers.py` to generate updated documentation for this (there is a `urlopen(url)` which slows things down, so I usually comment that out when running it).

Do you have this on a git clone somewhere? It's easier to take a patch and try it out from a git repo than phab.



================
Comment at: clang/include/clang/ASTMatchers/ASTMatchers.h:3919
+/// cxxFunctionalCastExpr(hasTypeLoc(loc(asString("struct Foo"))))
+///   matches Foo(1, 2)
+AST_POLYMORPHIC_MATCHER_P(
----------------
This should somehow document the types it works with. You don't have to add examples for all of the nodes though. `hasDeclaration` is another example of a matcher supporting many different nodes. It has `Usable as:` at the bottom of its docs which gets parsed by `dump_ast_matchers.py`. Please make a similar change here to verify that the script generates docs for each type.


================
Comment at: clang/include/clang/ASTMatchers/ASTMatchersInternal.h:138-150
+template <typename... T> struct disjunction;
+template <typename T> struct disjunction<T> : public T {};
+template <typename Head, typename... Tail> struct disjunction<Head, Tail...> {
+  using type =
+      typename std::conditional<Head::value, Head, disjunction<Tail...>>::type;
+  static constexpr bool value = type::value;
+};
----------------
SilensAngelusNex wrote:
> Is there a better way to express this? I was originally using
> 
> ```
> template <typename... Expected> struct is_one_of {
>   template <typename T>
>   static constexpr bool value = (std::is_base_of_v<Expected, T> || ...);
> };
> ```
> but that didn't compile because `is_base_of_v` and fold expressions are C++17 features.
> 
> Maybe it would be better to just explicitly write out an overload of `GetTypeSourceInfo` for each type?

I like your c++17 solution too, but given that we have `TypeListContainsSuperOf` in this file used for this kind of thing, I think it makes sense to use that. I think this will work:

```
template <typename T,
          std::enable_if_t<ast_matchers::internal::TypeListContainsSuperOf<
            ast_matchers::internal::TypeList<
              CXXBaseSpecifier, CXXCtorInitializer, CXXTemporaryObjectExpr,
              CXXUnresolvedConstructExpr, CompoundLiteralExpr, DeclaratorDecl,
              ObjCPropertyDecl, TemplateArgumentLoc, TypedefNameDecl>, T>::value>
              * = nullptr>
```



Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D101572/new/

https://reviews.llvm.org/D101572



More information about the cfe-commits mailing list