[clang] 08b2898 - [clang] Respect PrintingPolicy::FullyQualifiedName when printing a template-id

Nathan Ridge via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 19 14:32:04 PDT 2021


Author: Nathan Ridge
Date: 2021-07-19T17:31:51-04:00
New Revision: 08b289867b5adb45033db501461915234e9e1bd4

URL: https://github.com/llvm/llvm-project/commit/08b289867b5adb45033db501461915234e9e1bd4
DIFF: https://github.com/llvm/llvm-project/commit/08b289867b5adb45033db501461915234e9e1bd4.diff

LOG: [clang] Respect PrintingPolicy::FullyQualifiedName when printing a template-id

Fixes PR50774

Differential Revision: https://reviews.llvm.org/D104619

Added: 
    clang/unittests/AST/TypePrinterTest.cpp

Modified: 
    clang/lib/AST/TypePrinter.cpp
    clang/unittests/AST/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp
index a866ca978bc8f..5de22f76f4584 100644
--- a/clang/lib/AST/TypePrinter.cpp
+++ b/clang/lib/AST/TypePrinter.cpp
@@ -1459,7 +1459,7 @@ void TypePrinter::printTemplateId(const TemplateSpecializationType *T,
 void TypePrinter::printTemplateSpecializationBefore(
                                             const TemplateSpecializationType *T,
                                             raw_ostream &OS) {
-  printTemplateId(T, OS, false);
+  printTemplateId(T, OS, Policy.FullyQualifiedName);
 }
 
 void TypePrinter::printTemplateSpecializationAfter(

diff  --git a/clang/unittests/AST/CMakeLists.txt b/clang/unittests/AST/CMakeLists.txt
index 105bfd77df905..b04e2bdc20e8b 100644
--- a/clang/unittests/AST/CMakeLists.txt
+++ b/clang/unittests/AST/CMakeLists.txt
@@ -29,6 +29,7 @@ add_clang_unittest(ASTTests
   SourceLocationTest.cpp
   StmtPrinterTest.cpp
   StructuralEquivalenceTest.cpp
+  TypePrinterTest.cpp
   )
 
 clang_target_link_libraries(ASTTests

diff  --git a/clang/unittests/AST/TypePrinterTest.cpp b/clang/unittests/AST/TypePrinterTest.cpp
new file mode 100644
index 0000000000000..07dc21a88fba1
--- /dev/null
+++ b/clang/unittests/AST/TypePrinterTest.cpp
@@ -0,0 +1,65 @@
+//===- unittests/AST/TypePrinterTest.cpp --- Type printer tests -----------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains tests for QualType::print() and related methods.
+//
+//===----------------------------------------------------------------------===//
+
+#include "ASTPrint.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Tooling/Tooling.h"
+#include "llvm/ADT/SmallString.h"
+#include "gtest/gtest.h"
+
+using namespace clang;
+using namespace ast_matchers;
+using namespace tooling;
+
+namespace {
+
+static void PrintType(raw_ostream &Out, const ASTContext *Context,
+                      const QualType *T,
+                      PrintingPolicyAdjuster PolicyAdjuster) {
+  assert(T && !T->isNull() && "Expected non-null Type");
+  PrintingPolicy Policy = Context->getPrintingPolicy();
+  if (PolicyAdjuster)
+    PolicyAdjuster(Policy);
+  T->print(Out, Policy);
+}
+
+::testing::AssertionResult
+PrintedTypeMatches(StringRef Code, const std::vector<std::string> &Args,
+                   const DeclarationMatcher &NodeMatch,
+                   StringRef ExpectedPrinted,
+                   PrintingPolicyAdjuster PolicyAdjuster) {
+  return PrintedNodeMatches<QualType>(Code, Args, NodeMatch, ExpectedPrinted,
+                                      "", PrintType, PolicyAdjuster);
+}
+
+} // unnamed namespace
+
+TEST(TypePrinter, TemplateId) {
+  std::string Code = R"cpp(
+    namespace N {
+      template <typename> struct Type {};
+      
+      template <typename T>
+      void Foo(const Type<T> &Param);
+    }
+  )cpp";
+  auto Matcher = parmVarDecl(hasType(qualType().bind("id")));
+
+  ASSERT_TRUE(PrintedTypeMatches(
+      Code, {}, Matcher, "const Type<T> &",
+      [](PrintingPolicy &Policy) { Policy.FullyQualifiedName = false; }));
+
+  ASSERT_TRUE(PrintedTypeMatches(
+      Code, {}, Matcher, "const N::Type<T> &",
+      [](PrintingPolicy &Policy) { Policy.FullyQualifiedName = true; }));
+}
\ No newline at end of file


        


More information about the cfe-commits mailing list