llvm-project
100 строк · 3.3 Кб
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) {22LLVMModuleRef M = LLVMModuleCreateWithName("Mod");23LLVMValueRef Int = LLVMConstInt(LLVMInt32Type(), 0, 0);24
25// This used to trigger an assertion26LLVMAddNamedMetadataOperand(M, "name", LLVMMDNode(&Int, 1));27
28LLVMDisposeModule(M);29
30return 0;31}
32
33int llvm_set_metadata(void) {34LLVMBuilderRef Builder = LLVMCreateBuilder();35
36// This used to trigger an assertion37LLVMValueRef Return = LLVMBuildRetVoid(Builder);38
39const char Name[] = "kind";40LLVMValueRef Int = LLVMConstInt(LLVMInt32Type(), 0, 0);41LLVMSetMetadata(Return, LLVMGetMDKindID(Name, strlen(Name)),42LLVMMDNode(&Int, 1));43
44LLVMDisposeBuilder(Builder);45LLVMDeleteInstruction(Return);46
47return 0;48}
49
50int llvm_replace_md_operand(void) {51LLVMModuleRef M = LLVMModuleCreateWithName("Mod");52LLVMContextRef Context = LLVMGetModuleContext(M);53
54const char String1[] = "foo";55LLVMMetadataRef String1MD =56LLVMMDStringInContext2(Context, String1, strlen(String1));57LLVMMetadataRef NodeMD = LLVMMDNodeInContext2(Context, &String1MD, 1);58LLVMValueRef Value = LLVMMetadataAsValue(Context, NodeMD);59
60const char String2[] = "bar";61LLVMMetadataRef String2MD =62LLVMMDStringInContext2(Context, String2, strlen(String2));63LLVMReplaceMDNodeOperandWith(Value, 0, String2MD);64
65LLVMValueRef Operand = LLVMGetOperand(Value, 0);66
67unsigned int Len;68const char *String = LLVMGetMDString(Operand, &Len);69assert(Len == strlen(String2));70assert(strncmp(String, String2, Len) == 0);71(void)String;72
73LLVMDisposeModule(M);74
75return 0;76}
77
78int llvm_is_a_value_as_metadata(void) {79LLVMModuleRef M = LLVMModuleCreateWithName("Mod");80LLVMContextRef Context = LLVMGetModuleContext(M);81
82{83LLVMValueRef Int = LLVMConstInt(LLVMInt32Type(), 0, 0);84LLVMValueRef NodeMD = LLVMMDNode(&Int, 1);85assert(LLVMIsAValueAsMetadata(NodeMD) == NodeMD);86(void)NodeMD;87}88
89{90const char String[] = "foo";91LLVMMetadataRef StringMD =92LLVMMDStringInContext2(Context, String, strlen(String));93LLVMMetadataRef NodeMD = LLVMMDNodeInContext2(Context, &StringMD, 1);94LLVMValueRef Value = LLVMMetadataAsValue(Context, NodeMD);95assert(LLVMIsAValueAsMetadata(Value) == NULL);96(void)Value;97}98
99return 0;100}
101