1 | //===- unittests/AST/ProfilingTest.cpp --- Tests for Profiling ------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "clang/AST/ASTContext.h" |
10 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
11 | #include "clang/ASTMatchers/ASTMatchers.h" |
12 | #include "clang/Tooling/Tooling.h" |
13 | #include "gtest/gtest.h" |
14 | #include <utility> |
15 | |
16 | namespace clang { |
17 | namespace { |
18 | using namespace ast_matchers; |
19 | |
20 | static auto getClassTemplateRedecls() { |
21 | std::string Code = R"cpp( |
22 | template <class> struct A; |
23 | template <class> struct A; |
24 | template <class> struct A; |
25 | )cpp" ; |
26 | auto AST = tooling::buildASTFromCode(Code); |
27 | ASTContext &Ctx = AST->getASTContext(); |
28 | |
29 | auto MatchResults = match(Matcher: classTemplateDecl().bind(ID: "id" ), Context&: Ctx); |
30 | SmallVector<ClassTemplateDecl *, 3> Res; |
31 | for (BoundNodes &N : MatchResults) { |
32 | if (auto *CTD = const_cast<ClassTemplateDecl *>( |
33 | N.getNodeAs<ClassTemplateDecl>(ID: "id" ))) |
34 | Res.push_back(Elt: CTD); |
35 | } |
36 | assert(Res.size() == 3); |
37 | #ifndef NDEBUG |
38 | for (auto &&I : Res) |
39 | assert(I->getCanonicalDecl() == Res[0]); |
40 | #endif |
41 | return std::make_tuple(args: std::move(AST), args&: Res[1], args&: Res[2]); |
42 | } |
43 | |
44 | template <class T> static void testTypeNode(const T *T1, const T *T2) { |
45 | { |
46 | llvm::FoldingSetNodeID ID1, ID2; |
47 | T1->Profile(ID1); |
48 | T2->Profile(ID2); |
49 | ASSERT_NE(ID1, ID2); |
50 | } |
51 | auto *CT1 = cast<T>(T1->getCanonicalTypeInternal()); |
52 | auto *CT2 = cast<T>(T2->getCanonicalTypeInternal()); |
53 | { |
54 | llvm::FoldingSetNodeID ID1, ID2; |
55 | CT1->Profile(ID1); |
56 | CT2->Profile(ID2); |
57 | ASSERT_EQ(ID1, ID2); |
58 | } |
59 | } |
60 | |
61 | TEST(Profiling, DeducedTemplateSpecializationType_Name) { |
62 | auto [AST, CTD1, CTD2] = getClassTemplateRedecls(); |
63 | ASTContext &Ctx = AST->getASTContext(); |
64 | |
65 | auto *T1 = cast<DeducedTemplateSpecializationType>( |
66 | Val: Ctx.getDeducedTemplateSpecializationType(Template: TemplateName(CTD1), DeducedType: QualType(), |
67 | IsDependent: false)); |
68 | auto *T2 = cast<DeducedTemplateSpecializationType>( |
69 | Val: Ctx.getDeducedTemplateSpecializationType(Template: TemplateName(CTD2), DeducedType: QualType(), |
70 | IsDependent: false)); |
71 | testTypeNode(T1, T2); |
72 | } |
73 | |
74 | } // namespace |
75 | } // namespace clang |
76 | |