[clang] [clang][StaticAnalyzer] Add enclosing Decl information to bug reports of RawPtrRef(LocalVars|Member)Checker (PR #214102)

DonĂ¡t Nagy via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 7 04:27:55 PDT 2026


================
@@ -82,39 +82,15 @@ static std::string GetSignature(const FunctionDecl *Target) {
   return Signature;
 }
 
-static std::string GetEnclosingDeclContextSignature(const Decl *D) {
-  if (!D)
+static std::string GetEnclosingDeclContextSignature(const Decl *EnclosingDecl) {
+  if (!EnclosingDecl)
     return "";
 
-  if (const auto *ND = dyn_cast<NamedDecl>(D)) {
-    std::string DeclName;
-
-    switch (ND->getKind()) {
-    case Decl::Namespace:
-    case Decl::Record:
-    case Decl::CXXRecord:
-    case Decl::Enum:
-      DeclName = ND->getQualifiedNameAsString();
-      break;
-    case Decl::CXXConstructor:
-    case Decl::CXXDestructor:
-    case Decl::CXXConversion:
-    case Decl::CXXMethod:
-    case Decl::Function:
-      DeclName = GetSignature(dyn_cast_or_null<FunctionDecl>(ND));
-      break;
-    case Decl::ObjCMethod:
-      // ObjC Methods can not be overloaded, qualified name uniquely identifies
-      // the method.
-      DeclName = ND->getQualifiedNameAsString();
-      break;
-    default:
-      break;
-    }
-
-    return DeclName;
+  if (const auto *ND = dyn_cast<NamedDecl>(EnclosingDecl)) {
+    if (const auto *FD = dyn_cast<FunctionDecl>(EnclosingDecl))
+      return GetSignature(FD);
+    return ND->getQualifiedNameAsString();
----------------
NagyDonat wrote:

```suggestion
  if (const auto *ND = dyn_cast_or_null<NamedDecl>(EnclosingDecl)) {
    if (const auto *FD = dyn_cast<FunctionDecl>(ND)) {
      // To distinguish overloads we need to use the signature. 
      return GetSignature(FD);
    }
    return ND->getQualifiedNameAsString();
```
Several minor nitpicks:
- The early return can be eliminated if you use `dyn_cast_or_null`.
- I think it is more natural to cast `FD` from `ND`.
- Let's re-add the remark about overloads, given that it was the answer for https://github.com/llvm/llvm-project/pull/214102#discussion_r3735228151

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


More information about the cfe-commits mailing list