1

给定一个函数模板,例如:

template <typename T> void function(T &&t) { /*...*/ }

如何找到对传递右值的函数的调用:

function(1); // MATCH
int i;
function(i); // SKIP
int foo();
function(foo()); // MATCH
...

你明白了。

我在想类似的事情:

callExpr(callee(functionDecl(
                    hasName("function"),
                    unless(hasTemplateArgument(0,
                        refersToType(references(anything()))))))

过滤掉T被推断为引用类型的情况(表示传递了一个左值),但我不知道如何将Matcher<FunctionDecl>预期的 by连接functionDeclMatcher<TemplateSpecializationType>返回的 from hasTemplateArgument

我正在使用 Clang 3.8,以防万一(在线文档似乎是 5.0.0,并且http://releases.llvm.org/3.8.0/tools/clang/docs/LibASTMatchersReference.html给出了 404 )。

4

2 回答 2

1

这是询问参数类型的稍微不同的方法:

callExpr(
  callee(
    functionDecl(           // could also narrow on name, param count etc
      hasAnyParameter(      // could also use hasParameter(n,...)
        parmVarDecl(
          hasType(
            rValueReferenceType()
          )
        ).bind("pdecl")
      ) 
    ).bind("fdecl")
  )
)

在此测试代码上:

template <typename T> void g(T &&t){}

template <typename T> void g(T &t){}

void g(){
  int i = 2;
  g<int>(i);
  g<int>(2);
}

clang-query 显示匹配器匹配第一个 (rval) 调用,而不是第二个 (lval):

Match #1:

test_input_rval_call.cc:1:23: note: "fdecl" binds here
template <typename T> void g(T &&t){}
                      ^~~~~~~~~~~~~~~
test_input_rval_call.cc:1:30: note: "pdecl" binds here
template <typename T> void g(T &&t){}
                             ^~~~~
test_input_rval_call.cc:8:3: note: "root" binds here
  g<int>(2);
  ^~~~~~~~~
1 match.
于 2017-07-11T15:03:11.817 回答
0

这似乎有效:

callExpr(hasDeclaration(functionDecl(hasName("function"))),
         hasArgument(0, cxxBindTemporaryExpr()))

虽然我确信它错过了一些场景。

于 2017-07-11T12:46:39.587 回答