xref: /llvm-project/llvm/unittests/Analysis/CallGraphTest.cpp (revision eaa3dccfafec4b7fadc1fe994ce1225c27f66082)
1 //=======- CallGraphTest.cpp - Unit tests for the CG analysis -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/Analysis/CallGraph.h"
11 #include "llvm/IR/LLVMContext.h"
12 #include "llvm/IR/Module.h"
13 #include "gtest/gtest.h"
14 
15 using namespace llvm;
16 
17 namespace {
18 
19 template <typename Ty> void canSpecializeGraphTraitsIterators(Ty *G) {
20   typedef typename GraphTraits<Ty *>::NodeType NodeTy;
21 
22   auto I = GraphTraits<Ty *>::nodes_begin(G);
23   auto E = GraphTraits<Ty *>::nodes_end(G);
24   auto X = ++I;
25 
26   // Should be able to iterate over all nodes of the graph.
27   static_assert(std::is_same<decltype(*I), NodeTy &>::value,
28                 "Node type does not match");
29   static_assert(std::is_same<decltype(*X), NodeTy &>::value,
30                 "Node type does not match");
31   static_assert(std::is_same<decltype(*E), NodeTy &>::value,
32                 "Node type does not match");
33 
34   NodeTy *N = GraphTraits<Ty *>::getEntryNode(G);
35 
36   auto S = GraphTraits<NodeTy *>::child_begin(N);
37   auto F = GraphTraits<NodeTy *>::child_end(N);
38   auto Y = ++S;
39 
40   // Should be able to iterate over immediate successors of a node.
41   static_assert(std::is_same<decltype(*S), NodeTy *>::value,
42                 "Node type does not match");
43   static_assert(std::is_same<decltype(*F), NodeTy *>::value,
44                 "Node type does not match");
45   static_assert(std::is_same<decltype(*Y), NodeTy *>::value,
46                 "Node type does not match");
47 }
48 
49 TEST(CallGraphTest, GraphTraitsSpecialization) {
50   Module M("", getGlobalContext());
51   CallGraph CG(M);
52 
53   canSpecializeGraphTraitsIterators(&CG);
54 }
55 
56 TEST(CallGraphTest, GraphTraitsConstSpecialization) {
57   Module M("", getGlobalContext());
58   CallGraph CG(M);
59 
60   canSpecializeGraphTraitsIterators(const_cast<const CallGraph *>(&CG));
61 }
62 }
63