[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-28 Thread Vy Nguyen via Phabricator via cfe-commits
oontvoo closed this revision.
oontvoo added a comment.

Thanks, all!
Committed 
https://github.com/llvm/llvm-project/commit/51401a676c036f2bd4e6b4b38f3538615799de40


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80603



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-28 Thread Vy Nguyen via Phabricator via cfe-commits
oontvoo updated this revision to Diff 267008.
oontvoo added a comment.

Move matcher to near forEachArgumentWithParam


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80603

Files:
  clang/docs/LibASTMatchersReference.html
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/Dynamic/Registry.cpp
  clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2643,6 +2643,45 @@
   parmVarDecl(hasDefaultArgument(;
 }
 
+TEST(IsAtPosition, Basic) {
+  EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(notMatches("void x(int val) {}", parmVarDecl(isAtPosition(1;
+}
+
+TEST(IsAtPosition, FunctionDecl) {
+  EXPECT_TRUE(matches("void x(int a);", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b);", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b);", parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(notMatches("void x(int val);", parmVarDecl(isAtPosition(1;
+}
+
+TEST(IsAtPosition, Lambda) {
+  EXPECT_TRUE(
+  matches("void x() { [](int a) {};  }", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x() { [](int a, int b) {}; }",
+  parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x() { [](int a, int b) {}; }",
+  parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(
+  notMatches("void x() { [](int val) {}; }", parmVarDecl(isAtPosition(1;
+}
+
+TEST(IsAtPosition, BlockDecl) {
+  EXPECT_TRUE(matchesObjC(
+  "void func()  { void (^my_block)(int arg) = ^void(int arg) {}; } ",
+  parmVarDecl(isAtPosition(0;
+
+  EXPECT_TRUE(matchesObjC("void func()  { void (^my_block)(int x, int y) = "
+  "^void(int x, int y) {}; } ",
+  parmVarDecl(isAtPosition(1;
+
+  EXPECT_TRUE(notMatchesObjC(
+  "void func()  { void (^my_block)(int arg) = ^void(int arg) {}; } ",
+  parmVarDecl(isAtPosition(1;
+}
+
 TEST(IsArray, Basic) {
   EXPECT_TRUE(matches("struct MyClass {}; MyClass *p1 = new MyClass[10];",
   cxxNewExpr(isArray(;
Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -219,6 +219,7 @@
   REGISTER_MATCHER(floatLiteral);
   REGISTER_MATCHER(forEach);
   REGISTER_MATCHER(forEachArgumentWithParam);
+  REGISTER_MATCHER(isAtPosition);
   REGISTER_MATCHER(forEachConstructorInitializer);
   REGISTER_MATCHER(forEachDescendant);
   REGISTER_MATCHER(forEachOverridden);
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -4257,6 +4257,34 @@
   return Matched;
 }
 
+/// Matches the ParmVarDecl nodes that are at the N'th position in the parameter
+/// list. The parameter list could be that of either a block, function, or
+/// objc-method.
+///
+///
+/// Given
+///
+/// \code
+/// void f(int a, int b, int c) {
+/// }
+/// \endcode
+///
+/// ``parmVarDecl(isAtPosition(0))`` matches ``int a``.
+///
+/// ``parmVarDecl(isAtPosition(1))`` matches ``int b``.
+AST_MATCHER_P(clang::ParmVarDecl, isAtPosition, unsigned, N) {
+  const clang::DeclContext *Context = Node.getParentFunctionOrMethod();
+
+  if (const auto *Decl = dyn_cast_or_null(Context))
+return N < Decl->param_size() && Decl->getParamDecl(N) == 
+  if (const auto *Decl = dyn_cast_or_null(Context))
+return N < Decl->param_size() && Decl->getParamDecl(N) == 
+  if (const auto *Decl = dyn_cast_or_null(Context))
+return N < Decl->param_size() && Decl->getParamDecl(N) == 
+
+  return false;
+}
+
 /// Matches any parameter of a function or an ObjC method declaration or a
 /// block.
 ///
Index: clang/docs/LibASTMatchersReference.html
===
--- clang/docs/LibASTMatchersReference.html
+++ clang/docs/LibASTMatchersReference.html
@@ -4671,6 +4671,23 @@
 Usable as: Matcherhttps://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html;>FunctionDecl, Matcherhttps://clang.llvm.org/doxygen/classclang_1_1VarDecl.html;>VarDecl, Matcherhttps://clang.llvm.org/doxygen/classclang_1_1CXXRecordDecl.html;>CXXRecordDecl
 
 
+
+Matcherclang::ParmVarDeclisAtPositionunsigned N
+Matches the ParmVarDecl nodes that 

[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-28 Thread Vy Nguyen via Phabricator via cfe-commits
oontvoo added a comment.

In D80603#2061131 , @aaron.ballman 
wrote:

> In D80603#2059122 , @gribozavr2 
> wrote:
>
> > In D80603#2058019 , @aaron.ballman 
> > wrote:
> >
> > > I'm sorry if I'm being dense, but `hasParameter`  traverses to the 
> > > `ParmVarDecl`, so I'm not certain I understand why this new matcher is 
> > > needed as a public matcher. It seems like you can already accomplish this 
> > > without adding a second API to do effectively the same thing: 
> > > `functionDecl(hasParameter(0, parmVarDecl().bind("param")))`, can't you?
> >
> >
> > It is syntax sugar, true. Note that the proposed matcher is a narrowing 
> > matcher for `parmVarDecl()`, while your suggestion is a narrowing matcher 
> > for `functionDecl()`, so it is not an entirely apples-to-apples comparison. 
> > Think about use cases like: `declRefExpr(to(parmVarDecl(at(...`. To 
> > rewrite that with `hasParameter()`, we have to use `hasAncestor` to find 
> > the `functionDecl` first, and then compare the AST node pointers. So while 
> > it is possible to express this proposed operation today, it requires a 
> > complex matcher for such a conceptually simple operation.
>
>
> Thank you, that clarifies the need nicely for me!
>
> Is there a reason we want to support blocks but not lambdas?


Yes, the implementation does support lambdas

> Also, you need to update Registry.cpp to add this to the list of dynamic 
> matchers.

Done


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80603



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-28 Thread Vy Nguyen via Phabricator via cfe-commits
oontvoo updated this revision to Diff 266999.
oontvoo added a comment.

Updated Registry


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80603

Files:
  clang/docs/LibASTMatchersReference.html
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/Dynamic/Registry.cpp
  clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2643,6 +2643,45 @@
   parmVarDecl(hasDefaultArgument(;
 }
 
+TEST(IsAtPosition, Basic) {
+  EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(notMatches("void x(int val) {}", parmVarDecl(isAtPosition(1;
+}
+
+TEST(IsAtPosition, FunctionDecl) {
+  EXPECT_TRUE(matches("void x(int a);", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b);", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b);", parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(notMatches("void x(int val);", parmVarDecl(isAtPosition(1;
+}
+
+TEST(IsAtPosition, Lambda) {
+  EXPECT_TRUE(
+  matches("void x() { [](int a) {};  }", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x() { [](int a, int b) {}; }",
+  parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x() { [](int a, int b) {}; }",
+  parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(
+  notMatches("void x() { [](int val) {}; }", parmVarDecl(isAtPosition(1;
+}
+
+TEST(IsAtPosition, BlockDecl) {
+  EXPECT_TRUE(matchesObjC(
+  "void func()  { void (^my_block)(int arg) = ^void(int arg) {}; } ",
+  parmVarDecl(isAtPosition(0;
+
+  EXPECT_TRUE(matchesObjC("void func()  { void (^my_block)(int x, int y) = "
+  "^void(int x, int y) {}; } ",
+  parmVarDecl(isAtPosition(1;
+
+  EXPECT_TRUE(notMatchesObjC(
+  "void func()  { void (^my_block)(int arg) = ^void(int arg) {}; } ",
+  parmVarDecl(isAtPosition(1;
+}
+
 TEST(IsArray, Basic) {
   EXPECT_TRUE(matches("struct MyClass {}; MyClass *p1 = new MyClass[10];",
   cxxNewExpr(isArray(;
Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -261,6 +261,7 @@
   REGISTER_MATCHER(hasCastKind);
   REGISTER_MATCHER(hasCondition);
   REGISTER_MATCHER(hasConditionVariableStatement);
+  REGISTER_MATCHER(isAtPosition);
   REGISTER_MATCHER(hasDecayedType);
   REGISTER_MATCHER(hasDeclContext);
   REGISTER_MATCHER(hasDeclaration);
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -4615,6 +4615,34 @@
  InnerMatcher.matches(*DeclarationStatement, Finder, Builder);
 }
 
+/// Matches the ParmVarDecl nodes that are at the N'th position in the parameter
+/// list. The parameter list could be that of either a block, function, or
+/// objc-method.
+///
+///
+/// Given
+///
+/// \code
+/// void f(int a, int b, int c) {
+/// }
+/// \endcode
+///
+/// ``parmVarDecl(isAtPosition(0))`` matches ``int a``.
+///
+/// ``parmVarDecl(isAtPosition(1))`` matches ``int b``.
+AST_MATCHER_P(clang::ParmVarDecl, isAtPosition, unsigned, N) {
+  const clang::DeclContext *Context = Node.getParentFunctionOrMethod();
+
+  if (const auto *Decl = dyn_cast_or_null(Context))
+return N < Decl->param_size() && Decl->getParamDecl(N) == 
+  if (const auto *Decl = dyn_cast_or_null(Context))
+return N < Decl->param_size() && Decl->getParamDecl(N) == 
+  if (const auto *Decl = dyn_cast_or_null(Context))
+return N < Decl->param_size() && Decl->getParamDecl(N) == 
+
+  return false;
+}
+
 /// Matches the index expression of an array subscript expression.
 ///
 /// Given
Index: clang/docs/LibASTMatchersReference.html
===
--- clang/docs/LibASTMatchersReference.html
+++ clang/docs/LibASTMatchersReference.html
@@ -4671,6 +4671,23 @@
 Usable as: Matcherhttps://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html;>FunctionDecl, Matcherhttps://clang.llvm.org/doxygen/classclang_1_1VarDecl.html;>VarDecl, Matcherhttps://clang.llvm.org/doxygen/classclang_1_1CXXRecordDecl.html;>CXXRecordDecl
 
 
+
+Matcherclang::ParmVarDeclisAtPositionunsigned N
+Matches the ParmVarDecl nodes that 

[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-28 Thread Dmitri Gribenko via Phabricator via cfe-commits
gribozavr2 added a comment.

> Also, you need to update Registry.cpp to add this to the list of dynamic 
> matchers.

Good point. @oontvoo


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80603



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-28 Thread Dmitri Gribenko via Phabricator via cfe-commits
gribozavr2 added a comment.

> Is there a reason we want to support blocks but not lambdas?

Lambdas are supported (see tests). Lambdas generate classes with regular 
function decls, so there's no surprise there.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80603



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-28 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added a comment.

In D80603#2059122 , @gribozavr2 wrote:

> In D80603#2058019 , @aaron.ballman 
> wrote:
>
> > I'm sorry if I'm being dense, but `hasParameter`  traverses to the 
> > `ParmVarDecl`, so I'm not certain I understand why this new matcher is 
> > needed as a public matcher. It seems like you can already accomplish this 
> > without adding a second API to do effectively the same thing: 
> > `functionDecl(hasParameter(0, parmVarDecl().bind("param")))`, can't you?
>
>
> It is syntax sugar, true. Note that the proposed matcher is a narrowing 
> matcher for `parmVarDecl()`, while your suggestion is a narrowing matcher for 
> `functionDecl()`, so it is not an entirely apples-to-apples comparison. Think 
> about use cases like: `declRefExpr(to(parmVarDecl(at(...`. To rewrite 
> that with `hasParameter()`, we have to use `hasAncestor` to find the 
> `functionDecl` first, and then compare the AST node pointers. So while it is 
> possible to express this proposed operation today, it requires a complex 
> matcher for such a conceptually simple operation.


Thank you, that clarifies the need nicely for me!

Is there a reason we want to support blocks but not lambdas?

Also, you need to update Registry.cpp to add this to the list of dynamic 
matchers.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80603



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-28 Thread Vy Nguyen via Phabricator via cfe-commits
oontvoo updated this revision to Diff 266887.
oontvoo marked 2 inline comments as done.
oontvoo added a comment.

Change tests to us no/matchesObjC


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80603

Files:
  clang/docs/LibASTMatchersReference.html
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2643,6 +2643,45 @@
   parmVarDecl(hasDefaultArgument(;
 }
 
+TEST(IsAtPosition, Basic) {
+  EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(notMatches("void x(int val) {}", parmVarDecl(isAtPosition(1;
+}
+
+TEST(IsAtPosition, FunctionDecl) {
+  EXPECT_TRUE(matches("void x(int a);", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b);", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b);", parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(notMatches("void x(int val);", parmVarDecl(isAtPosition(1;
+}
+
+TEST(IsAtPosition, Lambda) {
+  EXPECT_TRUE(
+  matches("void x() { [](int a) {};  }", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x() { [](int a, int b) {}; }",
+  parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x() { [](int a, int b) {}; }",
+  parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(
+  notMatches("void x() { [](int val) {}; }", parmVarDecl(isAtPosition(1;
+}
+
+TEST(IsAtPosition, BlockDecl) {
+  EXPECT_TRUE(matchesObjC(
+  "void func()  { void (^my_block)(int arg) = ^void(int arg) {}; } ",
+  parmVarDecl(isAtPosition(0;
+
+  EXPECT_TRUE(matchesObjC("void func()  { void (^my_block)(int x, int y) = "
+  "^void(int x, int y) {}; } ",
+  parmVarDecl(isAtPosition(1;
+
+  EXPECT_TRUE(notMatchesObjC(
+  "void func()  { void (^my_block)(int arg) = ^void(int arg) {}; } ",
+  parmVarDecl(isAtPosition(1;
+}
+
 TEST(IsArray, Basic) {
   EXPECT_TRUE(matches("struct MyClass {}; MyClass *p1 = new MyClass[10];",
   cxxNewExpr(isArray(;
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -4615,6 +4615,34 @@
  InnerMatcher.matches(*DeclarationStatement, Finder, Builder);
 }
 
+/// Matches the ParmVarDecl nodes that are at the N'th position in the parameter
+/// list. The parameter list could be that of either a block, function, or
+/// objc-method.
+///
+///
+/// Given
+///
+/// \code
+/// void f(int a, int b, int c) {
+/// }
+/// \endcode
+///
+/// ``parmVarDecl(isAtPosition(0))`` matches ``int a``.
+///
+/// ``parmVarDecl(isAtPosition(1))`` matches ``int b``.
+AST_MATCHER_P(clang::ParmVarDecl, isAtPosition, unsigned, N) {
+  const clang::DeclContext *Context = Node.getParentFunctionOrMethod();
+
+  if (const auto *Decl = dyn_cast_or_null(Context))
+return N < Decl->param_size() && Decl->getParamDecl(N) == 
+  if (const auto *Decl = dyn_cast_or_null(Context))
+return N < Decl->param_size() && Decl->getParamDecl(N) == 
+  if (const auto *Decl = dyn_cast_or_null(Context))
+return N < Decl->param_size() && Decl->getParamDecl(N) == 
+
+  return false;
+}
+
 /// Matches the index expression of an array subscript expression.
 ///
 /// Given
Index: clang/docs/LibASTMatchersReference.html
===
--- clang/docs/LibASTMatchersReference.html
+++ clang/docs/LibASTMatchersReference.html
@@ -4671,6 +4671,23 @@
 Usable as: Matcherhttps://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html;>FunctionDecl, Matcherhttps://clang.llvm.org/doxygen/classclang_1_1VarDecl.html;>VarDecl, Matcherhttps://clang.llvm.org/doxygen/classclang_1_1CXXRecordDecl.html;>CXXRecordDecl
 
 
+
+Matcherclang::ParmVarDeclisAtPositionunsigned N
+Matches the ParmVarDecl nodes that are at the N'th position in the parameter
+list. The parameter list could be that of either a block, function, or
+objc-method.
+
+
+Given
+
+void f(int a, int b, int c) {
+}
+
+``parmVarDecl(isAtPosition(0))`` matches ``int a``.
+
+``parmVarDecl(isAtPosition(1))`` matches ``int b``.
+
+
 
 
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-27 Thread Dmitri Gribenko via Phabricator via cfe-commits
gribozavr2 added inline comments.



Comment at: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp:2672
+TEST(IsAtPosition, BlockDecl) {
+  // FIXME: Needs to enable block-support (ie., -fblocks)
+  if (true)

Please use `matchesObjC` / `notMatchesObjC`.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80603



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-27 Thread Dmitri Gribenko via Phabricator via cfe-commits
gribozavr2 added a comment.

In D80603#2058019 , @aaron.ballman 
wrote:

> I'm sorry if I'm being dense, but `hasParameter`  traverses to the 
> `ParmVarDecl`, so I'm not certain I understand why this new matcher is needed 
> as a public matcher. It seems like you can already accomplish this without 
> adding a second API to do effectively the same thing: 
> `functionDecl(hasParameter(0, parmVarDecl().bind("param")))`, can't you?


It is syntax sugar, true. Note that the proposed matcher is a narrowing matcher 
for `parmVarDecl()`, while your suggestion is a narrowing matcher for 
`functionDecl()`, so it is not an entirely apples-to-apples comparison. Think 
about use cases like: `declRefExpr(to(parmVarDecl(at(...`. To rewrite that 
with `hasParameter()`, we have to use `hasAncestor` to find the `functionDecl` 
first, and then compare the AST node pointers. So while it is possible to 
express this proposed operation today, it requires a complex matcher for such a 
conceptually simple operation.




Comment at: clang/include/clang/ASTMatchers/ASTMatchers.h:4621
+template 
+static bool __isParamAt(const clang::DeclContext *Context,
+const clang::ParmVarDecl , unsigned N) {

Identifiers with double underscores are reserved. Also, this file typically 
does not define helpers.

My best suggestion is to define a local lambda in the AST_MATCHER_P, or just 
copy the code three times -- there is not that much duplication.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80603



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-27 Thread Vy Nguyen via Phabricator via cfe-commits
oontvoo updated this revision to Diff 266735.
oontvoo added a comment.

More tests for Block


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80603

Files:
  clang/docs/LibASTMatchersReference.html
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2643,6 +2643,43 @@
   parmVarDecl(hasDefaultArgument(;
 }
 
+TEST(IsAtPosition, Basic) {
+  EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(notMatches("void x(int val) {}", parmVarDecl(isAtPosition(1;
+}
+
+TEST(IsAtPosition, FunctionDecl) {
+  EXPECT_TRUE(matches("void x(int a);", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b);", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b);", parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(notMatches("void x(int val);", parmVarDecl(isAtPosition(1;
+}
+
+TEST(IsAtPosition, Lambda) {
+  EXPECT_TRUE(
+  matches("void x() { [](int a) {};  }", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x() { [](int a, int b) {}; }",
+  parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x() { [](int a, int b) {}; }",
+  parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(
+  notMatches("void x() { [](int val) {}; }", parmVarDecl(isAtPosition(1;
+}
+
+TEST(IsAtPosition, BlockDecl) {
+  // FIXME: Needs to enable block-support (ie., -fblocks)
+  if (true)
+return;
+  EXPECT_TRUE(matches("void x()  { auto b = ^(int y) { }; } ",
+  parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x()  { auto b = ^(int y,int z) { }; } ",
+  parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(notMatches("void x()  { auto b = ^(int y) { }; } ",
+ parmVarDecl(isAtPosition(1;
+}
+
 TEST(IsArray, Basic) {
   EXPECT_TRUE(matches("struct MyClass {}; MyClass *p1 = new MyClass[10];",
   cxxNewExpr(isArray(;
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -4615,6 +4615,39 @@
  InnerMatcher.matches(*DeclarationStatement, Finder, Builder);
 }
 
+// Returns true if Node is a parameter at N in the given Context
+// of type DeclT.
+template 
+static bool __isParamAt(const clang::DeclContext *Context,
+const clang::ParmVarDecl , unsigned N) {
+  if (const auto *Decl = dyn_cast_or_null(Context))
+return N < Decl->param_size() && Decl->getParamDecl(N) == 
+
+  return false;
+}
+
+/// Matches the ParmVarDecl nodes that are at the N'th position in the parameter
+/// list. The parameter list could be that of either a block, function, or
+/// objc-method.
+///
+///
+/// Given
+///
+/// \code
+/// void f(int a, int b, int c) {
+/// }
+/// \endcode
+///
+/// ``parmVarDecl(isAtPosition(0))`` matches ``int a``.
+///
+/// ``parmVarDecl(isAtPosition(1))`` matches ``int b``.
+AST_MATCHER_P(clang::ParmVarDecl, isAtPosition, unsigned, N) {
+  const clang::DeclContext *ParentContext = Node.getParentFunctionOrMethod();
+  return __isParamAt(ParentContext, Node, N) ||
+ __isParamAt(ParentContext, Node, N) ||
+ __isParamAt(ParentContext, Node, N);
+}
+
 /// Matches the index expression of an array subscript expression.
 ///
 /// Given
Index: clang/docs/LibASTMatchersReference.html
===
--- clang/docs/LibASTMatchersReference.html
+++ clang/docs/LibASTMatchersReference.html
@@ -4671,6 +4671,23 @@
 Usable as: Matcherhttps://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html;>FunctionDecl, Matcherhttps://clang.llvm.org/doxygen/classclang_1_1VarDecl.html;>VarDecl, Matcherhttps://clang.llvm.org/doxygen/classclang_1_1CXXRecordDecl.html;>CXXRecordDecl
 
 
+
+Matcherclang::ParmVarDeclisAtPositionunsigned N
+Matches the ParmVarDecl nodes that are at the N'th position in the parameter
+list. The parameter list could be that of either a block, function, or
+objc-method.
+
+
+Given
+
+void f(int a, int b, int c) {
+}
+
+``parmVarDecl(isAtPosition(0))`` matches ``int a``.
+
+``parmVarDecl(isAtPosition(1))`` matches ``int b``.
+
+
 
 
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org

[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-27 Thread Vy Nguyen via Phabricator via cfe-commits
oontvoo updated this revision to Diff 266714.
oontvoo added a comment.

use helper for examining the node instead of nested matchers


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80603

Files:
  clang/docs/LibASTMatchersReference.html
  clang/include/clang/AST/ASTContext.h
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/AST/ASTContext.cpp
  clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2643,6 +2643,29 @@
   parmVarDecl(hasDefaultArgument(;
 }
 
+TEST(IsAtPosition, Basic) {
+  EXPECT_TRUE(matches("void x(int a) {}", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(notMatches("void x(int val) {}", parmVarDecl(isAtPosition(1;
+
+  // Tests with function-decls
+  EXPECT_TRUE(matches("void x(int a);", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b);", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b);", parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(notMatches("void x(int val);", parmVarDecl(isAtPosition(1;
+
+  // Tests with lamdas
+  EXPECT_TRUE(
+  matches("void x() { [](int a) {};  }", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x() { [](int a, int b) {}; }",
+  parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x() { [](int a, int b) {}; }",
+  parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(
+  notMatches("void x() { [](int val) {}; }", parmVarDecl(isAtPosition(1;
+}
+
 TEST(IsArray, Basic) {
   EXPECT_TRUE(matches("struct MyClass {}; MyClass *p1 = new MyClass[10];",
   cxxNewExpr(isArray(;
Index: clang/lib/AST/ASTContext.cpp
===
--- clang/lib/AST/ASTContext.cpp
+++ clang/lib/AST/ASTContext.cpp
@@ -10749,6 +10749,13 @@
   return I->second;
 }
 
+bool ASTContext::parameterHasIndex(const ParmVarDecl *D, unsigned index) const {
+  ParameterIndexTable::const_iterator I = ParamIndices.find(D);
+  if (I == ParamIndices.end())
+return false;
+  return I->second == index;
+}
+
 QualType ASTContext::getStringLiteralArrayType(QualType EltTy,
unsigned Length) const {
   // A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -4615,6 +4615,27 @@
  InnerMatcher.matches(*DeclarationStatement, Finder, Builder);
 }
 
+/// Matches the ParmVarDecl nodes that are at the N'th position in the parameter
+/// list. The parameter list could be that of either a block, function, or
+/// objc-method.
+///
+///
+/// Given
+///
+/// \code
+/// void f(int a, int b, int c) {
+/// }
+/// \endcode
+///
+/// ``parmVarDecl(isAtPosition(0))`` matches ``int a``.
+///
+/// ``parmVarDecl(isAtPosition(1))`` matches ``int b``.
+AST_MATCHER_P(clang::ParmVarDecl, isAtPosition, unsigned, N) {
+  const clang::DeclContext *ParentContext = Node.getParentFunctionOrMethod();
+  return ParentContext != nullptr &&
+ ParentContext->getParentASTContext().parameterHasIndex(, N);
+}
+
 /// Matches the index expression of an array subscript expression.
 ///
 /// Given
Index: clang/include/clang/AST/ASTContext.h
===
--- clang/include/clang/AST/ASTContext.h
+++ clang/include/clang/AST/ASTContext.h
@@ -2795,6 +2795,10 @@
   /// index of the parameter when it exceeds the size of the normal bitfield.
   unsigned getParameterIndex(const ParmVarDecl *D) const;
 
+  // Returns "true" iff the given ParmVarDecl exists in this context at the
+  // given position.
+  bool parameterHasIndex(const ParmVarDecl *D, unsigned index) const;
+
   /// Return a string representing the human readable name for the specified
   /// function declaration or file name. Used by SourceLocExpr and
   /// PredefinedExpr to cache evaluated results.
Index: clang/docs/LibASTMatchersReference.html
===
--- clang/docs/LibASTMatchersReference.html
+++ clang/docs/LibASTMatchersReference.html
@@ -4671,6 +4671,23 @@
 Usable as: Matcherhttps://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html;>FunctionDecl, Matcherhttps://clang.llvm.org/doxygen/classclang_1_1VarDecl.html;>VarDecl, 

[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-27 Thread Vy Nguyen via Phabricator via cfe-commits
oontvoo added a comment.

In D80603#2058019 , @aaron.ballman 
wrote:

> In D80603#2057970 , @oontvoo wrote:
>
> > In D80603#2057014 , @aaron.ballman 
> > wrote:
> >
> > > Does the `hasParameter()` matcher not already do this?
> >
> >
> > Yes, it does. But we'd like it to be able to match on the parmVarDecl().
>
>
> I'm sorry if I'm being dense, but `hasParameter`  traverses to the 
> `ParmVarDecl`, so I'm not certain I understand why this new matcher is needed 
> as a public matcher. It seems like you can already accomplish this without 
> adding a second API to do effectively the same thing: 
> `functionDecl(hasParameter(0, parmVarDecl().bind("param")))`, can't you?


Ah, I didn't mean to say it wasn't *already possible* to do any of this.

It would be *very* nice to have this (at least to some of us, that I know of). 
(make the logic a lot simpler in a few custom matchers).


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80603



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-27 Thread Vy Nguyen via Phabricator via cfe-commits
oontvoo updated this revision to Diff 266642.
oontvoo added a comment.

Remove unrelated doc generated by the script


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80603

Files:
  clang/docs/LibASTMatchersReference.html
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2643,6 +2643,29 @@
   parmVarDecl(hasDefaultArgument(;
 }
 
+TEST(IsAtPosition, Basic) {
+  EXPECT_TRUE(matches("void x(int a) {}", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(notMatches("void x(int val) {}", parmVarDecl(isAtPosition(1;
+
+  // Tests with function-decls
+  EXPECT_TRUE(matches("void x(int a);", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b);", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b);", parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(notMatches("void x(int val);", parmVarDecl(isAtPosition(1;
+
+  // Tests with lamdas
+  EXPECT_TRUE(
+  matches("void x() { [](int a) {};  }", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x() { [](int a, int b) {}; }",
+  parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x() { [](int a, int b) {}; }",
+  parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(
+  notMatches("void x() { [](int val) {}; }", parmVarDecl(isAtPosition(1;
+}
+
 TEST(IsArray, Basic) {
   EXPECT_TRUE(matches("struct MyClass {}; MyClass *p1 = new MyClass[10];",
   cxxNewExpr(isArray(;
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -4615,6 +4615,37 @@
  InnerMatcher.matches(*DeclarationStatement, Finder, Builder);
 }
 
+/// Matches the ParmVarDecl nodes that are at the N'th position in the parameter
+/// list. The parameter list could be that of either a block, function, or
+/// objc-method.
+///
+///
+/// Given
+///
+/// \code
+/// void f(int a, int b, int c) {
+/// }
+/// \endcode
+///
+/// ``parmVarDecl(isAtPosition(0))`` matches ``int a``.
+///
+/// ``parmVarDecl(isAtPosition(1))`` matches ``int b``.
+AST_MATCHER_P(clang::ParmVarDecl, isAtPosition, unsigned, N) {
+  constexpr char Name[] = "##isAtPosition_parmVar_";
+
+  // The direct parent of any parmVarDecl is a TypeLoc, and not a Decl,
+  // so we have to go up one more level.
+  return parmVarDecl(
+ hasParent(typeLoc(anyOf(hasParent(blockDecl(hasParameter(
+ N, parmVarDecl().bind(Name,
+ hasParent(objcMethodDecl(hasParameter(
+ N, parmVarDecl().bind(Name,
+ hasParent(functionDecl(hasParameter(
+ N, parmVarDecl().bind(Name))),
+ equalsBoundNode(Name))
+  .matches(Node, Finder, Builder);
+}
+
 /// Matches the index expression of an array subscript expression.
 ///
 /// Given
Index: clang/docs/LibASTMatchersReference.html
===
--- clang/docs/LibASTMatchersReference.html
+++ clang/docs/LibASTMatchersReference.html
@@ -4671,6 +4671,23 @@
 Usable as: Matcherhttps://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html;>FunctionDecl, Matcherhttps://clang.llvm.org/doxygen/classclang_1_1VarDecl.html;>VarDecl, Matcherhttps://clang.llvm.org/doxygen/classclang_1_1CXXRecordDecl.html;>CXXRecordDecl
 
 
+
+Matcherclang::ParmVarDeclisAtPositionunsigned N
+Matches the ParmVarDecl nodes that are at the N'th position in the parameter
+list. The parameter list could be that of either a block, function, or
+objc-method.
+
+
+Given
+
+void f(int a, int b, int c) {
+}
+
+``parmVarDecl(isAtPosition(0))`` matches ``int a``.
+
+``parmVarDecl(isAtPosition(1))`` matches ``int b``.
+
+
 
 
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-27 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added a comment.

In D80603#2057970 , @oontvoo wrote:

> In D80603#2057014 , @aaron.ballman 
> wrote:
>
> > Does the `hasParameter()` matcher not already do this?
>
>
> Yes, it does. But we'd like it to be able to match on the parmVarDecl().


I'm sorry if I'm being dense, but `hasParameter`  traverses to the 
`ParmVarDecl`, so I'm not certain I understand why this new matcher is needed 
as a public matcher. It seems like you can already accomplish this without 
adding a second API to do effectively the same thing: 
`functionDecl(hasParameter(0, parmVarDecl().bind("param")))`, can't you?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80603



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-27 Thread Vy Nguyen via Phabricator via cfe-commits
oontvoo updated this revision to Diff 266615.
oontvoo marked 7 inline comments as done.
oontvoo added a comment.

- Added docs
- Make it work with {block,objcMethod}Decl too.  More tests


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80603

Files:
  clang/docs/LibASTMatchersReference.html
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2643,6 +2643,29 @@
   parmVarDecl(hasDefaultArgument(;
 }
 
+TEST(IsAtPosition, Basic) {
+  EXPECT_TRUE(matches("void x(int a) {}", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(notMatches("void x(int val) {}", parmVarDecl(isAtPosition(1;
+
+  // Tests with function-decls
+  EXPECT_TRUE(matches("void x(int a);", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b);", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b);", parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(notMatches("void x(int val);", parmVarDecl(isAtPosition(1;
+
+  // Tests with lamdas
+  EXPECT_TRUE(
+  matches("void x() { [](int a) {};  }", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x() { [](int a, int b) {}; }",
+  parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x() { [](int a, int b) {}; }",
+  parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(
+  notMatches("void x() { [](int val) {}; }", parmVarDecl(isAtPosition(1;
+}
+
 TEST(IsArray, Basic) {
   EXPECT_TRUE(matches("struct MyClass {}; MyClass *p1 = new MyClass[10];",
   cxxNewExpr(isArray(;
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -3096,6 +3096,37 @@
 internal::TypeList>
 hasAncestor;
 
+/// Matches the ParmVarDecl nodes that are at the N'th position in the parameter
+/// list. The parameter list could be that of either a block, function, or
+/// objc-method.
+///
+///
+/// Given
+///
+/// \code
+/// void f(int a, int b, int c) {
+/// }
+/// \endcode
+///
+/// ``parmVarDecl(isAtPosition(0))`` matches ``int a``.
+///
+/// ``parmVarDecl(isAtPosition(1))`` matches ``int b``.
+AST_MATCHER_P(clang::ParmVarDecl, isAtPosition, unsigned, N) {
+  constexpr char Name[] = "##isAtPosition_parmVar_";
+
+  // The direct parent of any parmVarDecl is a TypeLoc, and not a Decl,
+  // so we have to go up one more level.
+  return parmVarDecl(
+ hasParent(typeLoc(anyOf(hasParent(blockDecl(hasParameter(
+ N, parmVarDecl().bind(Name,
+ hasParent(objcMethodDecl(hasParameter(
+ N, parmVarDecl().bind(Name,
+ hasParent(functionDecl(hasParameter(
+ N, parmVarDecl().bind(Name))),
+ equalsBoundNode(Name))
+  .matches(Node, Finder, Builder);
+}
+
 /// Matches if the provided matcher does not match.
 ///
 /// Example matches Y (matcher = cxxRecordDecl(unless(hasName("X"
Index: clang/docs/LibASTMatchersReference.html
===
--- clang/docs/LibASTMatchersReference.html
+++ clang/docs/LibASTMatchersReference.html
@@ -1226,6 +1226,11 @@
 
 
 
+Matcherhttps://clang.llvm.org/doxygen/classclang_1_1Stmt.html;>StmtfixedPointLiteralMatcherhttps://clang.llvm.org/doxygen/classclang_1_1FixedPointLiteral.html;>FixedPointLiteral...
+Matches fixed point literals
+
+
+
 Matcherhttps://clang.llvm.org/doxygen/classclang_1_1Stmt.html;>StmtfloatLiteralMatcherhttps://clang.llvm.org/doxygen/classclang_1_1FloatingLiteral.html;>FloatingLiteral...
 Matches float literals of all sizes / encodings, e.g.
 1.0, 1.0f, 1.0L and 1e10.
@@ -4671,6 +4676,23 @@
 Usable as: Matcherhttps://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html;>FunctionDecl, Matcherhttps://clang.llvm.org/doxygen/classclang_1_1VarDecl.html;>VarDecl, Matcherhttps://clang.llvm.org/doxygen/classclang_1_1CXXRecordDecl.html;>CXXRecordDecl
 
 
+
+Matcherclang::ParmVarDeclisAtPositionunsigned N
+Matches the ParmVarDecl nodes that are at the N'th position in the parameter
+list. The parameter list could be that of either a block, function, or
+objc-method.
+
+
+Given
+
+void f(int a, int b, int c) {
+}
+

[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-27 Thread Vy Nguyen via Phabricator via cfe-commits
oontvoo added a comment.

In D80603#2057014 , @aaron.ballman 
wrote:

> Does the `hasParameter()` matcher not already do this?


Yes, it does. But we'd like it to be able to match on the parmVarDecl().




Comment at: clang/include/clang/ASTMatchers/ASTMatchers.h:7018
+AST_MATCHER_P(clang::ParmVarDecl, isAtPosition, unsigned, N) {
+  return parmVarDecl(hasAncestor(functionDecl(
+ hasParameter(N, parmVarDecl().bind("this_decl",

ymandel wrote:
> I think that `hasAncestor` will keep going up the chain of ancestors if it 
> fails the match on the first surrounding `functionDecl`. That could make this 
> matcher very expensive.  Why hasParent (or, whatever combination of 
> `hasParent` and other matchers needed to precisely describe the immediately 
> surrounding `functionDecl`)?
> Why [not] hasParent ?

The direct parent of the param is a TypeLoc, so I thought `hasAncestor()` was 
more convenient. But I've changed it to two nested `hasParent()` now.




Comment at: clang/include/clang/ASTMatchers/ASTMatchers.h:7022
+  .matches(Node, Finder, Builder);
+}
+

gribozavr2 wrote:
> Could you move it closer to other parameter-related matchers, maybe near the 
> definition of `forEachArgumentWithParam`?
Moved it to after hasAncestor.
(Can't move to near forEachArgumentWithParam because that's before hasAncestor 
and hasParent, which are needed by this)


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80603



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-27 Thread Yitzhak Mandelbaum via Phabricator via cfe-commits
ymandel added inline comments.



Comment at: clang/include/clang/ASTMatchers/ASTMatchers.h:7018
+AST_MATCHER_P(clang::ParmVarDecl, isAtPosition, unsigned, N) {
+  return parmVarDecl(hasAncestor(functionDecl(
+ hasParameter(N, parmVarDecl().bind("this_decl",

I think that `hasAncestor` will keep going up the chain of ancestors if it 
fails the match on the first surrounding `functionDecl`. That could make this 
matcher very expensive.  Why hasParent (or, whatever combination of `hasParent` 
and other matchers needed to precisely describe the immediately surrounding 
`functionDecl`)?



Comment at: clang/include/clang/ASTMatchers/ASTMatchers.h:7019
+  return parmVarDecl(hasAncestor(functionDecl(
+ hasParameter(N, parmVarDecl().bind("this_decl",
+ equalsBoundNode("this_decl"))

Note that "this_decl" might be in use the by the caller (unlikely, but 
possible).  I'd suggest something less user friendly instead (for example, 
prefixing with the name of the matcher). That said, any binding inside a 
manager runs risk of conflict, because we don't have any scoping or fresh-name 
generation facilities, so any name will run some risk.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80603



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-27 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added a reviewer: aaron.ballman.
aaron.ballman added a comment.

Does the `hasParameter()` matcher not already do this?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80603



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-27 Thread Dmitri Gribenko via Phabricator via cfe-commits
gribozavr2 added a comment.

Please run clang/docs/tools/dump_ast_matchers.py to update the docs.




Comment at: clang/include/clang/ASTMatchers/ASTMatchers.h:7022
+  .matches(Node, Finder, Builder);
+}
+

Could you move it closer to other parameter-related matchers, maybe near the 
definition of `forEachArgumentWithParam`?



Comment at: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp:2650
+  EXPECT_TRUE(matches("void x(int a, int b) {}", 
parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(notMatches("void x(int val) {}", parmVarDecl(isAtPosition(1;
+}

Could you change some tests to have a function declaration instead of 
definition?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80603



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-26 Thread Vy Nguyen via Phabricator via cfe-commits
oontvoo added a comment.

P.S: Please ignore the child revision ... accidentally committed it to the 
wrong branch. I've fixed the git branches locally but can't figure out how to 
tell phabricator ...


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80603



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D80603: add isAtPosition narrowing matcher for parmVarDecl

2020-05-26 Thread Vy Nguyen via Phabricator via cfe-commits
oontvoo created this revision.
Herald added subscribers: cfe-commits, danielkiss, kristof.beyls.
Herald added a project: clang.
oontvoo added reviewers: gribozavr, ymandel.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D80603

Files:
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp


Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2643,6 +2643,13 @@
   parmVarDecl(hasDefaultArgument(;
 }
 
+TEST(IsAtPosition, Basic) {
+  EXPECT_TRUE(matches("void x(int a) {}", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b) {}", 
parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(matches("void x(int a, int b) {}", 
parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(notMatches("void x(int val) {}", parmVarDecl(isAtPosition(1;
+}
+
 TEST(IsArray, Basic) {
   EXPECT_TRUE(matches("struct MyClass {}; MyClass *p1 = new MyClass[10];",
   cxxNewExpr(isArray(;
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -7001,6 +7001,26 @@
   return InnerMatcher.matches(Node, Finder, Builder);
 }
 
+/// Matches the ParmVarDecl nodes that are at the N'th position in the 
parameter
+/// list.
+///
+/// Given
+///
+/// \code
+/// void f(int a, int b, int c) {
+/// }
+/// \endcode
+///
+/// ``parmVarDecl(isAtPosition(0))``` matches `int a`.
+///
+/// ``parmVarDecl(isAtPosition(1))`` matches `int b`.
+AST_MATCHER_P(clang::ParmVarDecl, isAtPosition, unsigned, N) {
+  return parmVarDecl(hasAncestor(functionDecl(
+ hasParameter(N, parmVarDecl().bind("this_decl",
+ equalsBoundNode("this_decl"))
+  .matches(Node, Finder, Builder);
+}
+
 
////
 // OpenMP handling.
 
////


Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2643,6 +2643,13 @@
   parmVarDecl(hasDefaultArgument(;
 }
 
+TEST(IsAtPosition, Basic) {
+  EXPECT_TRUE(matches("void x(int a) {}", parmVarDecl(isAtPosition(0;
+  EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1;
+  EXPECT_TRUE(notMatches("void x(int val) {}", parmVarDecl(isAtPosition(1;
+}
+
 TEST(IsArray, Basic) {
   EXPECT_TRUE(matches("struct MyClass {}; MyClass *p1 = new MyClass[10];",
   cxxNewExpr(isArray(;
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -7001,6 +7001,26 @@
   return InnerMatcher.matches(Node, Finder, Builder);
 }
 
+/// Matches the ParmVarDecl nodes that are at the N'th position in the parameter
+/// list.
+///
+/// Given
+///
+/// \code
+/// void f(int a, int b, int c) {
+/// }
+/// \endcode
+///
+/// ``parmVarDecl(isAtPosition(0))``` matches `int a`.
+///
+/// ``parmVarDecl(isAtPosition(1))`` matches `int b`.
+AST_MATCHER_P(clang::ParmVarDecl, isAtPosition, unsigned, N) {
+  return parmVarDecl(hasAncestor(functionDecl(
+ hasParameter(N, parmVarDecl().bind("this_decl",
+ equalsBoundNode("this_decl"))
+  .matches(Node, Finder, Builder);
+}
+
 ////
 // OpenMP handling.
 ////
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits