<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div class="moz-cite-prefix">On 10/3/20 3:40 PM, Oleksandr Koval via
cfe-dev wrote:<br>
</div>
<blockquote type="cite"
cite="mid:CAGatSHGuxARpSxiESd60Q_5AJbmbCDXuGjCQ6Yq8Cg+A5mL+jw@mail.gmail.com">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<div dir="ltr">
<div>Hi, I'm writing a check that detects and removes redundant
`this->` usage. Finding explicit `this->` was simple,
now I want to check whether it's safe to remove it. My main
concern is such case:</div>
<pre style="color:rgb(0,0,0);background:rgb(255,255,255) none repeat scroll 0% 0%"><span style="color:rgb(128,0,0);font-weight:bold">struct</span> X<span style="color:rgb(128,0,128)">{</span>
<span style="color:rgb(128,0,0);font-weight:bold">void</span> f<span style="color:rgb(128,128,48)">(</span><span style="color:rgb(128,0,0);font-weight:bold">int</span> x<span style="color:rgb(128,128,48)">)</span><span style="color:rgb(128,0,128)">{</span>
<span style="color:rgb(105,105,105)">// without `this->` will use parameter</span>
<span style="color:rgb(128,0,0);font-weight:bold">this</span><span style="color:rgb(128,128,48)">-</span><span style="color:rgb(128,128,48)">></span>x<span style="color:rgb(128,128,48)">+</span><span style="color:rgb(128,128,48)">+</span><span style="color:rgb(128,0,128)">;</span>
<span style="color:rgb(105,105,105)">// can remove `this->` because</span>
<span style="color:rgb(105,105,105)">// local `y` is not visible yet</span>
<span style="color:rgb(128,0,0);font-weight:bold">this</span><span style="color:rgb(128,128,48)">-</span><span style="color:rgb(128,128,48)">></span>y<span style="color:rgb(128,128,48)">+</span><span style="color:rgb(128,128,48)">+</span><span style="color:rgb(128,0,128)">;</span>
y<span style="color:rgb(128,128,48)">+</span><span style="color:rgb(128,128,48)">+</span><span style="color:rgb(128,0,128)">;</span>
<span style="color:rgb(128,0,0);font-weight:bold">int</span> y<span style="color:rgb(128,0,128)">;</span>
y<span style="color:rgb(128,128,48)">+</span><span style="color:rgb(128,128,48)">+</span><span style="color:rgb(128,0,128)">;</span>
<span style="color:rgb(128,0,128)">}</span>
<span style="color:rgb(128,0,0);font-weight:bold">int</span> x<span style="color:rgb(128,0,128)">;</span>
<span style="color:rgb(128,0,0);font-weight:bold">int</span> y<span style="color:rgb(128,0,128)">;</span>
<span style="color:rgb(128,0,128)">}</span><span style="color:rgb(128,0,128)">;</span>
</pre>
<div>I need to know whether a specific name(function or
variable) will resolve to the same declaration without
`this->` part. My matcher is <i>memberExpr(has(cxxThisExpr()))</i>,
I bind MemberExpr and CXXThisExpr, I can rewrite matcher to
also bind enclosing CXXMethodDecl. As I understand, simple
enumeration of parameter/local variable names won't work(as in
case with `y`) because of lookup/visibility rules. Is there
some `lookup()` function whose result I can compare against
the declaration of original function/variable? I found
Sema::LookupName() but can't figure out how to get Scope of
the found expression. Can you give me an example please?<br>
</div>
</div>
</blockquote>
<p><br>
</p>
<p> The Scope is a parser thing which contains an opaque ptr to the
DeclContext. In principle, you can create a fake Scope and attach
the relevant DeclContext via setEntity. I think just passing
Sema::TUScope may work. If you want some pseudocode:</p>
<p> Scope *fakeS = new Scope(...);<br>
fakeS->setEntity(DC);<br>
// you may need to do some work for the variable shadowing<br>
LookupResult R(Sema, Var->getName(), SourceLocation(),
Sema::LookupOrdinaryName, Sema::ForRedeclaration);<br>
Sema->LookupName(R, fakeS);<br>
delete fakeS;</p>
<p> Note that is an approximation as we do not attach the scope to
the chain of scopes like the parser does. It some extra work that
is also possible.</p>
<p><br>
</p>
<blockquote type="cite"
cite="mid:CAGatSHGuxARpSxiESd60Q_5AJbmbCDXuGjCQ6Yq8Cg+A5mL+jw@mail.gmail.com">
<div dir="ltr">
<div><br>
</div>
<div>-- <br>
<div dir="ltr" data-smartmail="gmail_signature">
<div dir="ltr">
<div>Regards,</div>
<div>Oleksandr Koval.<br>
</div>
</div>
</div>
</div>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
cfe-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>
<a class="moz-txt-link-freetext" href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a>
</pre>
</blockquote>
<p><br>
</p>
</body>
</html>