| 1 |
//===- GraphWriterTest.cpp - GraphWriter unit tests -----------------------===// |
| 2 |
// |
| 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 |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 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" |
| 19 |
#include <string> |
| 20 |
|
| 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()); \ |
| 29 |
} else { \ |
| 30 |
} |
| 31 |
|
| 32 |
namespace llvm { |
| 33 |
namespace { |
| 34 |
|
| 35 |
class GraphWriterTest : public testing::Test { |
| 36 |
protected: |
| 37 |
LLVMContext C; |
| 38 |
|
| 39 |
std::unique_ptr<Module> makeLLVMModule() { |
| 40 |
const char *ModuleStrig = "define i32 @f(i32 %x) {\n" |
| 41 |
"bb0:\n" |
| 42 |
" %y1 = icmp eq i32 %x, 0 \n" |
| 43 |
" br i1 %y1, label %bb1, label %bb2 \n" |
| 44 |
"bb1:\n" |
| 45 |
" br label %bb3\n" |
| 46 |
"bb2:\n" |
| 47 |
" br label %bb3\n" |
| 48 |
"bb3:\n" |
| 49 |
" %y2 = phi i32 [0, %bb1], [1, %bb2] \n" |
| 50 |
" ret i32 %y2\n" |
| 51 |
"}\n"; |
| 52 |
SMDiagnostic Err; |
| 53 |
return parseAssemblyString(ModuleStrig, Err, C); |
| 54 |
} |
| 55 |
}; |
| 56 |
|
| 57 |
static void writeCFGToDotFile(Function &F, std::string Name, |
| 58 |
bool CFGOnly = false) { |
| 59 |
std::error_code EC; |
| 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); |
| 64 |
|
| 65 |
DOTFuncInfo CFGInfo(&F); |
| 66 |
|
| 67 |
ASSERT_NO_ERROR(EC); |
| 68 |
// Test intentionally does not pass BPI, WriteGraph should work without it. |
| 69 |
WriteGraph(File, &CFGInfo, CFGOnly); |
| 70 |
} |
| 71 |
|
| 72 |
TEST_F(GraphWriterTest, WriteCFGDotFileTest) { |
| 73 |
auto M = makeLLVMModule(); |
| 74 |
Function *F = M->getFunction("f"); |
| 75 |
|
| 76 |
writeCFGToDotFile(*F, "test-full"); |
| 77 |
writeCFGToDotFile(*F, "test-cfg-only", true); |
| 78 |
} |
| 79 |
|
| 80 |
} // end anonymous namespace |
| 81 |
} // end namespace llvm |