Skip to content

Commit 28fa1fc

Browse files
committed
Revert "[clang] Fix missing diagnostic of declaration use when accessing TypeDecls through typename access (#129681)"
This caused incorrect -Wunguarded-availability warnings. See comment on the pull request. > We were missing a call to DiagnoseUseOfDecl when performing typename > access. > > This refactors the code so that TypeDecl lookups funnel through a helper > which performs all the necessary checks, removing some related > duplication on the way. > > Fixes #58547 > > Differential Revision: https://reviews.llvm.org/D136533 This reverts commit 4c4fd6b.
1 parent 848ba38 commit 28fa1fc

File tree

5 files changed

+30
-60
lines changed

5 files changed

+30
-60
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ Potentially Breaking Changes
3535
============================
3636

3737
- The Objective-C ARC migrator (ARCMigrate) has been removed.
38-
- Fix missing diagnostics for uses of declarations when performing typename access,
39-
such as when performing member access on a '[[deprecated]]' type alias.
40-
(#GH58547)
4138

4239
C/C++ Language Potentially Breaking Changes
4340
-------------------------------------------

clang/include/clang/Sema/Sema.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3168,13 +3168,6 @@ class Sema final : public SemaBase {
31683168

31693169
DeclGroupPtrTy ConvertDeclToDeclGroup(Decl *Ptr, Decl *OwnedType = nullptr);
31703170

3171-
enum class DiagCtorKind { None, Implicit, Typename };
3172-
/// Returns the TypeDeclType for the given type declaration,
3173-
/// as ASTContext::getTypeDeclType would, but
3174-
/// performs the required semantic checks for name lookup of said entity.
3175-
QualType getTypeDeclType(DeclContext *LookupCtx, DiagCtorKind DCK,
3176-
TypeDecl *TD, SourceLocation NameLoc);
3177-
31783171
/// If the identifier refers to a type name within this scope,
31793172
/// return the declaration of that type.
31803173
///

clang/lib/Sema/SemaDecl.cpp

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -139,26 +139,6 @@ class TypeNameValidatorCCC final : public CorrectionCandidateCallback {
139139

140140
} // end anonymous namespace
141141

142-
QualType Sema::getTypeDeclType(DeclContext *LookupCtx, DiagCtorKind DCK,
143-
TypeDecl *TD, SourceLocation NameLoc) {
144-
auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(LookupCtx);
145-
auto *FoundRD = dyn_cast<CXXRecordDecl>(TD);
146-
if (DCK != DiagCtorKind::None && LookupRD && FoundRD &&
147-
FoundRD->isInjectedClassName() &&
148-
declaresSameEntity(LookupRD, cast<Decl>(FoundRD->getParent()))) {
149-
Diag(NameLoc,
150-
DCK == DiagCtorKind::Typename
151-
? diag::ext_out_of_line_qualified_id_type_names_constructor
152-
: diag::err_out_of_line_qualified_id_type_names_constructor)
153-
<< TD->getIdentifier() << /*Type=*/1
154-
<< 0 /*if any keyword was present, it was 'typename'*/;
155-
}
156-
157-
DiagnoseUseOfDecl(TD, NameLoc);
158-
MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false);
159-
return Context.getTypeDeclType(TD);
160-
}
161-
162142
namespace {
163143
enum class UnqualifiedTypeNameLookupResult {
164144
NotFound,
@@ -315,11 +295,10 @@ ParsedType Sema::getTypeName(const IdentifierInfo &II, SourceLocation NameLoc,
315295
bool IsClassTemplateDeductionContext,
316296
ImplicitTypenameContext AllowImplicitTypename,
317297
IdentifierInfo **CorrectedII) {
318-
bool IsImplicitTypename = !isClassName && !IsCtorOrDtorName;
319298
// FIXME: Consider allowing this outside C++1z mode as an extension.
320299
bool AllowDeducedTemplate = IsClassTemplateDeductionContext &&
321-
getLangOpts().CPlusPlus17 && IsImplicitTypename &&
322-
!HasTrailingDot;
300+
getLangOpts().CPlusPlus17 && !IsCtorOrDtorName &&
301+
!isClassName && !HasTrailingDot;
323302

324303
// Determine where we will perform name lookup.
325304
DeclContext *LookupCtx = nullptr;
@@ -343,9 +322,11 @@ ParsedType Sema::getTypeName(const IdentifierInfo &II, SourceLocation NameLoc,
343322
// refer to a member of an unknown specialization.
344323
// In C++2a, in several contexts a 'typename' is not required. Also
345324
// allow this as an extension.
325+
if (AllowImplicitTypename == ImplicitTypenameContext::No &&
326+
!isClassName && !IsCtorOrDtorName)
327+
return nullptr;
328+
bool IsImplicitTypename = !isClassName && !IsCtorOrDtorName;
346329
if (IsImplicitTypename) {
347-
if (AllowImplicitTypename == ImplicitTypenameContext::No)
348-
return nullptr;
349330
SourceLocation QualifiedLoc = SS->getRange().getBegin();
350331
if (getLangOpts().CPlusPlus20)
351332
Diag(QualifiedLoc, diag::warn_cxx17_compat_implicit_typename);
@@ -534,10 +515,18 @@ ParsedType Sema::getTypeName(const IdentifierInfo &II, SourceLocation NameLoc,
534515
// C++ [class.qual]p2: A lookup that would find the injected-class-name
535516
// instead names the constructors of the class, except when naming a class.
536517
// This is ill-formed when we're not actually forming a ctor or dtor name.
537-
T = getTypeDeclType(LookupCtx,
538-
IsImplicitTypename ? DiagCtorKind::Implicit
539-
: DiagCtorKind::None,
540-
TD, NameLoc);
518+
auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(LookupCtx);
519+
auto *FoundRD = dyn_cast<CXXRecordDecl>(TD);
520+
if (!isClassName && !IsCtorOrDtorName && LookupRD && FoundRD &&
521+
FoundRD->isInjectedClassName() &&
522+
declaresSameEntity(LookupRD, cast<Decl>(FoundRD->getParent())))
523+
Diag(NameLoc, diag::err_out_of_line_qualified_id_type_names_constructor)
524+
<< &II << /*Type*/1;
525+
526+
DiagnoseUseOfDecl(IIDecl, NameLoc);
527+
528+
T = Context.getTypeDeclType(TD);
529+
MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false);
541530
} else if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(IIDecl)) {
542531
(void)DiagnoseUseOfDecl(IDecl, NameLoc);
543532
if (!HasTrailingDot)

clang/lib/Sema/SemaTemplate.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10918,15 +10918,20 @@ Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
1091810918
//
1091910919
// FIXME: That's not strictly true: mem-initializer-id lookup does not
1092010920
// ignore functions, but that appears to be an oversight.
10921-
QualType T = getTypeDeclType(Ctx,
10922-
Keyword == ElaboratedTypeKeyword::Typename
10923-
? DiagCtorKind::Typename
10924-
: DiagCtorKind::None,
10925-
Type, IILoc);
10921+
auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(Ctx);
10922+
auto *FoundRD = dyn_cast<CXXRecordDecl>(Type);
10923+
if (Keyword == ElaboratedTypeKeyword::Typename && LookupRD && FoundRD &&
10924+
FoundRD->isInjectedClassName() &&
10925+
declaresSameEntity(LookupRD, cast<Decl>(FoundRD->getParent())))
10926+
Diag(IILoc, diag::ext_out_of_line_qualified_id_type_names_constructor)
10927+
<< &II << 1 << 0 /*'typename' keyword used*/;
10928+
1092610929
// We found a type. Build an ElaboratedType, since the
1092710930
// typename-specifier was just sugar.
10928-
return Context.getElaboratedType(
10929-
Keyword, QualifierLoc.getNestedNameSpecifier(), T);
10931+
MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false);
10932+
return Context.getElaboratedType(Keyword,
10933+
QualifierLoc.getNestedNameSpecifier(),
10934+
Context.getTypeDeclType(Type));
1093010935
}
1093110936

1093210937
// C++ [dcl.type.simple]p2:

clang/test/CXX/dcl.dcl/dcl.attr/dcl.attr.deprecated/p1.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,3 @@ template <typename T>
5858
FunS2 f;// No warning, entire function is deprecated, so usage here should be fine.
5959

6060
}
61-
62-
namespace GH58547 {
63-
struct A {
64-
using ta [[deprecated]] = int; // expected-note 2{{marked deprecated here}}
65-
};
66-
67-
using t1 = typename A::ta; // expected-warning {{'ta' is deprecated}}
68-
69-
template <class B1> struct B {
70-
using tb = typename B1::ta; // expected-warning {{'ta' is deprecated}}
71-
};
72-
73-
template struct B<A>; // expected-note {{requested here}}
74-
} // namespace GH58547

0 commit comments

Comments
 (0)