Skip to content

Commit f27eab0

Browse files
committed
ast-exporter: add hack for strlen()
this is really perplexing. somehow the builtin definition of strlen overrides the return type of subsequent redeclarations to unsigned long. this does not seem to happen for other builtins, which happily take size_t as an argument type. it also doesn't seem to override declarations of strlen with incompatible types. but in any case by disabling the strlen builtin we can get a sane type based on its actual written declaration. a minimal reproducer is: `typedef __SIZE_TYPE__ size_t; size_t strlen(const char* x);` with `clang -Xclang -ast-dump -fsyntax-only`, this shows `unsigned long` as the return type for both the implicit and explicit declaration of `strlen`. with `-fno-builtin-strlen`, the explicit definition keeps its declared size_t return type.
1 parent 9043aaf commit f27eab0

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

c2rust-ast-exporter/src/AstExporter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2761,6 +2761,8 @@ constexpr size_t size(const _Tp (&)[_Sz]) noexcept {
27612761
// parsed and string literals are always treated as constant.
27622762
static std::vector<const char *> augment_argv(int argc, const char *argv[]) {
27632763
const char *const extras[] = {
2764+
"-extra-arg=-fno-builtin-strlen", // builtin strlen wrongly returns
2765+
// unsigned long despite declaration
27642766
"-extra-arg=-fparse-all-comments", // always parse comments
27652767
"-extra-arg=-Wwrite-strings", // string literals are constant
27662768
"-extra-arg=-D_FORTIFY_SOURCE=0", // we don't want to use checked

0 commit comments

Comments
 (0)