<div dir="ltr">(Oops, forgot there was this thread. Pasting my response to <span style="color:rgb(33,33,33)">r338255 here</span>).<div><br><div>> <span style="color:rgb(33,33,33)">The code seemed obviously wrong there (calling accessibility checking, passing a possibly unrelated class) and the tests didn't break after reverting it.</span></div><div><div class="inbox-inbox-uyb8Gf" style="color:rgb(33,33,33)"><div class="inbox-inbox-F3hlO"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div>It turns out that this was due to my lacking LIT test skill. LIT does partial string match... so "something" would match both "something" and "something (inaccessible)" XD </div><div>> I actually tried to pass the correct derived scope to CodeCompletionDeclConsumer on construction, it was not a lot of code and I think this should fix the problem you're describing.</div><div><div class="inbox-inbox-uyb8Gf"><div><div class="inbox-inbox-F3hlO"><div dir="ltr"><div>> I'll send a patch.</div></div></div></div></div></div><div>Any luck on this?</div></div></div></div></div></div></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Jul 31, 2018 at 12:24 PM Ilya Biryukov <<a href="mailto:ibiryukov@google.com">ibiryukov@google.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I actually tried to pass the correct derived scope to CodeCompletionDeclConsumer on construction, it was not a lot of code and I think this should fix the problem you're describing.<div>I'll send a patch.</div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Jul 31, 2018 at 11:33 AM Eric Liu <<a href="mailto:ioeric@google.com" target="_blank">ioeric@google.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Thanks a lot for looking into this! <div><br></div><div>You are right, the change in SemaAccess seemed to have done the job to fix the protected member bug in specific. I think I made the change in SemaCodeComplete before SemaAccess and didn't realized the previous one was unnecessary to fix the tests. I think the accessing context/naming class is still wrong though. Basically, we want the naming class to be the derived class instead of the base class if the access is from a derived class. Without this, accessibility check is relaxed, but it probably doesn't have very negative impact on code completion experience, so I think it's okay to revert the change in SemaCodeComplete. Thanks again!</div></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Jul 30, 2018 at 5:22 PM Ilya Biryukov <<a href="mailto:ibiryukov@google.com" target="_blank">ibiryukov@google.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Prtially reverted the commit in r338255 to fix a very frequent crash.<div>The code seemed obviously wrong there (calling accessibility checking, passing a possibly unrelated class) and the tests didn't break after reverting it.</div><div><br></div><div>Let me know if I missed anything.</div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Jul 19, 2018 at 3:37 PM Eric Liu via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@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">Author: ioeric<br>
Date: Thu Jul 19 06:32:00 2018<br>
New Revision: 337453<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=337453&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=337453&view=rev</a><br>
Log:<br>
[CodeComplete] Fix accessibilty of protected members from base class.<br>
<br>
Summary:<br>
Currently, protected members from base classes are marked as<br>
inaccessible when completing in derived class. This patch fixes the problem by<br>
setting the naming class correctly when looking up results in base class<br>
according to [11.2.p5].<br>
<br>
Reviewers: aaron.ballman, sammccall, rsmith<br>
<br>
Reviewed By: aaron.ballman<br>
<br>
Subscribers: cfe-commits<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D49421" rel="noreferrer" target="_blank">https://reviews.llvm.org/D49421</a><br>
<br>
Modified:<br>
cfe/trunk/lib/Sema/SemaAccess.cpp<br>
cfe/trunk/lib/Sema/SemaCodeComplete.cpp<br>
cfe/trunk/test/Index/complete-access-checks.cpp<br>
<br>
Modified: cfe/trunk/lib/Sema/SemaAccess.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaAccess.cpp?rev=337453&r1=337452&r2=337453&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaAccess.cpp?rev=337453&r1=337452&r2=337453&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Sema/SemaAccess.cpp (original)<br>
+++ cfe/trunk/lib/Sema/SemaAccess.cpp Thu Jul 19 06:32:00 2018<br>
@@ -11,6 +11,7 @@<br>
//<br>
//===----------------------------------------------------------------------===//<br>
<br>
+#include "clang/Basic/Specifiers.h"<br>
#include "clang/Sema/SemaInternal.h"<br>
#include "clang/AST/ASTContext.h"<br>
#include "clang/AST/CXXInheritance.h"<br>
@@ -1856,29 +1857,31 @@ void Sema::CheckLookupAccess(const Looku<br>
}<br>
}<br>
<br>
-/// Checks access to Decl from the given class. The check will take access<br>
+/// Checks access to Target from the given class. The check will take access<br>
/// specifiers into account, but no member access expressions and such.<br>
///<br>
-/// \param Decl the declaration to check if it can be accessed<br>
+/// \param Target the declaration to check if it can be accessed<br>
/// \param Ctx the class/context from which to start the search<br>
-/// \return true if the Decl is accessible from the Class, false otherwise.<br>
-bool Sema::IsSimplyAccessible(NamedDecl *Decl, DeclContext *Ctx) {<br>
+/// \return true if the Target is accessible from the Class, false otherwise.<br>
+bool Sema::IsSimplyAccessible(NamedDecl *Target, DeclContext *Ctx) {<br>
if (CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(Ctx)) {<br>
- if (!Decl->isCXXClassMember())<br>
+ if (!Target->isCXXClassMember())<br>
return true;<br>
<br>
+ if (Target->getAccess() == AS_public)<br>
+ return true;<br>
QualType qType = Class->getTypeForDecl()->getCanonicalTypeInternal();<br>
+ // The unprivileged access is AS_none as we don't know how the member was<br>
+ // accessed, which is described by the access in DeclAccessPair.<br>
+ // `IsAccessible` will examine the actual access of Target (i.e.<br>
+ // Decl->getAccess()) when calculating the access.<br>
AccessTarget Entity(Context, AccessedEntity::Member, Class,<br>
- DeclAccessPair::make(Decl, Decl->getAccess()),<br>
- qType);<br>
- if (Entity.getAccess() == AS_public)<br>
- return true;<br>
-<br>
+ DeclAccessPair::make(Target, AS_none), qType);<br>
EffectiveContext EC(CurContext);<br>
return ::IsAccessible(*this, EC, Entity) != ::AR_inaccessible;<br>
}<br>
- <br>
- if (ObjCIvarDecl *Ivar = dyn_cast<ObjCIvarDecl>(Decl)) {<br>
+<br>
+ if (ObjCIvarDecl *Ivar = dyn_cast<ObjCIvarDecl>(Target)) {<br>
// @public and @package ivars are always accessible.<br>
if (Ivar->getCanonicalAccessControl() == ObjCIvarDecl::Public ||<br>
Ivar->getCanonicalAccessControl() == ObjCIvarDecl::Package)<br>
<br>
Modified: cfe/trunk/lib/Sema/SemaCodeComplete.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCodeComplete.cpp?rev=337453&r1=337452&r2=337453&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCodeComplete.cpp?rev=337453&r1=337452&r2=337453&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Sema/SemaCodeComplete.cpp (original)<br>
+++ cfe/trunk/lib/Sema/SemaCodeComplete.cpp Thu Jul 19 06:32:00 2018<br>
@@ -1303,8 +1303,33 @@ namespace {<br>
void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,<br>
bool InBaseClass) override {<br>
bool Accessible = true;<br>
- if (Ctx)<br>
- Accessible = Results.getSema().IsSimplyAccessible(ND, Ctx);<br>
+ if (Ctx) {<br>
+ DeclContext *AccessingCtx = Ctx;<br>
+ // If ND comes from a base class, set the naming class back to the<br>
+ // derived class if the search starts from the derived class (i.e.<br>
+ // InBaseClass is true).<br>
+ //<br>
+ // Example:<br>
+ // class B { protected: int X; }<br>
+ // class D : public B { void f(); }<br>
+ // void D::f() { this->^; }<br>
+ // The completion after "this->" will have `InBaseClass` set to true and<br>
+ // `Ctx` set to "B", when looking up in `B`. We need to set the actual<br>
+ // accessing context (i.e. naming class) to "D" so that access can be<br>
+ // calculated correctly.<br>
+ if (InBaseClass && isa<CXXRecordDecl>(Ctx)) {<br>
+ CXXRecordDecl *RC = nullptr;<br>
+ // Get the enclosing record.<br>
+ for (DeclContext *DC = CurContext; !DC->isFileContext();<br>
+ DC = DC->getParent()) {<br>
+ if ((RC = dyn_cast<CXXRecordDecl>(DC)))<br>
+ break;<br>
+ }<br>
+ if (RC)<br>
+ AccessingCtx = RC;<br>
+ }<br>
+ Accessible = Results.getSema().IsSimplyAccessible(ND, AccessingCtx);<br>
+ }<br>
<br>
ResultBuilder::Result Result(ND, Results.getBasePriority(ND), nullptr,<br>
false, Accessible, FixIts);<br>
<br>
Modified: cfe/trunk/test/Index/complete-access-checks.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/complete-access-checks.cpp?rev=337453&r1=337452&r2=337453&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/complete-access-checks.cpp?rev=337453&r1=337452&r2=337453&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/test/Index/complete-access-checks.cpp (original)<br>
+++ cfe/trunk/test/Index/complete-access-checks.cpp Thu Jul 19 06:32:00 2018<br>
@@ -36,10 +36,10 @@ void Y::doSomething() {<br>
<br>
// CHECK-SUPER-ACCESS: CXXMethod:{ResultType void}{TypedText doSomething}{LeftParen (}{RightParen )} (34)<br>
// CHECK-SUPER-ACCESS: CXXMethod:{ResultType void}{Informative X::}{TypedText func1}{LeftParen (}{RightParen )} (36)<br>
-// CHECK-SUPER-ACCESS: CXXMethod:{ResultType void}{Informative X::}{TypedText func2}{LeftParen (}{RightParen )} (36) (inaccessible)<br>
+// CHECK-SUPER-ACCESS: CXXMethod:{ResultType void}{Informative X::}{TypedText func2}{LeftParen (}{RightParen )} (36)<br>
// CHECK-SUPER-ACCESS: CXXMethod:{ResultType void}{Informative X::}{TypedText func3}{LeftParen (}{RightParen )} (36) (inaccessible)<br>
// CHECK-SUPER-ACCESS: FieldDecl:{ResultType int}{Informative X::}{TypedText member1} (37)<br>
-// CHECK-SUPER-ACCESS: FieldDecl:{ResultType int}{Informative X::}{TypedText member2} (37) (inaccessible)<br>
+// CHECK-SUPER-ACCESS: FieldDecl:{ResultType int}{Informative X::}{TypedText member2} (37)<br>
// CHECK-SUPER-ACCESS: FieldDecl:{ResultType int}{Informative X::}{TypedText member3} (37) (inaccessible)<br>
// CHECK-SUPER-ACCESS: CXXMethod:{ResultType Y &}{TypedText operator=}{LeftParen (}{Placeholder const Y &}{RightParen )} (79)<br>
// CHECK-SUPER-ACCESS: CXXMethod:{ResultType X &}{Text X::}{TypedText operator=}{LeftParen (}{Placeholder const X &}{RightParen )} (81)<br>
@@ -87,3 +87,26 @@ void f(P x, Q y) {<br>
// CHECK-USING-ACCESSIBLE: ClassDecl:{TypedText Q}{Text ::} (75)<br>
// CHECK-USING-ACCESSIBLE: CXXDestructor:{ResultType void}{Informative P::}{TypedText ~P}{LeftParen (}{RightParen )} (81)<br>
// CHECK-USING-ACCESSIBLE: CXXDestructor:{ResultType void}{TypedText ~Q}{LeftParen (}{RightParen )} (79)<br>
+<br>
+class B {<br>
+protected:<br>
+ int member;<br>
+};<br>
+<br>
+class C : private B {};<br>
+<br>
+<br>
+class D : public C {<br>
+ public:<br>
+ void f(::B *b);<br>
+};<br>
+<br>
+void D::f(::B *that) {<br>
+ // RUN: c-index-test -code-completion-at=%s:106:9 %s | FileCheck -check-prefix=CHECK-PRIVATE-SUPER-THIS %s<br>
+ this->;<br>
+// CHECK-PRIVATE-SUPER-THIS: FieldDecl:{ResultType int}{Informative B::}{TypedText member} (37) (inaccessible)<br>
+<br>
+ // RUN: c-index-test -code-completion-at=%s:110:9 %s | FileCheck -check-prefix=CHECK-PRIVATE-SUPER-THAT %s<br>
+ that->;<br>
+// CHECK-PRIVATE-SUPER-THAT: FieldDecl:{ResultType int}{TypedText member} (35) (inaccessible)<br>
+}<br>
<br>
<br>
_______________________________________________<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="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="m_2032492813517478438m_7051160413745528565m_6089325753832860920gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>Regards,</div><div>Ilya Biryukov</div></div></div></div></div>
</blockquote></div>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="m_2032492813517478438gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>Regards,</div><div>Ilya Biryukov</div></div></div></div></div>
</blockquote></div>