[clang] [Sema]Use tag name lookup for class names (PR #112166)
Gábor Spaits via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 14 08:32:53 PDT 2024
https://github.com/spaits updated https://github.com/llvm/llvm-project/pull/112166
>From e9d43ef25b882071822cf3f16a988197c07967b1 Mon Sep 17 00:00:00 2001
From: Gabor Spaits <gaborspaits1 at gmail.com>
Date: Mon, 14 Oct 2024 10:12:42 +0200
Subject: [PATCH 1/7] Use tag name lookup for class names
This PR would fix #16855 .
I think the correct lookup to use for class names is Tag name lookup,
because it does not take namespaces into account. The current lookup
does and because of this some valid programs are not accepted.
If you think that Tag name lookup is not correct for all cases when
we are looking up types based on class names then we can only
do tag name lookup when looking up class names for inheritance.
In case of inheritance:
```
[class.derived]p2 says:
"During the lookup for a base class name, non-type names are ignored."
```
---
clang/lib/Sema/SemaDecl.cpp | 8 +++++---
clang/test/CXX/class.derived/p2.cpp | 12 ++++++++++++
2 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 118873bc93ad4b..5cce92ef118951 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -357,9 +357,10 @@ ParsedType Sema::getTypeName(const IdentifierInfo &II, SourceLocation NameLoc,
return nullptr;
}
- // FIXME: LookupNestedNameSpecifierName isn't the right kind of
- // lookup for class-names.
- LookupNameKind Kind = isClassName ? LookupNestedNameSpecifierName :
+ // In case we know that the identifier is a class name, we know that it is
+ // a type declaration (struct, class, union or enum) so we can use tag name
+ // lookup.
+ LookupNameKind Kind = isClassName ? LookupTagName :
LookupOrdinaryName;
LookupResult Result(*this, &II, NameLoc, Kind);
if (LookupCtx) {
@@ -547,6 +548,7 @@ ParsedType Sema::getTypeName(const IdentifierInfo &II, SourceLocation NameLoc,
if (T.isNull()) {
// If it's not plausibly a type, suppress diagnostics.
Result.suppressDiagnostics();
+ llvm::errs() << "Not a type\n";
return nullptr;
}
diff --git a/clang/test/CXX/class.derived/p2.cpp b/clang/test/CXX/class.derived/p2.cpp
index 87e0f748615456..588436c3a8d211 100644
--- a/clang/test/CXX/class.derived/p2.cpp
+++ b/clang/test/CXX/class.derived/p2.cpp
@@ -7,3 +7,15 @@ namespace PR5840 {
int Base = 10;
struct Derived : Base {};
}
+
+namespace issue_number {
+ struct x {};
+ namespace
+ {
+ namespace x
+ {
+ struct y : x
+ {};
+ }
+ }
+}
\ No newline at end of file
>From c690afa2c92fe1dd9a0a1b3f4e577934ac2bbcf0 Mon Sep 17 00:00:00 2001
From: Gabor Spaits <gaborspaits1 at gmail.com>
Date: Mon, 14 Oct 2024 10:26:18 +0200
Subject: [PATCH 2/7] Add issue number to the test for the issue
---
clang/test/CXX/class.derived/p2.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/CXX/class.derived/p2.cpp b/clang/test/CXX/class.derived/p2.cpp
index 588436c3a8d211..f63782761ae15e 100644
--- a/clang/test/CXX/class.derived/p2.cpp
+++ b/clang/test/CXX/class.derived/p2.cpp
@@ -8,7 +8,7 @@ namespace PR5840 {
struct Derived : Base {};
}
-namespace issue_number {
+namespace issue_16855 {
struct x {};
namespace
{
>From 18a7b2282e12767a065cb1c555168af2e561bb06 Mon Sep 17 00:00:00 2001
From: Gabor Spaits <gaborspaits1 at gmail.com>
Date: Mon, 14 Oct 2024 11:24:05 +0200
Subject: [PATCH 3/7] Remove debug print
---
clang/lib/Sema/SemaDecl.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 5cce92ef118951..eb24afec5cc366 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -548,7 +548,6 @@ ParsedType Sema::getTypeName(const IdentifierInfo &II, SourceLocation NameLoc,
if (T.isNull()) {
// If it's not plausibly a type, suppress diagnostics.
Result.suppressDiagnostics();
- llvm::errs() << "Not a type\n";
return nullptr;
}
>From 85e3786441b67e93c9bbf6d782d648d4f0ddad08 Mon Sep 17 00:00:00 2001
From: Gabor Spaits <gaborspaits1 at gmail.com>
Date: Mon, 14 Oct 2024 17:18:55 +0200
Subject: [PATCH 4/7] Add missing endline and namespace comments
---
clang/test/CXX/class.derived/p2.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang/test/CXX/class.derived/p2.cpp b/clang/test/CXX/class.derived/p2.cpp
index f63782761ae15e..401ee37ad6b64c 100644
--- a/clang/test/CXX/class.derived/p2.cpp
+++ b/clang/test/CXX/class.derived/p2.cpp
@@ -6,7 +6,7 @@ namespace PR5840 {
struct Base {};
int Base = 10;
struct Derived : Base {};
-}
+} // namespace PR5840
namespace issue_16855 {
struct x {};
@@ -16,6 +16,6 @@ namespace issue_16855 {
{
struct y : x
{};
- }
+ } // namespace x
}
-}
\ No newline at end of file
+} // namespace issue_16855
>From dc7a91f495224231d6b17ce51169788c1f9fad4d Mon Sep 17 00:00:00 2001
From: Gabor Spaits <gaborspaits1 at gmail.com>
Date: Mon, 14 Oct 2024 17:20:40 +0200
Subject: [PATCH 5/7] Cite standard and format changes
---
clang/lib/Sema/SemaDecl.cpp | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index eb24afec5cc366..519da58cda3920 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -359,9 +359,10 @@ ParsedType Sema::getTypeName(const IdentifierInfo &II, SourceLocation NameLoc,
// In case we know that the identifier is a class name, we know that it is
// a type declaration (struct, class, union or enum) so we can use tag name
- // lookup.
- LookupNameKind Kind = isClassName ? LookupTagName :
- LookupOrdinaryName;
+ // lookup:
+ // The lookup for the component name of the type-name or simple-template-id
+ // is type-only ([basic.lookup]).
+ LookupNameKind Kind = isClassName ? LookupTagName : LookupOrdinaryName;
LookupResult Result(*this, &II, NameLoc, Kind);
if (LookupCtx) {
// Perform "qualified" name lookup into the declaration context we
>From 1f4fabfd307eff9a4d51e124d89cd5b960c79fb3 Mon Sep 17 00:00:00 2001
From: Gabor Spaits <gaborspaits1 at gmail.com>
Date: Mon, 14 Oct 2024 17:24:37 +0200
Subject: [PATCH 6/7] Update release notes
---
clang/docs/ReleaseNotes.rst | 2 ++
1 file changed, 2 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e48835d4738007..1ac970e1dd5845 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -151,6 +151,8 @@ C++ Language Changes
- The builtin type alias ``__builtin_common_type`` has been added to improve the
performance of ``std::common_type``.
+- During the lookup for a base class name, non-type names are ignored.
+
C++2c Feature Support
^^^^^^^^^^^^^^^^^^^^^
>From 2bb1092f1a6a2cb148d9472ac1132b7155de9fe2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20Spaits?= <gaborspaits1 at gmail.com>
Date: Mon, 14 Oct 2024 17:32:42 +0200
Subject: [PATCH 7/7] Update clang/lib/Sema/SemaDecl.cpp
Co-authored-by: Sirraide <aeternalmail at gmail.com>
---
clang/lib/Sema/SemaDecl.cpp | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 519da58cda3920..b0cc91dccd9265 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -359,9 +359,10 @@ ParsedType Sema::getTypeName(const IdentifierInfo &II, SourceLocation NameLoc,
// In case we know that the identifier is a class name, we know that it is
// a type declaration (struct, class, union or enum) so we can use tag name
- // lookup:
- // The lookup for the component name of the type-name or simple-template-id
- // is type-only ([basic.lookup]).
+ // lookup.
+ //
+ // C++ [class.derived]p2 (wrt lookup in a base-specifier): The lookup for
+ // the component name of the type-name or simple-template-id is type-only.
LookupNameKind Kind = isClassName ? LookupTagName : LookupOrdinaryName;
LookupResult Result(*this, &II, NameLoc, Kind);
if (LookupCtx) {
More information about the cfe-commits
mailing list