Skip to content

Commit 8dbb337

Browse files
authored
[analyzer] Simplify CallEvent castArgToParamTypeIfNeeded (#120981)
I noticed recently that this code (that I wrote xD) uses the `getRuntimeDefinition()` which isn't quite necessary for the simple task this function was designed for. Why would it be better not using this API here? I'm experimenting with improving how virtual functions are inlined, where depending on our ability of deducing the dynamic type of the object we may end up with inaccurate type information. Such inaccuracy would mean that we may have multiple runtime definitions. After that, this code would become ambiguous. To resolve this, I decided to refactor this and use a simpler - but equivalent approach.
1 parent f0f8dab commit 8dbb337

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

clang/lib/StaticAnalyzer/Core/CallEvent.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -435,27 +435,27 @@ static SVal processArgument(SVal Value, const Expr *ArgumentExpr,
435435
/// runtime definition don't match in terms of argument and parameter count.
436436
static SVal castArgToParamTypeIfNeeded(const CallEvent &Call, unsigned ArgIdx,
437437
SVal ArgVal, SValBuilder &SVB) {
438-
const FunctionDecl *RTDecl =
439-
Call.getRuntimeDefinition().getDecl()->getAsFunction();
440438
const auto *CallExprDecl = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
441-
442-
if (!RTDecl || !CallExprDecl)
439+
if (!CallExprDecl)
443440
return ArgVal;
444441

442+
const FunctionDecl *Definition = CallExprDecl;
443+
Definition->hasBody(Definition);
444+
445445
// The function decl of the Call (in the AST) will not have any parameter
446446
// declarations, if it was 'only' declared without a prototype. However, the
447447
// engine will find the appropriate runtime definition - basically a
448448
// redeclaration, which has a function body (and a function prototype).
449-
if (CallExprDecl->hasPrototype() || !RTDecl->hasPrototype())
449+
if (CallExprDecl->hasPrototype() || !Definition->hasPrototype())
450450
return ArgVal;
451451

452452
// Only do this cast if the number arguments at the callsite matches with
453453
// the parameters at the runtime definition.
454-
if (Call.getNumArgs() != RTDecl->getNumParams())
454+
if (Call.getNumArgs() != Definition->getNumParams())
455455
return UnknownVal();
456456

457457
const Expr *ArgExpr = Call.getArgExpr(ArgIdx);
458-
const ParmVarDecl *Param = RTDecl->getParamDecl(ArgIdx);
458+
const ParmVarDecl *Param = Definition->getParamDecl(ArgIdx);
459459
return SVB.evalCast(ArgVal, Param->getType(), ArgExpr->getType());
460460
}
461461

0 commit comments

Comments
 (0)