nridge updated this revision to Diff 355104.
nridge added a comment.

fix comment at top of file


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D104619/new/

https://reviews.llvm.org/D104619

Files:
  clang/lib/AST/TypePrinter.cpp
  clang/unittests/AST/CMakeLists.txt
  clang/unittests/AST/TypePrinterTest.cpp

Index: clang/unittests/AST/TypePrinterTest.cpp
===================================================================
--- /dev/null
+++ clang/unittests/AST/TypePrinterTest.cpp
@@ -0,0 +1,119 @@
+//===- 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 {
+
+using PolicyAdjusterType =
+    Optional<llvm::function_ref<void(PrintingPolicy &Policy)>>;
+
+static void PrintType(raw_ostream &Out, const ASTContext *Context, QualType T,
+                      PolicyAdjusterType PolicyAdjuster) {
+  assert(!T.isNull() && "Expected non-null Type");
+  PrintingPolicy Policy = Context->getPrintingPolicy();
+  if (PolicyAdjuster)
+    (*PolicyAdjuster)(Policy);
+  T.print(Out, Policy);
+}
+
+class PrintMatch : public MatchFinder::MatchCallback {
+  SmallString<1024> Printed;
+  unsigned NumFoundTypes;
+  PolicyAdjusterType PolicyAdjuster;
+
+public:
+  explicit PrintMatch(PolicyAdjusterType PolicyAdjuster)
+      : NumFoundTypes(0), PolicyAdjuster(PolicyAdjuster) {}
+
+  void run(const MatchFinder::MatchResult &Result) override {
+    const QualType *T = Result.Nodes.getNodeAs<QualType>("id");
+    if (!T)
+      return;
+    NumFoundTypes++;
+    if (NumFoundTypes > 1)
+      return;
+
+    llvm::raw_svector_ostream Out(Printed);
+    PrintType(Out, Result.Context, *T, PolicyAdjuster);
+  }
+
+  StringRef getPrinted() const { return Printed; }
+
+  unsigned getNumFoundTypes() const { return NumFoundTypes; }
+};
+
+::testing::AssertionResult
+PrintedTypeMatches(StringRef Code, const std::vector<std::string> &Args,
+                   const DeclarationMatcher &NodeMatch,
+                   StringRef ExpectedPrinted,
+                   PolicyAdjusterType PolicyAdjuster) {
+  PrintMatch Printer(PolicyAdjuster);
+  MatchFinder Finder;
+  Finder.addMatcher(NodeMatch, &Printer);
+  std::unique_ptr<FrontendActionFactory> Factory =
+      newFrontendActionFactory(&Finder);
+
+  if (!runToolOnCodeWithArgs(Factory->create(), Code, Args))
+    return testing::AssertionFailure()
+           << "Parsing error in \"" << Code.str() << "\"";
+
+  if (Printer.getNumFoundTypes() == 0)
+    return testing::AssertionFailure() << "Matcher didn't find any types";
+
+  if (Printer.getNumFoundTypes() > 1)
+    return testing::AssertionFailure() << "Matcher should match only one type "
+                                          "(found "
+                                       << Printer.getNumFoundTypes() << ")";
+
+  if (Printer.getPrinted() != ExpectedPrinted)
+    return ::testing::AssertionFailure()
+           << "Expected \"" << ExpectedPrinted.str()
+           << "\", "
+              "got \""
+           << Printer.getPrinted().str() << "\"";
+
+  return ::testing::AssertionSuccess();
+}
+
+} // 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> &",
+                                 PolicyAdjusterType([](PrintingPolicy &Policy) {
+                                   Policy.FullyQualifiedName = false;
+                                 })));
+
+  ASSERT_TRUE(PrintedTypeMatches(Code, {}, Matcher, "const N::Type<T> &",
+                                 PolicyAdjusterType([](PrintingPolicy &Policy) {
+                                   Policy.FullyQualifiedName = true;
+                                 })));
+}
\ No newline at end of file
Index: clang/unittests/AST/CMakeLists.txt
===================================================================
--- clang/unittests/AST/CMakeLists.txt
+++ clang/unittests/AST/CMakeLists.txt
@@ -29,6 +29,7 @@
   SourceLocationTest.cpp
   StmtPrinterTest.cpp
   StructuralEquivalenceTest.cpp
+  TypePrinterTest.cpp
   )
 
 clang_target_link_libraries(ASTTests
Index: clang/lib/AST/TypePrinter.cpp
===================================================================
--- clang/lib/AST/TypePrinter.cpp
+++ clang/lib/AST/TypePrinter.cpp
@@ -1457,7 +1457,7 @@
 void TypePrinter::printTemplateSpecializationBefore(
                                             const TemplateSpecializationType *T,
                                             raw_ostream &OS) {
-  printTemplateId(T, OS, false);
+  printTemplateId(T, OS, Policy.FullyQualifiedName);
 }
 
 void TypePrinter::printTemplateSpecializationAfter(
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to