[clang-tools-extra] Determine the access method to a referenced symbol. (PR #172462)

Dimitri Ratz via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 16 03:26:13 PST 2025


https://github.com/ratzdi created https://github.com/llvm/llvm-project/pull/172462

The access method helps to know in a quick way, what is happening with the referenced symbol somewhere in the code, i.e. is the referenced symbol used as a rvalue object to read a value from or as lvalue object to write some value into.

The basic access methods to a referenced symbol can be write and/or read.

>From f4f37212f33182f0f65a0f9ae0f4db58f94dedf4 Mon Sep 17 00:00:00 2001
From: Dimitri Ratz <dimitri.ratz at thinkdigital.cc>
Date: Wed, 10 Dec 2025 16:20:56 +0100
Subject: [PATCH] Determine the access method to a referenced symbol.

The access method helps to know in a quick way, what is happening with the referenced symbol somewhere in the code, i.e.
is the referenced symbol used as a rvalue object to read a value from or as lvalue object to write some value into.

The basic access methods to a referenced symbol can be write and/or read.
---
 clang-tools-extra/clangd/Protocol.cpp |   9 +-
 clang-tools-extra/clangd/Protocol.h   |  13 +++
 clang-tools-extra/clangd/XRefs.cpp    | 147 ++++++++++++++++++++++++--
 3 files changed, 158 insertions(+), 11 deletions(-)

diff --git a/clang-tools-extra/clangd/Protocol.cpp b/clang-tools-extra/clangd/Protocol.cpp
index 560b8e00ed377..6027e60609fe2 100644
--- a/clang-tools-extra/clangd/Protocol.cpp
+++ b/clang-tools-extra/clangd/Protocol.cpp
@@ -209,7 +209,7 @@ bool fromJSON(const llvm::json::Value &Params, ChangeAnnotation &R,
          O.map("needsConfirmation", R.needsConfirmation) &&
          O.mapOptional("description", R.description);
 }
-llvm::json::Value toJSON(const ChangeAnnotation & CA) {
+llvm::json::Value toJSON(const ChangeAnnotation &CA) {
   llvm::json::Object Result{{"label", CA.label}};
   if (CA.needsConfirmation)
     Result["needsConfirmation"] = *CA.needsConfirmation;
@@ -1478,6 +1478,10 @@ llvm::json::Value toJSON(SymbolTag Tag) {
   return llvm::json::Value(static_cast<int>(Tag));
 }
 
+llvm::json::Value toJSON(ReferenceTag Tag) {
+  return llvm::json::Value(static_cast<int>(Tag));
+}
+
 llvm::json::Value toJSON(const CallHierarchyItem &I) {
   llvm::json::Object Result{{"name", I.name},
                             {"kind", static_cast<int>(I.kind)},
@@ -1490,6 +1494,9 @@ llvm::json::Value toJSON(const CallHierarchyItem &I) {
     Result["detail"] = I.detail;
   if (!I.data.empty())
     Result["data"] = I.data;
+  if (!I.referenceTags.empty())
+    Result["referenceTags"] = I.referenceTags;
+  
   return std::move(Result);
 }
 
diff --git a/clang-tools-extra/clangd/Protocol.h b/clang-tools-extra/clangd/Protocol.h
index 2248572060431..edb28ef33d6a2 100644
--- a/clang-tools-extra/clangd/Protocol.h
+++ b/clang-tools-extra/clangd/Protocol.h
@@ -405,6 +405,13 @@ enum class SymbolKind {
   Operator = 25,
   TypeParameter = 26
 };
+
+/// Tags describing the reference kind.
+enum class ReferenceTag {
+  Read = 1,
+  Write = 2,
+};
+
 bool fromJSON(const llvm::json::Value &, SymbolKind &, llvm::json::Path);
 constexpr auto SymbolKindMin = static_cast<size_t>(SymbolKind::File);
 constexpr auto SymbolKindMax = static_cast<size_t>(SymbolKind::TypeParameter);
@@ -1513,6 +1520,9 @@ struct TypeHierarchyItem {
   /// The kind of this item.
   SymbolKind kind;
 
+  /// The symbol tags for this item.
+  std::vector<ReferenceTag> referenceTags;
+
   /// More detail for this item, e.g. the signature of a function.
   std::optional<std::string> detail;
 
@@ -1590,6 +1600,9 @@ struct CallHierarchyItem {
   /// Tags for this item.
   std::vector<SymbolTag> tags;
 
+  /// The tags describing reference kinds of this item.
+  std::vector<ReferenceTag> referenceTags;
+
   /// More detaill for this item, e.g. the signature of a function.
   std::string detail;
 
diff --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp
index ef45acf501612..fd4dfe95213a5 100644
--- a/clang-tools-extra/clangd/XRefs.cpp
+++ b/clang-tools-extra/clangd/XRefs.cpp
@@ -68,6 +68,7 @@
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/raw_ostream.h"
+#include "support/Logger.h"
 #include <algorithm>
 #include <optional>
 #include <string>
@@ -1749,6 +1750,130 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
   return OS;
 }
 
+// This would require a visitor pattern:
+class ParamUsageVisitor : public RecursiveASTVisitor<ParamUsageVisitor> {
+public:
+  bool HasWrite = false;
+  bool HasRead = false;
+  const ParmVarDecl *TargetParam;
+  
+  ParamUsageVisitor(const ParmVarDecl *P) : TargetParam(P) {}
+  
+  bool VisitUnaryOperator(UnaryOperator *UO) {
+    // Check for increment/decrement on parameter
+    if (UO->isIncrementDecrementOp()) {
+      if (auto *DRE = dyn_cast<DeclRefExpr>(UO->getSubExpr())) {
+        if (DRE->getDecl() == TargetParam) {
+          HasWrite = true;
+        }
+      }
+    }
+    return true;
+  }
+  
+  bool VisitBinaryOperator(BinaryOperator *BO) {
+    // Check for assignment to parameter
+    if (BO->isAssignmentOp()) {
+      if (auto *DRE = dyn_cast<DeclRefExpr>(BO->getLHS())) {
+        if (DRE->getDecl() == TargetParam) {
+          HasWrite = true;
+        }
+      }
+    }
+    // Any other use is at least a read
+    if (auto *DRE = dyn_cast<DeclRefExpr>(BO->getRHS())) {
+      if (DRE->getDecl() == TargetParam) {
+        HasRead = true;
+      }
+    }
+    return true;
+  }
+};
+
+static std::vector<ReferenceTag> analyseParameterUsage(const FunctionDecl *FD) {
+  std::vector<ReferenceTag> Result;
+  // This requires more sophisticated analysis - checking if param is modified
+  const Stmt *Body = FD->getBody();
+  if (!Body)
+    return Result; // No definition available
+
+  for (unsigned I = 0; I < FD->getNumParams(); ++I) {
+    const ParmVarDecl *Param = FD->getParamDecl(I);
+
+    // Check const qualifier
+    // QualType ParamType = Param->getType();
+    // bool IsReadOnly = ParamType.isConstQualified();
+
+    // For deeper analysis, you'd need to:
+    // 1. Walk the AST of the function body
+    // 2. Find all references to the parameter
+    // 3. Check if they appear on the left side of assignments (write)
+    //    or only on the right side (read)
+
+    ParamUsageVisitor Visitor(Param);
+    Visitor.TraverseStmt(const_cast<Stmt *>(Body));
+    if (Visitor.HasWrite)
+      Result.push_back(ReferenceTag::Write);
+    if (Visitor.HasRead)
+      Result.push_back(ReferenceTag::Read);
+  }
+  return Result;
+}
+
+template <typename HierarchyItem>
+static void determineParameterUsage(const NamedDecl &ND, HierarchyItem &HI) {
+  // Get parent context and check if it's a function parameter
+  const DeclContext *DC = ND.getDeclContext();
+  elog("determineParameterUsage: called for ND={0}", ND.getNameAsString());
+  if (const auto *TD = llvm::dyn_cast<TagDecl>(DC)) {
+    elog("determineParameterUsage: ND is inside a TagDecl: {0}", TD->getNameAsString());
+    // No parameter analysis for TagDecl parent contexts.
+  } else if (const auto *FD = llvm::dyn_cast<FunctionDecl>(DC)) {
+    elog("determineParameterUsage: ND is inside a FunctionDecl");
+    for (unsigned I = 0; I < FD->getNumParams(); ++I) {
+      if (FD->getParamDecl(I) == &ND) {
+        elog("determineParameterUsage: ND is the {0}-th parameter of function {1}", I, FD->getNameAsString());
+
+        const ParmVarDecl *Param = FD->getParamDecl(I);
+        QualType ParamType = Param->getType();
+
+        bool IsConst = false;
+        bool IsConstRef = false;
+        bool IsConstPtr = false;
+
+        // Check if const (read-only)
+        IsConst = ParamType.isConstQualified();
+        elog("determineParameterUsage: ParamType.isConstQualified() = {0}", IsConst);
+
+        // Check if it's a const reference
+        if (const auto *RT = ParamType->getAs<ReferenceType>()) {
+          IsConstRef = RT->getPointeeType().isConstQualified();
+          elog("determineParameterUsage: ParamType is ReferenceType, isConstQualified = {0}", IsConstRef);
+        }
+
+        // Check if it's a const pointer
+        if (const auto *PT = ParamType->getAs<PointerType>()) {
+          IsConstPtr = PT->getPointeeType().isConstQualified();
+          elog("determineParameterUsage: ParamType is PointerType, isConstQualified = {0}", IsConstPtr);
+        }
+
+        if (IsConst && IsConstRef && IsConstPtr) {
+          elog("determineParameterUsage: All const checks passed, marking as Read");
+          HI.referenceTags.push_back(ReferenceTag::Read);
+        } else {
+          elog("determineParameterUsage: Performing analyseParameterUsage");
+          HI.referenceTags = analyseParameterUsage(FD);
+        }
+
+        break;
+      }
+      elog("determineParameterUsage: ND is not parameter {0} of function {1}", I, FD->getNameAsString());
+    }
+  } else {
+    elog("determineParameterUsage: ND is not inside a FunctionDecl or TagDecl");
+  }
+}
+
 template <typename HierarchyItem>
 static std::optional<HierarchyItem>
 declToHierarchyItem(const NamedDecl &ND, llvm::StringRef TUPath) {
@@ -1790,6 +1915,8 @@ declToHierarchyItem(const NamedDecl &ND, llvm::StringRef TUPath) {
     HI.range = HI.selectionRange;
   }
 
+  determineParameterUsage(ND, HI);
+
   HI.uri = URIForFile::canonicalize(*FilePath, TUPath);
 
   return HI;
@@ -2097,15 +2224,15 @@ static QualType typeForNode(const ASTContext &Ctx, const HeuristicResolver *H,
   return QualType();
 }
 
-// Given a type targeted by the cursor, return one or more types that are more interesting
-// to target.
-static void unwrapFindType(
-    QualType T, const HeuristicResolver* H, llvm::SmallVector<QualType>& Out) {
+// Given a type targeted by the cursor, return one or more types that are more
+// interesting to target.
+static void unwrapFindType(QualType T, const HeuristicResolver *H,
+                           llvm::SmallVector<QualType> &Out) {
   if (T.isNull())
     return;
 
   // If there's a specific type alias, point at that rather than unwrapping.
-  if (const auto* TDT = T->getAs<TypedefType>())
+  if (const auto *TDT = T->getAs<TypedefType>())
     return Out.push_back(QualType(TDT, 0));
 
   // Pointers etc => pointee type.
@@ -2139,8 +2266,8 @@ static void unwrapFindType(
 }
 
 // Convenience overload, to allow calling this without the out-parameter
-static llvm::SmallVector<QualType> unwrapFindType(
-    QualType T, const HeuristicResolver* H) {
+static llvm::SmallVector<QualType> unwrapFindType(QualType T,
+                                                  const HeuristicResolver *H) {
   llvm::SmallVector<QualType> Result;
   unwrapFindType(T, H, Result);
   return Result;
@@ -2162,9 +2289,9 @@ std::vector<LocatedSymbol> findType(ParsedAST &AST, Position Pos,
     std::vector<LocatedSymbol> LocatedSymbols;
 
     // NOTE: unwrapFindType might return duplicates for something like
-    // unique_ptr<unique_ptr<T>>. Let's *not* remove them, because it gives you some
-    // information about the type you may have not known before
-    // (since unique_ptr<unique_ptr<T>> != unique_ptr<T>).
+    // unique_ptr<unique_ptr<T>>. Let's *not* remove them, because it gives you
+    // some information about the type you may have not known before (since
+    // unique_ptr<unique_ptr<T>> != unique_ptr<T>).
     for (const QualType &Type : unwrapFindType(
              typeForNode(AST.getASTContext(), AST.getHeuristicResolver(), N),
              AST.getHeuristicResolver()))



More information about the cfe-commits mailing list