<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Oct 31, 2013 at 6:32 AM, Jesper Eskilson <span dir="ltr"><<a href="mailto:Jesper.Eskilson@iar.com" target="_blank">Jesper.Eskilson@iar.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<br>
Hi,<br>
<br>
I'm trying to figure out how to programmatically invoke clang-format
to format code. I would like to get from a declaration ("Decl *") to
a plain string containing the reformatted source code.<br></div></blockquote><div><br></div><div>It's generally advised that one should not attempt to round trip source code through the AST. Use the AST to inform mutations of the original source (by using source locations to identify pieces to move, insert, or remove), rather than to produce new source from the parsed AST.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div bgcolor="#FFFFFF" text="#000000">
<br>
Here's my feeble initial attempt:<br>
<br>
<br>
/*<br>
* Non-functioning attempt att trying to extract the source code of<br>
* 'decl' formatted using LLVM style.<br>
*/<br>
std::string get_formatted_source(SourceManager &mgr, const Decl
*decl)<br>
{<br>
StringRef code = get_source(mgr, decl); // implemented elsewhere<br>
std::vector<Range> ranges;<br>
LangOptions lo;<br>
Rewriter rw(mgr, lo);<br>
<br>
Range range(0, src.size());<br>
ranges.push_back(range);<br>
<br>
// Do the reformat<br>
auto repl = reformat(getLLVMStyle(), code, ranges);<br>
<br>
// Apply the replacements<br>
for (auto it = repl.begin(); it != repl.end(); ++it) {<br>
it->apply(rw);<br>
}<br>
<br>
// Extract the reformatted source code.<br>
auto formatted_source =
rw.getRewrittenText(decl->getSourceRange());<br>
<br>
// I would have expected 'formatted_source' to be the formatted
source<br>
// here, but it is always the same as the original, as if the <br>
// replacements where never applied.<br>
}<span class="HOEnZb"><font color="#888888"><br>
<br>
<br>
<div>-- <br>
<span style="color:#a6a6a6;font-family:arial,helvetica,sans-serif;font-size:10pt"><strong>Jesper
Eskilson</strong></span>
<span style="color:#fdb913;font-family:arial,helvetica,sans-serif;font-size:10pt"><em>Development
Engineer</em></span><br>
<span style="color:#808080;font-family:arial,helvetica,sans-serif;font-size:10pt">IAR Systems AB<br>
Box 23051, Strandbodgatan 1<br>
SE-750 23 Uppsala, SWEDEN<br>
E-mail: <a href="mailto:jesper.eskilson@iar.com" target="_blank">
<span style="color:#808080;font-family:arial,helvetica,sans-serif;font-size:10pt">jesper.eskilson@iar.com</span></a>
Website: <a href="http://www.iar.com" target="_blank">
<span style="color:#808080;font-family:arial,helvetica,sans-serif;font-size:10pt">www.iar.com<br>
</span></a>
Twitter: <a href="http://www.twitter.com/iarsystems" target="_blank">
<span style="color:#808080;font-family:arial,helvetica,sans-serif;font-size:10pt">www.twitter.com/iarsystems</span></a>
</span>
</div>
</font></span></div>
<br>_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div></div>