-
Notifications
You must be signed in to change notification settings - Fork 13.5k
offsetof() is not constant in clang-cl #59689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Simple repro: struct S { int i; };
constexpr auto x = offsetof(S, i);
|
MSVC working: https://godbolt.org/z/4jnvY9o9r |
Since offsetof() is not a constant expression on clang-cl, though it is on other compilers. llvm/llvm-project#59689
Since offsetof() is not a constant expression on clang-cl, though it is on other compilers. llvm/llvm-project#59689
Since offsetof() is not a constant expression on clang-cl, though it is on other compilers. llvm/llvm-project#59689
How are you invoking clang-cl? In the past, we made this work by making clang-cl use its own stddef.h, which has the proper definition of offsetof. Apparently recent SDKs guard the definition of offsetof with a define _CRT_USE_BUILTIN_OFFSETOF; if you define that, it uses __builtin_offsetof just like clang's stddef.h. Maybe clang-cl should be defining that. |
I am invoking it by constructing a I am running my tool against a compile database, and here's an example CommandLine from the compile database:
|
Passing it to MSVC with
Since I am using MSVC that means it will not be able to appear in my compile database. I will need to
It'd be nice if clang-cl did define that itself, if that's the answer. |
I do confirm that adding |
We need to define `_CRT_USE_BUILTIN_OFFSETOF ` when compiling with clang-cl, both for subspace (and its dependents) and for the CIR tool. The define works around the clang-cl issue llvm/llvm-project#59689 without requiring code changes.
We need to define `_CRT_USE_BUILTIN_OFFSETOF ` when compiling with clang-cl, both for subspace (and its dependents) and for the CIR tool. The define works around the clang-cl issue llvm/llvm-project#59689 without requiring code changes.
I suspect your tool isn't constructing the clang command-line correctly. Does it work if you invoke clang-cl directly? What's the output of if you pass "/clang:-###"? See also #58918 |
Thanks. I'm not really constructing a command line myself, but it does seem like executing I am just giving the compile db to ClangTool and giving it the path of one of the source files in the compile db. Clang figures out the command line from there. However, when I run the clang-cl binary (replacing cl.exe with clang-cl.exe in the compile db's command line) it works correctly with offsetof(). So, perhaps this is a bug in ClangTool? Actual code: void f(const clang::tooling::CompilationDatabase& compdb,
std::vector<std::string> paths,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs) noexcept {
auto tool = clang::tooling::ClangTool(
compdb, paths, std::make_shared<clang::PCHContainerOperations>(),
std::move(fs));
auto adj = [](auto args, llvm::StringRef Filename) {
// Clang-cl doesn't understand this argument, but it may appear in the
// command-line for MSVC in C++20 codebases.
std::erase(args, "/Zc:preprocessor");
// TODO: https://github.com/llvm/llvm-project/issues/59689 clang-cl requires
// this define in order to use offsetof() from constant expressions.
args.push_back("/D_CRT_USE_BUILTIN_OFFSETOF");
return std::move(args);
};
tool.appendArgumentsAdjuster(adj);
auto asts = std::vector<std::unique_ptr<clang::ASTUnit>>();
tool.buildASTs(asts); |
That sounds like the tool is failing to find the builtin include directory correctly. So clang-cl itself finds the correct directory, but clangtool somehow doesn't. So clang-cl uses its own stddef.h, but clangtool fails to find it. And instead of printing a diagnostic, it falls back to the SDK's stddef.h. Seems very similar to #58918. |
Justed wanted to say I encountered the exact same issue with clang-tidy. See microsoft/STL#3311 |
Ran into this using a libclang.dll retrieved via the Visual Studio 2022 installer just today, so presumably fix hasn't worked it's way there yet. Passing Keywords for the webcrawlers:
|
Visual Studio 2022 should be on track as of version 17.7 preview 3. We (MSVC) didn't notice that LLVM 16 changed clang's expected installation layout, so our repackaging put the override headers in the wrong place. |
I can also confirm that the I build on Linux, using clang-17, using this command line (here without the workaround):
|
* There's an issue with _CRT_USE_BUILTIN_OFFSETOF: llvm/llvm-project#59689
Looks like that Clang (clang-cl) should just predefine |
…127568) This patch makes Clang predefine `_CRT_USE_BUILTIN_OFFSETOF` in MS-compatible modes. The macro can make the `offsetof` provided by MS UCRT's `<stddef.h>` to select the `__builtin_offsetof` version, so with it Clang (Clang-cl) can directly consume UCRT's `offsetof`. MSVC predefines the macro as `1` since at least VS 2017 19.14, but I think it's also OK to define it in "older" compatible modes. Fixes #59689.
…lvm#127568) This patch makes Clang predefine `_CRT_USE_BUILTIN_OFFSETOF` in MS-compatible modes. The macro can make the `offsetof` provided by MS UCRT's `<stddef.h>` to select the `__builtin_offsetof` version, so with it Clang (Clang-cl) can directly consume UCRT's `offsetof`. MSVC predefines the macro as `1` since at least VS 2017 19.14, but I think it's also OK to define it in "older" compatible modes. Fixes llvm#59689.
I use
offsetof
in a template parameter, this works on all compilers (Clang, MSVC, GCC) except clang-cl.Obviously
reinterpret_cast
is not constant evaluable. So I guess MSVC provides a builtin, but clang-cl does not?The text was updated successfully, but these errors were encountered: