[clang] [WASM] add CheckWasmTableElement helper (PR #181172)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 12 08:00:10 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Jameson Nash (vtjnash)
<details>
<summary>Changes</summary>
Discard qualifiers from the types (e.g. addrspace, volatile, etc) before checking that the underlying types are the same, and DRY the code to emit a consistent Sema.Diag before returning failure.
---
Full diff: https://github.com/llvm/llvm-project/pull/181172.diff
1 Files Affected:
- (modified) clang/lib/Sema/SemaWasm.cpp (+21-15)
``````````diff
diff --git a/clang/lib/Sema/SemaWasm.cpp b/clang/lib/Sema/SemaWasm.cpp
index e7731136720e8..083460eb6e201 100644
--- a/clang/lib/Sema/SemaWasm.cpp
+++ b/clang/lib/Sema/SemaWasm.cpp
@@ -40,6 +40,20 @@ static bool CheckWasmBuiltinArgIsTable(Sema &S, CallExpr *E, unsigned ArgIndex,
return false;
}
+static bool CheckWasmTableElement(Sema &S, QualType &ElTy, CallExpr *E,
+ unsigned TableIndex, unsigned ArgIndex) {
+ Expr *NewElemArg = E->getArg(ArgIndex);
+ QualType QT = NewElemArg->getType();
+ // Compare the types after removing insignificant qualifiers
+ if (!S.getASTContext().hasSameType(ElTy.getTypePtr(), QT.getTypePtr())) {
+ return S.Diag(NewElemArg->getBeginLoc(),
+ diag::err_wasm_builtin_arg_must_match_table_element_type)
+ << (ArgIndex + 1) << (TableIndex + 1)
+ << NewElemArg->getSourceRange();
+ }
+ return false;
+}
+
/// Checks the argument at the given index is an integer.
static bool CheckWasmBuiltinArgIsInteger(Sema &S, CallExpr *E,
unsigned ArgIndex) {
@@ -115,7 +129,7 @@ bool SemaWasm::BuiltinWasmTableGet(CallExpr *TheCall) {
return false;
}
-/// Check that the first argumnet is a WebAssembly table, the second is
+/// Check that the first argument is a WebAssembly table, the second is
/// an index to use as index into the table and the third is the reference
/// type to set into the table.
bool SemaWasm::BuiltinWasmTableSet(CallExpr *TheCall) {
@@ -129,7 +143,7 @@ bool SemaWasm::BuiltinWasmTableSet(CallExpr *TheCall) {
if (CheckWasmBuiltinArgIsInteger(SemaRef, TheCall, 1))
return true;
- if (!getASTContext().hasSameType(ElTy, TheCall->getArg(2)->getType()))
+ if (CheckWasmTableElement(SemaRef, ElTy, TheCall, 0, 2))
return true;
return false;
@@ -158,12 +172,8 @@ bool SemaWasm::BuiltinWasmTableGrow(CallExpr *TheCall) {
if (CheckWasmBuiltinArgIsTable(SemaRef, TheCall, 0, ElTy))
return true;
- Expr *NewElemArg = TheCall->getArg(1);
- if (!getASTContext().hasSameType(ElTy, NewElemArg->getType())) {
- return Diag(NewElemArg->getBeginLoc(),
- diag::err_wasm_builtin_arg_must_match_table_element_type)
- << 2 << 1 << NewElemArg->getSourceRange();
- }
+ if (CheckWasmTableElement(SemaRef, ElTy, TheCall, 0, 1))
+ return true;
if (CheckWasmBuiltinArgIsInteger(SemaRef, TheCall, 2))
return true;
@@ -185,12 +195,8 @@ bool SemaWasm::BuiltinWasmTableFill(CallExpr *TheCall) {
if (CheckWasmBuiltinArgIsInteger(SemaRef, TheCall, 1))
return true;
- Expr *NewElemArg = TheCall->getArg(2);
- if (!getASTContext().hasSameType(ElTy, NewElemArg->getType())) {
- return Diag(NewElemArg->getBeginLoc(),
- diag::err_wasm_builtin_arg_must_match_table_element_type)
- << 3 << 1 << NewElemArg->getSourceRange();
- }
+ if (CheckWasmTableElement(SemaRef, ElTy, TheCall, 0, 2))
+ return true;
if (CheckWasmBuiltinArgIsInteger(SemaRef, TheCall, 3))
return true;
@@ -214,7 +220,7 @@ bool SemaWasm::BuiltinWasmTableCopy(CallExpr *TheCall) {
return true;
Expr *TableYArg = TheCall->getArg(1);
- if (!getASTContext().hasSameType(XElTy, YElTy)) {
+ if (!getASTContext().hasSameType(XElTy.getTypePtr(), YElTy.getTypePtr())) {
return Diag(TableYArg->getBeginLoc(),
diag::err_wasm_builtin_arg_must_match_table_element_type)
<< 2 << 1 << TableYArg->getSourceRange();
``````````
</details>
https://github.com/llvm/llvm-project/pull/181172
More information about the cfe-commits
mailing list