Skip to content

Commit 722346c

Browse files
[Tooling] Handle AttributedType in getFullyQualifiedType (llvm#134228)
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 adding a global namespace qualifier was requested.
1 parent 739fe98 commit 722346c

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

clang/lib/AST/QualTypeNames.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "clang/AST/DeclTemplate.h"
1111
#include "clang/AST/DeclarationName.h"
1212
#include "clang/AST/Mangle.h"
13+
#include "clang/AST/Type.h"
1314

1415
namespace clang {
1516

@@ -416,6 +417,18 @@ QualType getFullyQualifiedType(QualType QT, const ASTContext &Ctx,
416417
return QT;
417418
}
418419

420+
// Handle types with attributes such as `unique_ptr<int> _Nonnull`.
421+
if (auto *AT = dyn_cast<AttributedType>(QT.getTypePtr())) {
422+
QualType NewModified =
423+
getFullyQualifiedType(AT->getModifiedType(), Ctx, WithGlobalNsPrefix);
424+
QualType NewEquivalent =
425+
getFullyQualifiedType(AT->getEquivalentType(), Ctx, WithGlobalNsPrefix);
426+
Qualifiers Qualifiers = QT.getLocalQualifiers();
427+
return Ctx.getQualifiedType(
428+
Ctx.getAttributedType(AT->getAttrKind(), NewModified, NewEquivalent),
429+
Qualifiers);
430+
}
431+
419432
// Remove the part of the type related to the type being a template
420433
// parameter (we won't report it as part of the 'type name' and it
421434
// is actually make the code below to be more complex (to handle

clang/unittests/Tooling/QualTypeNamesTest.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,31 @@ TEST(QualTypeNameTest, ConstUsing) {
297297
using ::A::S;
298298
void foo(const S& param1, const S param2);)");
299299
}
300+
301+
TEST(QualTypeNameTest, NullableAttributesWithGlobalNs) {
302+
TypeNameVisitor Visitor;
303+
Visitor.WithGlobalNsPrefix = true;
304+
Visitor.ExpectedQualTypeNames["param1"] = "::std::unique_ptr<int> _Nullable";
305+
Visitor.ExpectedQualTypeNames["param2"] = "::std::unique_ptr<int> _Nonnull";
306+
Visitor.ExpectedQualTypeNames["param3"] =
307+
"::std::unique_ptr< ::std::unique_ptr<int> _Nullable> _Nonnull";
308+
Visitor.ExpectedQualTypeNames["param4"] =
309+
"::std::unique_ptr<int> _Nullable const *";
310+
Visitor.ExpectedQualTypeNames["param5"] =
311+
"::std::unique_ptr<int> _Nullable const *";
312+
Visitor.ExpectedQualTypeNames["param6"] =
313+
"::std::unique_ptr<int> _Nullable const *";
314+
Visitor.runOver(R"(namespace std {
315+
template<class T> class unique_ptr {};
316+
}
317+
void foo(
318+
std::unique_ptr<int> _Nullable param1,
319+
_Nonnull std::unique_ptr<int> param2,
320+
std::unique_ptr<std::unique_ptr<int> _Nullable> _Nonnull param3,
321+
const std::unique_ptr<int> _Nullable *param4,
322+
_Nullable std::unique_ptr<int> const *param5,
323+
std::unique_ptr<int> _Nullable const *param6
324+
);
325+
)");
326+
}
300327
} // end anonymous namespace

0 commit comments

Comments
 (0)