QualTypeNamesTest.cpp 5.8 KB

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