我目前正在为我的大学制定一套自定义 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)_/)
我该怎么做?
我就是这么做的,我猜它是有效的。
namespace oclint {
class StringCompareRule : public oclint::AbstractASTVisitorRule<StringCompareRule>
{
// ...
bool IsNull(clang::Expr *expr)
{
return expr->isNullPointerConstant(*this->_carrier->getASTContext(), clang::Expr::NPC_ValueDependentIsNotNull);
}
// ...
}