handle nullptr in various clang plugins

since we are using it so widely now, instead of NULL

Change-Id: I990ff1334f657663e8791ab064d69e56636fe6e7
This commit is contained in:
Noel Grandin
2016-09-12 08:20:17 +02:00
parent ba269f7294
commit 25e4e9694c
3 changed files with 33 additions and 5 deletions

View File

@@ -26,6 +26,8 @@ public:
virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
bool VisitCallExpr(CallExpr * callExpr);
private:
bool evaluate(const Expr* expr, APSInt& x);
};
bool DefaultParams::VisitCallExpr(CallExpr * callExpr) {
@@ -69,9 +71,7 @@ bool DefaultParams::VisitCallExpr(CallExpr * callExpr) {
if (!found)
{
APSInt x1, x2;
if (defaultArgExpr->EvaluateAsInt(x1, compiler.getASTContext())
&& arg->EvaluateAsInt(x2, compiler.getASTContext())
&& x1 == x2)
if (evaluate(defaultArgExpr, x1) && evaluate(arg, x2) && x1 == x2)
{
found = true;
}
@@ -108,6 +108,19 @@ bool DefaultParams::VisitCallExpr(CallExpr * callExpr) {
return true;
}
bool DefaultParams::evaluate(const Expr* expr, APSInt& x)
{
if (isa<CXXNullPtrLiteralExpr>(expr)) {
x = 0;
return true;
}
if (expr->EvaluateAsInt(x, compiler.getASTContext()))
{
return true;
}
return false;
}
loplugin::Plugin::Registration< DefaultParams > X("defaultparams");
}

View File

@@ -37,6 +37,7 @@ public:
private:
bool hasSameDefaultParams(const ParmVarDecl * parmVarDecl, const ParmVarDecl * superParmVarDecl);
bool evaluate(const Expr* expr, APSInt& x);
};
void OverrideParam::run()
@@ -155,8 +156,7 @@ bool OverrideParam::hasSameDefaultParams(const ParmVarDecl * parmVarDecl, const
return true;
}
APSInt x1, x2;
if (defaultArgExpr->EvaluateAsInt(x1, compiler.getASTContext())
&& superDefaultArgExpr->EvaluateAsInt(x2, compiler.getASTContext()))
if (evaluate(defaultArgExpr, x1) && evaluate(superDefaultArgExpr, x2))
{
return x1 == x2;
}
@@ -186,6 +186,18 @@ bool OverrideParam::hasSameDefaultParams(const ParmVarDecl * parmVarDecl, const
#endif
}
bool OverrideParam::evaluate(const Expr* expr, APSInt& x)
{
if (expr->EvaluateAsInt(x, compiler.getASTContext()))
{
return true;
}
if (isa<CXXNullPtrLiteralExpr>(expr)) {
x = 0;
return true;
}
return false;
}
loplugin::Plugin::Registration< OverrideParam > X("overrideparam");

View File

@@ -474,6 +474,9 @@ std::string SingleValFields::getExprValue(const Expr* arg)
{
return x1.toString(10);
}
if (isa<CXXNullPtrLiteralExpr>(arg)) {
return "0";
}
return "?";
}