]> granicus.if.org Git - clang/commit
Replace the implementation of __builtin_constant_p (which was based on the GCC
authorRichard Smith <richard-llvm@metafoo.co.uk>
Fri, 9 Dec 2011 02:04:48 +0000 (02:04 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Fri, 9 Dec 2011 02:04:48 +0000 (02:04 +0000)
commite052d46f4db91f9ba572859ffc984e85cbf5d5ff
tree9baebdec96646ad35d1242ca1f844311acab7d39
parent6a1db484f32eb791840dd55a8d45c86ff5bd0834
Replace the implementation of __builtin_constant_p (which was based on the GCC
documentation) with one based on what GCC's __builtin_constant_p is actually
intended to do (discovered by asking a friendly GCC developer).

In particular, an expression which folds to a pointer is now only considered to
be a "constant" by this builtin if it refers to the first character in a string
literal.

This fixes a rather subtle wrong-code issue when building with glibc. Given:

const char cs[4] = "abcd";
int f(const char *p) { return strncmp(p, cs, 4); }

... the macro magic for strncmp produces a (potentially crashing) call to
strlen(cs), because it expands to an expression starting with:

  __builtin_constant_p(cs) && strlen(cs) < 4 ? /* ... */

Under the secret true meaning of __builtin_constant_p, this is guaranteed to be
safe!

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@146236 91177308-0d34-0410-b5e6-96231b3b80d8
lib/AST/ExprConstant.cpp
test/Sema/builtins.c