[PATCH] D154853: [clangd][c++20]Consider the constraint of a constrained auto in FindTarget.

Jens Massberg via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 10 08:29:04 PDT 2023


massberg created this revision.
massberg added a reviewer: sammccall.
Herald added subscribers: kadircet, arphaman.
Herald added a project: All.
massberg requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

This enables support of hover on concepts with auto types.

I'm not 100% sure if this is the correct fix, but it works for
the exmaples and in vscode.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154853

Files:
  clang-tools-extra/clangd/FindTarget.cpp
  clang-tools-extra/clangd/unittests/FindTargetTests.cpp
  clang-tools-extra/clangd/unittests/HoverTests.cpp


Index: clang-tools-extra/clangd/unittests/HoverTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -487,6 +487,46 @@
          HI.Kind = index::SymbolKind::TypeAlias;
          HI.Definition = "int";
        }},
+      // constraint of constrained auto
+      {R"cpp(
+        template <class T> concept Fooable = true;
+        [[Foo^able]] auto x = 1;
+        )cpp",
+       [](HoverInfo &HI) {
+         HI.NamespaceScope = "";
+         HI.Name = "Fooable";
+         HI.Kind = index::SymbolKind::Concept;
+         HI.Definition = "template <class T>\n"
+                         "concept Fooable = true";
+       }},
+      {R"cpp(
+        template <class T> concept Fooable = true;
+        template<[[Fooa^ble]] auto x> void Foo() {}
+        )cpp",
+       [](HoverInfo &HI) {
+         HI.NamespaceScope = "";
+         HI.Name = "Fooable";
+         HI.Kind = index::SymbolKind::Concept;
+         HI.Definition = "template <class T>\n"
+                         "concept Fooable = true";
+       }},
+      // concept
+      {R"cpp(
+        template <typename T>
+        concept Fooable = requires (T t) { t.foo(); };
+
+        template <typename T> requires [[Fo^oable]]<T>
+        void bar(T t) {
+          t.foo();
+        }
+        )cpp",
+       [](HoverInfo &HI) {
+         HI.NamespaceScope = "";
+         HI.Name = "Fooable";
+         HI.Kind = index::SymbolKind::Concept;
+         HI.Definition = "template <typename T>\n"
+                         "concept Fooable = requires(T t) { t.foo(); }";
+       }},
       // auto on lambda
       {R"cpp(
         void foo() {
Index: clang-tools-extra/clangd/unittests/FindTargetTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/FindTargetTests.cpp
+++ clang-tools-extra/clangd/unittests/FindTargetTests.cpp
@@ -572,6 +572,27 @@
   )cpp";
   EXPECT_DECLS("ConceptSpecializationExpr",
                {"template <typename T, typename U> concept Fooable = true"});
+
+  // Constrained auto
+  Code = R"cpp(
+    template <typename T>
+    concept Fooable = true;
+
+    [[Fooable]] auto i = 42;
+  )cpp";
+  EXPECT_DECLS(
+      "AutoTypeLoc",
+      {"template <typename T> concept Fooable = true"});
+
+  Code = R"cpp(
+    template <typename T>
+    concept Fooable = true;
+
+    template<[[Fooable]] auto x> void Foo() {}
+  )cpp";
+  EXPECT_DECLS(
+      "AutoTypeLoc",
+      {"template <typename T> concept Fooable = true"});
 }
 
 TEST_F(TargetDeclTest, Coroutine) {
Index: clang-tools-extra/clangd/FindTarget.cpp
===================================================================
--- clang-tools-extra/clangd/FindTarget.cpp
+++ clang-tools-extra/clangd/FindTarget.cpp
@@ -471,6 +471,12 @@
       void VisitObjCInterfaceType(const ObjCInterfaceType *OIT) {
         Outer.add(OIT->getDecl(), Flags);
       }
+      void VisitAutoType(const AutoType *T) {
+        if (T->isConstrained()) {
+          Outer.add(T->getTypeConstraintConcept(), Flags);
+        }
+        TypeVisitor<Visitor>::VisitAutoType(T);
+      }
     };
     Visitor(*this, Flags).Visit(T.getTypePtr());
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154853.538661.patch
Type: text/x-patch
Size: 3273 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230710/614aeb86/attachment-0001.bin>


More information about the cfe-commits mailing list