[clang][bytecode][NFC] Only get expr when checking for UB (#125397)
[llvm-project.git] / llvm / tools / llvm-c-test / metadata.c
blob4fe8c00c57481b1cc8201f18e9f50a489371233b
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 \*===----------------------------------------------------------------------===*/
15 #include "llvm-c-test.h"
16 #include "llvm-c/Types.h"
18 #include <assert.h>
19 #include <string.h>
21 int llvm_add_named_metadata_operand(void) {
22 LLVMModuleRef M = LLVMModuleCreateWithName("Mod");
23 LLVMValueRef Int = LLVMConstInt(LLVMInt32Type(), 0, 0);
25 // This used to trigger an assertion
26 LLVMAddNamedMetadataOperand(M, "name", LLVMMDNode(&Int, 1));
28 LLVMDisposeModule(M);
30 return 0;
33 int llvm_set_metadata(void) {
34 LLVMBuilderRef Builder = LLVMCreateBuilder();
36 // This used to trigger an assertion
37 LLVMValueRef Return = LLVMBuildRetVoid(Builder);
39 const char Name[] = "kind";
40 LLVMValueRef Int = LLVMConstInt(LLVMInt32Type(), 0, 0);
41 LLVMSetMetadata(Return, LLVMGetMDKindID(Name, strlen(Name)),
42 LLVMMDNode(&Int, 1));
44 LLVMDisposeBuilder(Builder);
45 LLVMDeleteInstruction(Return);
47 return 0;
50 int llvm_replace_md_operand(void) {
51 LLVMModuleRef M = LLVMModuleCreateWithName("Mod");
52 LLVMContextRef Context = LLVMGetModuleContext(M);
54 const char String1[] = "foo";
55 LLVMMetadataRef String1MD =
56 LLVMMDStringInContext2(Context, String1, strlen(String1));
57 LLVMMetadataRef NodeMD = LLVMMDNodeInContext2(Context, &String1MD, 1);
58 LLVMValueRef Value = LLVMMetadataAsValue(Context, NodeMD);
60 const char String2[] = "bar";
61 LLVMMetadataRef String2MD =
62 LLVMMDStringInContext2(Context, String2, strlen(String2));
63 LLVMReplaceMDNodeOperandWith(Value, 0, String2MD);
65 LLVMValueRef Operand = LLVMGetOperand(Value, 0);
67 unsigned int Len;
68 const char *String = LLVMGetMDString(Operand, &Len);
69 assert(Len == strlen(String2));
70 assert(strncmp(String, String2, Len) == 0);
71 (void)String;
73 LLVMDisposeModule(M);
75 return 0;
78 int llvm_is_a_value_as_metadata(void) {
79 LLVMModuleRef M = LLVMModuleCreateWithName("Mod");
80 LLVMContextRef Context = LLVMGetModuleContext(M);
83 LLVMValueRef Int = LLVMConstInt(LLVMInt32Type(), 0, 0);
84 LLVMValueRef NodeMD = LLVMMDNode(&Int, 1);
85 assert(LLVMIsAValueAsMetadata(NodeMD) == NodeMD);
86 (void)NodeMD;
90 const char String[] = "foo";
91 LLVMMetadataRef StringMD =
92 LLVMMDStringInContext2(Context, String, strlen(String));
93 LLVMMetadataRef NodeMD = LLVMMDNodeInContext2(Context, &StringMD, 1);
94 LLVMValueRef Value = LLVMMetadataAsValue(Context, NodeMD);
95 assert(LLVMIsAValueAsMetadata(Value) == NULL);
96 (void)Value;
99 return 0;