Question about Clang Rewriter and white space

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Question about Clang Rewriter and white space

Alex Denisov via cfe-dev

Hi,


I am currently playing around with a Clang Libtool and I was wondering if there was a way to make the Clang Rewriter remove white space or if there is another utility class that can do this? For example I have an arbitrary set of template parameters:


template <typename X, typename Y>


And I wish to remove the first template parameter using the Rewriter in this case to modify the source code. To do this I remove the typename X and the comma. However, the result ends up like:


template < typename Y>


Which contains some unsightly white space! This persists even if I increase the SourceRange to just before the t of typename Y. So it seems to be ignoring the space.


So in essence is there a way to make the Rewriter consider white space for removal or is there an alternative tool or method (like using a function of Clangs AST) to avoid this? At the moment I work around this by injecting an arbitrary character in the empty space and then I delete it. Which works, but doesn't seem like the ideal solution. 


Sorry if this is a silly question and thank you very much for everyone's time. 


Best Regards,

Andrew  


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Question about Clang Rewriter and white space

Alex Denisov via cfe-dev
Hi Andrew,

white space is a job for clang-format. You can do your rewriting and apply it afterwards to get nice looking code.

Am 13.12.2017 6:32 vorm. schrieb "Andrew Gozillon via cfe-dev" <[hidden email]>:

Hi,


I am currently playing around with a Clang Libtool and I was wondering if there was a way to make the Clang Rewriter remove white space or if there is another utility class that can do this? For example I have an arbitrary set of template parameters:


template <typename X, typename Y>


And I wish to remove the first template parameter using the Rewriter in this case to modify the source code. To do this I remove the typename X and the comma. However, the result ends up like:


template < typename Y>


Which contains some unsightly white space! This persists even if I increase the SourceRange to just before the t of typename Y. So it seems to be ignoring the space.


So in essence is there a way to make the Rewriter consider white space for removal or is there an alternative tool or method (like using a function of Clangs AST) to avoid this? At the moment I work around this by injecting an arbitrary character in the empty space and then I delete it. Which works, but doesn't seem like the ideal solution. 


Sorry if this is a silly question and thank you very much for everyone's time. 


Best Regards,

Andrew  


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Question about Clang Rewriter and white space

Alex Denisov via cfe-dev
In reply to this post by Alex Denisov via cfe-dev
On 12/13/2017 06:32 AM, Andrew Gozillon via cfe-dev wrote:
> So in essence is there a way to make the Rewriter consider white space
> for removal or is there an alternative tool or method (like using a
> function of Clangs AST) to avoid this? At the moment I work around this
> by injecting an arbitrary character in the empty space and then I delete
> it. Which works, but doesn't seem like the ideal solution.

For inspiration, you could take a look at how the LibreOffice Clang
plugin solves that, RewritePlugin::adjustRangeForOptions at
<https://opengrok.libreoffice.org/xref/core/compilerplugins/clang/plugin.cxx#466>.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Question about Clang Rewriter and white space

Alex Denisov via cfe-dev
In reply to this post by Alex Denisov via cfe-dev

Hi Jonas,


Thank you very much, I was wondering if that was the right way to go about it. 


Best Regards,

Andrew



From: Jonas Toth <[hidden email]>
Sent: 13 December 2017 08:03:43
To: Andrew Gozillon
Cc: via cfe-dev
Subject: Re: [cfe-dev] Question about Clang Rewriter and white space
 
Hi Andrew,

white space is a job for clang-format. You can do your rewriting and apply it afterwards to get nice looking code.

Am 13.12.2017 6:32 vorm. schrieb "Andrew Gozillon via cfe-dev" <[hidden email]>:

Hi,


I am currently playing around with a Clang Libtool and I was wondering if there was a way to make the Clang Rewriter remove white space or if there is another utility class that can do this? For example I have an arbitrary set of template parameters:


template <typename X, typename Y>


And I wish to remove the first template parameter using the Rewriter in this case to modify the source code. To do this I remove the typename X and the comma. However, the result ends up like:


template < typename Y>


Which contains some unsightly white space! This persists even if I increase the SourceRange to just before the t of typename Y. So it seems to be ignoring the space.


So in essence is there a way to make the Rewriter consider white space for removal or is there an alternative tool or method (like using a function of Clangs AST) to avoid this? At the moment I work around this by injecting an arbitrary character in the empty space and then I delete it. Which works, but doesn't seem like the ideal solution. 


Sorry if this is a silly question and thank you very much for everyone's time. 


Best Regards,

Andrew  


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Question about Clang Rewriter and white space

Alex Denisov via cfe-dev

Hi Andrew,


that at least one possible way :)


clang-tidy has a '-format-style' option that will format code after fixing it. You might have a look into that for your tool as well.


All the Best, Jonas


Am 13.12.2017 um 11:21 schrieb Andrew Gozillon:

Hi Jonas,


Thank you very much, I was wondering if that was the right way to go about it. 


Best Regards,

Andrew



From: Jonas Toth [hidden email]
Sent: 13 December 2017 08:03:43
To: Andrew Gozillon
Cc: via cfe-dev
Subject: Re: [cfe-dev] Question about Clang Rewriter and white space
 
Hi Andrew,

white space is a job for clang-format. You can do your rewriting and apply it afterwards to get nice looking code.

Am 13.12.2017 6:32 vorm. schrieb "Andrew Gozillon via cfe-dev" <[hidden email]>:

Hi,


I am currently playing around with a Clang Libtool and I was wondering if there was a way to make the Clang Rewriter remove white space or if there is another utility class that can do this? For example I have an arbitrary set of template parameters:


template <typename X, typename Y>


And I wish to remove the first template parameter using the Rewriter in this case to modify the source code. To do this I remove the typename X and the comma. However, the result ends up like:


template < typename Y>


Which contains some unsightly white space! This persists even if I increase the SourceRange to just before the t of typename Y. So it seems to be ignoring the space.


So in essence is there a way to make the Rewriter consider white space for removal or is there an alternative tool or method (like using a function of Clangs AST) to avoid this? At the moment I work around this by injecting an arbitrary character in the empty space and then I delete it. Which works, but doesn't seem like the ideal solution. 


Sorry if this is a silly question and thank you very much for everyone's time. 


Best Regards,

Andrew  


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev