如何计算在具有特定类型的操作数的代码库中使用重载运算符的次数 [英] How can I count number of times an overloaded operator was used in a code base with particular type of operands

查看:108
本文介绍了如何计算在具有特定类型的操作数的代码库中使用重载运算符的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板类SafeInt<T>(由Microsoft提供).

I have a templated class SafeInt<T> (By Microsoft).

从理论上讲,此类可以用于代替POD整数类型,并且可以在算术运算期间检测任何整数溢出.

This class in theory can be used in place of a POD integer type and can detect any integer overflows during arithmetic operations.

对于该类,我编写了一些自定义的模板化重载算术运算符(+,-,*,/)函数,其两个参数均为SafeInt<T>的对象.

For this class I wrote some custom templatized overloaded arithmetic operator (+, -, *, /) functions whose both arguments are objects of SafeInt<T>.

我将所有整数类型都定义为SafeInt类类型.

I typedef'd all my integer types to SafeInt class type.

我想在我的代码库中搜索所述二进制运算符的实例,其中两个操作数均为SafeInt类型.

I want to search my codebase for instances of the said binary operators where both operands are of type SafeInt.

我能想到的一些方式

  1. 使用正则表达式进行字符串搜索,并遍历代码以检测两个操作数均为SafeInt对象的操作员用法实例.

  1. String search using regex and weed through the code to detect operator usage instances where both operands are SafeInt objects.

编写一个clang工具并处理AST以进行此搜索(我尚未学习如何编写这样的工具.)

Write a clang tool and process the AST to do this searching (I am yet to learn how to write such a tool.)

以某种方式添加一个计数器来计算自定义重载运算符实例化的次数.我花了很多时间尝试此操作,但似乎没有用.

Somehow add a counter to count the number of times the custom overloaded operator is instantiated. I spent a lot of time trying this but doesn't seem to work.

有人可以提出更好的方法吗?

Can anyone suggest a better way?

请让我知道是否需要澄清任何内容.

Please let me know if I need to clarify anything.

谢谢.

推荐答案

简短答案

您可以使用clang-query命令执行此操作:

Short answer

You can do this using the clang-query command:

$ clang-query \
  -c='m cxxOperatorCallExpr(callee(functionDecl(hasName("operator+"))), hasArgument(0, expr(hasType(cxxRecordDecl(hasName("SafeInt"))))), hasArgument(1, expr(hasType(cxxRecordDecl(hasName("SafeInt"))))))' \
  use-si.cc --

Match #1:

/home/scott/wrk/learn/clang/clang-query1/use-si.cc:10:3: note: "root" binds here
  x + y;           // reported
  ^~~~~
1 match.

什么是clang-query?

clang-query 是一个该实用程序旨在促进编写 clang-tidy 检查.特别是,它了解 AST Matchers 的语言,可用于交互式探索什么是由给定的匹配表达式匹配.但是,如此处所示,它也可以非交互地用于查找任意AST树模式.

What is clang-query?

clang-query is a utility intended to facilitate writing clang-tidy checks. In particular it understands the language of AST Matchers and can be used to interactively explore what is matched by a given match expression. However, as shown here, it can also be used non-interactively to look for arbitrary AST tree patterns.

博客文章探索Clang工具第2部分: noreferrer>斯蒂芬·凯利(Stephen Kelly)提供了有关使用clang-query的很好的介绍.

The blog post Exploring Clang Tooling Part 2: Examining the Clang AST with clang-query by Stephen Kelly provides a nice introduction to using clang-query.

clang-query程序包含在预构建的LLVM二进制文件中,或者可以按照 AST Matchers教程中所述的来源来构建.

The clang-query program is included in the pre-built LLVM binaries, or it can be built from source as described in the AST Matchers Tutorial.

-c参数提供了非交互运行的命令.添加空格后,命令为:

The -c argument provides a command to run non-interactively. With whitespace added, the command is:

m                                  // Match (and report) every
cxxOperatorCallExpr(               // operator function call
  callee(functionDecl(             // where the callee
    hasName("operator+"))),        // is "operator+", and
  hasArgument(0,                   // where the first argument
    expr(hasType(cxxRecordDecl(    // is a class type
      hasName("SafeInt"))))),      // called "SafeInt",
  hasArgument(1,                   // and the second argument
    expr(hasType(cxxRecordDecl(    // is also a class type
      hasName("SafeInt"))))))      // called "SafeInt".

命令行以use-si.cc --结尾,这意味着要分析use-si.cc,并且clang不需要额外的编译器标志来解释它.

The command line ends with use-si.cc --, meaning to analyze use-si.cc and there are no extra compiler flags needed by clang to interpret it.

clang-query命令行的基本结构与 clang-tidy <相同. /a>,包括通过-p compile_commands.json一次扫描多个文件的功能,每个文件可能具有不同的编译器选项.

The clang-query command line has the same basic structure as that of clang-tidy, including the ability to pass -p compile_commands.json to scan many files at once, possibly with different compiler options per file.

为完整起见,我用来测试匹配器的输入是use-si.cc:

For completeness, the input I used to test my matcher is use-si.cc:

// use-si.cc

#include "SafeInt.hpp"         // SafeInt

void f1()
{
  SafeInt<int> x(2);
  SafeInt<int> y(3);

  x + y;           // reported

  x + 2;           // not reported

  2 + x;           // not reported
}

其中SafeInt.hpp来自 https://github.com/dcleblanc/SafeInt ,在 Microsoft SafeInt页面上.

这篇关于如何计算在具有特定类型的操作数的代码库中使用重载运算符的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆