[libcxx-dev] Problem with __builtin_strlen among compilers

Yan Yichen via libcxx-dev libcxx-dev at lists.llvm.org
Mon Sep 9 13:50:37 PDT 2019


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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/libcxx-dev/attachments/20190909/170ddd03/attachment-0001.html>


More information about the libcxx-dev mailing list