使用 oclint 检查 clang::Expr 的计算结果是否为 NULL

问题描述 投票:0回答:1

我目前正在为我的大学制定一套自定义 OCLint 规则,我需要捕获如下语句:

char *str = strdup("Hello, world!");
if (str == NULL) {   // That`s what i`m interested in
    ...
}

那么如何检查

Expr
是否为
NULL
?我知道有一个方法
Expr::isNullPointerConstant(ASTContext &Ctx, NullPointerConstantValueDependence NPC)
,但我不知道如何从我的
ASTContext
中获取
oclint::AbstractASTVisitorRule
。文档中没有提及类似的内容(或者只是我,idk \_(0_0)_/)

我该怎么做?

c++ oclint
1个回答
0
投票

我就是这么做的,我猜它是有效的。

namespace oclint {
    class StringCompareRule : public oclint::AbstractASTVisitorRule<StringCompareRule>
    {

    // ...

        bool IsNull(clang::Expr *expr)
        {
            return expr->isNullPointerConstant(*this->_carrier->getASTContext(), clang::Expr::NPC_ValueDependentIsNotNull);
        }

    // ...
}

© www.soinside.com 2019 - 2024. All rights reserved.