antmo created this revision. Herald added a project: All. antmo requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
Since D140059 <https://reviews.llvm.org/D140059>, clang asserts on constant calls of string functions (memchr, strncmp, memcmp, ...) when the last argument (length) does not fit in an UInt63: strncmp("11", "12", -1); Fix this by directly calling getZExtValue, as an unsigned value is needed here Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D156711 Files: clang/lib/AST/ExprConstant.cpp clang/test/SemaCXX/constexpr-string.cpp Index: clang/test/SemaCXX/constexpr-string.cpp =================================================================== --- clang/test/SemaCXX/constexpr-string.cpp +++ clang/test/SemaCXX/constexpr-string.cpp @@ -93,6 +93,7 @@ static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar, 6) == -1); static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar, 7) == -1); // FIXME: Should we reject this? + static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar, (size_t)-1) == -1); // Check that this does not assert static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar + 6, 6) == 0); static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar + 6, 7) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}} @@ -377,6 +378,7 @@ static_assert(__builtin_memchr(kStr, 'a', 0) == nullptr); static_assert(__builtin_memchr(kStr, 'a', 1) == kStr); + static_assert(__builtin_memchr(kStr, 'a', (size_t)-1) == kStr); // Check that this does not assert static_assert(__builtin_memchr(kStr, '\0', 5) == nullptr); static_assert(__builtin_memchr(kStr, '\0', 6) == kStr + 5); static_assert(__builtin_memchr(kStr, '\xff', 8) == kStr + 4); Index: clang/lib/AST/ExprConstant.cpp =================================================================== --- clang/lib/AST/ExprConstant.cpp +++ clang/lib/AST/ExprConstant.cpp @@ -9346,7 +9346,7 @@ APSInt N; if (!EvaluateInteger(E->getArg(2), N, Info)) return false; - MaxLength = N.getExtValue(); + MaxLength = N.getZExtValue(); } // We cannot find the value if there are no candidates to match against. if (MaxLength == 0u) @@ -12356,7 +12356,7 @@ APSInt N; if (!EvaluateInteger(E->getArg(2), N, Info)) return false; - MaxLength = N.getExtValue(); + MaxLength = N.getZExtValue(); } // Empty substrings compare equal by definition.
Index: clang/test/SemaCXX/constexpr-string.cpp =================================================================== --- clang/test/SemaCXX/constexpr-string.cpp +++ clang/test/SemaCXX/constexpr-string.cpp @@ -93,6 +93,7 @@ static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar, 6) == -1); static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar, 7) == -1); // FIXME: Should we reject this? + static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar, (size_t)-1) == -1); // Check that this does not assert static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar + 6, 6) == 0); static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar + 6, 7) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}} @@ -377,6 +378,7 @@ static_assert(__builtin_memchr(kStr, 'a', 0) == nullptr); static_assert(__builtin_memchr(kStr, 'a', 1) == kStr); + static_assert(__builtin_memchr(kStr, 'a', (size_t)-1) == kStr); // Check that this does not assert static_assert(__builtin_memchr(kStr, '\0', 5) == nullptr); static_assert(__builtin_memchr(kStr, '\0', 6) == kStr + 5); static_assert(__builtin_memchr(kStr, '\xff', 8) == kStr + 4); Index: clang/lib/AST/ExprConstant.cpp =================================================================== --- clang/lib/AST/ExprConstant.cpp +++ clang/lib/AST/ExprConstant.cpp @@ -9346,7 +9346,7 @@ APSInt N; if (!EvaluateInteger(E->getArg(2), N, Info)) return false; - MaxLength = N.getExtValue(); + MaxLength = N.getZExtValue(); } // We cannot find the value if there are no candidates to match against. if (MaxLength == 0u) @@ -12356,7 +12356,7 @@ APSInt N; if (!EvaluateInteger(E->getArg(2), N, Info)) return false; - MaxLength = N.getExtValue(); + MaxLength = N.getZExtValue(); } // Empty substrings compare equal by definition.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits