1

我正在尝试将不必要的调用匹配到c_str()调用乐于std::string直接使用的函数时,以便我可以删除不必要的调用,以便我可以编写一个clang-tidy检查来自动转换语句,例如:

fmt::print("{} {}", s1.c_str(), s2.c_str());

进入

fmt::print("{} {}", s1, s2);

虽然我已经能够想出一个匹配整个语句的匹配器,但如果我可以c_str单独绑定所有调用会更方便。我试过了

auto StringType = hasUnqualifiedDesugaredType(recordType(hasDeclaration(cxxRecordDecl(
                      hasName("::std::basic_string")))));
auto PrintCall = hasName("::fmt::print");
StatementMatcher CStrMatcher = traverse(                                                                                                                                                                                                                              
      TK_AsIs, callExpr(callee(functionDecl(PrintCall)),                                                                                                                                                                                                                
                        hasAnyArgument(cxxMemberCallExpr(                                                                                                                                                                                                               
                                            callee(cxxMethodDecl(hasName("c_str"))),                                                                                                                                                                                    
                                            on(hasType(StringType))).bind("c_str")))                                                                                                                                                                                    
      );                                                                                                                                                                                                                                                                

但无论调用多少个参数,我都只能得到一个匹配c_str。有没有办法迭代我绑定的单独参数匹配,或者我是否需要自己在check成员中迭代所有参数(无论它们是否匹配)?

4

0 回答 0