Rafael Espindola | eaa3dcc | 2014-11-17 17:51:45 +0000 | [diff] [blame] | 1 | //=======- CallGraphTest.cpp - Unit tests for the CG analysis -------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Rafael Espindola | eaa3dcc | 2014-11-17 17:51:45 +0000 | [diff] [blame] | 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 | |
| 18 | template <typename Ty> void canSpecializeGraphTraitsIterators(Ty *G) { |
Tim Shen | f2187ed | 2016-08-22 21:09:30 +0000 | [diff] [blame] | 19 | typedef typename GraphTraits<Ty *>::NodeRef NodeRef; |
Rafael Espindola | eaa3dcc | 2014-11-17 17:51:45 +0000 | [diff] [blame] | 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. |
Tim Shen | f2187ed | 2016-08-22 21:09:30 +0000 | [diff] [blame] | 26 | static_assert(std::is_same<decltype(*I), NodeRef>::value, |
Rafael Espindola | eaa3dcc | 2014-11-17 17:51:45 +0000 | [diff] [blame] | 27 | "Node type does not match"); |
Tim Shen | f2187ed | 2016-08-22 21:09:30 +0000 | [diff] [blame] | 28 | static_assert(std::is_same<decltype(*X), NodeRef>::value, |
Rafael Espindola | eaa3dcc | 2014-11-17 17:51:45 +0000 | [diff] [blame] | 29 | "Node type does not match"); |
Tim Shen | f2187ed | 2016-08-22 21:09:30 +0000 | [diff] [blame] | 30 | static_assert(std::is_same<decltype(*E), NodeRef>::value, |
Rafael Espindola | eaa3dcc | 2014-11-17 17:51:45 +0000 | [diff] [blame] | 31 | "Node type does not match"); |
| 32 | |
Tim Shen | f2187ed | 2016-08-22 21:09:30 +0000 | [diff] [blame] | 33 | NodeRef N = GraphTraits<Ty *>::getEntryNode(G); |
Rafael Espindola | eaa3dcc | 2014-11-17 17:51:45 +0000 | [diff] [blame] | 34 | |
Tim Shen | f2187ed | 2016-08-22 21:09:30 +0000 | [diff] [blame] | 35 | auto S = GraphTraits<NodeRef>::child_begin(N); |
| 36 | auto F = GraphTraits<NodeRef>::child_end(N); |
Rafael Espindola | eaa3dcc | 2014-11-17 17:51:45 +0000 | [diff] [blame] | 37 | |
| 38 | // Should be able to iterate over immediate successors of a node. |
Tim Shen | f2187ed | 2016-08-22 21:09:30 +0000 | [diff] [blame] | 39 | static_assert(std::is_same<decltype(*S), NodeRef>::value, |
Rafael Espindola | eaa3dcc | 2014-11-17 17:51:45 +0000 | [diff] [blame] | 40 | "Node type does not match"); |
Tim Shen | f2187ed | 2016-08-22 21:09:30 +0000 | [diff] [blame] | 41 | static_assert(std::is_same<decltype(*F), NodeRef>::value, |
Rafael Espindola | eaa3dcc | 2014-11-17 17:51:45 +0000 | [diff] [blame] | 42 | "Node type does not match"); |
Rafael Espindola | eaa3dcc | 2014-11-17 17:51:45 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | TEST(CallGraphTest, GraphTraitsSpecialization) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 46 | LLVMContext Context; |
| 47 | Module M("", Context); |
Rafael Espindola | eaa3dcc | 2014-11-17 17:51:45 +0000 | [diff] [blame] | 48 | CallGraph CG(M); |
| 49 | |
| 50 | canSpecializeGraphTraitsIterators(&CG); |
| 51 | } |
| 52 | |
| 53 | TEST(CallGraphTest, GraphTraitsConstSpecialization) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 54 | LLVMContext Context; |
| 55 | Module M("", Context); |
Rafael Espindola | eaa3dcc | 2014-11-17 17:51:45 +0000 | [diff] [blame] | 56 | CallGraph CG(M); |
| 57 | |
| 58 | canSpecializeGraphTraitsIterators(const_cast<const CallGraph *>(&CG)); |
| 59 | } |
| 60 | } |