llvm / llvm/llvm-project

Mark `static_cast` as Clang HLSL Extensions

Open
#203,371 0 comments 0 reactions 0 assignees View on GitHub
HLSL
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

The HLSL specification does not include named C++ cast operators; HLSL uses C-style casts and built-in conversion functions instead. However, `static_cast` is relatively benign casts that map cleanly to operations HLSL already supports, is a likely candidate for inclusion in the HLSL standard in the future. Clang should allow 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` 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";
```

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(i)` produces an extension warning in HLSL mode.
- The warnings can be silenced with `-Wno-hlsl-extensions` or similar flag.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.