diff options
author | Mythreya Kuricheti <git@mythreya.dev> | 2025-08-19 22:24:04 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-08-20 01:24:04 -0400 |
commit | d0cde323c3e34c9c1fd348ebdbb212d6bba0f225 (patch) | |
tree | 4859c99a48518d15769ed2206692604c9779764f /clang/lib/Sema/SemaCodeComplete.cpp | |
parent | 32ba045bea95442e37302c17b344f7695b6520e7 (diff) | |
download | llvm-d0cde323c3e34c9c1fd348ebdbb212d6bba0f225.zip llvm-d0cde323c3e34c9c1fd348ebdbb212d6bba0f225.tar.gz llvm-d0cde323c3e34c9c1fd348ebdbb212d6bba0f225.tar.bz2 |
[clang][CodeComplete] Do not suggest unqualified members in explicit-object member functions (#153760)
Fixes https://github.com/llvm/llvm-project/issues/141291
Diffstat (limited to 'clang/lib/Sema/SemaCodeComplete.cpp')
-rw-r--r-- | clang/lib/Sema/SemaCodeComplete.cpp | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp index e4f2760..eb1bbec 100644 --- a/clang/lib/Sema/SemaCodeComplete.cpp +++ b/clang/lib/Sema/SemaCodeComplete.cpp @@ -197,6 +197,9 @@ private: /// Whether the \p ObjectTypeQualifiers field is active. bool HasObjectTypeQualifiers; + // Whether the member function is using an explicit object parameter + bool IsExplicitObjectMemberFunction; + /// The selector that we prefer. Selector PreferredSelector; @@ -218,8 +221,8 @@ public: LookupFilter Filter = nullptr) : SemaRef(SemaRef), Allocator(Allocator), CCTUInfo(CCTUInfo), Filter(Filter), AllowNestedNameSpecifiers(false), - HasObjectTypeQualifiers(false), CompletionContext(CompletionContext), - ObjCImplementation(nullptr) { + HasObjectTypeQualifiers(false), IsExplicitObjectMemberFunction(false), + CompletionContext(CompletionContext), ObjCImplementation(nullptr) { // If this is an Objective-C instance method definition, dig out the // corresponding implementation. switch (CompletionContext.getKind()) { @@ -275,6 +278,10 @@ public: HasObjectTypeQualifiers = true; } + void setExplicitObjectMemberFn(bool IsExplicitObjectFn) { + IsExplicitObjectMemberFunction = IsExplicitObjectFn; + } + /// Set the preferred selector. /// /// When an Objective-C method declaration result is added, and that @@ -1428,6 +1435,15 @@ void ResultBuilder::AddResult(Result R, DeclContext *CurContext, AdjustResultPriorityForDecl(R); + if (IsExplicitObjectMemberFunction && + R.Kind == CodeCompletionResult::RK_Declaration && + (isa<CXXMethodDecl>(R.Declaration) || isa<FieldDecl>(R.Declaration))) { + // If result is a member in the context of an explicit-object member + // function, drop it because it must be accessed through the object + // parameter + return; + } + if (HasObjectTypeQualifiers) if (const auto *Method = dyn_cast<CXXMethodDecl>(R.Declaration)) if (Method->isInstance()) { @@ -4636,12 +4652,19 @@ void SemaCodeCompletion::CodeCompleteOrdinaryName( break; } - // If we are in a C++ non-static member function, check the qualifiers on - // the member function to filter/prioritize the results list. auto ThisType = SemaRef.getCurrentThisType(); - if (!ThisType.isNull()) + if (ThisType.isNull()) { + // check if function scope is an explicit object function + if (auto *MethodDecl = llvm::dyn_cast_if_present<CXXMethodDecl>( + SemaRef.getCurFunctionDecl())) + Results.setExplicitObjectMemberFn( + MethodDecl->isExplicitObjectMemberFunction()); + } else { + // If we are in a C++ non-static member function, check the qualifiers on + // the member function to filter/prioritize the results list. Results.setObjectTypeQualifiers(ThisType->getPointeeType().getQualifiers(), VK_LValue); + } CodeCompletionDeclConsumer Consumer(Results, SemaRef.CurContext); SemaRef.LookupVisibleDecls(S, SemaRef.LookupOrdinaryName, Consumer, |