blob: 2d4e63facf3b54f7c070f6d19df1d3d4015b1bd3 [file] [log] [blame]
Rafael Espindola95764ec2014-11-17 17:51:45 +00001//=======- 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
15using namespace llvm;
16
17namespace {
18
19template <typename Ty> void canSpecializeGraphTraitsIterators(Ty *G) {
Tim Shen22fca382016-08-22 21:09:30 +000020 typedef typename GraphTraits<Ty *>::NodeRef NodeRef;
Rafael Espindola95764ec2014-11-17 17:51:45 +000021
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 Shen22fca382016-08-22 21:09:30 +000027 static_assert(std::is_same<decltype(*I), NodeRef>::value,
Rafael Espindola95764ec2014-11-17 17:51:45 +000028 "Node type does not match");
Tim Shen22fca382016-08-22 21:09:30 +000029 static_assert(std::is_same<decltype(*X), NodeRef>::value,
Rafael Espindola95764ec2014-11-17 17:51:45 +000030 "Node type does not match");
Tim Shen22fca382016-08-22 21:09:30 +000031 static_assert(std::is_same<decltype(*E), NodeRef>::value,
Rafael Espindola95764ec2014-11-17 17:51:45 +000032 "Node type does not match");
33
Tim Shen22fca382016-08-22 21:09:30 +000034 NodeRef N = GraphTraits<Ty *>::getEntryNode(G);
Rafael Espindola95764ec2014-11-17 17:51:45 +000035
Tim Shen22fca382016-08-22 21:09:30 +000036 auto S = GraphTraits<NodeRef>::child_begin(N);
37 auto F = GraphTraits<NodeRef>::child_end(N);
Rafael Espindola95764ec2014-11-17 17:51:45 +000038
39 // Should be able to iterate over immediate successors of a node.
Tim Shen22fca382016-08-22 21:09:30 +000040 static_assert(std::is_same<decltype(*S), NodeRef>::value,
Rafael Espindola95764ec2014-11-17 17:51:45 +000041 "Node type does not match");
Tim Shen22fca382016-08-22 21:09:30 +000042 static_assert(std::is_same<decltype(*F), NodeRef>::value,
Rafael Espindola95764ec2014-11-17 17:51:45 +000043 "Node type does not match");
Rafael Espindola95764ec2014-11-17 17:51:45 +000044}
45
46TEST(CallGraphTest, GraphTraitsSpecialization) {
Mehdi Amini8be77072016-04-14 21:59:01 +000047 LLVMContext Context;
48 Module M("", Context);
Rafael Espindola95764ec2014-11-17 17:51:45 +000049 CallGraph CG(M);
50
51 canSpecializeGraphTraitsIterators(&CG);
52}
53
54TEST(CallGraphTest, GraphTraitsConstSpecialization) {
Mehdi Amini8be77072016-04-14 21:59:01 +000055 LLVMContext Context;
56 Module M("", Context);
Rafael Espindola95764ec2014-11-17 17:51:45 +000057 CallGraph CG(M);
58
59 canSpecializeGraphTraitsIterators(const_cast<const CallGraph *>(&CG));
60}
61}