From: Serge Pavlov Date: Tue, 28 Nov 2017 16:14:14 +0000 (+0000) Subject: Refactor functions PrintTemplateArgumentList X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9fadf2235fe09410022228e4967eec29e0b92ca7;p=clang Refactor functions PrintTemplateArgumentList These functions were defined as static members of TemplateSpecializationType. Now they are moved to namespace level. Previously there were different implementations for lists containing TemplateArgument and TemplateArgumentLoc, now these implementations share the same code. This change is a result of refactoring patch D40508. NFC. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@319178 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/Type.h b/include/clang/AST/Type.h index 9a236370f9..02d785fac1 100644 --- a/include/clang/AST/Type.h +++ b/include/clang/AST/Type.h @@ -4533,21 +4533,6 @@ public: static bool anyDependentTemplateArguments(const TemplateArgumentListInfo &, bool &InstantiationDependent); - /// \brief Print a template argument list, including the '<' and '>' - /// enclosing the template arguments. - static void PrintTemplateArgumentList(raw_ostream &OS, - ArrayRef Args, - const PrintingPolicy &Policy, - bool SkipBrackets = false); - - static void PrintTemplateArgumentList(raw_ostream &OS, - ArrayRef Args, - const PrintingPolicy &Policy); - - static void PrintTemplateArgumentList(raw_ostream &OS, - const TemplateArgumentListInfo &, - const PrintingPolicy &Policy); - /// True if this template specialization type matches a current /// instantiation in the context in which it is found. bool isCurrentInstantiation() const { @@ -4623,6 +4608,20 @@ public: } }; +/// \brief Print a template argument list, including the '<' and '>' +/// enclosing the template arguments. +void printTemplateArgumentList(raw_ostream &OS, + ArrayRef Args, + const PrintingPolicy &Policy); + +void printTemplateArgumentList(raw_ostream &OS, + ArrayRef Args, + const PrintingPolicy &Policy); + +void printTemplateArgumentList(raw_ostream &OS, + const TemplateArgumentListInfo &Args, + const PrintingPolicy &Policy); + /// The injected class name of a C++ class template or class /// template partial specialization. Used to record that a type was /// spelled with a bare identifier rather than as a template-id; the diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index 5ca5b58e0f..d4c94368e6 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -6274,9 +6274,8 @@ void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, = dyn_cast(RDecl)) { const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); llvm::raw_string_ostream OS(S); - TemplateSpecializationType::PrintTemplateArgumentList(OS, - TemplateArgs.asArray(), - (*this).getPrintingPolicy()); + printTemplateArgumentList(OS, TemplateArgs.asArray(), + getPrintingPolicy()); } } else { S += '?'; diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index ce9b46c070..a755e61b75 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -1506,8 +1506,7 @@ void NamedDecl::printQualifiedName(raw_ostream &OS, if (const auto *Spec = dyn_cast(DC)) { OS << Spec->getName(); const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); - TemplateSpecializationType::PrintTemplateArgumentList( - OS, TemplateArgs.asArray(), P); + printTemplateArgumentList(OS, TemplateArgs.asArray(), P); } else if (const auto *ND = dyn_cast(DC)) { if (P.SuppressUnwrittenScope && (ND->isAnonymousNamespace() || ND->isInline())) @@ -2556,8 +2555,7 @@ void FunctionDecl::getNameForDiagnostic( NamedDecl::getNameForDiagnostic(OS, Policy, Qualified); const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); if (TemplateArgs) - TemplateSpecializationType::PrintTemplateArgumentList( - OS, TemplateArgs->asArray(), Policy); + printTemplateArgumentList(OS, TemplateArgs->asArray(), Policy); } bool FunctionDecl::isVariadic() const { diff --git a/lib/AST/DeclTemplate.cpp b/lib/AST/DeclTemplate.cpp index 00a6739478..792597238f 100644 --- a/lib/AST/DeclTemplate.cpp +++ b/lib/AST/DeclTemplate.cpp @@ -726,12 +726,10 @@ void ClassTemplateSpecializationDecl::getNameForDiagnostic( auto *PS = dyn_cast(this); if (const ASTTemplateArgumentListInfo *ArgsAsWritten = PS ? PS->getTemplateArgsAsWritten() : nullptr) { - TemplateSpecializationType::PrintTemplateArgumentList( - OS, ArgsAsWritten->arguments(), Policy); + printTemplateArgumentList(OS, ArgsAsWritten->arguments(), Policy); } else { const TemplateArgumentList &TemplateArgs = getTemplateArgs(); - TemplateSpecializationType::PrintTemplateArgumentList( - OS, TemplateArgs.asArray(), Policy); + printTemplateArgumentList(OS, TemplateArgs.asArray(), Policy); } } @@ -1058,12 +1056,10 @@ void VarTemplateSpecializationDecl::getNameForDiagnostic( auto *PS = dyn_cast(this); if (const ASTTemplateArgumentListInfo *ArgsAsWritten = PS ? PS->getTemplateArgsAsWritten() : nullptr) { - TemplateSpecializationType::PrintTemplateArgumentList( - OS, ArgsAsWritten->arguments(), Policy); + printTemplateArgumentList(OS, ArgsAsWritten->arguments(), Policy); } else { const TemplateArgumentList &TemplateArgs = getTemplateArgs(); - TemplateSpecializationType::PrintTemplateArgumentList( - OS, TemplateArgs.asArray(), Policy); + printTemplateArgumentList(OS, TemplateArgs.asArray(), Policy); } } diff --git a/lib/AST/NestedNameSpecifier.cpp b/lib/AST/NestedNameSpecifier.cpp index 26483f54eb..889f8308a9 100644 --- a/lib/AST/NestedNameSpecifier.cpp +++ b/lib/AST/NestedNameSpecifier.cpp @@ -326,8 +326,8 @@ NestedNameSpecifier::print(raw_ostream &OS, SpecType->getTemplateName().print(OS, InnerPolicy, true); // Print the template argument list. - TemplateSpecializationType::PrintTemplateArgumentList( - OS, SpecType->template_arguments(), InnerPolicy); + printTemplateArgumentList(OS, SpecType->template_arguments(), + InnerPolicy); } else { // Print the type normally QualType(T, 0).print(OS, InnerPolicy); diff --git a/lib/AST/StmtPrinter.cpp b/lib/AST/StmtPrinter.cpp index b367519425..d7e668a832 100644 --- a/lib/AST/StmtPrinter.cpp +++ b/lib/AST/StmtPrinter.cpp @@ -1321,8 +1321,7 @@ void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) { OS << "template "; OS << Node->getNameInfo(); if (Node->hasExplicitTemplateArgs()) - TemplateSpecializationType::PrintTemplateArgumentList( - OS, Node->template_arguments(), Policy); + printTemplateArgumentList(OS, Node->template_arguments(), Policy); } void StmtPrinter::VisitDependentScopeDeclRefExpr( @@ -1333,8 +1332,7 @@ void StmtPrinter::VisitDependentScopeDeclRefExpr( OS << "template "; OS << Node->getNameInfo(); if (Node->hasExplicitTemplateArgs()) - TemplateSpecializationType::PrintTemplateArgumentList( - OS, Node->template_arguments(), Policy); + printTemplateArgumentList(OS, Node->template_arguments(), Policy); } void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) { @@ -1344,8 +1342,7 @@ void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) { OS << "template "; OS << Node->getNameInfo(); if (Node->hasExplicitTemplateArgs()) - TemplateSpecializationType::PrintTemplateArgumentList( - OS, Node->template_arguments(), Policy); + printTemplateArgumentList(OS, Node->template_arguments(), Policy); } static bool isImplicitSelf(const Expr *E) { @@ -1717,8 +1714,7 @@ void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { OS << "template "; OS << Node->getMemberNameInfo(); if (Node->hasExplicitTemplateArgs()) - TemplateSpecializationType::PrintTemplateArgumentList( - OS, Node->template_arguments(), Policy); + printTemplateArgumentList(OS, Node->template_arguments(), Policy); } void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) { PrintExpr(Node->getBase()); @@ -2111,8 +2107,7 @@ void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) { if (Args->size() != 1) { OS << "operator\"\"" << Node->getUDSuffix()->getName(); - TemplateSpecializationType::PrintTemplateArgumentList( - OS, Args->asArray(), Policy); + printTemplateArgumentList(OS, Args->asArray(), Policy); OS << "()"; return; } @@ -2439,8 +2434,7 @@ void StmtPrinter::VisitCXXDependentScopeMemberExpr( OS << "template "; OS << Node->getMemberNameInfo(); if (Node->hasExplicitTemplateArgs()) - TemplateSpecializationType::PrintTemplateArgumentList( - OS, Node->template_arguments(), Policy); + printTemplateArgumentList(OS, Node->template_arguments(), Policy); } void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) { @@ -2454,8 +2448,7 @@ void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) { OS << "template "; OS << Node->getMemberNameInfo(); if (Node->hasExplicitTemplateArgs()) - TemplateSpecializationType::PrintTemplateArgumentList( - OS, Node->template_arguments(), Policy); + printTemplateArgumentList(OS, Node->template_arguments(), Policy); } static const char *getTypeTraitName(TypeTrait TT) { diff --git a/lib/AST/TypePrinter.cpp b/lib/AST/TypePrinter.cpp index 7351d972b1..612af4749e 100644 --- a/lib/AST/TypePrinter.cpp +++ b/lib/AST/TypePrinter.cpp @@ -982,8 +982,7 @@ void TypePrinter::AppendScope(DeclContext *DC, raw_ostream &OS) { IncludeStrongLifetimeRAII Strong(Policy); OS << Spec->getIdentifier()->getName(); const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); - TemplateSpecializationType::PrintTemplateArgumentList( - OS, TemplateArgs.asArray(), Policy); + printTemplateArgumentList(OS, TemplateArgs.asArray(), Policy); OS << "::"; } else if (TagDecl *Tag = dyn_cast(DC)) { if (TypedefNameDecl *Typedef = Tag->getTypedefNameForAnonDecl()) @@ -1070,7 +1069,7 @@ void TypePrinter::printTag(TagDecl *D, raw_ostream &OS) { Args = TemplateArgs.asArray(); } IncludeStrongLifetimeRAII Strong(Policy); - TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, Policy); + printTemplateArgumentList(OS, Args, Policy); } spaceBeforePlaceHolder(OS); @@ -1129,8 +1128,7 @@ void TypePrinter::printTemplateSpecializationBefore( IncludeStrongLifetimeRAII Strong(Policy); T->getTemplateName().print(OS, Policy); - TemplateSpecializationType::PrintTemplateArgumentList( - OS, T->template_arguments(), Policy); + printTemplateArgumentList(OS, T->template_arguments(), Policy); spaceBeforePlaceHolder(OS); } void TypePrinter::printTemplateSpecializationAfter( @@ -1198,19 +1196,18 @@ void TypePrinter::printDependentNameAfter(const DependentNameType *T, void TypePrinter::printDependentTemplateSpecializationBefore( const DependentTemplateSpecializationType *T, raw_ostream &OS) { IncludeStrongLifetimeRAII Strong(Policy); - + OS << TypeWithKeyword::getKeywordName(T->getKeyword()); if (T->getKeyword() != ETK_None) OS << " "; - + if (T->getQualifier()) - T->getQualifier()->print(OS, Policy); + T->getQualifier()->print(OS, Policy); OS << T->getIdentifier()->getName(); - TemplateSpecializationType::PrintTemplateArgumentList(OS, - T->template_arguments(), - Policy); + printTemplateArgumentList(OS, T->template_arguments(), Policy); spaceBeforePlaceHolder(OS); } + void TypePrinter::printDependentTemplateSpecializationAfter( const DependentTemplateSpecializationType *T, raw_ostream &OS) { } @@ -1521,41 +1518,40 @@ void TypePrinter::printObjCObjectPointerBefore(const ObjCObjectPointerType *T, OS << '*'; } } + void TypePrinter::printObjCObjectPointerAfter(const ObjCObjectPointerType *T, raw_ostream &OS) { } -void TemplateSpecializationType:: - PrintTemplateArgumentList(raw_ostream &OS, - const TemplateArgumentListInfo &Args, - const PrintingPolicy &Policy) { - return PrintTemplateArgumentList(OS, - Args.arguments(), - Policy); +static +const TemplateArgument &getArgument(const TemplateArgument &A) { return A; } + +static const TemplateArgument &getArgument(const TemplateArgumentLoc &A) { + return A.getArgument(); } -void TemplateSpecializationType::PrintTemplateArgumentList( - raw_ostream &OS, ArrayRef Args, - const PrintingPolicy &Policy, bool SkipBrackets) { +namespace { +template +void printTo(raw_ostream &OS, ArrayRef Args, const PrintingPolicy &Policy, + bool SkipBrackets) { const char *Comma = Policy.MSVCFormatting ? "," : ", "; if (!SkipBrackets) OS << '<'; - bool needSpace = false; + bool NeedSpace = false; bool FirstArg = true; - for (const TemplateArgument &Arg : Args) { + for (const auto &Arg : Args) { // Print the argument into a string. SmallString<128> Buf; llvm::raw_svector_ostream ArgOS(Buf); - if (Arg.getKind() == TemplateArgument::Pack) { - if (Arg.pack_size() && !FirstArg) + const TemplateArgument &Argument = getArgument(Arg); + if (Argument.getKind() == TemplateArgument::Pack) { + if (Argument.pack_size() && !FirstArg) OS << Comma; - PrintTemplateArgumentList(ArgOS, - Arg.getPackAsArray(), - Policy, true); + printTo(ArgOS, Argument.getPackAsArray(), Policy, true); } else { if (!FirstArg) OS << Comma; - Arg.print(Policy, ArgOS); + Argument.print(Policy, ArgOS); } StringRef ArgString = ArgOS.str(); @@ -1567,65 +1563,37 @@ void TemplateSpecializationType::PrintTemplateArgumentList( OS << ArgString; - needSpace = (!ArgString.empty() && ArgString.back() == '>'); + NeedSpace = (!ArgString.empty() && ArgString.back() == '>'); FirstArg = false; } // If the last character of our string is '>', add another space to // keep the two '>''s separate tokens. We don't *have* to do this in // C++0x, but it's still good hygiene. - if (needSpace) + if (NeedSpace) OS << ' '; if (!SkipBrackets) OS << '>'; } +} -// Sadly, repeat all that with TemplateArgLoc. -void TemplateSpecializationType:: -PrintTemplateArgumentList(raw_ostream &OS, - ArrayRef Args, - const PrintingPolicy &Policy) { - OS << '<'; - const char *Comma = Policy.MSVCFormatting ? "," : ", "; - - bool needSpace = false; - bool FirstArg = true; - for (const TemplateArgumentLoc &Arg : Args) { - if (!FirstArg) - OS << Comma; - - // Print the argument into a string. - SmallString<128> Buf; - llvm::raw_svector_ostream ArgOS(Buf); - if (Arg.getArgument().getKind() == TemplateArgument::Pack) { - PrintTemplateArgumentList(ArgOS, - Arg.getArgument().getPackAsArray(), - Policy, true); - } else { - Arg.getArgument().print(Policy, ArgOS); - } - StringRef ArgString = ArgOS.str(); - - // If this is the first argument and its string representation - // begins with the global scope specifier ('::foo'), add a space - // to avoid printing the diagraph '<:'. - if (FirstArg && !ArgString.empty() && ArgString[0] == ':') - OS << ' '; - - OS << ArgString; - - needSpace = (!ArgString.empty() && ArgString.back() == '>'); - FirstArg = false; - } +void clang::printTemplateArgumentList(raw_ostream &OS, + const TemplateArgumentListInfo &Args, + const PrintingPolicy &Policy) { + return printTo(OS, Args.arguments(), Policy, false); +} - // If the last character of our string is '>', add another space to - // keep the two '>''s separate tokens. We don't *have* to do this in - // C++0x, but it's still good hygiene. - if (needSpace) - OS << ' '; +void clang::printTemplateArgumentList(raw_ostream &OS, + ArrayRef Args, + const PrintingPolicy &Policy) { + printTo(OS, Args, Policy, false); +} - OS << '>'; +void clang::printTemplateArgumentList(raw_ostream &OS, + ArrayRef Args, + const PrintingPolicy &Policy) { + printTo(OS, Args, Policy, false); } std::string Qualifiers::getAsString() const { diff --git a/lib/CodeGen/CGDebugInfo.cpp b/lib/CodeGen/CGDebugInfo.cpp index 3e4acd208f..5a288abe36 100644 --- a/lib/CodeGen/CGDebugInfo.cpp +++ b/lib/CodeGen/CGDebugInfo.cpp @@ -265,8 +265,7 @@ StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) { // Add any template specialization args. if (Info) { const TemplateArgumentList *TArgs = Info->TemplateArguments; - TemplateSpecializationType::PrintTemplateArgumentList(OS, TArgs->asArray(), - getPrintingPolicy()); + printTemplateArgumentList(OS, TArgs->asArray(), getPrintingPolicy()); } // Copy this name on the side and use its reference. @@ -941,11 +940,8 @@ llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty, SmallString<128> NS; llvm::raw_svector_ostream OS(NS); - Ty->getTemplateName().print(OS, getPrintingPolicy(), - /*qualified*/ false); - - TemplateSpecializationType::PrintTemplateArgumentList( - OS, Ty->template_arguments(), getPrintingPolicy()); + Ty->getTemplateName().print(OS, getPrintingPolicy(), /*qualified*/ false); + printTemplateArgumentList(OS, Ty->template_arguments(), getPrintingPolicy()); auto *AliasDecl = cast( Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl(); diff --git a/lib/Sema/SemaTemplateInstantiate.cpp b/lib/Sema/SemaTemplateInstantiate.cpp index 16f7381330..03ad89100d 100644 --- a/lib/Sema/SemaTemplateInstantiate.cpp +++ b/lib/Sema/SemaTemplateInstantiate.cpp @@ -496,8 +496,8 @@ void Sema::PrintInstantiationStack() { SmallVector TemplateArgsStr; llvm::raw_svector_ostream OS(TemplateArgsStr); Template->printName(OS); - TemplateSpecializationType::PrintTemplateArgumentList( - OS, Active->template_arguments(), getPrintingPolicy()); + printTemplateArgumentList(OS, Active->template_arguments(), + getPrintingPolicy()); Diags.Report(Active->PointOfInstantiation, diag::note_default_arg_instantiation_here) << OS.str() @@ -562,8 +562,8 @@ void Sema::PrintInstantiationStack() { SmallVector TemplateArgsStr; llvm::raw_svector_ostream OS(TemplateArgsStr); FD->printName(OS); - TemplateSpecializationType::PrintTemplateArgumentList( - OS, Active->template_arguments(), getPrintingPolicy()); + printTemplateArgumentList(OS, Active->template_arguments(), + getPrintingPolicy()); Diags.Report(Active->PointOfInstantiation, diag::note_default_function_arg_instantiation_here) << OS.str() diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index 74a17d21e0..98532fe525 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -4718,12 +4718,12 @@ CXString clang_getCursorDisplayName(CXCursor C) { // If the type was explicitly written, use that. if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten()) return cxstring::createDup(TSInfo->getType().getAsString(Policy)); - + SmallString<128> Str; llvm::raw_svector_ostream OS(Str); OS << *ClassSpec; - TemplateSpecializationType::PrintTemplateArgumentList( - OS, ClassSpec->getTemplateArgs().asArray(), Policy); + printTemplateArgumentList(OS, ClassSpec->getTemplateArgs().asArray(), + Policy); return cxstring::createDup(OS.str()); }