1 //===- unittests/Passes/PassBuilderBindingsTest.cpp -----------------------===//
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-c/Core.h"
10 #include "llvm-c/Transforms/PassBuilder.h"
11 #include "llvm-c/Types.h"
12 #include "gtest/gtest.h"
17 class PassBuilderCTest
: public testing::Test
{
18 void SetUp() override
{
19 char *Triple
= LLVMGetDefaultTargetTriple();
20 if (strlen(Triple
) == 0) {
22 LLVMDisposeMessage(Triple
);
25 LLVMInitializeAllTargetInfos();
28 if (LLVMGetTargetFromTriple(Triple
, &Target
, &Err
)) {
29 FAIL() << "Failed to create target from default triple (" << Triple
32 TM
= LLVMCreateTargetMachine(Target
, Triple
, "generic", "",
33 LLVMCodeGenLevelDefault
, LLVMRelocDefault
,
34 LLVMCodeModelDefault
);
35 LLVMDisposeMessage(Triple
);
36 Context
= LLVMContextCreate();
37 Module
= LLVMModuleCreateWithNameInContext("test", Context
);
40 void TearDown() override
{
41 char *Triple
= LLVMGetDefaultTargetTriple();
42 if (strlen(Triple
) == 0) {
43 LLVMDisposeMessage(Triple
);
44 return; // Skipped, so nothing to tear down
46 LLVMDisposeMessage(Triple
);
47 LLVMDisposeTargetMachine(TM
);
48 LLVMDisposeModule(Module
);
49 LLVMContextDispose(Context
);
53 LLVMTargetMachineRef TM
;
55 LLVMContextRef Context
;
58 TEST_F(PassBuilderCTest
, Basic
) {
59 LLVMPassBuilderOptionsRef Options
= LLVMCreatePassBuilderOptions();
60 LLVMPassBuilderOptionsSetLoopUnrolling(Options
, 1);
61 LLVMPassBuilderOptionsSetVerifyEach(Options
, 1);
62 LLVMPassBuilderOptionsSetDebugLogging(Options
, 0);
63 if (LLVMErrorRef E
= LLVMRunPasses(Module
, "default<O2>", TM
, Options
)) {
64 char *Msg
= LLVMGetErrorMessage(E
);
66 LLVMDisposePassBuilderOptions(Options
);
67 FAIL() << "Failed to run passes: " << Msg
;
69 LLVMDisposePassBuilderOptions(Options
);
72 TEST_F(PassBuilderCTest
, InvalidPassIsError
) {
73 LLVMPassBuilderOptionsRef Options
= LLVMCreatePassBuilderOptions();
74 LLVMErrorRef E1
= LLVMRunPasses(Module
, "", TM
, Options
);
75 LLVMErrorRef E2
= LLVMRunPasses(Module
, "does-not-exist-pass", TM
, Options
);
80 LLVMDisposePassBuilderOptions(Options
);