Skip to content

Cannot constant fold functions from <cwchar> #100929

@philnik777

Description

@philnik777

Clang has builtins for a lot of the <cwchar> functions and can constant evaluate them when using the __builtin_ variants, but the backend can't constant fold them. There doesn't seem to be much of a technical reason, since their <cstring> counterparts can often be constant folded, and some even have passes dedicated to them.

For example,

#include <cstddef>

auto test(wchar_t *ptr, size_t count) {
  wchar_t buffer[] = L"Banane";
  return __builtin_wmemchr(buffer, L'a', __builtin_wcslen(buffer)) != nullptr;
}

static_assert([] {
  wchar_t buffer[] = L"Banane";
  return __builtin_wmemchr(buffer, L'a', __builtin_wcslen(buffer)) != nullptr;
}());

compiles just fine, but test generates horrible code compared to what should be possible: https://godbolt.org/z/xP3vM9jPn

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions