[clang-tools-extra] [clangd] Support go-to-definition on type hints. The core part (PR #86629)

Nathan Ridge via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 18 21:11:29 PDT 2024


================
@@ -372,6 +374,292 @@ maybeDropCxxExplicitObjectParameters(ArrayRef<const ParmVarDecl *> Params) {
   return Params;
 }
 
+class TypeInlayHintLabelPartBuilder
+    : public TypeVisitor<TypeInlayHintLabelPartBuilder> {
+  QualType CurrentType;
+  NestedNameSpecifier *CurrentNestedNameSpecifier;
+  ASTContext &Context;
+  StringRef MainFilePath;
+  const PrintingPolicy &PP;
+  SourceManager &SM;
+  std::vector<InlayHintLabelPart> &LabelChunks;
+
+  struct CurrentTypeRAII {
+    TypeInlayHintLabelPartBuilder &Builder;
+    QualType PreviousType;
+    NestedNameSpecifier *PreviousNestedNameSpecifier;
+    CurrentTypeRAII(TypeInlayHintLabelPartBuilder &Builder, QualType New,
+                    NestedNameSpecifier *NNS = nullptr)
+        : Builder(Builder), PreviousType(Builder.CurrentType),
+          PreviousNestedNameSpecifier(Builder.CurrentNestedNameSpecifier) {
+      Builder.CurrentType = New;
+      if (NNS)
+        Builder.CurrentNestedNameSpecifier = NNS;
+    }
+    ~CurrentTypeRAII() {
+      Builder.CurrentType = PreviousType;
+      Builder.CurrentNestedNameSpecifier = PreviousNestedNameSpecifier;
+    }
+  };
+
+  void addLabel(llvm::function_ref<void(llvm::raw_ostream &)> NamePrinter,
+                llvm::function_ref<SourceLocation()> SourceLocationGetter) {
----------------
HighCommander4 wrote:

Since this function calls `SourceLocationGetter` unconditionally, it's simpler to just have it take a `SourceLocation`.

Call sites like `VisitUsingType` can use an immediately-invoked lambda (`[&] { ... } ()`) if desired.

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


More information about the cfe-commits mailing list