Klocwork(或其他工具)可以知道类型,typedef和#define指令吗? [英] Can Klocwork (or other tools) be aware of types, typedefs and #define directives?

查看:161
本文介绍了Klocwork(或其他工具)可以知道类型,typedef和#define指令吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已寻找工具来帮助检测阻止程序作为64位代码正常运行的错误。最近,我一直在玩Klocwork及其自定义检查功能,这让我使用XPath作为树来浏览源代码。这对于正则表达式是一个更聪明的替代方法,但是我还是不能知道类型。

I have been looking for tools to help detect errors that prevent a program from running properly as 64-bit code. Most recently, I've been toying with Klocwork and its custom checkers feature, which lets me navigate the source code as a tree using XPath. This is useful as a "smarter" alternative to regular expressions, but I have not been able to make it aware of types.

例如,我想使用 int long 查找循环的 code>来计数。以下代码很容易找到。

For example, let's say I'd like to find every instance of a for loop that uses either an int or a long to count. The following code is easy to find.

for (int i = 0; i < 10; i++)
    // ...

搜索此代码很简单,因为变量定义在循环内。但是,请考虑以下示例。

Searching for this code is trivial because the variable definition is right inside the loop. However, consider the following example.

int i;
// ...
for (i = 0; i < 10; i++)
    // ...

这很难找到,因为变量定义与循环是分开的,必要的XPath表达式将是笨重的或易出错的。

This is difficult to find because the variable definition is separate from the loop, and the necessary XPath expression would be either unwieldy or bug-prone.

所以,可以自定义Klocwork规则找到这样的表达式,类型感知是必要的,包括解析 typedef #define 语句?是否有其他工具可以做到这一点?

So, can custom Klocwork rules find expressions like this where type-awareness is necessary, including resolving typedef and #define statements? Are there other tools which can do this?

编辑1:请考虑以下示例。

typedef int myint;

void Foo() {
    int i;
    for (i = 0; i < 10; i++) {
        Bar();
    }

    myint j;
    for (j = 0; j < 10; j++) {
        Bar();
    }
}

找到第一个循环因为 i 的类型显式定义为 int 。然而,没有找到第二个循环,因为typedef掩盖了底层类型。什么工具跟踪类型的方式,将标识第二个循环变量 j 确实是 int

The solution provided by ahmeddirie finds the first loop because the type of i is explicitly defined as int. The second loop is not found, however, because the typedef has obscured the underlying type. What tools keep track of types in a way that would identify the second loop variable j as indeed being an int?

推荐答案

不完全确定这是否是你想要的,但你可以随时使用内置函数轻松地解析类型。例如,回答你的问题(虽然可能不是你的根本需要):

Not entirely sure if this is what you want, but you can always resolve types quite easily with built-in functions. For example, answering your question (although perhaps not your underlying need):

//ForStmt / Init::ExprStmt / Expr::BinaryExpr [ $type := Left.getTypeName() ] [ $type = 'int' | $type.contains('long') ]

'或'long int'计数器类型非常方便,并且显然可以应用于基于表达式语句的任何元素。

This will find ‘for’ loops that use ‘int’ or ‘long int’ counter types quite handily, and can obviously be applied to any element of an expression-based statement.

类型定义适合这种操作,无论是程序员定义还是语言定义。然而,预处理器定义只会产生其本地语言类型(即宏本身不能通过KAST操作,只有它扩展到)。

Type definitions are amenable to this kind of manipulation, whether programmer-defined or language-defined. Pre-processor definitions, however, will only yield their native language type (i.e. the macro itself isn’t available for manipulation via KAST, only what it expands to).

这篇关于Klocwork(或其他工具)可以知道类型,typedef和#define指令吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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