[cfe-dev] __func__ should be of type char[], not char*

Eli Friedman eli.friedman at gmail.com
Sat Jan 12 09:24:18 PST 2008


On Jan 12, 2008 12:16 AM, Chris Lattner <clattner at apple.com> wrote:
> Very nice, applied:
> http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20080107/003745.html

Cool.

I have a minor correction to the patch: __func__ is a static const
char[], so the type in the array is actually
"Context.CharTy.getQualifiedType(QualType::Const)".  Test:
void a() {
__func__[0] = 'a';
}
should give an error with clang -fsyntax-only.

Another small thing I didn't think of when I submitted the patch:
CurFunctionDecl needs to be null-checked.  Testcase:
char* a = __func__; // File scope declaration

gcc's exact behavior here is kind of weird, though... it considers
this mistake to be only a warning, and it lets through __FUNCTION__
without even complaining; both __func__ and __FUNCTION__ end up
resolving to an empty string.  I'm not sure if clang needs to copy gcc
here.

> I wrapped a few lines to 80 columns and changed strlen to ask the
> identifier its length directly.
>
> Also note that your test above works better, but it is still not
> entirely happy:
>
> predef.c:2:25: error: initializer element is not constant
>   static const char* s = __func__;
>                          ^~~~~~~~

I think that's dependent on my patch for compound literals... clang
doesn't currently deal with constant-testing of ImplicitCastExpr
correctly.

-Eli



More information about the cfe-dev mailing list