[cfe-dev] Preprocessor Macros Parser

Norman Rink via cfe-dev cfe-dev at lists.llvm.org
Fri May 20 03:20:50 PDT 2016


Hi,

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.

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?

Best,

Norman


From:  Timur Iskhakov <iskhakovt at gmail.com>
Date:  Thursday 19 May 2016 20:45
To:  <cfe-dev at lists.llvm.org>, Norman Rink <norman.rink at tu-dresden.de>
Subject:  Re: [cfe-dev] Preprocessor Macros Parser

Hi,

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.

For instance, for code:
#define sum(a, b) (a) + (b)
int main() { sum(1, 2); }
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)").

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).

I hone, someone can give me an advice how to implement it!

Timur 


On Thu, May 19, 2016 at 11:18 AM, Norman Rink via cfe-dev
<cfe-dev at lists.llvm.org> wrote:
> Hi Timur,
> 
> Are you tied to the preprocessor (pp) and pp macros? What is your use case?
> 
> I recently did some work on what you might call ³syntax macros² [1]. The
> definition of a syntax macro captures an AST subtree. Later, when the
> macro is called, the subtree is instantiated (i.e. pasted into the AST at
> the place of the macro call). This is orthogonal to preprocessor macros:
> since pp macros perform textual replacement, they may be more flexible
> than syntax macros. On the other hand, syntax macros let you do type
> checking at two levels: the ³standard² type checking or semantic analysis
> of the language (i.e. C/C++), and checking of whether pasting a syntax
> macro in a certain place in the AST is legal (i.e. type checking on the
> AST node type). Since a syntax macro captures a subtree of the AST, you
> can also perform tree transformations on the macro.
> 
> I implemented syntax macros as a language extension, which required quite
> a bit of concentrated work. When I had to stop working on this, my
> prototype was (and is) far from being complete, certainly not at the level
> of the system described in [1].
> 
> I hope this helps more than it confuses.
> 
> Best regards,
> 
> Norman Rink
> 
> 
> [1]
> https://www.cs.rice.edu/~taha/teaching/05S/511/papers/weise93programmable.p
> df 
> <https://www.cs.rice.edu/~taha/teaching/05S/511/papers/weise93programmable.pdf
> > 
> 
> 
> 
> 
>> >Date: Wed, 18 May 2016 12:50:43 +0300
>> >From: Timur Iskhakov via cfe-dev <cfe-dev at lists.llvm.org>
>> >To: cfe-dev at lists.llvm.org
>> >Subject: [cfe-dev] Preprocessor Macros Parser
>> >Message-ID:
>> >       <CAMDUZojd10uj3sqQix70cua=+bJKYk=u5Ey0o2bV67t46tc2uA at mail.gmail.com>
>> >Content-Type: text/plain; charset="utf-8"
>> >
>> >Hi All,
>> >
>> >I am attempting to implement a supercompilation method in optimizing the
>> >preprocessor.
>> >
>> >So, I need to parse the preprocessor directives (best case scenario: build
>> >an AST of the macroses) before compilation.
>> >Can anyone suggest a better way to do it? If it is quite hard to parse the
>> >macroses, it is ok to just get get all of them and then without clang
>> >usage.
>> >
>> >(It would be nice but not necessary if it is possible to implement as an
>> >extension. But I still cannot find a way to do it.)
>> >
>> >Thakns!
>> >
>> >Timur
>> >-------------- next part --------------
>> >An HTML attachment was scrubbed...
>> >URL:
>> ><http://lists.llvm.org/pipermail/cfe-dev/attachments/20160518/382bda5a/att
>> >achment-0001.html>
>> >
>> >------------------------------
> 
> 
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160520/802e4d41/attachment.html>


More information about the cfe-dev mailing list