1eaa3dccfSRafael Espindola //=======- CallGraphTest.cpp - Unit tests for the CG analysis -------------===//
2eaa3dccfSRafael Espindola //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6eaa3dccfSRafael Espindola //
7eaa3dccfSRafael Espindola //===----------------------------------------------------------------------===//
8eaa3dccfSRafael Espindola 
9eaa3dccfSRafael Espindola #include "llvm/Analysis/CallGraph.h"
10eaa3dccfSRafael Espindola #include "llvm/IR/LLVMContext.h"
11eaa3dccfSRafael Espindola #include "llvm/IR/Module.h"
12eaa3dccfSRafael Espindola #include "gtest/gtest.h"
13eaa3dccfSRafael Espindola 
14eaa3dccfSRafael Espindola using namespace llvm;
15eaa3dccfSRafael Espindola 
16eaa3dccfSRafael Espindola namespace {
17eaa3dccfSRafael Espindola 
canSpecializeGraphTraitsIterators(Ty * G)18eaa3dccfSRafael Espindola template <typename Ty> void canSpecializeGraphTraitsIterators(Ty *G) {
19f2187ed3STim Shen   typedef typename GraphTraits<Ty *>::NodeRef NodeRef;
20eaa3dccfSRafael Espindola 
21eaa3dccfSRafael Espindola   auto I = GraphTraits<Ty *>::nodes_begin(G);
22eaa3dccfSRafael Espindola   auto E = GraphTraits<Ty *>::nodes_end(G);
23eaa3dccfSRafael Espindola   auto X = ++I;
24eaa3dccfSRafael Espindola 
25eaa3dccfSRafael Espindola   // Should be able to iterate over all nodes of the graph.
26f2187ed3STim Shen   static_assert(std::is_same<decltype(*I), NodeRef>::value,
27eaa3dccfSRafael Espindola                 "Node type does not match");
28f2187ed3STim Shen   static_assert(std::is_same<decltype(*X), NodeRef>::value,
29eaa3dccfSRafael Espindola                 "Node type does not match");
30f2187ed3STim Shen   static_assert(std::is_same<decltype(*E), NodeRef>::value,
31eaa3dccfSRafael Espindola                 "Node type does not match");
32eaa3dccfSRafael Espindola 
33f2187ed3STim Shen   NodeRef N = GraphTraits<Ty *>::getEntryNode(G);
34eaa3dccfSRafael Espindola 
35f2187ed3STim Shen   auto S = GraphTraits<NodeRef>::child_begin(N);
36f2187ed3STim Shen   auto F = GraphTraits<NodeRef>::child_end(N);
37eaa3dccfSRafael Espindola 
38eaa3dccfSRafael Espindola   // Should be able to iterate over immediate successors of a node.
39f2187ed3STim Shen   static_assert(std::is_same<decltype(*S), NodeRef>::value,
40eaa3dccfSRafael Espindola                 "Node type does not match");
41f2187ed3STim Shen   static_assert(std::is_same<decltype(*F), NodeRef>::value,
42eaa3dccfSRafael Espindola                 "Node type does not match");
43eaa3dccfSRafael Espindola }
44eaa3dccfSRafael Espindola 
TEST(CallGraphTest,GraphTraitsSpecialization)45eaa3dccfSRafael Espindola TEST(CallGraphTest, GraphTraitsSpecialization) {
4603b42e41SMehdi Amini   LLVMContext Context;
4703b42e41SMehdi Amini   Module M("", Context);
48eaa3dccfSRafael Espindola   CallGraph CG(M);
49eaa3dccfSRafael Espindola 
50eaa3dccfSRafael Espindola   canSpecializeGraphTraitsIterators(&CG);
51eaa3dccfSRafael Espindola }
52eaa3dccfSRafael Espindola 
TEST(CallGraphTest,GraphTraitsConstSpecialization)53eaa3dccfSRafael Espindola TEST(CallGraphTest, GraphTraitsConstSpecialization) {
5403b42e41SMehdi Amini   LLVMContext Context;
5503b42e41SMehdi Amini   Module M("", Context);
56eaa3dccfSRafael Espindola   CallGraph CG(M);
57eaa3dccfSRafael Espindola 
58eaa3dccfSRafael Espindola   canSpecializeGraphTraitsIterators(const_cast<const CallGraph *>(&CG));
59eaa3dccfSRafael Espindola }
60eaa3dccfSRafael Espindola }
61