diff: builtin macros

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

diff: builtin macros

Vladimir Kirillov
Hello, cfe-dev!

There's a diff to support builtin macros defined at the preprocessor
initialization time (like __clang__, __i386__, etc) to be also treated
as builtin (MacroInfo::IsBuiltinMacro), so programs using clang
libraries would be able to distinct these macros from others properly.

I have added a new preprocessor keyword __define_builtin (i saw
__include_macros, so i guess its ok) to implement such feature.

If you suggest any better way of the implementation -- i'm also willing
to help then.


Thanks!

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

clang__define_builtin.diff (3K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: diff: builtin macros

Eric Christopher-2

On Sep 1, 2010, at 9:15 PM, Vladimir Kirillov wrote:

> There's a diff to support builtin macros defined at the preprocessor
> initialization time (like __clang__, __i386__, etc) to be also treated
> as builtin (MacroInfo::IsBuiltinMacro), so programs using clang
> libraries would be able to distinct these macros from others properly.

Uh, why? I mean, I can see the usefulness, but most projects just
go ahead and do things like prepending the name of the project
ahead of macros they define.

-eric
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: diff: builtin macros

Vladimir Kirillov
On 21:55 Wed 01 Sep, Eric Christopher wrote:

>
> On Sep 1, 2010, at 9:15 PM, Vladimir Kirillov wrote:
>
> > There's a diff to support builtin macros defined at the preprocessor
> > initialization time (like __clang__, __i386__, etc) to be also treated
> > as builtin (MacroInfo::IsBuiltinMacro), so programs using clang
> > libraries would be able to distinct these macros from others properly.
>
> Uh, why? I mean, I can see the usefulness, but most projects just
> go ahead and do things like prepending the name of the project
> ahead of macros they define.

Well, they do prepend such stuff with console defines (-D, etc).
My diff leaves this intact, only compiler internal macros are marked as
IsBuiltinMacro.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: diff: builtin macros

Chris Lattner
In reply to this post by Vladimir Kirillov

On Sep 1, 2010, at 9:15 PM, Vladimir Kirillov wrote:

> Hello, cfe-dev!
>
> There's a diff to support builtin macros defined at the preprocessor
> initialization time (like __clang__, __i386__, etc) to be also treated
> as builtin (MacroInfo::IsBuiltinMacro), so programs using clang
> libraries would be able to distinct these macros from others properly.
>
> I have added a new preprocessor keyword __define_builtin (i saw
> __include_macros, so i guess its ok) to implement such feature.
>
> If you suggest any better way of the implementation -- i'm also willing
> to help then.

Hi Vladimir,

What problem are you trying to solve with this?  Is it sufficient to see if the macro is in the predefines buffer?

-Chris
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: diff: builtin macros

Vladimir Kirillov
On 09:53 Thu 02 Sep, Chris Lattner wrote:
> Hi Vladimir,
>
> What problem are you trying to solve with this?
> Is it sufficient to see if the macro is in the predefines buffer?

Hi Chris!

Do you mean the Preprocessor::Predefines string by the predefines
buffer? (and suggest doing string::find() on it?)

I didn't consider taking it into account actually, but marked
the rest of internal macros to be really 'builtin' (aren't they built in
by the compiler by definition?).

My goal is to filter out such builtins in PPCallbacks for my data
gathering tool.

If you don't agree with this -- i think it would be proper to refactor
such mentions of builtins, like DefineBuiltinMacro in
lib/Frontend/InitPreprocessor.cpp, because such macros are not really
'builtin' within MacroInfo context.

Just for consistency's sake, sorry for all the pedantry :-)

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: diff: builtin macros

Chris Lattner

On Sep 2, 2010, at 10:11 AM, Vladimir Kirillov wrote:

> On 09:53 Thu 02 Sep, Chris Lattner wrote:
>> Hi Vladimir,
>>
>> What problem are you trying to solve with this?
>> Is it sufficient to see if the macro is in the predefines buffer?
>
> Hi Chris!
>
> Do you mean the Preprocessor::Predefines string by the predefines
> buffer? (and suggest doing string::find() on it?)
>
> I didn't consider taking it into account actually, but marked
> the rest of internal macros to be really 'builtin' (aren't they built in
> by the compiler by definition?).
>
> My goal is to filter out such builtins in PPCallbacks for my data
> gathering tool.
>
> If you don't agree with this -- i think it would be proper to refactor
> such mentions of builtins, like DefineBuiltinMacro in
> lib/Frontend/InitPreprocessor.cpp, because such macros are not really
> 'builtin' within MacroInfo context.

I'm more concerned with keeping the preprocessor as simple as possible and consistent.  "builtin" macros aren't actually macros: they're magic things that get expanded by the preprocessor like __FILE__ __COUNTER__ etc.  If you'd like to tell whether a macro is "part of the implicit gunk that clang sets up" then I think you can just check to see if the SourceLocation is part of the predefines buffer.

It looks like PrintPreprocessedOutput.cpp does this by doing this:

if (Loc.isFileID() &&
         !strcmp(SourceMgr.getPresumedLoc(Loc).getFilename(),
                 "<built-in>"))
  // it is in the predefines buffer.

Which isn't particularly elegant, but is effective :-)

-Chris
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: diff: builtin macros

Vladimir Kirillov
> I'm more concerned with keeping the preprocessor as simple as
> possible and consistent.
> "builtin" macros aren't actually macros:
> they're magic things that get expanded by the preprocessor like
> __FILE__ __COUNTER__ etc.  If you'd like to tell whether a macro
> is "part of the implicit gunk that clang sets up" then I think you
> can just check to see if the SourceLocation is part of the predefines
> buffer.

>
> It looks like PrintPreprocessedOutput.cpp does this by doing this:
>
> if (Loc.isFileID() &&
>          !strcmp(SourceMgr.getPresumedLoc(Loc).getFilename(),
>                  "<built-in>"))
>   // it is in the predefines buffer.
>

Great! I'll stick to it.
This really avoids having all the weird parser keywords and such.

Thank you!
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev