<div dir="ltr">
<div>When parsing a named declaration with an equals sign
with clang -std c++11/14, clang builds an initializer expression whose
SourceRange covers from the variable name through the end of the initial
value:<br></div><div><span style="font-family:monospace"> std::string foo = "bar";</span></div><div><span style="font-family:monospace"></span>
<div><span style="font-family:monospace"> ^~~~~~~~~~~<br></span></div>
</div><div><br></div><div>When parsing the same code with clang -std
c++17/2x, the initializer expression's SourceRange only includes the
initial value itself, and not the variable name or the equals sign:</div><div>
<div><span style="font-family:monospace"> std::string foo = "bar";</span><span style="font-family:monospace"><br></span></div><div><span style="font-family:monospace"> ^~~~~<br></span>
</div><br></div><div>
<div>If the string is initialized using parentheses rather than an
equals sign, in all of c++11/14/17/2x the initializer expression's
SourceRange includes the variable name and both parentheses:</div><div>
<div><span style="font-family:monospace"> std::string foo("bar");</span><span style="font-family:monospace"><br></span></div><div><span style="font-family:monospace"> ^~~~~~~~~~<br></span>
</div><br></div>
</div><div>This difference has broken clang-tidy's <a href="https://clang.llvm.org/extra/clang-tidy/checks/readability-redundant-string-init.html" target="_blank">readability-remove-redundant-string</a> for c++17 and c++2x, as described in at <a href="https://reviews.llvm.org/D69238/" target="_blank">reviews.llvm.org/D69238</a>.
Is this
SourceRange
difference intentional, and if not does anyone have thoughts on how to
make the SourceRange consistent across C++ standards? Thanks!<div class="gmail-adL"><br></div><div class="gmail-adL">-- Conrad</div><div class="gmail-adL"><br></div><div class="gmail-adL">P.S. I've tried stepping through clang-tidy parsing the
<span style="font-family:monospace">std::string a = "";</span>
line in <span style="font-family:monospace">clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp</span> with -std=c++11 and again with -std=c++17. In both cases <span style="font-family:monospace">Sema::AddInitializerToDecl</span> seems to create a correct <span style="font-family:monospace">InitializationKind</span> with the <span style="font-family:monospace">Locations[0]</span> properly pointing to the start of the variable name. I'm not sure how or where that gets translated to the expression's SourceRange later on in some way that presumably differs between c++11 and c++17. If this SourceRange difference is unintentional and needs to be tracked down and fixed, any tips on where to look would be appreciated.<br></div></div>
</div>