erichkeane created this revision. erichkeane added reviewers: cfe-commits, majnemer, vbyakovl, DavidKreitzer, andreybokhanko, rsmith. erichkeane set the repository for this revision to rL LLVM.
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. Repository: rL LLVM https://reviews.llvm.org/D23692 Files: lib/AST/ExprConstant.cpp test/SemaCXX/constexpr-strlen.cpp Index: lib/AST/ExprConstant.cpp =================================================================== --- lib/AST/ExprConstant.cpp +++ lib/AST/ExprConstant.cpp @@ -7022,11 +7022,9 @@ } case Builtin::BIstrlen: - // A call to strlen is not a constant expression. - if (Info.getLangOpts().CPlusPlus11) - Info.CCEDiag(E, diag::note_constexpr_invalid_function) - << /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'"; - else + // As a GCC compatibility extension, we support strlen() + // as a constant expression. + if (!Info.getLangOpts().CPlusPlus11) Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); // Fall through. case Builtin::BI__builtin_strlen: { Index: test/SemaCXX/constexpr-strlen.cpp =================================================================== --- test/SemaCXX/constexpr-strlen.cpp +++ test/SemaCXX/constexpr-strlen.cpp @@ -8,7 +8,7 @@ # 10 "SemaCXX/constexpr-strlen.cpp" 2 constexpr int n = __builtin_strlen("hello"); // ok -constexpr int m = strlen("hello"); // expected-error {{constant expression}} expected-note {{non-constexpr function 'strlen' cannot be used in a constant expression}} +constexpr int m = strlen("hello"); // ok // Make sure we can evaluate a call to strlen. int arr[3]; // expected-note {{here}}
Index: lib/AST/ExprConstant.cpp =================================================================== --- lib/AST/ExprConstant.cpp +++ lib/AST/ExprConstant.cpp @@ -7022,11 +7022,9 @@ } case Builtin::BIstrlen: - // A call to strlen is not a constant expression. - if (Info.getLangOpts().CPlusPlus11) - Info.CCEDiag(E, diag::note_constexpr_invalid_function) - << /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'"; - else + // As a GCC compatibility extension, we support strlen() + // as a constant expression. + if (!Info.getLangOpts().CPlusPlus11) Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); // Fall through. case Builtin::BI__builtin_strlen: { Index: test/SemaCXX/constexpr-strlen.cpp =================================================================== --- test/SemaCXX/constexpr-strlen.cpp +++ test/SemaCXX/constexpr-strlen.cpp @@ -8,7 +8,7 @@ # 10 "SemaCXX/constexpr-strlen.cpp" 2 constexpr int n = __builtin_strlen("hello"); // ok -constexpr int m = strlen("hello"); // expected-error {{constant expression}} expected-note {{non-constexpr function 'strlen' cannot be used in a constant expression}} +constexpr int m = strlen("hello"); // ok // Make sure we can evaluate a call to strlen. int arr[3]; // expected-note {{here}}
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits