brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.3 KiB · 4fe8c00 Raw
101 lines · c
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("Mod");23  LLVMValueRef Int = LLVMConstInt(LLVMInt32Type(), 0, 0);24 25  // This used to trigger an assertion26  LLVMAddNamedMetadataOperand(M, "name", LLVMMDNode(&Int, 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 assertion37  LLVMValueRef Return = LLVMBuildRetVoid(Builder);38 39  const char Name[] = "kind";40  LLVMValueRef Int = LLVMConstInt(LLVMInt32Type(), 0, 0);41  LLVMSetMetadata(Return, LLVMGetMDKindID(Name, strlen(Name)),42                  LLVMMDNode(&Int, 1));43 44  LLVMDisposeBuilder(Builder);45  LLVMDeleteInstruction(Return);46 47  return 0;48}49 50int llvm_replace_md_operand(void) {51  LLVMModuleRef M = LLVMModuleCreateWithName("Mod");52  LLVMContextRef Context = LLVMGetModuleContext(M);53 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);59 60  const char String2[] = "bar";61  LLVMMetadataRef String2MD =62      LLVMMDStringInContext2(Context, String2, strlen(String2));63  LLVMReplaceMDNodeOperandWith(Value, 0, String2MD);64 65  LLVMValueRef Operand = LLVMGetOperand(Value, 0);66 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;72 73  LLVMDisposeModule(M);74 75  return 0;76}77 78int llvm_is_a_value_as_metadata(void) {79  LLVMModuleRef M = LLVMModuleCreateWithName("Mod");80  LLVMContextRef Context = LLVMGetModuleContext(M);81 82  {83    LLVMValueRef Int = LLVMConstInt(LLVMInt32Type(), 0, 0);84    LLVMValueRef NodeMD = LLVMMDNode(&Int, 1);85    assert(LLVMIsAValueAsMetadata(NodeMD) == NodeMD);86    (void)NodeMD;87  }88 89  {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;97  }98 99  return 0;100}101