1 //===- unittests/AST/TemplateNameTest.cpp --- Tests for TemplateName ------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchers.h"
12 #include "llvm/Support/raw_ostream.h"
13 #include "gtest/gtest.h"
17 using namespace ast_matchers
;
19 std::string
printTemplateName(TemplateName TN
, const PrintingPolicy
&Policy
,
20 TemplateName::Qualified Qual
) {
22 llvm::raw_string_ostream
Out(Result
);
23 TN
.print(Out
, Policy
, Qual
);
27 TEST(TemplateName
, PrintUsingTemplate
) {
28 std::string Code
= R
"cpp(
30 template <typename> struct vector {};
32 namespace absl { using std::vector; }
34 template<template <typename> class T> class X;
39 auto AST
= tooling::buildASTFromCode(Code
);
40 ASTContext
&Ctx
= AST
->getASTContext();
41 // Match the template argument vector in X<vector>.
42 auto MatchResults
= match(templateArgumentLoc().bind("id"), Ctx
);
43 const auto *Template
= selectFirst
<TemplateArgumentLoc
>("id", MatchResults
);
44 ASSERT_TRUE(Template
);
46 TemplateName TN
= Template
->getArgument().getAsTemplate();
47 EXPECT_EQ(TN
.getKind(), TemplateName::UsingTemplate
);
48 EXPECT_EQ(TN
.getAsUsingShadowDecl()->getTargetDecl(), TN
.getAsTemplateDecl());
50 EXPECT_EQ(printTemplateName(TN
, Ctx
.getPrintingPolicy(),
51 TemplateName::Qualified::Fully
),
53 EXPECT_EQ(printTemplateName(TN
, Ctx
.getPrintingPolicy(),
54 TemplateName::Qualified::AsWritten
),
56 EXPECT_EQ(printTemplateName(TN
, Ctx
.getPrintingPolicy(),
57 TemplateName::Qualified::None
),
61 TEST(TemplateName
, QualifiedUsingTemplate
) {
62 std::string Code
= R
"cpp(
64 template <typename> struct vector {};
66 namespace absl { using std::vector; }
68 template<template <typename> class T> class X;
70 using A = X<absl::vector>; // QualifiedTemplateName in a template argument.
72 auto AST
= tooling::buildASTFromCode(Code
);
73 // Match the template argument absl::vector in X<absl::vector>.
74 auto Matcher
= templateArgumentLoc().bind("id");
75 auto MatchResults
= match(Matcher
, AST
->getASTContext());
76 const auto *TAL
= MatchResults
.front().getNodeAs
<TemplateArgumentLoc
>("id");
78 TemplateName TN
= TAL
->getArgument().getAsTemplate();
79 EXPECT_EQ(TN
.getKind(), TemplateName::QualifiedTemplate
);
80 const auto *QTN
= TN
.getAsQualifiedTemplateName();
81 // Verify that we have the Using template name in the QualifiedTemplateName.
82 const auto *USD
= QTN
->getUnderlyingTemplate().getAsUsingShadowDecl();
84 EXPECT_EQ(USD
->getTargetDecl(), TN
.getAsTemplateDecl());
85 EXPECT_EQ(TN
.getAsUsingShadowDecl(), USD
);
88 TEST(TemplateName
, UsingTemplate
) {
89 auto AST
= tooling::buildASTFromCode(R
"cpp(
91 template <typename T> struct vector { vector(T); };
93 namespace absl { using std::vector; }
94 // The "absl::vector
<int>" is an elaborated TemplateSpecializationType with
95 // an inner Using TemplateName (not a Qualified TemplateName, the qualifiers
96 // are rather part of the ElaboratedType)!
97 absl::vector<int> v(123);
99 auto Matcher
= elaboratedTypeLoc(
100 hasNamedTypeLoc(loc(templateSpecializationType().bind("id"))));
101 auto MatchResults
= match(Matcher
, AST
->getASTContext());
103 MatchResults
.front().getNodeAs
<TemplateSpecializationType
>("id");
105 EXPECT_EQ(TST
->getTemplateName().getKind(), TemplateName::UsingTemplate
);
107 AST
= tooling::buildASTFromCodeWithArgs(R
"cpp(
109 template <typename T> struct vector { vector(T); };
111 namespace absl { using std::vector; }
112 // Similiar to the TemplateSpecializationType, absl::vector is an elaborated
113 // DeducedTemplateSpecializationType with an inner Using TemplateName!
114 absl::vector DTST(123);
117 Matcher
= elaboratedTypeLoc(
118 hasNamedTypeLoc(loc(deducedTemplateSpecializationType().bind("id"))));
119 MatchResults
= match(Matcher
, AST
->getASTContext());
121 MatchResults
.front().getNodeAs
<DeducedTemplateSpecializationType
>("id");
123 EXPECT_EQ(DTST
->getTemplateName().getKind(), TemplateName::UsingTemplate
);