Index: include/clang/Tooling/Core/QualTypeNames.h =================================================================== --- /dev/null +++ include/clang/Tooling/Core/QualTypeNames.h @@ -0,0 +1,83 @@ +//===--- QualTypeNames.h - Generate Complete QualType Names ----*- C++ -*-===// +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +// ===----------------------------------------------------------------------===// +// +// \file +// Functionality to generate the fully-qualified names of QualTypes, +// including recursively expanding any subtypes and template +// parameters. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLING_CORE_QUALTYPENAMES_H +#define LLVM_CLANG_TOOLING_CORE_QUALTYPENAMES_H + +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/SmallSet.h" +#include "llvm/ADT/StringRef.h" + +namespace clang { +class ASTContext; +class NamespaceDecl; +class NestedNameSpecifier; +class QualType; +class TagDecl; +class Type; +class TypedefNameDecl; + +namespace TypeName { +///\brief Convert the type into one with fully qualified template +/// arguments. +///\param[in] QT - the type for which the fully qualified type will be +/// returned. +///\param[in] Ctx - the ASTContext to be used. +clang::QualType getFullyQualifiedType(clang::QualType QT, + const clang::ASTContext &Ctx); + +///\brief Get the fully qualified name for a type. This includes full +/// qualification of all template parameters etc. +/// +///\param[in] QT - the type for which the fully qualified name will be +/// returned. +///\param[in] Ctx - the ASTContext to be used. +std::string getFullyQualifiedName(clang::QualType QT, + const clang::ASTContext &Ctx); + +///\brief Create a NestedNameSpecifier for Namesp and its enclosing +/// scopes. +/// +///\param[in] Ctx - the AST Context to be used. +///\param[in] Namesp - the NamespaceDecl for which a NestedNameSpecifier +/// is requested. +clang::NestedNameSpecifier *createNestedNameSpecifier( + const clang::ASTContext &Ctx, const clang::NamespaceDecl *Namesp); + +///\brief Create a NestedNameSpecifier for TagDecl and its enclosing +/// scopes. +/// +///\param[in] Ctx - the AST Context to be used. +///\param[in] TD - the TagDecl for which a NestedNameSpecifier is +/// requested. +///\param[in] FullyQualify - Convert all template arguments into fully +/// qualified names. +clang::NestedNameSpecifier *createNestedNameSpecifier( + const clang::ASTContext &Ctx, const clang::TagDecl *TD, bool FullyQualify); + +///\brief Create a NestedNameSpecifier for TypedefDecl and its enclosing +/// scopes. +/// +///\param[in] Ctx - the AST Context to be used. +///\param[in] TD - the TypedefDecl for which a NestedNameSpecifier is +/// requested. +///\param[in] FullyQualify - Convert all template arguments (of possible +/// parent scopes) into fully qualified names. +clang::NestedNameSpecifier *createNestedNameSpecifier( + const clang::ASTContext &Ctx, const clang::TypedefNameDecl *TD, + bool FullyQualify); + +} // end namespace TypeName +} // end namespace clang +#endif // LLVM_CLANG_TOOLING_CORE_QUALTYPENAMES_H Index: lib/Tooling/Core/CMakeLists.txt =================================================================== --- lib/Tooling/Core/CMakeLists.txt +++ lib/Tooling/Core/CMakeLists.txt @@ -3,6 +3,7 @@ add_clang_library(clangToolingCore Lookup.cpp Replacement.cpp + QualTypeNames.cpp LINK_LIBS clangAST Index: lib/Tooling/Core/QualTypeNames.cpp =================================================================== --- /dev/null +++ lib/Tooling/Core/QualTypeNames.cpp @@ -0,0 +1,403 @@ +//===------- QualTypeNames.cpp - Generate Complete QualType Names ---------===// +// +// The LLVM Compiler Infrastructure +// +//===----------------------------------------------------------------------===// +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "clang/Tooling/Core/QualTypeNames.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/DeclarationName.h" +#include "clang/AST/GlobalDecl.h" +#include "clang/AST/Mangle.h" +#include "clang/Sema/Lookup.h" +#include "clang/Sema/Sema.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/StringRef.h" + +#include +#include + +namespace clang { + +static NestedNameSpecifier *createNestedNameSpecifierForScopeOf( + const ASTContext &Ctx, const Decl *decl, bool FullyQualified); + +static NestedNameSpecifier *getFullyQualifiedNameSpecifier( + const ASTContext &Ctx, NestedNameSpecifier *scope); + +static bool getFullyQualifiedTemplateName(const ASTContext &Ctx, + TemplateName &TName) { + bool Changed = false; + NestedNameSpecifier *NNS = nullptr; + + TemplateDecl *ArgTDecl = TName.getAsTemplateDecl(); + QualifiedTemplateName *QTName = TName.getAsQualifiedTemplateName(); + + if (QTName && !QTName->hasTemplateKeyword()) { + NNS = QTName->getQualifier(); + NestedNameSpecifier *QNNS = getFullyQualifiedNameSpecifier(Ctx, NNS); + if (QNNS != NNS) { + Changed = true; + NNS = QNNS; + } else { + NNS = nullptr; + } + } else { + NNS = createNestedNameSpecifierForScopeOf(Ctx, ArgTDecl, true); + } + if (NNS) { + TName = Ctx.getQualifiedTemplateName(NNS, + /*TemplateKeyword=*/false, ArgTDecl); + Changed = true; + } + return Changed; +} + +static bool getFullyQualifiedTemplateArgument(const ASTContext &Ctx, + TemplateArgument &Arg) { + bool Changed = false; + + // Note: we do not handle TemplateArgument::Expression, to replace it + // we need the information for the template instance decl. + + if (Arg.getKind() == TemplateArgument::Template) { + TemplateName TName = Arg.getAsTemplate(); + Changed = getFullyQualifiedTemplateName(Ctx, TName); + if (Changed) { + Arg = TemplateArgument(TName); + } + } else if (Arg.getKind() == TemplateArgument::Type) { + QualType SubTy = Arg.getAsType(); + // Check if the type needs more desugaring and recurse. + QualType QTFQ = TypeName::getFullyQualifiedType(SubTy, Ctx); + if (QTFQ != SubTy) { + Arg = TemplateArgument(QTFQ); + Changed = true; + } + } + return Changed; +} + +static const Type *getFullyQualifiedLocalType(const ASTContext &Ctx, + const Type *TypePtr) { + // We really just want to handle the template parameter if any .... + // In case of template specializations iterate over the arguments and + // fully qualify them as well. + if (const TemplateSpecializationType *TST = + llvm::dyn_cast(TypePtr)) { + bool MightHaveChanged = false; + llvm::SmallVector DesArgs; + for (TemplateSpecializationType::iterator I = TST->begin(), E = TST->end(); + I != E; ++I) { + // cheap to copy and potentially modified by + // getFullyQualifedTemplateArgument + TemplateArgument Arg(*I); + MightHaveChanged |= getFullyQualifiedTemplateArgument(Ctx, Arg); + DesArgs.push_back(Arg); + } + + // If desugaring happened allocate new type in the AST. + if (MightHaveChanged) { + QualType QT = Ctx.getTemplateSpecializationType( + TST->getTemplateName(), DesArgs.data(), DesArgs.size(), + TST->getCanonicalTypeInternal()); + return QT.getTypePtr(); + } + } else if (const RecordType *TSTRecord = + llvm::dyn_cast(TypePtr)) { + // We are asked to fully qualify and we have a Record Type, + // which can point to a template instantiation with no sugar in any of + // its template argument, however we still need to fully qualify them. + + if (const ClassTemplateSpecializationDecl *TSTDecl = + llvm::dyn_cast( + TSTRecord->getDecl())) { + const TemplateArgumentList &TemplateArgs = TSTDecl->getTemplateArgs(); + + bool MightHaveChanged = false; + llvm::SmallVector DesArgs; + for (unsigned int I = 0, E = TemplateArgs.size(); I != E; ++I) { + // cheap to copy and potentially modified by + // getFullyQualifedTemplateArgument + TemplateArgument Arg(TemplateArgs[I]); + MightHaveChanged |= getFullyQualifiedTemplateArgument(Ctx, Arg); + DesArgs.push_back(Arg); + } + + // If desugaring happened allocate new type in the AST. + if (MightHaveChanged) { + TemplateName TN(TSTDecl->getSpecializedTemplate()); + QualType QT = Ctx.getTemplateSpecializationType( + TN, DesArgs.data(), DesArgs.size(), + TSTRecord->getCanonicalTypeInternal()); + return QT.getTypePtr(); + } + } + } + return TypePtr; +} + +static NestedNameSpecifier *createOuterNNS(const ASTContext &Ctx, const Decl *D, + bool FullyQualify) { + const DeclContext *DC = D->getDeclContext(); + if (const NamespaceDecl *NS = dyn_cast(DC)) { + while (NS && NS->isInline()) { + // Ignore inline namespace; + NS = dyn_cast_or_null(NS->getDeclContext()); + } + if (NS->getDeclName()) return TypeName::createNestedNameSpecifier(Ctx, NS); + return nullptr; // no starting '::', no anonymous + } else if (const TagDecl *TD = dyn_cast(DC)) { + return TypeName::createNestedNameSpecifier(Ctx, TD, FullyQualify); + } else if (const TypedefNameDecl *TDD = dyn_cast(DC)) { + return TypeName::createNestedNameSpecifier(Ctx, TDD, FullyQualify); + } + return nullptr; // no starting '::' +} + +static NestedNameSpecifier *getFullyQualifiedNameSpecifier( + const ASTContext &Ctx, NestedNameSpecifier *Scope) { + // Return a fully qualified version of this name specifier + if (Scope->getKind() == NestedNameSpecifier::Global) { + // Already fully qualified. + return Scope; + } + + if (const Type *Type = Scope->getAsType()) { + // Find decl context. + const TagDecl *TD = nullptr; + if (const TagType *TagDeclType = dyn_cast(Type)) { + TD = TagDeclType->getDecl(); + } else { + TD = Type->getAsCXXRecordDecl(); + } + if (TD) { + return TypeName::createNestedNameSpecifier(Ctx, TD, + true /*FullyQualified*/); + } else if (const TypedefType *TDD = dyn_cast(Type)) { + return TypeName::createNestedNameSpecifier(Ctx, TDD->getDecl(), + true /*FullyQualified*/); + } + } else if (const NamespaceDecl *NS = Scope->getAsNamespace()) { + return TypeName::createNestedNameSpecifier(Ctx, NS); + } else if (const NamespaceAliasDecl *Alias = Scope->getAsNamespaceAlias()) { + const NamespaceDecl *NS = Alias->getNamespace()->getCanonicalDecl(); + return TypeName::createNestedNameSpecifier(Ctx, NS); + } + + return Scope; +} + +static NestedNameSpecifier *createNestedNameSpecifierForScopeOf( + const ASTContext &Ctx, const Decl *Decl, bool FullyQualified) { + // Create a nested name specifier for the declaring context of the type. + + assert(Decl); + + const NamedDecl *Outer = + llvm::dyn_cast_or_null(Decl->getDeclContext()); + const NamespaceDecl *OuterNS = + llvm::dyn_cast_or_null(Decl->getDeclContext()); + if (Outer && !(OuterNS && OuterNS->isAnonymousNamespace())) { + if (const CXXRecordDecl *CxxDecl = + llvm::dyn_cast(Decl->getDeclContext())) { + if (ClassTemplateDecl *ClassTempl = + CxxDecl->getDescribedClassTemplate()) { + // We are in the case of a type(def) that was declared in a + // class template but is *not* type dependent. In clang, it + // gets attached to the class template declaration rather than + // any specific class template instantiation. This result in + // 'odd' fully qualified typename: + // + // vector<_Tp,_Alloc>::size_type + // + // Make the situation is 'useable' but looking a bit odd by + // picking a random instance as the declaring context. + if (ClassTempl->spec_begin() != ClassTempl->spec_end()) { + Decl = *(ClassTempl->spec_begin()); + Outer = llvm::dyn_cast(Decl); + OuterNS = llvm::dyn_cast(Decl); + } + } + } + + if (OuterNS) { + return TypeName::createNestedNameSpecifier(Ctx, OuterNS); + } else if (const TagDecl *TD = llvm::dyn_cast(Outer)) { + return TypeName::createNestedNameSpecifier(Ctx, TD, FullyQualified); + } + } + return nullptr; +} + +static NestedNameSpecifier *createNestedNameSpecifierForScopeOf( + const ASTContext &Ctx, const Type *TypePtr, bool FullyQualified) { + // Create a nested name specifier for the declaring context of the type. + + if (!TypePtr) return nullptr; + + Decl *Decl = nullptr; + if (const TypedefType *TDT = llvm::dyn_cast(TypePtr)) { + Decl = TDT->getDecl(); + } else { + // There are probably other cases ... + if (const TagType *TagDeclType = llvm::dyn_cast_or_null(TypePtr)) + Decl = TagDeclType->getDecl(); + else + Decl = TypePtr->getAsCXXRecordDecl(); + } + + if (!Decl) return nullptr; + + return createNestedNameSpecifierForScopeOf(Ctx, Decl, FullyQualified); +} + +NestedNameSpecifier *TypeName::createNestedNameSpecifier( + const ASTContext &Ctx, const NamespaceDecl *Namespace) { + while (Namespace && Namespace->isInline()) { + // Ignore inline namespace; + Namespace = dyn_cast_or_null(Namespace->getDeclContext()); + } + if (!Namespace) return nullptr; + + bool FullyQualified = true; // doesn't matter, DeclContexts are namespaces + return NestedNameSpecifier::Create( + Ctx, createOuterNNS(Ctx, Namespace, FullyQualified), Namespace); +} + +NestedNameSpecifier *TypeName::createNestedNameSpecifier( + const ASTContext &Ctx, const TypedefNameDecl *TD, bool FullyQualify) { + return NestedNameSpecifier::Create(Ctx, createOuterNNS(Ctx, TD, FullyQualify), + true /*Template*/, TD->getTypeForDecl()); +} + +NestedNameSpecifier *TypeName::createNestedNameSpecifier(const ASTContext &Ctx, + const TagDecl *TD, + bool FullyQualify) { + const Type *Ty = Ctx.getTypeDeclType(TD).getTypePtr(); + if (FullyQualify) Ty = getFullyQualifiedLocalType(Ctx, Ty); + return NestedNameSpecifier::Create(Ctx, createOuterNNS(Ctx, TD, FullyQualify), + false /* template keyword wanted */, Ty); +} + +QualType TypeName::getFullyQualifiedType(QualType QT, const ASTContext &Ctx) { + // Return the fully qualified type, including for any template + // parameters. + + // In case of myType* we need to strip the pointer first, fully + // qualify and attach the pointer once again. + if (llvm::isa(QT.getTypePtr())) { + // Get the qualifiers. + Qualifiers Quals = QT.getQualifiers(); + QT = getFullyQualifiedType(QT->getPointeeType(), Ctx); + QT = Ctx.getPointerType(QT); + // Add back the qualifiers. + QT = Ctx.getQualifiedType(QT, Quals); + return QT; + } + + // In case of myType& we need to strip the reference first, fully + // qualifiy and attach the reference once again. + if (llvm::isa(QT.getTypePtr())) { + // Get the qualifiers. + bool IsLValueRefTy = llvm::isa(QT.getTypePtr()); + Qualifiers Quals = QT.getQualifiers(); + QT = getFullyQualifiedType(QT->getPointeeType(), Ctx); + // Add the r- or l-value reference type back to the fully + // qualified one. + if (IsLValueRefTy) + QT = Ctx.getLValueReferenceType(QT); + else + QT = Ctx.getRValueReferenceType(QT); + // Add back the qualifiers. + QT = Ctx.getQualifiedType(QT, Quals); + return QT; + } + + // 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 + // those) + while (isa(QT.getTypePtr())) { + // Get the qualifiers. + Qualifiers Quals = QT.getQualifiers(); + + QT = dyn_cast(QT.getTypePtr())->desugar(); + + // Add back the qualifiers. + QT = Ctx.getQualifiedType(QT, Quals); + } + + NestedNameSpecifier *Prefix = nullptr; + Qualifiers PrefixQualifiers; + if (const ElaboratedType *ETypeInput = + llvm::dyn_cast(QT.getTypePtr())) { + // Intentionally, we do not care about the other compononent of + // the elaborated type (the keyword) as part of the name + // normalization is to remove it. + Prefix = ETypeInput->getQualifier(); + if (Prefix) { + const NamespaceDecl *NS = Prefix->getAsNamespace(); + if (Prefix != NestedNameSpecifier::GlobalSpecifier(Ctx) && + !(NS && NS->isAnonymousNamespace())) { + PrefixQualifiers = QT.getLocalQualifiers(); + Prefix = getFullyQualifiedNameSpecifier(Ctx, Prefix); + QT = QualType(ETypeInput->getNamedType().getTypePtr(), 0); + } else { + Prefix = nullptr; + } + } + } else { + // Create a nested name specifier if needed (i.e. if the decl context + // is not the global scope. + Prefix = createNestedNameSpecifierForScopeOf(Ctx, QT.getTypePtr(), + true /*FullyQualified*/); + + // move the qualifiers on the outer type (avoid 'std::const string'!) + if (Prefix) { + PrefixQualifiers = QT.getLocalQualifiers(); + QT = QualType(QT.getTypePtr(), 0); + } + } + + // In case of template specializations iterate over the arguments and + // fully qualify them as well. + if (llvm::isa(QT.getTypePtr())) { + Qualifiers Quals = QT.getLocalQualifiers(); + const Type *TypePtr = getFullyQualifiedLocalType(Ctx, QT.getTypePtr()); + QT = Ctx.getQualifiedType(TypePtr, Quals); + + } else if (llvm::isa(QT.getTypePtr())) { + // We are asked to fully qualify and we have a Record Type, + // which can point to a template instantiation with no sugar in any of + // its template argument, however we still need to fully qualify them. + + Qualifiers Quals = QT.getLocalQualifiers(); + const Type *TypePtr = getFullyQualifiedLocalType(Ctx, QT.getTypePtr()); + QT = Ctx.getQualifiedType(TypePtr, Quals); + } + if (Prefix) { + // We intentionally always use ETK_None, we never want + // the keyword (humm ... what about anonymous types?) + QT = Ctx.getElaboratedType(ETK_None, Prefix, QT); + QT = Ctx.getQualifiedType(QT, PrefixQualifiers); + } + return QT; +} + +std::string TypeName::getFullyQualifiedName(QualType QT, + const ASTContext &Ctx) { + QualType FQQT = getFullyQualifiedType(QT, Ctx); + PrintingPolicy Policy(Ctx.getPrintingPolicy()); + Policy.SuppressScope = false; + Policy.AnonymousTagLocations = false; + return FQQT.getAsString(Policy); +} + +} // end namespace clang Index: unittests/Tooling/CMakeLists.txt =================================================================== --- unittests/Tooling/CMakeLists.txt +++ unittests/Tooling/CMakeLists.txt @@ -17,6 +17,7 @@ RewriterTest.cpp RefactoringCallbacksTest.cpp ReplacementsYamlTest.cpp + QualTypeNamesTest.cpp ) target_link_libraries(ToolingTests Index: unittests/Tooling/QualTypeNamesTest.cpp =================================================================== --- /dev/null +++ unittests/Tooling/QualTypeNamesTest.cpp @@ -0,0 +1,92 @@ +//===- unittest/Tooling/QualTypeNameTest.cpp ------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "clang/Tooling/Core/QualTypeNames.h" +#include "TestVisitor.h" +using namespace clang; + +namespace { +struct TypeNameVisitor : TestVisitor { + llvm::StringMap ExpectedQualTypeNames; + + // ValueDecls are the least-derived decl with both a qualtype and a + // name. + bool traverseDecl(Decl *D) { + return true; // Always continue + } + + bool VisitValueDecl(const ValueDecl *VD) { + std::string ExpectedName = + ExpectedQualTypeNames.lookup(VD->getNameAsString()); + std::string ActualName = + TypeName::getFullyQualifiedName(VD->getType(), *Context); + if (ExpectedName != "" && ExpectedName != ActualName) { + // A custom message makes it much easier to see what declaration + // failed compared to EXPECT_EQ. + EXPECT_TRUE(false) << "Typename::getFullyQualifiedName failed for " + << VD->getQualifiedNameAsString() << std::endl + << " Actual: " << ActualName << std::endl + << " Exepcted: " << ExpectedName; + } + return true; + } +}; + +TEST(QualTypeNameTest, getFullyQualifiedName) { + TypeNameVisitor Visitor; + // Simple case to test the test framework itself. + Visitor.ExpectedQualTypeNames["CheckInt"] = "int"; + Visitor.runOver(""); + + // Keeping the names of the variables whose types we check unique + // within the entire test--regardless of their own scope--makes it + // easier to diagnose test failures. + + // Simple namespace qualifier + Visitor.ExpectedQualTypeNames["CheckA"] = "A::B::Class0"; + // Lookup up the enclosing scopes, then down another one. (These + // appear as elaborated type in the AST. In that case--even if + // policy.SuppressScope = 0--qual_type.getAsString(policy) only + // gives the name as it appears in the source, not the full name. + Visitor.ExpectedQualTypeNames["CheckB"] = "A::B::C::Class1"; + // Template parameter expansion. + Visitor.ExpectedQualTypeNames["CheckC"] = + "A::B::Template0"; + // Recursive template parameter expansion. + Visitor.ExpectedQualTypeNames["CheckD"] = + "A::B::Template0, " + "A::B::Template0 >"; + // Variadic Template expansion. + Visitor.ExpectedQualTypeNames["CheckE"] = + "A::Variadic, " + "A::B::Template1, A::B::C::MyInt>"; + Visitor.runOver( + "int CheckInt;\n" + "namespace A {\n" + " namespace B {\n" + " class Class0 { };\n" + " namespace C {\n" + " typedef int MyInt;" + " }\n" + " template class Template0;" + " template class Template1;" + " typedef B::Class0 AnotherClass;\n" + " void Function1(Template0 CheckC);\n" + " void Function2(Template0,\n" + " Template0 > CheckD);\n" + " }\n" + "template class Variadic {};\n" + "Variadic, " + " B::Template1, " + " B::C::MyInt > CheckE;\n" + "}\n"); +} + +} // end anonymous namespace