llvm-project
75 строк · 2.3 Кб
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
16namespace clang {17namespace {18using namespace ast_matchers;19
20static auto getClassTemplateRedecls() {21std::string Code = R"cpp(22template <class> struct A;
23template <class> struct A;
24template <class> struct A;
25)cpp";26auto AST = tooling::buildASTFromCode(Code);27ASTContext &Ctx = AST->getASTContext();28
29auto MatchResults = match(classTemplateDecl().bind("id"), Ctx);30SmallVector<ClassTemplateDecl *, 3> Res;31for (BoundNodes &N : MatchResults) {32if (auto *CTD = const_cast<ClassTemplateDecl *>(33N.getNodeAs<ClassTemplateDecl>("id")))34Res.push_back(CTD);35}36assert(Res.size() == 3);37#ifndef NDEBUG38for (auto &&I : Res)39assert(I->getCanonicalDecl() == Res[0]);40#endif41return std::make_tuple(std::move(AST), Res[1], Res[2]);42}
43
44template <class T> static void testTypeNode(const T *T1, const T *T2) {45{46llvm::FoldingSetNodeID ID1, ID2;47T1->Profile(ID1);48T2->Profile(ID2);49ASSERT_NE(ID1, ID2);50}51auto *CT1 = cast<T>(T1->getCanonicalTypeInternal());52auto *CT2 = cast<T>(T2->getCanonicalTypeInternal());53{54llvm::FoldingSetNodeID ID1, ID2;55CT1->Profile(ID1);56CT2->Profile(ID2);57ASSERT_EQ(ID1, ID2);58}59}
60
61TEST(Profiling, DeducedTemplateSpecializationType_Name) {62auto [AST, CTD1, CTD2] = getClassTemplateRedecls();63ASTContext &Ctx = AST->getASTContext();64
65auto *T1 = cast<DeducedTemplateSpecializationType>(66Ctx.getDeducedTemplateSpecializationType(TemplateName(CTD1), QualType(),67false));68auto *T2 = cast<DeducedTemplateSpecializationType>(69Ctx.getDeducedTemplateSpecializationType(TemplateName(CTD2), QualType(),70false));71testTypeNode(T1, T2);72}
73
74} // namespace75} // namespace clang76