#include "ASTPrint.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
namespace clang {
namespace {
using namespace ast_matchers;
std::string printTemplateName(TemplateName TN, const PrintingPolicy &Policy,
TemplateName::Qualified Qual) {
std::string Result;
llvm::raw_string_ostream Out(Result);
TN.print(Out, Policy, Qual);
return Out.str();
}
TEST(TemplateName, PrintUsingTemplate) {
std::string Code = R"cpp(
namespace std {
template <typename> struct vector {};
}
namespace absl { using std::vector; }
template<template <typename> class T> class X;
using absl::vector;
using A = X<vector>;
)cpp";
auto AST = tooling::buildASTFromCode(Code);
ASTContext &Ctx = AST->getASTContext();
auto MatchResults = match(templateArgumentLoc().bind("id"), Ctx);
const auto *Template = selectFirst<TemplateArgumentLoc>("id", MatchResults);
ASSERT_TRUE(Template);
TemplateName TN = Template->getArgument().getAsTemplate();
EXPECT_EQ(TN.getKind(), TemplateName::UsingTemplate);
EXPECT_EQ(TN.getAsUsingShadowDecl()->getTargetDecl(), TN.getAsTemplateDecl());
EXPECT_EQ(printTemplateName(TN, Ctx.getPrintingPolicy(),
TemplateName::Qualified::Fully),
"std::vector");
EXPECT_EQ(printTemplateName(TN, Ctx.getPrintingPolicy(),
TemplateName::Qualified::AsWritten),
"vector");
EXPECT_EQ(printTemplateName(TN, Ctx.getPrintingPolicy(),
TemplateName::Qualified::None),
"vector");
}
TEST(TemplateName, QualifiedUsingTemplate) {
std::string Code = R"cpp(
namespace std {
template <typename> struct vector {};
}
namespace absl { using std::vector; }
template<template <typename> class T> class X;
using A = X<absl::vector>; // QualifiedTemplateName in a template argument.
)cpp";
auto AST = tooling::buildASTFromCode(Code);
auto Matcher = templateArgumentLoc().bind("id");
auto MatchResults = match(Matcher, AST->getASTContext());
const auto *TAL = MatchResults.front().getNodeAs<TemplateArgumentLoc>("id");
ASSERT_TRUE(TAL);
TemplateName TN = TAL->getArgument().getAsTemplate();
EXPECT_EQ(TN.getKind(), TemplateName::QualifiedTemplate);
const auto *QTN = TN.getAsQualifiedTemplateName();
const auto *USD = QTN->getUnderlyingTemplate().getAsUsingShadowDecl();
EXPECT_TRUE(USD);
EXPECT_EQ(USD->getTargetDecl(), TN.getAsTemplateDecl());
EXPECT_EQ(TN.getAsUsingShadowDecl(), USD);
}
TEST(TemplateName, UsingTemplate) {
auto AST = tooling::buildASTFromCode(R"cpp(
namespace std {
template <typename T> struct vector { vector(T); };
}
namespace absl { using std::vector; }
// The "absl::vector<int>" is an elaborated TemplateSpecializationType with
// an inner Using TemplateName (not a Qualified TemplateName, the qualifiers
// are rather part of the ElaboratedType)!
absl::vector<int> v(123);
)cpp");
auto Matcher = elaboratedTypeLoc(
hasNamedTypeLoc(loc(templateSpecializationType().bind("id"))));
auto MatchResults = match(Matcher, AST->getASTContext());
const auto *TST =
MatchResults.front().getNodeAs<TemplateSpecializationType>("id");
ASSERT_TRUE(TST);
EXPECT_EQ(TST->getTemplateName().getKind(), TemplateName::UsingTemplate);
AST = tooling::buildASTFromCodeWithArgs(R"cpp(
namespace std {
template <typename T> struct vector { vector(T); };
}
namespace absl { using std::vector; }
// Similiar to the TemplateSpecializationType, absl::vector is an elaborated
// DeducedTemplateSpecializationType with an inner Using TemplateName!
absl::vector DTST(123);
)cpp",
{"-std=c++17"});
Matcher = elaboratedTypeLoc(
hasNamedTypeLoc(loc(deducedTemplateSpecializationType().bind("id"))));
MatchResults = match(Matcher, AST->getASTContext());
const auto *DTST =
MatchResults.front().getNodeAs<DeducedTemplateSpecializationType>("id");
ASSERT_TRUE(DTST);
EXPECT_EQ(DTST->getTemplateName().getKind(), TemplateName::UsingTemplate);
}
} }