]> granicus.if.org Git - clang/commitdiff
Allow modifying the PrintingPolicy for fully qualified names.
authorSterling Augustine <saugustine@google.com>
Fri, 4 May 2018 20:12:39 +0000 (20:12 +0000)
committerSterling Augustine <saugustine@google.com>
Fri, 4 May 2018 20:12:39 +0000 (20:12 +0000)
Author: mikhail.ramalho@gmail.com

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@331552 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/QualTypeNames.h
lib/AST/QualTypeNames.cpp
unittests/Tooling/QualTypeNamesTest.cpp

index 86d805feeed764436b77fcdbce538440edad7f61..e2bf7dfdefd306b49b54bc3a943a9d268f4be613 100644 (file)
@@ -72,6 +72,7 @@ namespace TypeName {
 /// \param[in] WithGlobalNsPrefix - If true, then the global namespace
 /// specifier "::" will be prepended to the fully qualified name.
 std::string getFullyQualifiedName(QualType QT, const ASTContext &Ctx,
+                                  const PrintingPolicy &Policy,
                                   bool WithGlobalNsPrefix = false);
 
 /// \brief Generates a QualType that can be used to name the same type
index 86c0eff9f78c31fbeea0454951b0f7cba369e48d..9b773231d2d2e41b1012dd5d4452130d4b567670 100644 (file)
@@ -452,12 +452,8 @@ QualType getFullyQualifiedType(QualType QT, const ASTContext &Ctx,
 
 std::string getFullyQualifiedName(QualType QT,
                                   const ASTContext &Ctx,
+                                  const PrintingPolicy &Policy,
                                   bool WithGlobalNsPrefix) {
-  PrintingPolicy Policy(Ctx.getPrintingPolicy());
-  Policy.SuppressScope = false;
-  Policy.AnonymousTagLocations = false;
-  Policy.PolishForDeclaration = true;
-  Policy.SuppressUnwrittenScope = true;
   QualType FQQT = getFullyQualifiedType(QT, Ctx, WithGlobalNsPrefix);
   return FQQT.getAsString(Policy);
 }
index dd48f3bf4595d0e7529faee392bb4959f25d31cf..b4c56f7bd5c1079d53e982151d2ae423e2392863 100644 (file)
@@ -26,9 +26,13 @@ struct TypeNameVisitor : TestVisitor<TypeNameVisitor> {
     std::string ExpectedName =
         ExpectedQualTypeNames.lookup(VD->getNameAsString());
     if (ExpectedName != "") {
-      std::string ActualName =
-          TypeName::getFullyQualifiedName(VD->getType(), *Context,
-                                          WithGlobalNsPrefix);
+      PrintingPolicy Policy(Context->getPrintingPolicy());
+      Policy.SuppressScope = false;
+      Policy.AnonymousTagLocations = true;
+      Policy.PolishForDeclaration = true;
+      Policy.SuppressUnwrittenScope = true;
+      std::string ActualName = TypeName::getFullyQualifiedName(
+          VD->getType(), *Context, Policy, WithGlobalNsPrefix);
       if (ExpectedName != ActualName) {
         // A custom message makes it much easier to see what declaration
         // failed compared to EXPECT_EQ.
@@ -217,6 +221,26 @@ TEST(QualTypeNameTest, getFullyQualifiedName) {
       "  }\n"
       "}\n"
   );
+
+  TypeNameVisitor AnonStrucs;
+  AnonStrucs.ExpectedQualTypeNames["a"] = "short";
+  AnonStrucs.ExpectedQualTypeNames["un_in_st_1"] =
+      "union (anonymous struct at input.cc:1:1)::(anonymous union at "
+      "input.cc:2:27)";
+  AnonStrucs.ExpectedQualTypeNames["b"] = "short";
+  AnonStrucs.ExpectedQualTypeNames["un_in_st_2"] =
+      "union (anonymous struct at input.cc:1:1)::(anonymous union at "
+      "input.cc:5:27)";
+  AnonStrucs.ExpectedQualTypeNames["anon_st"] =
+      "struct (anonymous struct at input.cc:1:1)";
+  AnonStrucs.runOver(R"(struct {
+                          union {
+                            short a;
+                          } un_in_st_1;
+                          union {
+                            short b;
+                          } un_in_st_2;
+                        } anon_st;)");
 }
 
 }  // end anonymous namespace