[clang] [ClangRepl] Type Directed Code Completion (PR #67349)

Vassil Vassilev via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 6 10:58:16 PST 2023


================
@@ -56,26 +69,151 @@ class ReplCompletionConsumer : public CodeCompleteConsumer {
   std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator;
   CodeCompletionTUInfo CCTUInfo;
   std::vector<std::string> &Results;
+  ReplCodeCompleter &CC;
+};
+
+/*
+  The abstract class CompletionContextHandler contains four interfaces, each of
+  which handles one type of completion result.
+
+  Its substract classes are used to create concrete handlers based on
+  CodeCompletionContext.
+ */
+class CompletionContextHandler {
+protected:
+  CodeCompletionContext CCC;
+  std::vector<std::string> &Results;
+
+public:
+  CompletionContextHandler(CodeCompletionContext CCC,
+                           std::vector<std::string> &Results)
+      : CCC(CCC), Results(Results) {}
+
+  // convert a Declaration completion result  to a completion string, and then store it in Results.
+  virtual void handleDeclaration(const CodeCompletionResult &Result) {}
+
+  // convert a Keyword completion result  to a completion string, and then store it in Results.
+  virtual void handleKeyword(const CodeCompletionResult &Result) {}
+
+  // convert a Pattern completion result  to a completion string, and then store it in Results.
+  virtual void handlePattern(const CodeCompletionResult &Result) {}
+
+  // convert a Macro completion result  to a completion string, and then store it in Results.
+  virtual void handleMacro(const CodeCompletionResult &Result) {}
+};
+
+class DotMemberAccessHandler : public CompletionContextHandler {
+public:
+  DotMemberAccessHandler(CodeCompletionContext CCC,
+                         std::vector<std::string> &Results)
+      : CompletionContextHandler(CCC, Results) {}
+  void handleDeclaration(const CodeCompletionResult &Result) override {
+    auto *ID = Result.Declaration->getIdentifier();
+    if (!ID)
+      return;
+    if (!isa<CXXMethodDecl>(Result.Declaration))
+      return;
+    const auto *Fun = cast<CXXMethodDecl>(Result.Declaration);
+    if (Fun->getParent()->getCanonicalDecl() ==
+        CCC.getBaseType()->getAsCXXRecordDecl()->getCanonicalDecl()) {
+      LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : "
+                              << ID->getName() << "\n");
+      Results.push_back(ID->getName().str());
+    }
+  }
+};
+
+class DefaultAccessHandler : public CompletionContextHandler {
+private:
+  Sema &S;
+
+public:
+  DefaultAccessHandler(Sema &S, CodeCompletionContext CCC,
+                       std::vector<std::string> &Results)
+      : CompletionContextHandler(CCC, Results), S(S) {}
+  void handleDeclaration(const CodeCompletionResult &Result) override {
+    auto PreferredType = CCC.getPreferredType();
+    if (PreferredType.isNull()) {
+      Results.push_back(Result.Declaration->getName().str());
+      return;
+    }
+
+    if (auto *VD = dyn_cast<VarDecl>(Result.Declaration)) {
+      auto ArgumentType = VD->getType();
+      if (PreferredType->isReferenceType()) {
+        QualType RT = PreferredType->castAs<ReferenceType>()->getPointeeType();
+        Sema::ReferenceConversions RefConv;
+        Sema::ReferenceCompareResult RefRelationship =
+            S.CompareReferenceRelationship(SourceLocation(), RT, ArgumentType,
+                                           &RefConv);
+        switch (RefRelationship) {
+        case Sema::Ref_Compatible:
+        case Sema::Ref_Related:
+          Results.push_back(VD->getName().str());
+          break;
+        case Sema::Ref_Incompatible:
+          break;
+        }
+      } else if (S.Context.hasSameType(ArgumentType, PreferredType)) {
+        Results.push_back(VD->getName().str());
+      }
+    }
+  }
+
+  void handleKeyword(const CodeCompletionResult &Result) override {
+    auto Prefix = S.getPreprocessor().getCodeCompletionFilter();
+    // add keyword to the completion results only if we are in a type-aware
+    // situation.
----------------
vgvassilev wrote:

```suggestion
    // Add keyword to the completion results only if we are in a type-aware
    // situation.
```

https://github.com/llvm/llvm-project/pull/67349


More information about the cfe-commits mailing list