[clang] [Sema] Fix ICE when passing invalid types to abs #204777 (PR #205017)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 21 17:28:40 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Hüseyin Dönmez (huseyin-donmez)
<details>
<summary>Changes</summary>
fix for ICE in `Sema::CheckAbsoluteValueFunction`
problem:
when a 1-element vector is passed to a `abs` function, it implicitly casts to its scalar type, passing the initial type checks. However, `QualType ArgType = Call->getArg(0)->IgnoreParenImpCasts()->getType();` strips this cast, leaving the raw vector type, which is then passed to getAbsoluteValueKind() triggering an `llvm_unreachable`, since it's expecting only integer, enum, real floating or any complex type.
solution:
early return for invalid types before `getAbsoluteValueKind()`. A regression test has been added to ensure the compiler no longer crashes.
Fixes: #<!-- -->204777
---
Full diff: https://github.com/llvm/llvm-project/pull/205017.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaChecking.cpp (+6)
- (added) clang/test/Sema/builtin-abs-invalid.c (+10)
``````````diff
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index ec4a9037f5c23..294729b33a6ec 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -10527,6 +10527,12 @@ void Sema::CheckAbsoluteValueFunction(const CallExpr *Call,
if (IsStdAbs)
return;
+ // Prevent reaching unreachable code in getAbsoluteValueKind for unsupported
+ // types.
+ if (!ArgType->isIntegralOrEnumerationType() &&
+ !ArgType->isRealFloatingType() && !ArgType->isAnyComplexType())
+ return;
+
AbsoluteValueKind ArgValueKind = getAbsoluteValueKind(ArgType);
AbsoluteValueKind ParamValueKind = getAbsoluteValueKind(ParamType);
diff --git a/clang/test/Sema/builtin-abs-invalid.c b/clang/test/Sema/builtin-abs-invalid.c
new file mode 100644
index 0000000000000..9ec6c0edc491a
--- /dev/null
+++ b/clang/test/Sema/builtin-abs-invalid.c
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -verify %s
+// expected-no-diagnostics
+
+int abs(int);
+
+typedef int int1 __attribute__((__vector_size__(4)));
+
+void test_vector_abs(int1 x) {
+ (void)abs(x);
+}
\ No newline at end of file
``````````
</details>
https://github.com/llvm/llvm-project/pull/205017
More information about the cfe-commits
mailing list