1 //===- GraphWriterTest.cpp - GraphWriter unit tests -----------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/GraphWriter.h"
10 #include "llvm/Analysis/CFGPrinter.h"
11 #include "llvm/AsmParser/Parser.h"
12 #include "llvm/IR/Function.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/Testing/Support/SupportHelpers.h"
16 #include "llvm/Support/SourceMgr.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "gtest/gtest.h"
21 #define ASSERT_NO_ERROR(x) \
22 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
23 SmallString<128> MessageStorage; \
24 raw_svector_ostream Message(MessageStorage); \
25 Message << #x ": did not return errc::success.\n" \
26 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
27 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
28 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
35 class GraphWriterTest
: public testing::Test
{
39 std::unique_ptr
<Module
> makeLLVMModule() {
40 const char *ModuleStrig
= "define i32 @f(i32 %x) {\n"
42 " %y1 = icmp eq i32 %x, 0 \n"
43 " br i1 %y1, label %bb1, label %bb2 \n"
49 " %y2 = phi i32 [0, %bb1], [1, %bb2] \n"
53 return parseAssemblyString(ModuleStrig
, Err
, C
);
57 static void writeCFGToDotFile(Function
&F
, std::string Name
,
58 bool CFGOnly
= false) {
60 llvm::unittest::TempDir
Tmp("tmpdir", /*Unique=*/true);
61 SmallString
<128> FileName(Tmp
.path().begin(), Tmp
.path().end());
62 sys::path::append(FileName
, Name
+ ".dot");
63 raw_fd_ostream
File(FileName
, EC
, sys::fs::OpenFlags::OF_Text
);
65 DOTFuncInfo
CFGInfo(&F
);
68 // Test intentionally does not pass BPI, WriteGraph should work without it.
69 WriteGraph(File
, &CFGInfo
, CFGOnly
);
72 TEST_F(GraphWriterTest
, WriteCFGDotFileTest
) {
73 auto M
= makeLLVMModule();
74 Function
*F
= M
->getFunction("f");
76 writeCFGToDotFile(*F
, "test-full");
77 writeCFGToDotFile(*F
, "test-cfg-only", true);
80 } // end anonymous namespace
81 } // end namespace llvm