1 //===- unittest/Tooling/QualTypeNameTest.cpp ------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "clang/Tooling/Core/QualTypeNames.h" 11 #include "TestVisitor.h" 12 using namespace clang; 13 14 namespace { 15 struct TypeNameVisitor : TestVisitor<TypeNameVisitor> { 16 llvm::StringMap<std::string> ExpectedQualTypeNames; 17 18 // ValueDecls are the least-derived decl with both a qualtype and a 19 // name. 20 bool traverseDecl(Decl *D) { 21 return true; // Always continue 22 } 23 24 bool VisitValueDecl(const ValueDecl *VD) { 25 std::string ExpectedName = 26 ExpectedQualTypeNames.lookup(VD->getNameAsString()); 27 if (ExpectedName != "") { 28 std::string ActualName = 29 TypeName::getFullyQualifiedName(VD->getType(), *Context); 30 if (ExpectedName != ActualName) { 31 // A custom message makes it much easier to see what declaration 32 // failed compared to EXPECT_EQ. 33 EXPECT_TRUE(false) << "Typename::getFullyQualifiedName failed for " 34 << VD->getQualifiedNameAsString() << std::endl 35 << " Actual: " << ActualName << std::endl 36 << " Exepcted: " << ExpectedName; 37 } 38 } 39 return true; 40 } 41 }; 42 43 // named namespaces inside anonymous namespaces 44 45 TEST(QualTypeNameTest, getFullyQualifiedName) { 46 TypeNameVisitor Visitor; 47 // Simple case to test the test framework itself. 48 Visitor.ExpectedQualTypeNames["CheckInt"] = "int"; 49 50 // Keeping the names of the variables whose types we check unique 51 // within the entire test--regardless of their own scope--makes it 52 // easier to diagnose test failures. 53 54 // Simple namespace qualifier 55 Visitor.ExpectedQualTypeNames["CheckA"] = "A::B::Class0"; 56 // Lookup up the enclosing scopes, then down another one. (These 57 // appear as elaborated type in the AST. In that case--even if 58 // policy.SuppressScope = 0--qual_type.getAsString(policy) only 59 // gives the name as it appears in the source, not the full name. 60 Visitor.ExpectedQualTypeNames["CheckB"] = "A::B::C::Class1"; 61 // Template parameter expansion. 62 Visitor.ExpectedQualTypeNames["CheckC"] = 63 "A::B::Template0<A::B::C::MyInt, A::B::AnotherClass>"; 64 // Recursive template parameter expansion. 65 Visitor.ExpectedQualTypeNames["CheckD"] = 66 "A::B::Template0<A::B::Template1<A::B::C::MyInt, A::B::AnotherClass>, " 67 "A::B::Template0<int, long> >"; 68 // Variadic Template expansion. 69 Visitor.ExpectedQualTypeNames["CheckE"] = 70 "A::Variadic<int, A::B::Template0<int, char>, " 71 "A::B::Template1<int, long>, A::B::C::MyInt>"; 72 // Using declarations should be fully expanded. 73 Visitor.ExpectedQualTypeNames["CheckF"] = "A::B::Class0"; 74 // Elements found within "using namespace foo;" should be fully 75 // expanded. 76 Visitor.ExpectedQualTypeNames["CheckG"] = "A::B::C::MyInt"; 77 // Type inside function 78 Visitor.ExpectedQualTypeNames["CheckH"] = "struct X"; 79 // Anonymous Namespaces 80 Visitor.ExpectedQualTypeNames["CheckI"] = "aClass"; 81 // Keyword inclusion with namespaces 82 Visitor.ExpectedQualTypeNames["CheckJ"] = "struct A::aStruct"; 83 // Anonymous Namespaces nested in named namespaces and vice-versa. 84 Visitor.ExpectedQualTypeNames["CheckK"] = "D::aStruct"; 85 // Namespace alias 86 Visitor.ExpectedQualTypeNames["CheckL"] = "A::B::C::MyInt"; 87 Visitor.ExpectedQualTypeNames["non_dependent_type_var"] = 88 "Foo<X>::non_dependent_type"; 89 Visitor.ExpectedQualTypeNames["AnEnumVar"] = "EnumScopeClass::AnEnum"; 90 Visitor.runOver( 91 "int CheckInt;\n" 92 "namespace A {\n" 93 " namespace B {\n" 94 " class Class0 { };\n" 95 " namespace C {\n" 96 " typedef int MyInt;" 97 " }\n" 98 " template<class X, class Y> class Template0;" 99 " template<class X, class Y> class Template1;" 100 " typedef B::Class0 AnotherClass;\n" 101 " void Function1(Template0<C::MyInt,\n" 102 " AnotherClass> CheckC);\n" 103 " void Function2(Template0<Template1<C::MyInt, AnotherClass>,\n" 104 " Template0<int, long> > CheckD);\n" 105 " }\n" 106 "template<typename... Values> class Variadic {};\n" 107 "Variadic<int, B::Template0<int, char>, " 108 " B::Template1<int, long>, " 109 " B::C::MyInt > CheckE;\n" 110 " namespace BC = B::C;\n" 111 " BC::MyInt CheckL;\n" 112 "}\n" 113 "using A::B::Class0;\n" 114 "void Function(Class0 CheckF);\n" 115 "using namespace A::B::C;\n" 116 "void Function(MyInt CheckG);\n" 117 "void f() {\n" 118 " struct X {} CheckH;\n" 119 "}\n" 120 "namespace {\n" 121 " class aClass {};\n" 122 " aClass CheckI;\n" 123 "}\n" 124 "namespace A {\n" 125 " struct aStruct {} CheckJ;\n" 126 "}\n" 127 "namespace {\n" 128 " namespace D {\n" 129 " namespace {\n" 130 " class aStruct {};\n" 131 " aStruct CheckK;\n" 132 " }\n" 133 " }\n" 134 "}\n" 135 "template<class T> struct Foo {\n" 136 " typedef typename T::A dependent_type;\n" 137 " typedef int non_dependent_type;\n" 138 " dependent_type dependent_type_var;\n" 139 " non_dependent_type non_dependent_type_var;\n" 140 "};\n" 141 "struct X { typedef int A; };" 142 "Foo<X> var;" 143 "void F() {\n" 144 " var.dependent_type_var = 0;\n" 145 "var.non_dependent_type_var = 0;\n" 146 "}\n" 147 "class EnumScopeClass {\n" 148 "public:\n" 149 " enum AnEnum { ZERO, ONE };\n" 150 "};\n" 151 "EnumScopeClass::AnEnum AnEnumVar;\n" 152 ); 153 154 TypeNameVisitor Complex; 155 Complex.ExpectedQualTypeNames["CheckTX"] = "B::TX"; 156 Complex.runOver( 157 "namespace A {" 158 " struct X {};" 159 "}" 160 "using A::X;" 161 "namespace fake_std {" 162 " template<class... Types > class tuple {};" 163 "}" 164 "namespace B {" 165 " using fake_std::tuple;" 166 " typedef tuple<X> TX;" 167 " TX CheckTX;" 168 " struct A { typedef int X; };" 169 "}"); 170 } 171 172 } // end anonymous namespace 173