-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[Clang][TypePrinter] Make printNestedNameSpecifier look at typedefs #169364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Clang][TypePrinter] Make printNestedNameSpecifier look at typedefs #169364
Conversation
This is to resolve a regression caused by llvm#168534. Now when we have an anonymous object like a struct or union that has a typedef attached, we print the typedef name instead of listing it as anonymous.
|
@llvm/pr-subscribers-clang Author: Aiden Grossman (boomanaiden154) ChangesThis is to resolve a regression caused by #168534. Now when we have an anonymous object like a struct or union that has a typedef attached, we print the typedef name instead of listing it as anonymous. Full diff: https://github.com/llvm/llvm-project/pull/169364.diff 2 Files Affected:
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 555aa5c050ffd..13919a56af0e8 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -1790,7 +1790,9 @@ void NamedDecl::printNestedNameSpecifier(raw_ostream &OS,
else
OS << *ND;
} else if (const auto *RD = dyn_cast<RecordDecl>(DC)) {
- if (!RD->getIdentifier())
+ if (TypedefNameDecl *Typedef = RD->getTypedefNameForAnonDecl())
+ OS << Typedef->getIdentifier()->getName();
+ else if (!RD->getIdentifier())
OS << "(anonymous " << RD->getKindName() << ')';
else
OS << *RD;
diff --git a/clang/unittests/AST/TypePrinterTest.cpp b/clang/unittests/AST/TypePrinterTest.cpp
index 410ec021d6e72..7bf2bef5ac641 100644
--- a/clang/unittests/AST/TypePrinterTest.cpp
+++ b/clang/unittests/AST/TypePrinterTest.cpp
@@ -341,3 +341,23 @@ TEST(TypePrinter, NestedNameSpecifiers) {
Policy.AnonymousTagLocations = false;
}));
}
+
+TEST(TypePrinter, NestedNameSpecifiersTypedef) {
+ constexpr char Code[] = R"cpp(
+ typedef union {
+ struct {
+ struct {
+ unsigned int baz;
+ } bar;
+ };
+ } foo;
+ )cpp";
+
+ ASSERT_TRUE(PrintedTypeMatches(
+ Code, {}, fieldDecl(hasName("bar"), hasType(qualType().bind("id"))),
+ "struct foo::(anonymous struct)::(unnamed)",
+ [](PrintingPolicy &Policy) {
+ Policy.FullyQualifiedName = true;
+ Policy.AnonymousTagLocations = false;
+ }));
+}
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
mizvekov
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, Thanks!
…lvm#169364) This is to resolve a regression caused by llvm#168534. Now when we have an anonymous object like a struct or union that has a typedef attached, we print the typedef name instead of listing it as anonymous.
This is to resolve a regression caused by #168534.
Now when we have an anonymous object like a struct or union that has a typedef attached, we print the typedef name instead of listing it as anonymous.