Skip to content

Commit

Permalink
Fix handling of FP-classify where the last arg fails to convert
Browse files Browse the repository at this point in the history
The last argument of an FP-classify function was checked for vailidity
as an expression, but we never ensured that the usual unary
conversions/etc properly resulted in a valid value. Thus, when we got
the value, it was null, so we had a null dereference.

This patch instead fails out/marks the function call as invalid if the
argument is incorrect.  I DID consider just allowing it to continue, but
the result was an extraneous error about how the last argument wasn't a
float (in this case, it was an overload set).

Fixes: llvm#107411
  • Loading branch information
erichkeane committed Sep 5, 2024
1 parent 122874c commit 0c8d6df
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
17 changes: 13 additions & 4 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4936,10 +4936,19 @@ bool Sema::BuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs,
// Usual Unary Conversions will convert half to float, which we want for
// machines that use fp16 conversion intrinsics. Else, we wnat to leave the
// type how it is, but do normal L->Rvalue conversions.
if (Context.getTargetInfo().useFP16ConversionIntrinsics())
OrigArg = UsualUnaryConversions(OrigArg).get();
else
OrigArg = DefaultFunctionArrayLvalueConversion(OrigArg).get();
if (Context.getTargetInfo().useFP16ConversionIntrinsics()) {
ExprResult Res = UsualUnaryConversions(OrigArg);

if (!Res.isUsable())
return true;
OrigArg = Res.get();
} else {
ExprResult Res = DefaultFunctionArrayLvalueConversion(OrigArg);

if (!Res.isUsable())
return true;
OrigArg = Res.get();
}
TheCall->setArg(FPArgNo, OrigArg);

QualType VectorResultTy;
Expand Down
4 changes: 4 additions & 0 deletions clang/test/Sema/builtin-unary-fp.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ void a(void) {
check(__builtin_fpclassify(0, 1, 2, 3, 4.5, 5.0)); // expected-warning{{implicit conversion from 'double' to 'int' changes value from 4.5 to 4}}
check(__builtin_fpclassify(0, 0, 0, 0, 1)); // expected-error{{too few arguments}}
check(__builtin_fpclassify(0, 0, 0, 0, 0, 1, 0)); // expected-error{{too many arguments}}

check(__builtin_fpclassify(0,0,0,0,0, (invalid))); // expected-error{{use of undeclared identifier 'invalid'}}
check(__builtin_fpclassify(0,0,0,0,0, (inf))); // expected-error{{use of undeclared identifier 'inf'}}
// expected-error@-1{{reference to overloaded function could not be resolved}}
}

0 comments on commit 0c8d6df

Please sign in to comment.