<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/101581>101581</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang] initializeBuiltins is setting all `LangBuiltin` not defined in `builtinIsSupported` to true
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
farzonl
</td>
</tr>
</table>
<pre>
Recently when modifying (#101543) the names of a few HLSL builtins We noticed test failures on [coroutine-builtins.cpp:18](https://github.com/llvm/llvm-project/blob/main/clang/test/SemaCXX/coroutine-builtins.cpp#L18) when `DERRORS` was defined.
The expected behavior with that flag is that `__builtin_coro_alloc();` would not be defined because without `-std=c++20` the coroutine language ops are not set.
What is actually happening is that `initializeBuiltins` is [iterating through all builtins](https://github.com/llvm/llvm-project/blob/main/clang/lib/Basic/Builtins.cpp#L134C3-L142C78) and running `builtinIsSupported` which defaults to true.
In the below screenshot you can see two debug windows, one on main and one on the effected branch. On the main branch `__builtin_coro_alloc` evaluates to `UnresolvedLookupExpr`. On the effected branch its a DeclRef pointing at `__builtin_hlsl_all`
![](https://github.com/user-attachments/assets/556e340f-54ff-481c-8638-fa2a89fee560)
I ended up fixing this issue for now by adding HLSL as a language option: https://github.com/llvm/llvm-project/pull/101543/commits/0bb647d95b11df12c7105a9f3f2ddd0cb570e1ae
That said a code restructure is needed or someone else working on LangBuiltin is going to run into this because this ends up being a bad pattern. Below I enumerate two reasons why:
1. if there are missing lang opts in this list then all those builtins get turned on by default without the maintainers of the languages options knowing. This should be an opt in for language options.
2. `__builtin_coro_alloc` should always be associated with `COR_LANG` whether or not `LangOpts.Coroutines` is true or false. That way if the builtin is seen and the feature is not enabled `builtinIsSupported` would return false.
So instead of
```cpp
if (!LangOpts.Coroutines && (BuiltinInfo.Langs & COR_LANG))
return false;
...
return true;
```
We could do
```cpp
if (BuiltinInfo.Langs & COR_LANG) {
if(!LangOpts.Coroutines)
return false;
return true;
}
....
return false;
```
I'm open to other code chages here aswell this is just one suggestion on how to fix this issue.
@bogner who pair debugged this issue with me.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVl1v2zoS_TX0yyCCRFmy_OCHOLneLRBsgeQu7n0LRtJI4i1NCiQV1_fXL4aSXSfbdlFggaKO-DUzZ845JHqvekO0E8VeFI8rnMJg3a5D97c1elXb9rx7poZM0Gc4DWTgaFvVnZXpQchKyDxLs2KdC7kFCAOBwSN5sB0gdHSCfz69PEE9KR2U8QB_EBgbVEMtBPIBOlR6crzBgCj2jXV2CsrQ3WVL0oyjyO-zShSPQlZDCKMX-b2QByEPvQrDVCeNPQp50Prt8nM3OvsXNUHIQ61tLeThiMoIeWg0ml7IA8cW8vBCR3z480-e-H5gmT9lFZcWKwdRpo-_PT9_fn4RZQon9NBSpwy1iUgfRXo____7QEBfR2oCtVDTgG_KOjipMEAYMECnsQfl5w9Rpq-vS8xXzuIVtbZNhHYr8n0MZCfdMm5Q0yUi1NTg5Cmea6d40J0PrcgfGyH3Qu5lynu5JdfigMufsCewowd0sRngKbzL_w_OS3nAJkyo9RkGHEcy3PGbrJVRQaFWf9N-QYzDKc9tVIEcBt4QBmenfgDU-sqC_2snteLBPXrV8O_H5uXrh_zuKVvLh03sI5oW3GRiMaJMl5Q--ZdpHK0L1Ea8B9UMDDROOngIFoKbKIFbkD6ZCG1N2p7AN47I-MEGONsJGjTgiSCcLLRUTz2clGntyQv5ANYQk53LiNks33wYdd1CGoemGRL4PI_HtfMY_JAwZQr0hnrCQDFlUab_No681W_UPln7ZRp_-zo6UabXcz_EAxU8IDxSo5-pg9EqE3v4kaWD9pqDijJdEJHZ7B4_b-vkyd1hCNgMRzLBC3lA7yn-URQl5eu0uyvWXXe3rrLmrirz6q5DidW2IyrKlAVx2wEg01IL0wid-jqTTXlQ3k8EnXVg7AnqM2Db8mQ0IuQCb0QQlDUiv4dfJuM4aS3k4eJ9h8Yej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