<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; color: rgb(0, 0, 0); font-size: 14px; font-family: Calibri, sans-serif;"><div>Hi,</div><div><br></div><div>I must admit I am not an expert on how clang implements preprocessing, but my experience with another C-like compiler tells me that preprocessing is usually very tightly coupled into the lexer. Clang’s lexer claims to be highly optimized, so I think you will find the same tight coupling there. Hence, I doubt that you can achieve what you need without changing any of clang’s source code.</div><div><br></div><div>That said, a minimally invasive approach might be to derive your own preprocessor class from clang’s “Preprocessor” class and overload the methods you would like to modify (e.g. “HandleDefineDirective”). For this to work, you will have to make the overloaded methods virtual in the base class, i.e. In the “Preprocessor” class. Does this sound like a sensible approach?</div><div><br></div><div>Best,</div><div><br></div><div>Norman</div><div><br></div><div><br></div><span id="OLK_SRC_BODY_SECTION"><div style="font-family:Calibri; font-size:11pt; text-align:left; color:black; BORDER-BOTTOM: medium none; BORDER-LEFT: medium none; PADDING-BOTTOM: 0in; PADDING-LEFT: 0in; PADDING-RIGHT: 0in; BORDER-TOP: #b5c4df 1pt solid; BORDER-RIGHT: medium none; PADDING-TOP: 3pt"><span style="font-weight:bold">From: </span> Timur Iskhakov <<a href="mailto:iskhakovt@gmail.com">iskhakovt@gmail.com</a>><br><span style="font-weight:bold">Date: </span> Thursday 19 May 2016 20:45<br><span style="font-weight:bold">To: </span> <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>>, Norman Rink <<a href="mailto:norman.rink@tu-dresden.de">norman.rink@tu-dresden.de</a>><br><span style="font-weight:bold">Subject: </span> Re: [cfe-dev] Preprocessor Macros Parser<br></div><div><br></div><div dir="ltr">Hi,<div><br></div><div><font color="#500050"><span style="font-size:12.8px">In fact, I am confused in very basic things about clang: I have found no way to get the macro definition, make some magic with it and then make some magic on calling macroses.</span></font></div><div><font color="#500050"><span style="font-size:12.8px"><br></span></font></div><div><font color="#500050"><span style="font-size:12.8px">For instance, for code:</span></font></div><div>#define sum(a, b) (a) + (b)</div><div>int main() { sum(1, 2); }</div><div>I want to do something on macro sum initialization and on macro sum call processing (in fact, at this moment I will evaluate the macros by myself and then push "3" instead of "(1) + (2)").</div><div><br></div><div>So, generally speaking, I need to replace the clang preprocessor in the easiest way if possible (I still hope that it is possible to make it as an extension without changing anything in clang code).</div><div><br></div><div>I hone, someone can give me an advice how to implement it!</div><div><br></div><div>Timur </div><div class="gmail_extra"><div><div class="gmail_signature"><div dir="ltr"><div dir="ltr"><br></div></div></div></div><br><div class="gmail_quote">On Thu, May 19, 2016 at 11:18 AM, Norman Rink via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Timur,<br><br>
Are you tied to the preprocessor (pp) and pp macros? What is your use case?<br><br>
I recently did some work on what you might call ©øsyntax macros©÷ [1]. The<br>
definition of a syntax macro captures an AST subtree. Later, when the<br>
macro is called, the subtree is instantiated (i.e. pasted into the AST at<br>
the place of the macro call). This is orthogonal to preprocessor macros:<br>
since pp macros perform textual replacement, they may be more flexible<br>
than syntax macros. On the other hand, syntax macros let you do type<br>
checking at two levels: the ©østandard©÷ type checking or semantic analysis<br>
of the language (i.e. C/C++), and checking of whether pasting a syntax<br>
macro in a certain place in the AST is legal (i.e. type checking on the<br>
AST node type). Since a syntax macro captures a subtree of the AST, you<br>
can also perform tree transformations on the macro.<br><br>
I implemented syntax macros as a language extension, which required quite<br>
a bit of concentrated work. When I had to stop working on this, my<br>
prototype was (and is) far from being complete, certainly not at the level<br>
of the system described in [1].<br><br>
I hope this helps more than it confuses.<br><br>
Best regards,<br><br>
Norman Rink<br><br><br>
[1]<br><a href="https://www.cs.rice.edu/~taha/teaching/05S/511/papers/weise93programmable.p
df" rel="noreferrer" target="_blank">https://www.cs.rice.edu/~taha/teaching/05S/511/papers/weise93programmable.p<br>
df</a><br><br><br><br><br>
>Date: Wed, 18 May 2016 12:50:43 +0300<br>
>From: Timur Iskhakov via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>><br>
>To: <a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br>
>Subject: [cfe-dev] Preprocessor Macros Parser<br>
>Message-ID:<br>
>       <CAMDUZojd10uj3sqQix70cua=+bJKYk=<a href="mailto:u5Ey0o2bV67t46tc2uA@mail.gmail.com">u5Ey0o2bV67t46tc2uA@mail.gmail.com</a>><br>
>Content-Type: text/plain; charset="utf-8"<br><div><div class="h5">><br>
>Hi All,<br>
><br>
>I am attempting to implement a supercompilation method in optimizing the<br>
>preprocessor.<br>
><br>
>So, I need to parse the preprocessor directives (best case scenario: build<br>
>an AST of the macroses) before compilation.<br>
>Can anyone suggest a better way to do it? If it is quite hard to parse the<br>
>macroses, it is ok to just get get all of them and then without clang<br>
>usage.<br>
><br>
>(It would be nice but not necessary if it is possible to implement as an<br>
>extension. But I still cannot find a way to do it.)<br>
><br>
>Thakns!<br>
><br>
>Timur<br></div></div>>-------------- next part --------------<br>
>An HTML attachment was scrubbed...<br>
>URL:<br>
><<a href="http://lists.llvm.org/pipermail/cfe-dev/attachments/20160518/382bda5a/att" rel="noreferrer" target="_blank">http://lists.llvm.org/pipermail/cfe-dev/attachments/20160518/382bda5a/att</a><br>
>achment-0001.html><br>
><br>
>------------------------------<br><br><br>
_______________________________________________<br>
cfe-dev mailing list<br><a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br></blockquote></div><br></div></div></span></body></html>