xref: /llvm-project/clang/unittests/AST/ProfilingTest.cpp (revision 8b435c18314e62530367a8721883a28c532e02ad)
12e1ad939SMatheus Izvekov //===- unittests/AST/ProfilingTest.cpp --- Tests for Profiling ------===//
22e1ad939SMatheus Izvekov //
32e1ad939SMatheus Izvekov // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42e1ad939SMatheus Izvekov // See https://llvm.org/LICENSE.txt for license information.
52e1ad939SMatheus Izvekov // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62e1ad939SMatheus Izvekov //
72e1ad939SMatheus Izvekov //===----------------------------------------------------------------------===//
82e1ad939SMatheus Izvekov 
92e1ad939SMatheus Izvekov #include "clang/AST/ASTContext.h"
102e1ad939SMatheus Izvekov #include "clang/ASTMatchers/ASTMatchFinder.h"
112e1ad939SMatheus Izvekov #include "clang/ASTMatchers/ASTMatchers.h"
122e1ad939SMatheus Izvekov #include "clang/Tooling/Tooling.h"
132e1ad939SMatheus Izvekov #include "gtest/gtest.h"
142e1ad939SMatheus Izvekov #include <utility>
152e1ad939SMatheus Izvekov 
162e1ad939SMatheus Izvekov namespace clang {
172e1ad939SMatheus Izvekov namespace {
182e1ad939SMatheus Izvekov using namespace ast_matchers;
192e1ad939SMatheus Izvekov 
getClassTemplateRedecls()202e1ad939SMatheus Izvekov static auto getClassTemplateRedecls() {
212e1ad939SMatheus Izvekov   std::string Code = R"cpp(
222e1ad939SMatheus Izvekov     template <class> struct A;
232e1ad939SMatheus Izvekov     template <class> struct A;
242e1ad939SMatheus Izvekov     template <class> struct A;
252e1ad939SMatheus Izvekov   )cpp";
262e1ad939SMatheus Izvekov   auto AST = tooling::buildASTFromCode(Code);
272e1ad939SMatheus Izvekov   ASTContext &Ctx = AST->getASTContext();
282e1ad939SMatheus Izvekov 
292e1ad939SMatheus Izvekov   auto MatchResults = match(classTemplateDecl().bind("id"), Ctx);
302e1ad939SMatheus Izvekov   SmallVector<ClassTemplateDecl *, 3> Res;
312e1ad939SMatheus Izvekov   for (BoundNodes &N : MatchResults) {
322e1ad939SMatheus Izvekov     if (auto *CTD = const_cast<ClassTemplateDecl *>(
332e1ad939SMatheus Izvekov             N.getNodeAs<ClassTemplateDecl>("id")))
342e1ad939SMatheus Izvekov       Res.push_back(CTD);
352e1ad939SMatheus Izvekov   }
362e1ad939SMatheus Izvekov   assert(Res.size() == 3);
37*8b435c18SNAKAMURA Takumi #ifndef NDEBUG
382e1ad939SMatheus Izvekov   for (auto &&I : Res)
392e1ad939SMatheus Izvekov     assert(I->getCanonicalDecl() == Res[0]);
40*8b435c18SNAKAMURA Takumi #endif
412e1ad939SMatheus Izvekov   return std::make_tuple(std::move(AST), Res[1], Res[2]);
422e1ad939SMatheus Izvekov }
432e1ad939SMatheus Izvekov 
testTypeNode(const T * T1,const T * T2)442e1ad939SMatheus Izvekov template <class T> static void testTypeNode(const T *T1, const T *T2) {
452e1ad939SMatheus Izvekov   {
462e1ad939SMatheus Izvekov     llvm::FoldingSetNodeID ID1, ID2;
472e1ad939SMatheus Izvekov     T1->Profile(ID1);
482e1ad939SMatheus Izvekov     T2->Profile(ID2);
492e1ad939SMatheus Izvekov     ASSERT_NE(ID1, ID2);
502e1ad939SMatheus Izvekov   }
512e1ad939SMatheus Izvekov   auto *CT1 = cast<T>(T1->getCanonicalTypeInternal());
522e1ad939SMatheus Izvekov   auto *CT2 = cast<T>(T2->getCanonicalTypeInternal());
532e1ad939SMatheus Izvekov   {
542e1ad939SMatheus Izvekov     llvm::FoldingSetNodeID ID1, ID2;
552e1ad939SMatheus Izvekov     CT1->Profile(ID1);
562e1ad939SMatheus Izvekov     CT2->Profile(ID2);
572e1ad939SMatheus Izvekov     ASSERT_EQ(ID1, ID2);
582e1ad939SMatheus Izvekov   }
592e1ad939SMatheus Izvekov }
602e1ad939SMatheus Izvekov 
TEST(Profiling,DeducedTemplateSpecializationType_Name)612e1ad939SMatheus Izvekov TEST(Profiling, DeducedTemplateSpecializationType_Name) {
622e1ad939SMatheus Izvekov   auto [AST, CTD1, CTD2] = getClassTemplateRedecls();
632e1ad939SMatheus Izvekov   ASTContext &Ctx = AST->getASTContext();
642e1ad939SMatheus Izvekov 
652e1ad939SMatheus Izvekov   auto *T1 = cast<DeducedTemplateSpecializationType>(
662e1ad939SMatheus Izvekov       Ctx.getDeducedTemplateSpecializationType(TemplateName(CTD1), QualType(),
672e1ad939SMatheus Izvekov                                                false));
682e1ad939SMatheus Izvekov   auto *T2 = cast<DeducedTemplateSpecializationType>(
692e1ad939SMatheus Izvekov       Ctx.getDeducedTemplateSpecializationType(TemplateName(CTD2), QualType(),
702e1ad939SMatheus Izvekov                                                false));
712e1ad939SMatheus Izvekov   testTypeNode(T1, T2);
722e1ad939SMatheus Izvekov }
732e1ad939SMatheus Izvekov 
742e1ad939SMatheus Izvekov } // namespace
752e1ad939SMatheus Izvekov } // namespace clang
76