Question about Clang Rewriter and white space

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

Hi Andrew,

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

For inspiration, you could take a look at how the LibreOffice Clang plugin solves that, RewritePlugin::adjustRangeForOptions at <plugin.cxx (revision e63e769b) - OpenGrok cross reference for /core/compilerplugins/clang/plugin.cxx.

Hi Jonas,

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

Best Regards,

Andrew

Hi Andrew,

that at least one possible way :slight_smile:

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