li.zhe.hua created this revision. Herald added a project: All. li.zhe.hua requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
Elaborated types are sugar that represent how the type was spelled in the original source. When printing a type outside of that original context, the qualifiers as saved in the elaborated type will be incorrect. Additionally, their existence also inhibits the use of `PrintingCallbacks::isScopeVisible` as a customization point. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D149677 Files: clang/include/clang/AST/PrettyPrinter.h clang/lib/AST/TypePrinter.cpp clang/unittests/AST/TypePrinterTest.cpp
Index: clang/unittests/AST/TypePrinterTest.cpp =================================================================== --- clang/unittests/AST/TypePrinterTest.cpp +++ clang/unittests/AST/TypePrinterTest.cpp @@ -97,6 +97,35 @@ "const f<Tp &> *", Clean)); } +TEST(TypePrinter, IgnoreElaboratedQualifiers) { + llvm::StringLiteral Code = R"cpp( + namespace shared { + namespace a { + template <typename T> + struct S {}; + } // namespace a + namespace b { + struct Foo {}; + } // namespace b + using Alias = a::S<b::Foo>; + } // namespace shared + )cpp"; + + auto Matcher = typedefNameDecl(hasName("::shared::Alias"), + hasType(qualType().bind("id"))); + ASSERT_TRUE(PrintedTypeMatches(Code, {}, Matcher, // + "a::S<b::Foo>", // + [](PrintingPolicy &Policy) { + Policy.FullyQualifiedName = true; // + })); + ASSERT_TRUE(PrintedTypeMatches(Code, {}, Matcher, + "shared::a::S<shared::b::Foo>", + [](PrintingPolicy &Policy) { + Policy.IgnoreElaboratedQualifiers = true; + Policy.FullyQualifiedName = true; + })); +} + TEST(TypePrinter, TemplateIdWithNTTP) { constexpr char Code[] = R"cpp( template <int N> Index: clang/lib/AST/TypePrinter.cpp =================================================================== --- clang/lib/AST/TypePrinter.cpp +++ clang/lib/AST/TypePrinter.cpp @@ -1567,6 +1567,11 @@ return; } + if (Policy.IgnoreElaboratedQualifiers) { + printBefore(T->getNamedType(), OS); + return; + } + // The tag definition will take care of these. if (!Policy.IncludeTagDefinition) { @@ -1586,6 +1591,12 @@ raw_ostream &OS) { if (Policy.IncludeTagDefinition && T->getOwnedTagDecl()) return; + + if (Policy.IgnoreElaboratedQualifiers) { + printAfter(T->getNamedType(), OS); + return; + } + ElaboratedTypePolicyRAII PolicyRAII(Policy); printAfter(T->getNamedType(), OS); } Index: clang/include/clang/AST/PrettyPrinter.h =================================================================== --- clang/include/clang/AST/PrettyPrinter.h +++ clang/include/clang/AST/PrettyPrinter.h @@ -60,14 +60,15 @@ : Indentation(2), SuppressSpecifiers(false), SuppressTagKeyword(LO.CPlusPlus), IncludeTagDefinition(false), SuppressScope(false), SuppressUnwrittenScope(false), - SuppressInlineNamespace(true), SuppressInitializers(false), - ConstantArraySizeAsWritten(false), AnonymousTagLocations(true), - SuppressStrongLifetime(false), SuppressLifetimeQualifiers(false), + SuppressInlineNamespace(true), IgnoreElaboratedQualifiers(false), + SuppressInitializers(false), ConstantArraySizeAsWritten(false), + AnonymousTagLocations(true), SuppressStrongLifetime(false), + SuppressLifetimeQualifiers(false), SuppressTemplateArgsInCXXConstructors(false), SuppressDefaultTemplateArgs(true), Bool(LO.Bool), Nullptr(LO.CPlusPlus11 || LO.C2x), NullptrTypeInNamespace(LO.CPlusPlus), - Restrict(LO.C99), Alignof(LO.CPlusPlus11), - UnderscoreAlignof(LO.C11), UseVoidForZeroParams(!LO.CPlusPlus), + Restrict(LO.C99), Alignof(LO.CPlusPlus11), UnderscoreAlignof(LO.C11), + UseVoidForZeroParams(!LO.CPlusPlus), SplitTemplateClosers(!LO.CPlusPlus11), TerseOutput(false), PolishForDeclaration(false), Half(LO.Half), MSWChar(LO.MicrosoftExt && !LO.WChar), IncludeNewlines(true), @@ -139,6 +140,10 @@ /// removed. unsigned SuppressInlineNamespace : 1; + /// Ignore qualifiers as specified by elaborated type sugar, instead letting + /// the underlying type handle printing the qualifiers. + unsigned IgnoreElaboratedQualifiers : 1; + /// Suppress printing of variable initializers. /// /// This flag is used when printing the loop variable in a for-range
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits