aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Smith <richard-llvm@metafoo.co.uk>2012-01-18 03:06:12 +0000
committerRichard Smith <richard-llvm@metafoo.co.uk>2012-01-18 03:06:12 +0000
commit40b993a826728214c869ee4fbc9d296a2e1e1f71 (patch)
tree82d41030c27a004663031dbb387fb0d2f1d58ad5
parent6b3014b07c40a6ed8b0c8ed63950df02eeb82c28 (diff)
A call to strlen is not a constant expression, even if we're treating it as a
builtin. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@148374 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/AST/ExprConstant.cpp9
-rw-r--r--test/SemaCXX/constexpr-strlen.cpp15
2 files changed, 23 insertions, 1 deletions
diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp
index 1ad0bc5474..fc3c5aac01 100644
--- a/lib/AST/ExprConstant.cpp
+++ b/lib/AST/ExprConstant.cpp
@@ -3891,8 +3891,15 @@ bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
case Builtin::BI__builtin_expect:
return Visit(E->getArg(0));
-
+
case Builtin::BIstrlen:
+ // A call to strlen is not a constant expression.
+ if (Info.getLangOpts().CPlusPlus0x)
+ Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_invalid_function)
+ << /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'";
+ else
+ Info.CCEDiag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr);
+ // Fall through.
case Builtin::BI__builtin_strlen:
// As an extension, we support strlen() and __builtin_strlen() as constant
// expressions when the argument is a string literal.
diff --git a/test/SemaCXX/constexpr-strlen.cpp b/test/SemaCXX/constexpr-strlen.cpp
new file mode 100644
index 0000000000..5e28e7f0ce
--- /dev/null
+++ b/test/SemaCXX/constexpr-strlen.cpp
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 %s -std=c++11 -fsyntax-only -verify -pedantic
+
+# 1 "/usr/include/string.h" 1 3 4
+extern "C" {
+ typedef decltype(sizeof(int)) size_t;
+ extern size_t strlen(const char *p);
+}
+
+# 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}}
+
+// Make sure we can evaluate a call to strlen.
+int arr[3]; // expected-note {{here}}
+int k = arr[strlen("hello")]; // expected-warning {{array index 5}}