Issue 203371
Summary Mark `static_cast` and `const_cast` as Clang HLSL Extensions
Labels HLSL
Assignees
Reporter llvm-beanz
    Part of https://github.com/llvm/wg-hlsl/issues/300

`static_cast` and `const_cast` are C++ named cast operators. While `reinterpret_cast` and `dynamic_cast` are being fully removed from HLSL, `static_cast` and `const_cast` are being classified as Clang-specific extensions to HLSL — supported by Clang's HLSL implementation but not part of any HLSL specification. They should produce an extension warning.

## HLSL Context
The HLSL specification does not include named C++ cast operators; HLSL uses C-style casts and built-in conversion functions instead. However, `static_cast` and `const_cast` are relatively benign casts that map cleanly to operations HLSL already supports. Clang allows them as extensions to improve C++ compatibility and ease porting, but shader authors should be warned that these are not portable to all HLSL compilers.

## Current Behavior in Clang
`static_cast` and `const_cast` in HLSL mode are currently accepted without any HLSL-specific diagnostic.

See example: https://godbolt.org/z/8hsTW5bcE

## Required Changes in Clang

### Diagnostics
Use a shared `ext_hlsl_clang_extension` diagnostic (parameterized by feature name) rather than adding per-feature entries to `DiagnosticSemaKinds.td`. All Clang-extension cases can share a single extension warning.

### Sema Checks
In `SemaExprCXX.cpp`, where `CXXStaticCastExpr` and `CXXConstCastExpr` are built, add checks for HLSL mode:
```cpp
// For static_cast:
if (getLangOpts().HLSL)
  Diag(OpLoc, diag::ext_hlsl_clang_extension) << "static_cast";

// For const_cast:
if (getLangOpts().HLSL)
  Diag(OpLoc, diag::ext_hlsl_clang_extension) << "const_cast";
```

These diagnostics should be warnings (off by default or under `-Whlsl-extensions`) rather than errors.

### Test
Add tests to `clang/test/SemaHLSL/` verifying that:
- `static_cast<float>(i)` produces an extension warning in HLSL mode.
- `const_cast<int*>(p)` produces an extension warning in HLSL mode.
- The warnings can be silenced with `-Wno-hlsl-extensions` or similar flag.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to