[gcov] Bump default version to 11.1
[llvm-project.git] / llvm / unittests / Target / TargetMachineOptionsTest.cpp
blob27f8855b47551f3b04b6eff5b44797412f69fa79
1 //===-- llvm/unittests/Target/TargetMachineOptionsTest.cpp ----------
2 //-----===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file contains unit tests for the opaque structure describing options
12 /// for TargetMachine creation via the C API.
13 ///
14 //===----------------------------------------------------------------------===//
16 #include "llvm-c/Core.h"
17 #include "llvm-c/TargetMachine.h"
18 #include "llvm/Config/llvm-config.h"
19 #include "gtest/gtest.h"
21 namespace llvm {
23 TEST(TargetMachineCTest, TargetMachineOptions) {
24 auto *Options = LLVMCreateTargetMachineOptions();
26 LLVMTargetMachineOptionsSetCPU(Options, "cortex-a53");
27 LLVMTargetMachineOptionsSetFeatures(Options, "+neon");
28 LLVMTargetMachineOptionsSetABI(Options, "aapcs");
29 LLVMTargetMachineOptionsSetCodeGenOptLevel(Options, LLVMCodeGenLevelNone);
30 LLVMTargetMachineOptionsSetRelocMode(Options, LLVMRelocStatic);
31 LLVMTargetMachineOptionsSetCodeModel(Options, LLVMCodeModelKernel);
33 LLVMDisposeTargetMachineOptions(Options);
36 TEST(TargetMachineCTest, TargetMachineCreation) {
37 LLVMInitializeAllTargets();
38 LLVMInitializeAllTargetInfos();
39 LLVMInitializeAllTargetMCs();
41 // Get the default target to keep the test as generic as possible. This may
42 // not be a target for which we can generate code; in that case we give up.
44 auto *Triple = LLVMGetDefaultTargetTriple();
45 if (strlen(Triple) == 0) {
46 LLVMDisposeMessage(Triple);
47 GTEST_SKIP();
50 LLVMTargetRef Target = nullptr;
51 char *Error = nullptr;
52 if (LLVMGetTargetFromTriple(Triple, &Target, &Error))
53 FAIL() << "Failed to create target from default triple (" << Triple
54 << "): " << Error;
56 ASSERT_NE(Target, nullptr);
58 if (!LLVMTargetHasTargetMachine(Target))
59 GTEST_SKIP() << "Default target doesn't support code generation";
61 // We don't know which target we're creating a machine for, so don't set any
62 // non-default options; they might cause fatal errors.
64 auto *Options = LLVMCreateTargetMachineOptions();
65 auto *TM = LLVMCreateTargetMachineWithOptions(Target, Triple, Options);
66 ASSERT_NE(TM, nullptr);
68 LLVMDisposeMessage(Triple);
69 LLVMDisposeTargetMachineOptions(Options);
70 LLVMDisposeTargetMachine(TM);
73 } // namespace llvm