[libcxx-dev] Problem with __builtin_strlen among compilers

Eric Fiselier via libcxx-dev libcxx-dev at lists.llvm.org
Tue Sep 10 05:13:04 PDT 2019


I think __builtin_strlen is a red herring here. This just seems like a
general issue with GCC's constexpr implementation of strings.

On Tue, Sep 10, 2019, 1:10 AM Zoe Carver via libcxx-dev <
libcxx-dev at lists.llvm.org> wrote:

> "__builtin_strlen" works with GCC 7.x and up and clang 6.x and up. Here is
> a Godbolt: https://godbolt.org/z/G9NpAX
>
> On Mon, Sep 9, 2019 at 1:50 PM Yan Yichen via libcxx-dev <
> libcxx-dev at lists.llvm.org> wrote:
>
>> I got a little bit confused trying to get a repro of __builtin_strlen in
>> libc++ with gcc.
>>
>> At least the behavior of __builtin_strlen is different in clang and gcc.
>>
>>
>>
>> This passed
>>
>>
>>
>> g++ -o /dev/null -std=c++17 --compile test2.cc
>>
>> while this failed
>>
>>
>>
>> clang++ -o /dev/null -std=c++17 --compile test2.cc
>>
>> with error messages:
>>
>>
>>
>> test2.cc:6:19: error: static_assert expression is not an integral
>> constant expression
>>
>>     static_assert(!test(s));
>>
>>                   ^~~~~~~~
>>
>> test2.cc:3:12: note: read of non-constexpr variable 's' is not allowed in
>> a constant expression
>>
>>     return __builtin_strlen(s);
>>
>>            ^
>>
>> test2.cc:6:20: note: in call to 'test(&s[0])'
>>
>>     static_assert(!test(s));
>>
>>                    ^
>>
>> 1 error generated.
>>
>>
>>
>> For one same file:
>>
>>
>>
>>
>>
>> const char s[] = "";
>>
>> constexpr int test(const char* s) {
>>
>>     return __builtin_strlen(s);
>>
>> }
>>
>> constexpr void __main() {
>>
>>     static_assert(!test(s));
>>
>> }
>>
>>
>>
>>
>>
>>
>>
>> Hmmm...  I think __builtin_strlen is constexpr on gcc.
>>
>>
>>
>> Looking at a more minimal case:
>>
>>
>>
>> *int* main() { *static_assert*(!__builtin_strlen("")); }
>>
>>
>>
>> It compiles fine under:
>>
>>
>>
>> gcc 9.2, libstdc++
>>
>> /usr/local/gcc/bin/g++-9 -std=c++17 a.cpp
>>
>>
>>
>> clang 8, libc++
>>
>> /usr/local/clang/bin/clang++ -std=c++17 a.cpp
>>
>>
>>
>> gcc 9.2, libc++
>>
>> /usr/local/gcc/bin/g++-9 -nostdinc++ -I /usr/local/clang/include/c++/v1
>> -nodefaultlibs -lc++ -lc++abi -lm -lc -lgcc -std=c++17 a.cpp
>>
>>
>> _______________________________________________
>> libcxx-dev mailing list
>> libcxx-dev at lists.llvm.org
>> https://lists.llvm.org/cgi-bin/mailman/listinfo/libcxx-dev
>>
> _______________________________________________
> libcxx-dev mailing list
> libcxx-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/libcxx-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/libcxx-dev/attachments/20190910/0ebeaa68/attachment.html>


More information about the libcxx-dev mailing list