[clang] [WASM] add CheckWasmTableElement helper (PR #181172)

Jameson Nash via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 12 07:59:28 PST 2026


https://github.com/vtjnash created https://github.com/llvm/llvm-project/pull/181172

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.

>From ce5f60d22fea82a45cff863f407ee366a83ba65e Mon Sep 17 00:00:00 2001
From: Jameson Nash <vtjnash at gmail.com>
Date: Thu, 12 Feb 2026 14:19:55 +0000
Subject: [PATCH] [WASM] add CheckWasmTableElement helper

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.
---
 clang/lib/Sema/SemaWasm.cpp | 36 +++++++++++++++++++++---------------
 1 file changed, 21 insertions(+), 15 deletions(-)

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();



More information about the cfe-commits mailing list