1/*===-- object.c - tool for testing libLLVM and llvm-c API ----------------===*\
2|* *|
3|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
4|* 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|* This file implements the --add-named-metadata-operand and --set-metadata *|
11|* commands in llvm-c-test. *|
12|* *|
13\*===----------------------------------------------------------------------===*/
14
15#include "llvm-c-test.h"
16#include "llvm-c/Types.h"
17
18#include <assert.h>
19#include <string.h>
20
21int llvm_add_named_metadata_operand(void) {
22 LLVMModuleRef M = LLVMModuleCreateWithName(ModuleID: "Mod");
23 LLVMValueRef Int = LLVMConstInt(IntTy: LLVMInt32Type(), N: 0, SignExtend: 0);
24
25 // This used to trigger an assertion
26 LLVMAddNamedMetadataOperand(M, Name: "name", Val: LLVMMDNode(Vals: &Int, Count: 1));
27
28 LLVMDisposeModule(M);
29
30 return 0;
31}
32
33int llvm_set_metadata(void) {
34 LLVMBuilderRef Builder = LLVMCreateBuilder();
35
36 // This used to trigger an assertion
37 LLVMValueRef Return = LLVMBuildRetVoid(Builder);
38
39 const char Name[] = "kind";
40 LLVMValueRef Int = LLVMConstInt(IntTy: LLVMInt32Type(), N: 0, SignExtend: 0);
41 LLVMSetMetadata(Val: Return, KindID: LLVMGetMDKindID(Name, SLen: strlen(s: Name)),
42 Node: LLVMMDNode(Vals: &Int, Count: 1));
43
44 LLVMDisposeBuilder(Builder);
45 LLVMDeleteInstruction(Inst: Return);
46
47 return 0;
48}
49
50int llvm_replace_md_operand(void) {
51 LLVMModuleRef M = LLVMModuleCreateWithName(ModuleID: "Mod");
52 LLVMContextRef Context = LLVMGetModuleContext(M);
53
54 const char String1[] = "foo";
55 LLVMMetadataRef String1MD =
56 LLVMMDStringInContext2(C: Context, Str: String1, SLen: strlen(s: String1));
57 LLVMMetadataRef NodeMD = LLVMMDNodeInContext2(C: Context, MDs: &String1MD, Count: 1);
58 LLVMValueRef Value = LLVMMetadataAsValue(C: Context, MD: NodeMD);
59
60 const char String2[] = "bar";
61 LLVMMetadataRef String2MD =
62 LLVMMDStringInContext2(C: Context, Str: String2, SLen: strlen(s: String2));
63 LLVMReplaceMDNodeOperandWith(V: Value, Index: 0, Replacement: String2MD);
64
65 LLVMValueRef Operand = LLVMGetOperand(Val: Value, Index: 0);
66
67 unsigned int Len;
68 const char *String = LLVMGetMDString(V: Operand, Length: &Len);
69 assert(Len == strlen(String2));
70 assert(strncmp(String, String2, Len) == 0);
71 (void)String;
72
73 LLVMDisposeModule(M);
74
75 return 0;
76}
77
78int llvm_is_a_value_as_metadata(void) {
79 LLVMModuleRef M = LLVMModuleCreateWithName(ModuleID: "Mod");
80 LLVMContextRef Context = LLVMGetModuleContext(M);
81
82 {
83 LLVMValueRef Int = LLVMConstInt(IntTy: LLVMInt32Type(), N: 0, SignExtend: 0);
84 LLVMValueRef NodeMD = LLVMMDNode(Vals: &Int, Count: 1);
85 assert(LLVMIsAValueAsMetadata(NodeMD) == NodeMD);
86 (void)NodeMD;
87 }
88
89 {
90 const char String[] = "foo";
91 LLVMMetadataRef StringMD =
92 LLVMMDStringInContext2(C: Context, Str: String, SLen: strlen(s: String));
93 LLVMMetadataRef NodeMD = LLVMMDNodeInContext2(C: Context, MDs: &StringMD, Count: 1);
94 LLVMValueRef Value = LLVMMetadataAsValue(C: Context, MD: NodeMD);
95 assert(LLVMIsAValueAsMetadata(Value) == NULL);
96 (void)Value;
97 }
98
99 return 0;
100}
101