[PATCH] Added code-completion results to override virtual methods from base class
Sergey Shambir
sergey.shambir.auto at gmail.com
Thu Apr 4 07:03:06 PDT 2013
Hi rsmith,
Inside C++ class definition, QtCreator adds virtual methods from base class to completion list. The patch ports such feature to clang.
http://llvm-reviews.chandlerc.com/D622
Files:
lib/Sema/SemaCodeComplete.cpp
include/clang/Sema/CodeCompleteConsumer.h
Index: lib/Sema/SemaCodeComplete.cpp
===================================================================
--- lib/Sema/SemaCodeComplete.cpp
+++ lib/Sema/SemaCodeComplete.cpp
@@ -1259,12 +1259,55 @@
bool Accessible = true;
if (Ctx)
Accessible = Results.getSema().IsSimplyAccessible(ND, Ctx);
-
ResultBuilder::Result Result(ND, Results.getBasePriority(ND), 0, false,
Accessible);
Results.AddResult(Result, CurContext, Hiding, InBaseClass);
}
};
+
+ /// \brief Visible declaration consumer that adds a code-completion result
+ /// for each C++ non-final virtual method.
+ /// Works in two passes: 1. ban unsuitable methods and 2. collect suitable
+ class OverridableMethodDeclConsumer : public VisibleDeclConsumer {
+ ResultBuilder &Results;
+ DeclContext *CurContext;
+ llvm::StringSet<> ConsumedMethods;
+ bool IsCollectPass;
+
+ public:
+ OverridableMethodDeclConsumer(ResultBuilder &Results, DeclContext *CurContext)
+ : Results(Results), CurContext(CurContext), IsCollectPass(false) { }
+
+ void StartCollectPass()
+ {
+ IsCollectPass = true;
+ }
+
+ virtual void FoundDecl(NamedDecl *ND, NamedDecl *,
+ DeclContext *Ctx, bool InBaseClass) {
+ const CXXMethodDecl *MD = dyn_cast<const CXXMethodDecl>(ND);
+ if (!MD || isa<CXXDestructorDecl>(ND) || !MD->isVirtual())
+ return;
+
+ if (!IsCollectPass) {
+ if (MD->getAttr<FinalAttr>() || !InBaseClass)
+ ConsumedMethods.insert(MD->getName());
+ return;
+ }
+
+ if (0 == ConsumedMethods.count(ND->getName())) {
+ ConsumedMethods.insert(MD->getName());
+ unsigned priority = CCP_MemberDeclaration;
+ if (MD->isPure())
+ priority /= CCF_OverridePureVirtual;
+ bool Accessible = true;
+ if (Ctx)
+ Accessible = Results.getSema().IsSimplyAccessible(ND, Ctx);
+ ResultBuilder::Result Result(ND, priority, 0, false, Accessible);
+ Results.AddResult(Result);
+ }
+ }
+ };
}
/// \brief Add type specifiers for the current language as keyword results.
@@ -3227,7 +3270,6 @@
// only allowed where we can have an expression.
switch (CompletionContext) {
case PCC_Namespace:
- case PCC_Class:
case PCC_ObjCInterface:
case PCC_ObjCImplementation:
case PCC_ObjCInstanceVariableList:
@@ -3238,6 +3280,16 @@
Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName);
break;
+ case PCC_Class: {
+ // Collect non-final virtual methods from base classes
+ OverridableMethodDeclConsumer Consumer(Results, CurContext);
+ LookupVisibleDecls(S, LookupMemberName, Consumer, false);
+ Consumer.StartCollectPass();
+ LookupVisibleDecls(S, LookupMemberName, Consumer, false);
+ Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName);
+ }
+ break;
+
case PCC_Statement:
case PCC_ParenthesizedExpression:
case PCC_Expression:
Index: include/clang/Sema/CodeCompleteConsumer.h
===================================================================
--- include/clang/Sema/CodeCompleteConsumer.h
+++ include/clang/Sema/CodeCompleteConsumer.h
@@ -99,7 +99,10 @@
/// \brief Divide by this factor when a code-completion result's type is
/// similar to the type we expect (e.g., both arithmetic types, both
/// Objective-C object pointer types).
- CCF_SimilarTypeMatch = 2
+ CCF_SimilarTypeMatch = 2,
+ /// \brief Divide by this factor when a code-completion will override
+ /// pure virtual method rather than just virtual
+ CCF_OverridePureVirtual = 2
};
/// \brief A simplified classification of types used when determining
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D622.1.patch
Type: text/x-patch
Size: 3731 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130404/493ef834/attachment.bin>
More information about the cfe-commits
mailing list