[cfe-commits] r113580 - in /cfe/trunk: lib/AST/ExprConstant.cpp test/Sema/constant-builtins-2.c
Douglas Gregor
dgregor at apple.com
Thu Sep 9 23:27:15 PDT 2010
Author: dgregor
Date: Fri Sep 10 01:27:15 2010
New Revision: 113580
URL: http://llvm.org/viewvc/llvm-project?rev=113580&view=rev
Log:
Support strlen() and __builtin_strlen() as constant expressions with
the call argument is a string literal. Fixes
<rdar://problem/8413477>.
Modified:
cfe/trunk/lib/AST/ExprConstant.cpp
cfe/trunk/test/Sema/constant-builtins-2.c
Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=113580&r1=113579&r2=113580&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Fri Sep 10 01:27:15 2010
@@ -1159,6 +1159,24 @@
case Builtin::BI__builtin_expect:
return Visit(E->getArg(0));
+
+ case Builtin::BIstrlen:
+ case Builtin::BI__builtin_strlen:
+ // As an extension, we support strlen() and __builtin_strlen() as constant
+ // expressions when the argument is a string literal.
+ if (StringLiteral *S
+ = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) {
+ // The string literal may have embedded null characters. Find the first
+ // one and truncate there.
+ llvm::StringRef Str = S->getString();
+ llvm::StringRef::size_type Pos = Str.find(0);
+ if (Pos != llvm::StringRef::npos)
+ Str = Str.substr(0, Pos);
+
+ return Success(Str.size(), E);
+ }
+
+ return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
}
}
Modified: cfe/trunk/test/Sema/constant-builtins-2.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/constant-builtins-2.c?rev=113580&r1=113579&r2=113580&view=diff
==============================================================================
--- cfe/trunk/test/Sema/constant-builtins-2.c (original)
+++ cfe/trunk/test/Sema/constant-builtins-2.c Fri Sep 10 01:27:15 2010
@@ -50,3 +50,7 @@
//int h2 = __builtin_expect(0, 0);
extern long int bi0;
extern __typeof__(__builtin_expect(0, 0)) bi0;
+
+// Strings
+int array1[__builtin_strlen("ab\0cd")];
+int array2[(sizeof(array1)/sizeof(int)) == 2? 1 : -1];
More information about the cfe-commits
mailing list