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