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 
14 using namespace llvm;
15 
16 namespace {
17 
canSpecializeGraphTraitsIterators(Ty * G)18 template <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<decltype(*I), NodeRef>::value,
27                 "Node type does not match");
28   static_assert(std::is_same<decltype(*X), NodeRef>::value,
29                 "Node type does not match");
30   static_assert(std::is_same<decltype(*E), NodeRef>::value,
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<decltype(*S), NodeRef>::value,
40                 "Node type does not match");
41   static_assert(std::is_same<decltype(*F), NodeRef>::value,
42                 "Node type does not match");
43 }
44 
TEST(CallGraphTest,GraphTraitsSpecialization)45 TEST(CallGraphTest, GraphTraitsSpecialization) {
46   LLVMContext Context;
47   Module M("", Context);
48   CallGraph CG(M);
49 
50   canSpecializeGraphTraitsIterators(&CG);
51 }
52 
TEST(CallGraphTest,GraphTraitsConstSpecialization)53 TEST(CallGraphTest, GraphTraitsConstSpecialization) {
54   LLVMContext Context;
55   Module M("", Context);
56   CallGraph CG(M);
57 
58   canSpecializeGraphTraitsIterators(const_cast<const CallGraph *>(&CG));
59 }
60 }
61