[PATCH] D129973: [clang] Pass FoundDecl to DeclRefExpr creator for operator overloads

Danny Mösch via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 17 12:29:46 PDT 2022


SimplyDanny created this revision.
Herald added subscribers: usaxena95, kadircet.
Herald added a project: All.
SimplyDanny requested review of this revision.
Herald added subscribers: cfe-commits, ilya-biryukov.
Herald added projects: clang, clang-tools-extra.

Without the "found declaration" it is later not possible to know where the operator declaration
was brought into the scope calling it.

The initial motivation for this fix came from #55095. However, this also has an influence on
`clang -ast-dump` which now prints a `UsingShadow` attribute for operators only visible through
`using` statements. Also, clangd now correctly references the `using` statement instead of the
operator directly.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D129973

Files:
  clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.cpp
  clang/lib/Sema/SemaOverload.cpp
  clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp


Index: clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
===================================================================
--- clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -4675,6 +4675,9 @@
   EXPECT_TRUE(notMatches(
     "namespace a { void f(); } using a::f; void g() { a::f(); }",
     declRefExpr(throughUsingDecl(anything()))));
+  EXPECT_TRUE(matches(
+    "struct S {}; namespace a { int operator+(S s1, S s2) { return 0; }; } using a::operator+; int g() { S a, b; return a + b; }",
+    declRefExpr(throughUsingDecl(anything()))));
 }
 
 TEST(SingleDecl, IsSingleDecl) {
Index: clang/lib/Sema/SemaOverload.cpp
===================================================================
--- clang/lib/Sema/SemaOverload.cpp
+++ clang/lib/Sema/SemaOverload.cpp
@@ -63,8 +63,10 @@
   // being used.
   if (FoundDecl != Fn && S.DiagnoseUseOfDecl(Fn, Loc))
     return ExprError();
-  DeclRefExpr *DRE = new (S.Context)
-      DeclRefExpr(S.Context, Fn, false, Fn->getType(), VK_LValue, Loc, LocInfo);
+  DeclRefExpr *DRE =
+      DeclRefExpr::Create(S.Context, Fn->getQualifierLoc(), Loc, Fn, false,
+                          Loc, Fn->getType(),  VK_LValue,
+                          FoundDecl);
   if (HadMultipleCandidates)
     DRE->setHadMultipleCandidates(true);
 
Index: clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.cpp
@@ -210,3 +210,14 @@
 // We used to report Q unsued, because we only checked the first template
 // argument.
 Bar<int, Q> *bar;
+
+namespace internal { 
+  struct S {};
+  int operator+(S s1, S s2) { return 0; };
+} 
+
+// Make sure this statement is not reported as unused.
+using internal::operator+; 
+using internal::S;
+
+int j() { return S() + S(); }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129973.445343.patch
Type: text/x-patch
Size: 2007 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220717/b78d3390/attachment.bin>


More information about the cfe-commits mailing list