<div dir="ltr">Hmm, the braces in the if (bases.find(...)...) are not needed.<div><br></div><div>Could you also add a test case for virtual inheritance?<br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 30, 2017 at 8:34 PM, James Sun <span dir="ltr"><<a href="mailto:jamessun@fb.com" target="_blank">jamessun@fb.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="white" lang="EN-US" link="blue" vlink="purple">
<div class="m_-6625784765298484012WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">Hi Saleem<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">Thanks for the quick response. A test case is added. It covers some ordinary cases as well as corner cases like multiple paths to the same base.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">Thanks<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">James<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<div style="border:none;border-top:solid #b5c4df 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-family:Calibri;color:black">From: </span>
</b><span style="font-family:Calibri;color:black">Saleem Abdulrasool <<a href="mailto:compnerd@compnerd.org" target="_blank">compnerd@compnerd.org</a>><br>
<b>Date: </b>Monday, January 30, 2017 at 6:50 PM<br>
<b>To: </b>James Sun <<a href="mailto:jamessun@fb.com" target="_blank">jamessun@fb.com</a>><br>
<b>Cc: </b>Richard Smith <<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>>, "<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>" <<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>>, Aaron Ballman <<a href="mailto:aaron@aaronballman.com" target="_blank">aaron@aaronballman.com</a>></span></p><div><div class="h5"><br>
<b>Subject: </b>Re: Add warning for c++ member variable shadowing<u></u><u></u></div></div><p></p>
</div><div><div class="h5">
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">I think that the patch is starting to look pretty good! <u></u>
<u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Can you add some test cases for the particular cases to diagnose in a separate test set to ensure that we have proper coverage of the various cases rather than relying on the existing test cases? Something to make sure that we get the
simple case right as well as the complex cases (e.g. we don't print duplicate warnings for multiple paths).<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">On Mon, Jan 30, 2017 at 5:50 PM, James Sun <<a href="mailto:jamessun@fb.com" target="_blank">jamessun@fb.com</a>> wrote:<u></u><u></u></p>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">Hi Richard</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">Sorry for the late reply. Thank you for giving the feedback! The updated version is attached. Please let me know if there is anything
improper.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">Thanks</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">James</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"> </span><u></u><u></u></p>
<div style="border:none;border-top:solid #b5c4df 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-family:Calibri;color:black">From:
</span></b><span style="font-family:Calibri;color:black"><<a href="mailto:metafoo@gmail.com" target="_blank">metafoo@gmail.com</a>> on behalf of Richard Smith <<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>><br>
<b>Date: </b>Friday, January 27, 2017 at 3:03 PM<br>
<b>To: </b>James Sun <<a href="mailto:jamessun@fb.com" target="_blank">jamessun@fb.com</a>><br>
<b>Cc: </b>Saleem Abdulrasool <<a href="mailto:compnerd@compnerd.org" target="_blank">compnerd@compnerd.org</a>>, "<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>" <<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>>,
Aaron Ballman <<a href="mailto:aaron@aaronballman.com" target="_blank">aaron@aaronballman.com</a>></span><u></u><u></u></p>
<div>
<div>
<p class="MsoNormal"><br>
<b>Subject: </b>Re: Add warning for c++ member variable shadowing<u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">+def warn_shadow_member_variable : Warning<<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">+ "shadowed variable '%0' in type '%1' inheriting from type '%2'">,<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">The phrasing of this is incorrect: the things you're warning about are not variables, they're non-static data members. Perhaps something like:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> "non-static data member '%0' of '%1' shadows member inherited from type '%2'"<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">+ InGroup<Shadow>;<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Would it make sense to put this in a subgroup of -Wshadow so that it can be controlled separately?<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<div>
<p class="MsoNormal">+ /// Check if there is a member variable shadowing<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Please end comments in a period.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">+ void CheckShadowInheritedVariables(<wbr>const SourceLocation &Loc,<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Likewise, 'Variables' is wrong. We would typically use the C term 'Fields' for these cases within Clang sources.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<div>
<p class="MsoNormal">+ for (const auto &Base : DC->bases()) {<u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">+ if (const auto *TSI = Base.getTypeSourceInfo())<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">+ if (const auto *BaseClass = TSI->getType()-><wbr>getAsCXXRecordDecl()) {<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">+ for (const auto *Field : BaseClass->fields())<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">+ if (Field->getName() == FieldName)<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">+ Diag(Loc, diag::warn_shadow_member_<wbr>variable)<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">+ << FieldName << RD->getName() << BaseClass->getName();<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">+ // Search parent's parents<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">+ <wbr>CheckShadowInheritedVariables(<wbr>Loc, FieldName, RD, BaseClass);<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">+ }<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">+ }<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Maybe we should avoid diagnosing shadowing of members that are inaccessible from the derived class? What about if the field name is ambiguous? Also, we shouldn't recurse if lookup
finds something with the given name in this class, and ideally we would only visit each class once, even if it appears multiple times in a multiple-inheritance scenario. CXXRecordDecl::lookupInBases can handle most of these cases for you automatically, and
will also let you build a set of paths to problematic base classes in case you want to report those.<u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">On 24 January 2017 at 20:52, James Sun <<a href="mailto:jamessun@fb.com" target="_blank">jamessun@fb.com</a>> wrote:<u></u><u></u></p>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-top:5.0pt;margin-right:0in;margin-bottom:5.0pt">
<div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">Thanks for the comments. The new version is attached.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">Wrt two of your questions:</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">(1) “The description that you have on <wbr>CheckShadowInheritedVariables isn't really the type of comments that we have in doxygen
form. Im not sure if its in line with the rest of the code.”</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">I’ve read through the doxygen wiki; hopefully it’s fixed; let me know if it’s still wrong</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">(2) “Why are you checking that the DeclContext has a definition rather than the record itself?”</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">There are cases like “struct A; struct B : A {};”, where A does not have a definition. The compiler will hit an assertion failure
if we call A.bases() directly.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">Thanks</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">James</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"> </span><u></u><u></u></p>
<div style="border:none;border-top:solid #b5c4df 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-family:Calibri;color:black">From:
</span></b><span style="font-family:Calibri;color:black">Saleem Abdulrasool <<a href="mailto:compnerd@compnerd.org" target="_blank">compnerd@compnerd.org</a>><br>
<b>Date: </b>Tuesday, January 24, 2017 at 7:10 PM<br>
<b>To: </b>James Sun <<a href="mailto:jamessun@fb.com" target="_blank">jamessun@fb.com</a>><br>
<b>Cc: </b>"<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>" <<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>>, Aaron Ballman <<a href="mailto:aaron@aaronballman.com" target="_blank">aaron@aaronballman.com</a>>,
Richard Smith <<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>><br>
<b>Subject: </b>Re: Add warning for c++ member variable shadowing</span><u></u><u></u></p>
</div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Some more stylistic comments:
<u></u><u></u></p>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">The description that you have on <wbr>CheckShadowInheritedVariables isn't really the type of comments that we have in doxygen form. Im not sure if its in line with the rest of the code.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">The ignore warning comments are restating what is in the code, please remove them.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Could you make the header and the source file match the name?<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Why are you checking that the DeclContext has a definition rather than the record itself?<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Space after the <<.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Don't use the cast for the check, use isa. Although, since you use the value later, it is probably better to write this as:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> if (const auto *RD = cast<CXXRecordDecl>(<wbr>CurContext))<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> CheckShadowInheritedVariabless<wbr>(Loc, Name.getAsString(), RD, RD);<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
<div>
<p class="MsoNormal">On Tue, Jan 24, 2017 at 4:06 PM, James Sun via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>> wrote:<u></u><u></u></p>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-top:5.0pt;margin-right:0in;margin-bottom:5.0pt">
<div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt">Coding style change</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt"> </span><u></u><u></u></p>
<div style="border:none;border-top:solid #b5c4df 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="color:black">From:
</span></b><span style="color:black">James Sun <<a href="mailto:jamessun@fb.com" target="_blank">jamessun@fb.com</a>><br>
<b>Date: </b>Tuesday, January 24, 2017 at 2:36 PM<br>
<b>To: </b>"<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>" <<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>><br>
<b>Subject: </b>Add warning for c++ member variable shadowing</span><u></u><u></u></p>
</div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<p class="MsoNormal"><span style="font-size:11.0pt">Dear members</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt">Here is a patch (attached) to create warnings where a member variable shadows the one in one of its inheriting classes. For cases where we really
don't want to shadow member variables, e.g. </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt">class a {</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt"> int foo;
</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt">}</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt">class b : a {
</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt"> int foo; // Generate a warning</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt">}</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt">This patch
</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt">(1) adds a member variable shadowing checking, and
</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt">(2) incorporates it to the unit tests.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt">Comments are welcome.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt">Thanks</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt">James</span><u></u><u></u></p>
</div>
</div>
</div>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><br>
______________________________<wbr>_________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a><br>
<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__lists.llvm.org_cgi-2Dbin_mailman_listinfo_cfe-2Dcommits&d=DwMFaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=ikRH8URaurZA7JMys57d3w&m=lheFEjRie_ahss0mWHaJIa1eNMlFv2DMH5ZWHGQvo8U&s=750RLygVMQIDJB7IKBhOef4zIDHerGwb7aJZAY2aP9U&e=" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-commits</a><u></u><u></u></p>
</blockquote>
</div>
<p class="MsoNormal"><br>
<br clear="all">
<u></u><u></u></p>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<p class="MsoNormal">--
<u></u><u></u></p>
<div>
<p class="MsoNormal">Saleem Abdulrasool<br>
compnerd (at) compnerd (dot) org<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<p class="MsoNormal"><br>
<br clear="all">
<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<p class="MsoNormal">-- <u></u><u></u></p>
<div>
<p class="MsoNormal">Saleem Abdulrasool<br>
compnerd (at) compnerd (dot) org<u></u><u></u></p>
</div>
</div>
</div></div></div>
</div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Saleem Abdulrasool<br>compnerd (at) compnerd (dot) org</div>
</div></div></div>