blob: 0060d2c4b23969c62eeabdc19d3ff17b91b05e93 [file] [log] [blame]
Rafael Espindolaeaa3dcc2014-11-17 17:51:45 +00001//=======- CallGraphTest.cpp - Unit tests for the CG analysis -------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Espindolaeaa3dcc2014-11-17 17:51:45 +00006//
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
14using namespace llvm;
15
16namespace {
17
18template <typename Ty> void canSpecializeGraphTraitsIterators(Ty *G) {
Tim Shenf2187ed2016-08-22 21:09:30 +000019 typedef typename GraphTraits<Ty *>::NodeRef NodeRef;
Rafael Espindolaeaa3dcc2014-11-17 17:51:45 +000020
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 Shenf2187ed2016-08-22 21:09:30 +000026 static_assert(std::is_same<decltype(*I), NodeRef>::value,
Rafael Espindolaeaa3dcc2014-11-17 17:51:45 +000027 "Node type does not match");
Tim Shenf2187ed2016-08-22 21:09:30 +000028 static_assert(std::is_same<decltype(*X), NodeRef>::value,
Rafael Espindolaeaa3dcc2014-11-17 17:51:45 +000029 "Node type does not match");
Tim Shenf2187ed2016-08-22 21:09:30 +000030 static_assert(std::is_same<decltype(*E), NodeRef>::value,
Rafael Espindolaeaa3dcc2014-11-17 17:51:45 +000031 "Node type does not match");
32
Tim Shenf2187ed2016-08-22 21:09:30 +000033 NodeRef N = GraphTraits<Ty *>::getEntryNode(G);
Rafael Espindolaeaa3dcc2014-11-17 17:51:45 +000034
Tim Shenf2187ed2016-08-22 21:09:30 +000035 auto S = GraphTraits<NodeRef>::child_begin(N);
36 auto F = GraphTraits<NodeRef>::child_end(N);
Rafael Espindolaeaa3dcc2014-11-17 17:51:45 +000037
38 // Should be able to iterate over immediate successors of a node.
Tim Shenf2187ed2016-08-22 21:09:30 +000039 static_assert(std::is_same<decltype(*S), NodeRef>::value,
Rafael Espindolaeaa3dcc2014-11-17 17:51:45 +000040 "Node type does not match");
Tim Shenf2187ed2016-08-22 21:09:30 +000041 static_assert(std::is_same<decltype(*F), NodeRef>::value,
Rafael Espindolaeaa3dcc2014-11-17 17:51:45 +000042 "Node type does not match");
Rafael Espindolaeaa3dcc2014-11-17 17:51:45 +000043}
44
45TEST(CallGraphTest, GraphTraitsSpecialization) {
Mehdi Amini03b42e42016-04-14 21:59:01 +000046 LLVMContext Context;
47 Module M("", Context);
Rafael Espindolaeaa3dcc2014-11-17 17:51:45 +000048 CallGraph CG(M);
49
50 canSpecializeGraphTraitsIterators(&CG);
51}
52
53TEST(CallGraphTest, GraphTraitsConstSpecialization) {
Mehdi Amini03b42e42016-04-14 21:59:01 +000054 LLVMContext Context;
55 Module M("", Context);
Rafael Espindolaeaa3dcc2014-11-17 17:51:45 +000056 CallGraph CG(M);
57
58 canSpecializeGraphTraitsIterators(const_cast<const CallGraph *>(&CG));
59}
60}