[clang] Use tag name lookup for class names (PR #112166)

via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 14 01:19:43 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Gábor Spaits (spaits)

<details>
<summary>Changes</summary>

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."
```

---
Full diff: https://github.com/llvm/llvm-project/pull/112166.diff


2 Files Affected:

- (modified) clang/lib/Sema/SemaDecl.cpp (+5-3) 
- (modified) clang/test/CXX/class.derived/p2.cpp (+12) 


``````````diff
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

``````````

</details>


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


More information about the cfe-commits mailing list