1//=======- CallGraphTest.cpp - Unit tests for the CG analysis -------------===//
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 "llvm/Analysis/CallGraph.h"
10#include "llvm/IR/LLVMContext.h"
11#include "llvm/IR/Module.h"
12#include "gtest/gtest.h"
13
14using namespace llvm;
15
16namespace {
17
18template <typename Ty> void canSpecializeGraphTraitsIterators(Ty *G) {
19 typedef typename GraphTraits<Ty *>::NodeRef NodeRef;
20
21 auto I = GraphTraits<Ty *>::nodes_begin(G);
22 auto E = GraphTraits<Ty *>::nodes_end(G);
23 auto X = ++I;
24
25 // Should be able to iterate over all nodes of the graph.
26 static_assert(std::is_same_v<decltype(*I), NodeRef>,
27 "Node type does not match");
28 static_assert(std::is_same_v<decltype(*X), NodeRef>,
29 "Node type does not match");
30 static_assert(std::is_same_v<decltype(*E), NodeRef>,
31 "Node type does not match");
32
33 NodeRef N = GraphTraits<Ty *>::getEntryNode(G);
34
35 auto S = GraphTraits<NodeRef>::child_begin(N);
36 auto F = GraphTraits<NodeRef>::child_end(N);
37
38 // Should be able to iterate over immediate successors of a node.
39 static_assert(std::is_same_v<decltype(*S), NodeRef>,
40 "Node type does not match");
41 static_assert(std::is_same_v<decltype(*F), NodeRef>,
42 "Node type does not match");
43}
44
45TEST(CallGraphTest, GraphTraitsSpecialization) {
46 LLVMContext Context;
47 Module M("", Context);
48 CallGraph CG(M);
49
50 canSpecializeGraphTraitsIterators(G: &CG);
51}
52
53TEST(CallGraphTest, GraphTraitsConstSpecialization) {
54 LLVMContext Context;
55 Module M("", Context);
56 CallGraph CG(M);
57
58 canSpecializeGraphTraitsIterators(G: const_cast<const CallGraph *>(&CG));
59}
60}
61

source code of llvm/unittests/Analysis/CallGraphTest.cpp