<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/57883>57883</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Pointer template parameters have the wrong value in some situations
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Supermario1313
      </td>
    </tr>
</table>

<pre>
    I'm using clang 14.0.6-2 which I've installed with pacman on Arch Linux.

I'm currently trying to cross-compile [LunaLua](https://github.com/WohlSoft/LunaLua/) on Linux with clang. This project makes extensive use of casts from integers to pointers in `constexpr` contexts in order to reference static variables from `smbx.exe`. Such casts are allowed in Visual C++ 2015 but not in clang. To overcome this restrictions, I used the following workarounds:

1. Uses the [`__builtin_constant_p`](https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fconstant_005fp) GCC builtin:
```c++
#include <cstdio>

template<typename To, typename From>
inline constexpr To constexpr_reinterpret_cast(From from) {
    return __builtin_constant_p( reinterpret_cast<To>(from) ) ? reinterpret_cast<To>(from) : reinterpret_cast<To>(from);
}

constexpr int* test = constexpr_reinterpret_cast<int*>(42UL);

template <int* i>
static void someFunc() {
    std::printf("In templated function: %p\n", i);
}

int main(int argc, char* argv[]) {
    std::printf("In main function: %p\n", test);
    someFunc<test>();
}
```

2. Uses a reference to a dereferenced casted `unsigned long` literal:
```c++
#include <cstdio>

static int& test = *(int*)42UL;

template <int* i>
static void someFunc() {
    std::printf("In templated function: %p\n", i);
}

int main(int argc, char* argv[]) {
    std::printf("In main function: %p\n", &test);
    someFunc<&test>();
}
```

Both codes compile successfully and the compiler doesn't raise warnings when using `-fsanitize=undefined,address`. Furthermore, `test` or `&test` have the correct value when passed to a function or assigned to a variable. The problem arises when passing `test` or `&test` as a template parameter. In that case, they inexplicably take the value `nullptr`. Here's the output of both programs when compiled and executed:
```
In main function: 0x2a
In templated function: (nil)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztVk1v4zYQ_TXyZRBBluKvgw-J07QBArTA7rZHg5Yom12aFPjhOP31fUNJThabTVP02kCRSXE4fPNmhjM72zyvH7JycaToldlTrQXe0-u8yOdXJT0dVH0gFjhJUsYHobVs6EmFA3WiPgpD1tCNg9CjMvGcZ8VdVtz0715vHZ2TJuhnCu6ZjwiWame9v6rtsVNaUja7fYxGPEaRze6ycnkIofNZdZOV93j2OCvucghj8oc96E-2DRiOW1hoxSgSgB5asiKnzwflqXP2T1kHOoqv0pM8B2m8gjXRS7It1cIHT62zR9gX5F46zwg7yzOMlaFsXtQWtstz5zAmTDAOac26Rjre4GQrYWctCSQFVdNJOCV2Wg7KsdEfd-dcniWGOX2K4Kw_XDhJ4NU-gVmo_F35KDRtsvIWD5XFdEa7GMjYwMujbZbsSTrQIimwnU764FQdFKBm5YYe2MIGa5Jay9qZ-yfrvgpno2kSwa-cNc3piwdYlodDgHG73UWlgzLbZL0wYdvh85s-qut8b2Ju3R4za7QysrG175fw_hVq3dVtr8_nh3DUWVkp08jz1bYoZm16jefx-HImTzp28c-bDQ0SL9iBJz11z9bwlVXXOjYwpdrUPjTKZtVPr80N8thpESTWw3MnjQCNny3zdpnew22XXSrZRJc4YP4vk62TKVo6J8OWfQqCeHfyPEPPFgMywh-EojP0Jr3lkr7TVW0-J_Tl8qKO_6v7D4lWNx8Qy6qRucXda5ZezIWGrLyhgCCDzrv3bK82vXB_wnX55fGbE76hny7SpC5cjwlkVUMe8X0fDdy7_I5I-JUDobrpHHS0SaR8MDQqb6jFTk4IZiErZ1022xjIsJvVO2ZDG64LhFm55KFw-5q31AfhGCnmJ04RToQPQmJt76FhXl8DSrpG0xGivJzofBv1mAWvjSiHhBavribcU4JwYY0fmnQD4Qd7I67FPdKWtDV7vua0gluF_u-5Nvgz-Xn-EkIcIongNFilQPk_St5DA_r-IVBGiX8ZK7eWi6ZtEC5j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