Skip to content

Commit

Permalink
[CodeComplete] Fix accessibility of protected members when accessing …
Browse files Browse the repository at this point in the history
…members implicitly.

Reviewers: ilya-biryukov

Subscribers: arphaman, cfe-commits

Differential Revision: https://reviews.llvm.org/D53369

llvm-svn: 344889
  • Loading branch information
Eric Liu committed Oct 22, 2018
1 parent ea65647 commit 2bfd3f5
Showing 2 changed files with 27 additions and 9 deletions.
15 changes: 11 additions & 4 deletions clang/lib/Sema/SemaCodeComplete.cpp
Original file line number Diff line number Diff line change
@@ -3686,13 +3686,20 @@ void Sema::CodeCompleteOrdinaryName(Scope *S,
}

// If we are in a C++ non-static member function, check the qualifiers on
// the member function to filter/prioritize the results list.
if (CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext))
if (CurMethod->isInstance())
// the member function to filter/prioritize the results list and set the
// context to the record context so that accessibility check in base class
// works correctly.
RecordDecl *MemberCompletionRecord = nullptr;
if (CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext)) {
if (CurMethod->isInstance()) {
Results.setObjectTypeQualifiers(
Qualifiers::fromCVRMask(CurMethod->getTypeQualifiers()));
MemberCompletionRecord = CurMethod->getParent();
}
}

CodeCompletionDeclConsumer Consumer(Results, CurContext);
CodeCompletionDeclConsumer Consumer(Results, CurContext, /*FixIts=*/{},
MemberCompletionRecord);
LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
CodeCompleter->includeGlobals(),
CodeCompleter->loadExternal());
21 changes: 16 additions & 5 deletions clang/test/Index/complete-access-checks.cpp
Original file line number Diff line number Diff line change
@@ -29,8 +29,11 @@ void Y::doSomething() {
// RUN: c-index-test -code-completion-at=%s:30:9 %s | FileCheck -check-prefix=CHECK-SUPER-ACCESS %s
this->;

// RUN: c-index-test -code-completion-at=%s:33:3 %s | FileCheck -check-prefix=CHECK-SUPER-ACCESS-IMPLICIT %s


Z that;
// RUN: c-index-test -code-completion-at=%s:34:8 %s | FileCheck -check-prefix=CHECK-ACCESS %s
// RUN: c-index-test -code-completion-at=%s:37:8 %s | FileCheck -check-prefix=CHECK-ACCESS %s
that.
}

@@ -48,6 +51,14 @@ void Y::doSomething() {
// CHECK-SUPER-ACCESS: CXXDestructor:{ResultType void}{Informative X::}{TypedText ~X}{LeftParen (}{RightParen )} (81)
// CHECK-SUPER-ACCESS: CXXDestructor:{ResultType void}{TypedText ~Y}{LeftParen (}{RightParen )} (79)

// CHECK-SUPER-ACCESS-IMPLICIT: CXXMethod:{ResultType void}{TypedText doSomething}{LeftParen (}{RightParen )} (34)
// CHECK-SUPER-ACCESS-IMPLICIT: CXXMethod:{ResultType void}{TypedText func1}{LeftParen (}{RightParen )} (36)
// CHECK-SUPER-ACCESS-IMPLICIT: CXXMethod:{ResultType void}{TypedText func2}{LeftParen (}{RightParen )} (36){{$}}
// CHECK-SUPER-ACCESS-IMPLICIT: CXXMethod:{ResultType void}{TypedText func3}{LeftParen (}{RightParen )} (36) (inaccessible)
// CHECK-SUPER-ACCESS-IMPLICIT: FieldDecl:{ResultType int}{TypedText member1} (37)
// CHECK-SUPER-ACCESS-IMPLICIT: FieldDecl:{ResultType int}{TypedText member2} (37){{$}}
// CHECK-SUPER-ACCESS-IMPLICIT: FieldDecl:{ResultType int}{TypedText member3} (37) (inaccessible)

// CHECK-ACCESS: CXXMethod:{ResultType void}{TypedText func1}{LeftParen (}{RightParen )} (34)
// CHECK-ACCESS: CXXMethod:{ResultType void}{TypedText func2}{LeftParen (}{RightParen )} (34) (inaccessible)
// CHECK-ACCESS: CXXMethod:{ResultType void}{TypedText func3}{LeftParen (}{RightParen )} (34) (inaccessible)
@@ -69,9 +80,9 @@ class Q : public P {
};

void f(P x, Q y) {
// RUN: c-index-test -code-completion-at=%s:73:5 %s | FileCheck -check-prefix=CHECK-USING-INACCESSIBLE %s
// RUN: c-index-test -code-completion-at=%s:84:5 %s | FileCheck -check-prefix=CHECK-USING-INACCESSIBLE %s
x.; // member is inaccessible
// RUN: c-index-test -code-completion-at=%s:75:5 %s | FileCheck -check-prefix=CHECK-USING-ACCESSIBLE %s
// RUN: c-index-test -code-completion-at=%s:86:5 %s | FileCheck -check-prefix=CHECK-USING-ACCESSIBLE %s
y.; // member is accessible
}

@@ -102,11 +113,11 @@ class D : public C {
};

void D::f(::B *that) {
// RUN: c-index-test -code-completion-at=%s:106:9 %s | FileCheck -check-prefix=CHECK-PRIVATE-SUPER-THIS %s
// RUN: c-index-test -code-completion-at=%s:117:9 %s | FileCheck -check-prefix=CHECK-PRIVATE-SUPER-THIS %s
this->;
// CHECK-PRIVATE-SUPER-THIS: FieldDecl:{ResultType int}{Informative B::}{TypedText member} (37) (inaccessible)

// RUN: c-index-test -code-completion-at=%s:110:9 %s | FileCheck -check-prefix=CHECK-PRIVATE-SUPER-THAT %s
// RUN: c-index-test -code-completion-at=%s:121:9 %s | FileCheck -check-prefix=CHECK-PRIVATE-SUPER-THAT %s
that->;
// CHECK-PRIVATE-SUPER-THAT: FieldDecl:{ResultType int}{TypedText member} (35) (inaccessible)
}

0 comments on commit 2bfd3f5

Please sign in to comment.