clang AST匹配ostream<<

时间:2018-01-02 22:28:42

标签: c++ clang clang-ast-matchers

我试图为涉及流媒体的某些场景写一个铿锵有力的检查。考虑这个简单的功能:

#include <iostream>
#include <stdint.h>

void foo(std::ostream& os, uint8_t i) {
    os << i;
    os << 4 << i;
}

如果我运行clang-query来测试一些匹配器:

$ clang-query foo.cxx --
clang-query> match cxxOperatorCallExpr(hasOverloadedOperatorName("<<"))
// [ ... snip some std library matches ... ]
Match #7:

foo.cxx:5:5: note: "root" binds here
    os << i;
    ^~~~~~~

Match #8:

foo.cxx:6:5: note: "root" binds here
    os << 4 << i;
    ^~~~~~~~~~~~

Match #9:

foo.cxx:6:5: note: "root" binds here
    os << 4 << i;
    ^~~~~~~

这与我在此计划中使用的operator<<的所有3次使用相匹配。大。但是,如果我尝试为第一个参数添加过滤器:

clang-query> match cxxOperatorCallExpr(hasOverloadedOperatorName("<<"), hasArgument(0, expr(hasType(asString("std::ostream")))))

Match #1:

foo.cxx:5:5: note: "root" binds here
    os << i;
    ^~~~~~~

Match #2:

foo.cxx:6:5: note: "root" binds here
    os << 4 << i;
    ^~~~~~~

那就是它。它与整个表达式os << 4 << i不匹配。为什么不?该表达式的类型为std::ostream。如果我直接转储AST,我会看到:

|-CXXOperatorCallExpr 0x953f618 <line:6:5, col:16> 'basic_ostream<char, struct std::char_traits<char> >':'class std::basic_ostream<char>' lvalue
| |-ImplicitCastExpr 0x953f600 <col:13> 'basic_ostream<char, struct std::char_traits<char> > &(*)(basic_ostream<char, struct std::char_traits<char> > &, unsigned char)' <FunctionToPointerDecay>
| | `-DeclRefExpr 0x953f5d8 <col:13> 'basic_ostream<char, struct std::char_traits<char> > &(basic_ostream<char, struct std::char_traits<char> > &, unsigned char)' lvalue Function 0x94bcd40 'operator<<' 'basic_ostream<char, struct std::char_traits<char> > &(basic_ostream<char, struct std::char_traits<char> > &, unsigned char)'
| |-CXXOperatorCallExpr 0x953f160 <col:5, col:11> 'std::basic_ostream<char, struct std::char_traits<char> >::__ostream_type':'class std::basic_ostream<char>' lvalue
| | |-ImplicitCastExpr 0x953f148 <col:8> 'std::basic_ostream<char, struct std::char_traits<char> >::__ostream_type &(*)(int)' <FunctionToPointerDecay>
| | | `-DeclRefExpr 0x953f0c0 <col:8> 'std::basic_ostream<char, struct std::char_traits<char> >::__ostream_type &(int)' lvalue CXXMethod 0x94b7740 'operator<<' 'std::basic_ostream<char, struct std::char_traits<char> >::__ostream_type &(int)'
| | |-DeclRefExpr 0x953ec88 <col:5> 'std::ostream':'class std::basic_ostream<char>' lvalue ParmVar 0x953e470 'os' 'std::ostream &'
| | `-IntegerLiteral 0x953ecb0 <col:11> 'int' 4
| `-ImplicitCastExpr 0x953f5c0 <col:16> 'uint8_t':'unsigned char' <LValueToRValue>
|   `-DeclRefExpr 0x953f1a8 <col:16> 'uint8_t':'unsigned char' lvalue ParmVar 0x953e500 'i' 'uint8_t':'unsigned char'

外部和内部CXXOperatorCallExpr都说他们的表达是class std::basic_ostream<char> lvalue。什么是正确匹配这个论点的正确方法?

1 个答案:

答案 0 :(得分:0)

对于<<的第一个子调用的返回类型肯定存在问题,请参阅以下匹配器,该匹配器也与"class std::__1::basic_ostream<char>"匹配,并且缺少此调用:

clang-query> match callExpr(hasArgument(0, expr(hasType(anyOf(asString("class std::__1::basic_ostream<char>"), asString("std::ostream"))))))

Match #1:

/Users/ug/clangtest/main.cc:5:5: note: "root" binds here
    os << i;
    ^~~~~~~

Match #2:

/Users/ug/clangtest/main.cc:6:5: note: "root" binds here
    os << 4 << i;
    ^~~~~~~~~~~~

Match #3:

/Users/ug/clangtest/main.cc:6:5: note: "root" binds here
    os << 4 << i;
    ^~~~~~~
3 matches.