diff: builtin macros

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!

clang__define_builtin.diff (3.67 KB)

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

Well, they do prepend such stuff with console defines (-D, etc).
My diff leaves this intact, only compiler internal macros are marked as
IsBuiltinMacro.

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

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 :slight_smile:

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 :slight_smile:

-Chris

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!