[cfe-dev] Source rewrite

Fariborz Jahanian fjahanian at apple.com
Thu Dec 17 09:49:04 PST 2009


On Dec 17, 2009, at 9:44 AM, Abramo Bagnara wrote:

> Il 17/12/2009 18:32, Fariborz Jahanian ha scritto:
>>
>> On Dec 17, 2009, at 9:23 AM, Douglas Gregor wrote:
>>
>>>
>>> On Dec 17, 2009, at 9:13 AM, Abramo Bagnara wrote:
>>>
>>>> Il 17/12/2009 16:57, Douglas Gregor ha scritto:
>>>>>
>>>>> On Dec 17, 2009, at 5:23 AM, steve naroff wrote:
>>>>>
>>>>>>
>>>>>> On Dec 17, 2009, at 7:05 AM, Abramo Bagnara wrote:
>>>>>>
>>>>>>> In an application that uses clang libraries we need to transform
>>>>>>> some
>>>>>>> specially written comments in C construct.
>>>>>>>
>>>>>>> e.g:
>>>>>>> int p() {
>>>>>>> int a = 3;
>>>>>>> /* #instrument(a)# */
>>>>>>> }
>>>>>>>
>>>>>>> should be parsed as
>>>>>>> int p() {
>>>>>>> int a = 3;
>>>>>>> instrument(a);
>>>>>>> }
>>>>>>>
>>>>>>> I've added a CommentHandler to Preprocessor to catch comments  
>>>>>>> and I
>>>>>>> thought to use Rewriter class to manipulate the input buffer
>>>>>>> inserting
>>>>>>> after the comment the text to be parsed, but then I've realized
>>>>>>> that
>>>>>>> Rewriter class is not designed to rewrite lexer input buffers,
>>>>>>> but to
>>>>>>> build a separate object where original text and changes live
>>>>>>> together.
>>>>>>>
>>>>>>> Now I'm a bit confused: what's the proper way to cope with the  
>>>>>>> need
>>>>>>> described above?
>>>>>>>
>>>>>>
>>>>>> Hi Abramo,
>>>>>>
>>>>>> The ASTContext class has a 'Comments' member that contains all  
>>>>>> the
>>>>>> SourceRanges's for all comments in the source file.
>>>>>>
>>>>>> This assumes you've told the preprocessor to keep comments.
>>>>>
>>>>> You actually don't have to tell the preprocessor to keep  
>>>>> comments; it
>>>>> keeps the source ranges for the comments regardless, and you can  
>>>>> go
>>>>> back
>>>>> to the source to get the content of the comments.
>>>>
>>>> I think to have explained badly what we need: I've no problems to  
>>>> get
>>>> the comment content, my problem is to translate, during the  
>>>> parsing,
>>>> the
>>>> comment content in another text to be lexed/parsed instead of (or  
>>>> just
>>>> after) the comment.
>>>>
>>>> As I write above, what I need is that the AST built from:
>>>>
>>>> int p() {
>>>> int a = 3;
>>>> /* #instrument(a)# */
>>>> }
>>>>
>> Somehow, all semantics checks need be performed and an 'invisible'  
>> AST
>> generated and passed on so rewriter can do
>> the rewrite.
>
> I believe to not understand what you mean... I think that the rewriter
> is not involved in any way.

Don't you want to replace the comment to instrument(a) in the  
rewritten source? If you just want to build ASTs then
you do not need the rewriter.

- Fariborz




More information about the cfe-dev mailing list