[cfe-dev] How to programatically define a symbol?

Yaakov Davis yaakov.davis at gmail.com
Sat Apr 7 13:22:53 PDT 2012


Found a way. For anyone interested:

MacroInfo* RegisterMacro(Preprocessor &PP, const char *Name){
  // Get the identifier.
  IdentifierInfo *Id = PP.getIdentifierInfo(Name);

  // Mark it as being a macro that is builtin.
  MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
  MI->setIsBuiltinMacro();
  PP.setMacroInfo(Id, MI);

  return MI;
}

...

RegisterMacro(preprocessor, "FOO");


If you need replacement strings, manipulate the returned MacroInfo as
follows:

        Token t;
t.startToken();
auto& i = preprocessor.getIdentifierTable().get("Replacement_String",
tok::TokenKind::identifier);
t.setIdentifierInfo(&i);
macroInfo->AddTokenToBody(t);

On Sat, Apr 7, 2012 at 9:45 PM, Yaakov Davis <yaakov.davis at gmail.com> wrote:

> I'm not using the command line, but the lib through code. How do I achieve
> the desired effect *programatically*?
>
>
> On Sat, Apr 7, 2012 at 9:42 PM, Matthieu Monrocq <
> matthieu.monrocq at gmail.com> wrote:
>
>>
>>
>> Le 7 avril 2012 20:17, Yaakov Davis <yaakov.davis at gmail.com> a écrit :
>>
>>> I want to programatically define a symbol, e.g. FOO, such that the code
>>> "#if defined(FOO)" will evaluate to true during pre-processing.
>>> How do I that?
>>>
>>> I tried addMacroDef("FOO"), and other methods, but to no avail.
>>>
>>> _______________________________________________
>>> cfe-dev mailing list
>>> cfe-dev at cs.uiuc.edu
>>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>>>
>>>
>> Either:
>>
>> #define FOO
>>
>> in the code or
>>
>> -DFOO
>>
>> on the command line during the compilation (for gcc and clang at least)
>>
>> -- Matthieu
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120407/7c6ab3a6/attachment.html>


More information about the cfe-dev mailing list