[PATCH] D114446: [clang] Fix wrong -Wunused-local-typedef warning if use is a dependent qialified identifier

Kristina Bessonova via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 2 06:43:04 PST 2021


krisb updated this revision to Diff 391312.
krisb added a comment.

Simplified diagnostic condition in TemplateDeclInstantiator::InstantiateTypedefNameDecl().


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114446/new/

https://reviews.llvm.org/D114446

Files:
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
  clang/test/SemaCXX/warn-unused-local-typedef.cpp


Index: clang/test/SemaCXX/warn-unused-local-typedef.cpp
===================================================================
--- clang/test/SemaCXX/warn-unused-local-typedef.cpp
+++ clang/test/SemaCXX/warn-unused-local-typedef.cpp
@@ -255,5 +255,20 @@
 }
 } // TypedefInLocalClassOfTemplateClassMember
 
+namespace TypedefInDependentQualifiedIdentifier {
+struct A { void f() const {} };
+
+template <typename T>
+void handler(const T &item) {
+	using a_type_t = A; // no-diag
+  using b_type_t = A; // expected-warning {{unused type alias 'b_type_t'}}
+	item.a_type_t::f();
+}
+
+void foo() {
+	handler(A());
+}
+} // TypedefInDependentQualifiedIdentifier
+
 // This should not disable any warnings:
 #pragma clang diagnostic ignored "-Wunused-local-typedef"
Index: clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
===================================================================
--- clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -955,6 +955,10 @@
   Typedef->setAccess(D->getAccess());
   Typedef->setReferenced(D->isReferenced());
 
+  // Diagnose unused local typedefs.
+  if (!Typedef->isInvalidDecl() && (!RD || RD->isLocalClass()))
+    SemaRef.DiagnoseUnusedDecl(Typedef);
+
   return Typedef;
 }
 
@@ -1907,8 +1911,6 @@
     LocalInstantiations.perform();
   }
 
-  SemaRef.DiagnoseUnusedNestedTypedefs(Record);
-
   if (IsInjectedClassName)
     assert(Record->isInjectedClassName() && "Broken injected-class-name");
 
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -1794,16 +1794,19 @@
 
   // Except for labels, we only care about unused decls that are local to
   // functions.
-  bool WithinFunction = D->getDeclContext()->isFunctionOrMethod();
-  if (const auto *R = dyn_cast<CXXRecordDecl>(D->getDeclContext()))
-    // For dependent types, the diagnostic is deferred.
-    WithinFunction =
-        WithinFunction || (R->isLocalClass() && !R->isDependentType());
+  auto *Context = D->getDeclContext();
+  bool WithinFunction = Context->isFunctionOrMethod();
+  if (const auto *R = dyn_cast<CXXRecordDecl>(Context))
+    WithinFunction = WithinFunction || R->isLocalClass();
   if (!WithinFunction)
     return false;
 
-  if (isa<TypedefNameDecl>(D))
+  if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
+    // Defer warnings for typedefs within a dependent context.
+    if (Context->isDependentContext())
+      return false;
     return true;
+  }
 
   // White-list anything that isn't a local variable.
   if (!isa<VarDecl>(D) || isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114446.391312.patch
Type: text/x-patch
Size: 2685 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211202/2f565e20/attachment-0001.bin>


More information about the cfe-commits mailing list