<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><br><div><div>On Jun 17, 2013, at 1:00 PM, "Bernal, Ariel J" <<a href="mailto:ariel.j.bernal@intel.com">ariel.j.bernal@intel.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div lang="EN-US" link="blue" vlink="purple" style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div class="WordSection1" style="page: WordSection1;"><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif; color: rgb(31, 73, 125);">I did try getLookupParent() and I get the same results as getParent(). The problem that I found by using DeclContext it that all VarDecls within the same FunctionDecl share the same DeclContext even if they belong to different CompoundStmts.</span></div></div></div></blockquote><div dir="auto"><br></div><div dir="auto">That’s the “conservative” part of this. You can use DeclContexts to figure out whether there are any declarations of the same name within the same FunctionDecl, but you don’t actually have enough scope information to tell whether they will actually intersect. For that, you need to walk the statements looking for the actual declaration.</div><div dir="auto"><br></div><div dir="auto"><span class="Apple-tab-span" style="white-space:pre"> </span>- Doug</div><div dir="auto"><br></div><br><blockquote type="cite"><div lang="EN-US" link="blue" vlink="purple" style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div class="WordSection1" style="page: WordSection1;"><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif; color: rgb(31, 73, 125);"><o:p></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif; color: rgb(31, 73, 125);">I also tried using ASTContext.getParents() which gives more information that DeclContext but I still agree with Doug that I should be able to get the information from DeclContexts.<o:p></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif; color: rgb(31, 73, 125);"> </span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif; color: rgb(31, 73, 125);">Airel<o:p></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif; color: rgb(31, 73, 125);"> </span></div><div><div style="border-style: solid none none; border-top-color: rgb(181, 196, 223); border-top-width: 1pt; padding: 3pt 0in 0in;"><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;"><b><span style="font-size: 10pt; font-family: Tahoma, sans-serif;">From:</span></b><span style="font-size: 10pt; font-family: Tahoma, sans-serif;"><span class="Apple-converted-space"> </span>Douglas Gregor [<a href="mailto:dgregor@apple.com">mailto:dgregor@apple.com</a>]<span class="Apple-converted-space"> </span><br><b>Sent:</b><span class="Apple-converted-space"> </span>Monday, June 17, 2013 12:30 PM<br><b>To:</b><span class="Apple-converted-space"> </span>Manuel Klimek<br><b>Cc:</b><span class="Apple-converted-space"> </span>Bernal, Ariel J; clang-dev Developers<br><b>Subject:</b><span class="Apple-converted-space"> </span>Re: LoopConvert shadowing variables<o:p></o:p></span></div></div></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;"><o:p> </o:p></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;"><o:p> </o:p></div><div><div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;">On Jun 17, 2013, at 12:29 AM, Manuel Klimek <<a href="mailto:klimek@google.com" style="color: purple; text-decoration: underline;">klimek@google.com</a>> wrote:<o:p></o:p></div></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;"><br><br><o:p></o:p></div><div><div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;">+cfe-dev<o:p></o:p></div></div><div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;"><o:p> </o:p></div></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;">On Fri, Jun 14, 2013 at 9:48 PM, Bernal, Ariel J<span class="apple-converted-space"> </span><<a href="mailto:ariel.j.bernal@intel.com" target="_blank" style="color: purple; text-decoration: underline;">ariel.j.bernal@intel.com</a>><span class="apple-converted-space"> </span>wrote:<o:p></o:p></div><div><blockquote style="border-style: none none none solid; border-left-color: rgb(204, 204, 204); border-left-width: 1pt; padding: 0in 0in 0in 6pt; margin-left: 4.8pt; margin-right: 0in;"><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;">Manuel, Douglas<o:p></o:p></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;"> <o:p></o:p></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;">I’m working on a patch to prevent LoopConvert for shadowing variables <a href="http://llvm-reviews.chandlerc.com/D950" target="_blank" style="color: purple; text-decoration: underline;">http://llvm-reviews.chandlerc.com/D950</a><span class="apple-converted-space"> </span> but I don’t feel like it’s the right solution. Maybe you could shed some light on this.<o:p></o:p></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;"> <o:p></o:p></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;">The first loop goes over CompoundStms and we look for VarDecls that could match the name of the new variable until we reach the FunctionDecl then the next loop goes over Decls to get any VarDecl/FieldDecl/ParamVarDecl... defined in a parent context.<o:p></o:p></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;"> <o:p></o:p></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;">Unfortunately DeclContext::getParent doesn’t return the parent Decl when it comes to templates but I think it could be possible to use DeclContext instead of using a Decl-Decl map to reverse the AST.<o:p></o:p></div></blockquote><div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;"><o:p> </o:p></div></div><div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;">Added some comments to the review thread. Also, for trying to simulate the unqualified lookup, did you also try getLookupParent()?<o:p></o:p></div></div><div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;"><o:p> </o:p></div></div><div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;">Regarding templates, note that statement nodes are shared between the "model" CXXRecordDecl formed by the template definition and containing lots of dependent types and the ClassTemplateSpecializationDecl, which you probably want. So it's important that the first declcontext you get to from the statement where you want to insert the declaration is the one created by the ClassTemplateSpecializationDecl.<o:p></o:p></div></div></div></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;"><o:p> </o:p></div></div><div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;">Statement and expression nodes in the instantiation will only be shared between the template definition and template instantiation when there are no dependent expressions, types, or declarations anywhere in the statement/expression. A declaration in a template is always instantiated, and will have the instantiation as its DeclContext. There should be enough information in the DeclContexts to conservatively determine whether a newly-introduced variable will shadow an existing variable.<o:p></o:p></div></div><div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;"><o:p> </o:p></div></div><div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif;"><span class="apple-tab-span"> <span class="Apple-converted-space"> </span></span>- Doug</div></div></div></div></blockquote></div><br></body></html>