<div dir="ltr">clang-format is token-based, it sees the raw token stream (without even running the preprocessor, which is why it doesn't need -I and -D flags). clang's cc1 flag -dump-raw-tokens shows you what clang-format sees as input. Note how it #if 0 gets printed instead of evaluated:<div><br></div><div><div>$ cat foo.cc</div><div>#if 0</div><div>asdf</div><div>#endif</div><div>int f();<br></div><div><br></div><div>$ bin/clang -c -Xclang -dump-raw-tokens foo.cc</div><div>hash '#'<span style="white-space:pre">    </span> [StartOfLine]<span style="white-space:pre">       </span>Loc=<foo.cc:1:1></div><div>raw_identifier 'if'<span style="white-space:pre">             </span>Loc=<foo.cc:1:2></div><div>unknown ' '<span style="white-space:pre">             </span>Loc=<foo.cc:1:4></div><div>numeric_constant '0'<span style="white-space:pre">            </span>Loc=<foo.cc:1:5></div><div>unknown '</div><div>'<span style="white-space:pre">               </span>Loc=<foo.cc:1:6></div><div>raw_identifier 'asdf'<span style="white-space:pre">   </span> [StartOfLine]<span style="white-space:pre">       </span>Loc=<foo.cc:2:1></div><div>unknown '</div><div>'<span style="white-space:pre">               </span>Loc=<foo.cc:2:5></div><div>hash '#'<span style="white-space:pre">        </span> [StartOfLine]<span style="white-space:pre">       </span>Loc=<foo.cc:3:1></div><div>raw_identifier 'endif'<span style="white-space:pre">          </span>Loc=<foo.cc:3:2></div><div>unknown '</div><div><br></div><div>'<span style="white-space:pre">              </span>Loc=<foo.cc:3:7></div><div>raw_identifier 'int'<span style="white-space:pre">    </span> [StartOfLine]<span style="white-space:pre">       </span>Loc=<foo.cc:5:1></div><div>unknown ' '<span style="white-space:pre">             </span>Loc=<foo.cc:5:4></div><div>raw_identifier 'f'<span style="white-space:pre">              </span>Loc=<foo.cc:5:5></div><div>l_paren '('<span style="white-space:pre">             </span>Loc=<foo.cc:5:6></div><div>r_paren ')'<span style="white-space:pre">             </span>Loc=<foo.cc:5:7></div><div>semi ';'<span style="white-space:pre">                </span>Loc=<foo.cc:5:8></div><div>unknown '</div><div>'<span style="white-space:pre">               </span>Loc=<foo.cc:5:9></div></div><div><br></div><div><br></div><div>clang-format then has a bunch of heuristics to decide if `a * b` is a multiplication or a declaration, but since it doesn't build an AST as you say, it doesn't know if "a" in two different places refer to the same variable. So in general it can't be used for most automated refactorings, since you usually need ASTs for that.</div><div><br></div><div>(clang-format works great for formatting the output of an automated refactoring though.)</div></div><br><div class="gmail_quote"><div dir="ltr">On Wed, Aug 1, 2018 at 5:25 AM Stuart Thomson via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">





<div lang="EN-GB" link="#0563C1" vlink="#954F72">
<div class="m_-8826874704487089657WordSection1">
<p class="MsoNormal">Hi,<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">I’m interested in using clang to refactor snippets of C++ for which I can’t produce an AST. AFAIK this precludes the use of clang tools like clang-check and I wondered if clang-format could be used instead as it doesn’t seem to require
 the production of an AST. I don’t quite understand how clang-format works and have a couple of questions:<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<ol style="margin-top:0cm" start="1" type="1">
<li class="m_-8826874704487089657MsoListParagraph" style="margin-left:0cm">Is it possible to somehow use clang-format for refactoring C++ according to custom rules? These refactors would be larger scale things than it seems to usually be used for.<u></u><u></u></li><li class="m_-8826874704487089657MsoListParagraph" style="margin-left:0cm">How does clang-format parse C++ without e.g. parsing the includes?<u></u><u></u></li></ol>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Thanks,<u></u><u></u></p>
<p class="MsoNormal">Stuart<u></u><u></u></p>
</div>
</div>

_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div>