This is an archive of the discontinued LLVM Phabricator instance.

Interpret strlen as constexpr for GCC Compatibility
AbandonedPublic

Authored by erichkeane on Aug 18 2016, 1:47 PM.

Details

Summary

GCC (and other compilers) treat strlen as a 'constexpr' function as an extension to the language. Clang previously treated builtin_strlen as a constexpr extension, so for this patch it was only necessary to remove the error-causing diagnostic for the strlen case, which falls through to the builtin_strlen handling.

Additionally, the strlen test previously expected this error, so this patch removes the error-expectation from the test.

Diff Detail

Repository
rL LLVM

Event Timeline

erichkeane updated this revision to Diff 68599.Aug 18 2016, 1:47 PM
erichkeane retitled this revision from to Interpret strlen as constexpr for GCC Compatibility.
erichkeane updated this object.
erichkeane set the repository for this revision to rL LLVM.
rsmith requested changes to this revision.Aug 18 2016, 1:59 PM
rsmith edited edge metadata.

This is not a conforming extension. We are explicitly not allowed to make standard library functions constexpr if the standard doesn't say they are; see [constexpr.functions] (17.6.5.6) in the C++ standard. Clang previously used to allow constant-folding strlen calls, but we removed that support intentionally for this reason.

It would seem reasonable for this constant-folding to be enabled for strlen if the callee was explicitly marked as constexpr in the source code. That way, if the standard library chooses to mark it as constexpr as a (currently) non-conforming extension, we would provide an optimized implementation.

This revision now requires changes to proceed.Aug 18 2016, 1:59 PM
erichkeane abandoned this revision.Aug 18 2016, 2:04 PM

This is not a conforming extension. We are explicitly not allowed to make standard library functions constexpr if the standard doesn't say they are; see [constexpr.functions] (17.6.5.6) in the C++ standard. Clang previously used to allow constant-folding strlen calls, but we removed that support intentionally for this reason.

It would seem reasonable for this constant-folding to be enabled for strlen if the callee was explicitly marked as constexpr in the source code. That way, if the standard library chooses to mark it as constexpr as a (currently) non-conforming extension, we would provide an optimized implementation.

Ok, thank you for your prompt response. I'll see if that is a viable solution for this case.

Thanks!

This is not a conforming extension. We are explicitly not allowed to make standard library functions constexpr if the standard doesn't say they are; see [constexpr.functions] (17.6.5.6) in the C++ standard. Clang previously used to allow constant-folding strlen calls, but we removed that support intentionally for this reason.

It would seem reasonable for this constant-folding to be enabled for strlen if the callee was explicitly marked as constexpr in the source code. That way, if the standard library chooses to mark it as constexpr as a (currently) non-conforming extension, we would provide an optimized implementation.

Ok, thank you for your prompt response. I'll see if that is a viable solution for this case.

Thanks!

Sadly, it doesn't seem that this would be a viable fix. libstdc++ doesn't actually seem to implement strlen, and all references to it are "extern size_t strlen", with no constexpr. I suspect they are doing what I did here, just treating it as constexpr as a special function. As mentioned, I didn't even see an implementation at all, so I think they're counting on the compiler replacing it with the built-in immediately anyway.