[clang] [Tooling] Handle AttributedType in getFullyQualifiedType (PR #134228)
Ilya Biryukov via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 3 04:56:17 PDT 2025
https://github.com/ilya-biryukov updated https://github.com/llvm/llvm-project/pull/134228
>From 7edb987104cf59b106a1f13ae0bad0c5ecd4627b Mon Sep 17 00:00:00 2001
From: Ilya Biryukov <ibiryukov at google.com>
Date: Thu, 3 Apr 2025 12:22:39 +0200
Subject: [PATCH 1/3] [Tooling] Handle AttributedType in getFullyQualifiedType
Before this change the code used to add extra qualifiers, e.g.
`std::unique_ptr<int> _Nonnull` became `::std::std::unique_ptr<int> _Nonnull`
when global namespace qualifier was requested.
---
clang/lib/AST/QualTypeNames.cpp | 9 +++++++++
clang/unittests/Tooling/QualTypeNamesTest.cpp | 17 +++++++++++++++++
2 files changed, 26 insertions(+)
diff --git a/clang/lib/AST/QualTypeNames.cpp b/clang/lib/AST/QualTypeNames.cpp
index e4d2a6937f6eb..6b44be343e13e 100644
--- a/clang/lib/AST/QualTypeNames.cpp
+++ b/clang/lib/AST/QualTypeNames.cpp
@@ -417,6 +417,15 @@ QualType getFullyQualifiedType(QualType QT, const ASTContext &Ctx,
return QT;
}
+ // Handle types that have attributes attached such as `unique_ptr<int> _Nonnull`.
+ if (auto *AT = dyn_cast<AttributedType>(QT.getTypePtr())) {
+ QualType NewModified =
+ getFullyQualifiedType(AT->getModifiedType(), Ctx, WithGlobalNsPrefix);
+ QualType NewEquivalent =
+ getFullyQualifiedType(AT->getEquivalentType(), Ctx, WithGlobalNsPrefix);
+ return Ctx.getAttributedType(AT->getAttrKind(), NewModified, NewEquivalent);
+ }
+
// Remove the part of the type related to the type being a template
// parameter (we won't report it as part of the 'type name' and it
// is actually make the code below to be more complex (to handle
diff --git a/clang/unittests/Tooling/QualTypeNamesTest.cpp b/clang/unittests/Tooling/QualTypeNamesTest.cpp
index 5ded64d4fcc8c..d9303e6d61c4a 100644
--- a/clang/unittests/Tooling/QualTypeNamesTest.cpp
+++ b/clang/unittests/Tooling/QualTypeNamesTest.cpp
@@ -297,4 +297,21 @@ TEST(QualTypeNameTest, ConstUsing) {
using ::A::S;
void foo(const S& param1, const S param2);)");
}
+
+TEST(QualTypeNameTest, NullableAttributesWithGlobalNs) {
+ TypeNameVisitor Visitor;
+ Visitor.WithGlobalNsPrefix = true;
+ Visitor.ExpectedQualTypeNames["param1"] = "::std::unique_ptr<int> _Nullable";
+ Visitor.ExpectedQualTypeNames["param2"] = "::std::unique_ptr<int> _Nonnull";
+ Visitor.ExpectedQualTypeNames["param3"] =
+ "::std::unique_ptr< ::std::unique_ptr<int> _Nullable> _Nonnull";
+ Visitor.runOver(R"(namespace std {
+ template<class T> class unique_ptr {};
+ }
+ void foo(
+ std::unique_ptr<int> _Nullable param1,
+ _Nonnull std::unique_ptr<int> param2,
+ std::unique_ptr<std::unique_ptr<int> _Nullable> _Nonnull param3);
+ )");
+}
} // end anonymous namespace
>From 69426f2b92ac174b8c8347fb595f4ce88420e1b8 Mon Sep 17 00:00:00 2001
From: Ilya Biryukov <ibiryukov at google.com>
Date: Thu, 3 Apr 2025 12:40:14 +0200
Subject: [PATCH 2/3] shorten the comment to make clang-format happy
---
clang/lib/AST/QualTypeNames.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/AST/QualTypeNames.cpp b/clang/lib/AST/QualTypeNames.cpp
index 6b44be343e13e..9c3332ee8428f 100644
--- a/clang/lib/AST/QualTypeNames.cpp
+++ b/clang/lib/AST/QualTypeNames.cpp
@@ -417,7 +417,7 @@ QualType getFullyQualifiedType(QualType QT, const ASTContext &Ctx,
return QT;
}
- // Handle types that have attributes attached such as `unique_ptr<int> _Nonnull`.
+ // Handle types with attributes such as `unique_ptr<int> _Nonnull`.
if (auto *AT = dyn_cast<AttributedType>(QT.getTypePtr())) {
QualType NewModified =
getFullyQualifiedType(AT->getModifiedType(), Ctx, WithGlobalNsPrefix);
>From bc15a4295c080ad84ef325921110df00d2f2b676 Mon Sep 17 00:00:00 2001
From: Ilya Biryukov <ibiryukov at google.com>
Date: Thu, 3 Apr 2025 13:55:30 +0200
Subject: [PATCH 3/3] Also handle qualifiers like const
---
clang/lib/AST/QualTypeNames.cpp | 6 +++++-
clang/unittests/Tooling/QualTypeNamesTest.cpp | 12 +++++++++++-
2 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/clang/lib/AST/QualTypeNames.cpp b/clang/lib/AST/QualTypeNames.cpp
index 9c3332ee8428f..7614be47154a0 100644
--- a/clang/lib/AST/QualTypeNames.cpp
+++ b/clang/lib/AST/QualTypeNames.cpp
@@ -10,6 +10,7 @@
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/DeclarationName.h"
#include "clang/AST/Mangle.h"
+#include "clang/AST/Type.h"
namespace clang {
@@ -423,7 +424,10 @@ QualType getFullyQualifiedType(QualType QT, const ASTContext &Ctx,
getFullyQualifiedType(AT->getModifiedType(), Ctx, WithGlobalNsPrefix);
QualType NewEquivalent =
getFullyQualifiedType(AT->getEquivalentType(), Ctx, WithGlobalNsPrefix);
- return Ctx.getAttributedType(AT->getAttrKind(), NewModified, NewEquivalent);
+ Qualifiers Qualifiers = QT.getLocalQualifiers();
+ return Ctx.getQualifiedType(
+ Ctx.getAttributedType(AT->getAttrKind(), NewModified, NewEquivalent),
+ Qualifiers);
}
// Remove the part of the type related to the type being a template
diff --git a/clang/unittests/Tooling/QualTypeNamesTest.cpp b/clang/unittests/Tooling/QualTypeNamesTest.cpp
index d9303e6d61c4a..dc81f0188b4fc 100644
--- a/clang/unittests/Tooling/QualTypeNamesTest.cpp
+++ b/clang/unittests/Tooling/QualTypeNamesTest.cpp
@@ -305,13 +305,23 @@ TEST(QualTypeNameTest, NullableAttributesWithGlobalNs) {
Visitor.ExpectedQualTypeNames["param2"] = "::std::unique_ptr<int> _Nonnull";
Visitor.ExpectedQualTypeNames["param3"] =
"::std::unique_ptr< ::std::unique_ptr<int> _Nullable> _Nonnull";
+ Visitor.ExpectedQualTypeNames["param4"] =
+ "::std::unique_ptr<int> _Nullable const *";
+ Visitor.ExpectedQualTypeNames["param5"] =
+ "::std::unique_ptr<int> _Nullable const *";
+ Visitor.ExpectedQualTypeNames["param6"] =
+ "::std::unique_ptr<int> _Nullable const *";
Visitor.runOver(R"(namespace std {
template<class T> class unique_ptr {};
}
void foo(
std::unique_ptr<int> _Nullable param1,
_Nonnull std::unique_ptr<int> param2,
- std::unique_ptr<std::unique_ptr<int> _Nullable> _Nonnull param3);
+ std::unique_ptr<std::unique_ptr<int> _Nullable> _Nonnull param3,
+ const std::unique_ptr<int> _Nullable *param4,
+ _Nullable std::unique_ptr<int> const *param5,
+ std::unique_ptr<int> _Nullable const *param6
+ );
)");
}
} // end anonymous namespace
More information about the cfe-commits
mailing list