LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DECLARE_VALUE_CAST)
LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val);
+LLVMValueRef LLVMIsAValueAsMetadata(LLVMValueRef Val);
LLVMValueRef LLVMIsAMDString(LLVMValueRef Val);
/** Deprecated: Use LLVMGetValueName2 instead. */
*/
void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest);
+/**
+ * Replace an operand at a specific index in a llvm::MDNode value.
+ *
+ * @see llvm::MDNode::replaceOperandWith()
+ */
+void LLVMReplaceMDNodeOperandWith(LLVMValueRef V, unsigned Index,
+ LLVMMetadataRef Replacement);
+
/** Deprecated: Use LLVMMDStringInContext2 instead. */
LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
unsigned SLen);
return nullptr;
}
+LLVMValueRef LLVMIsAValueAsMetadata(LLVMValueRef Val) {
+ if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
+ if (isa<ValueAsMetadata>(MD->getMetadata()))
+ return Val;
+ return nullptr;
+}
+
LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
if (isa<MDString>(MD->getMetadata()))
Dest[i] = getMDNodeOperandImpl(Context, N, i);
}
+void LLVMReplaceMDNodeOperandWith(LLVMValueRef V, unsigned Index,
+ LLVMMetadataRef Replacement) {
+ auto *MD = cast<MetadataAsValue>(unwrap(V));
+ auto *N = cast<MDNode>(MD->getMetadata());
+ N->replaceOperandWith(Index, unwrap<Metadata>(Replacement));
+}
+
unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
return N->getNumOperands();
--- /dev/null
+; RUN: llvm-c-test --is-a-value-as-metadata < /dev/null
+; This used to trigger an assertion
--- /dev/null
+; RUN: llvm-c-test --replace-md-operand < /dev/null
+; This used to trigger an assertion
// metadata.c
int llvm_add_named_metadata_operand(void);
int llvm_set_metadata(void);
+int llvm_replace_md_operand(void);
+int llvm_is_a_value_as_metadata(void);
// object.c
int llvm_object_list_sections(void);
fprintf(stderr, " Read lines of triple, hex ascii machine code from stdin "
"- print disassembly\n\n");
fprintf(stderr, " * --calc\n");
+ fprintf(stderr, " * --replace-md-operand\n");
+ fprintf(stderr, " Run test for replacing MDNode operands\n");
+ fprintf(stderr, " * --is-a-value-as-metadata\n");
+ fprintf(stderr,
+ " Run test for checking if LLVMValueRef is a ValueAsMetadata\n");
fprintf(
stderr,
" Read lines of name, rpn from stdin - print generated module\n\n");
return llvm_add_named_metadata_operand();
} else if (argc == 2 && !strcmp(argv[1], "--set-metadata")) {
return llvm_set_metadata();
+ } else if (argc == 2 && !strcmp(argv[1], "--replace-md-operand")) {
+ return llvm_replace_md_operand();
+ } else if (argc == 2 && !strcmp(argv[1], "--is-a-value-as-metadata")) {
+ return llvm_is_a_value_as_metadata();
} else if (argc == 2 && !strcmp(argv[1], "--test-function-attributes")) {
return llvm_test_function_attributes();
} else if (argc == 2 && !strcmp(argv[1], "--test-callsite-attributes")) {
#include "llvm-c-test.h"
+#include <assert.h>
+#include <string.h>
+
int llvm_add_named_metadata_operand(void) {
LLVMModuleRef m = LLVMModuleCreateWithName("Mod");
LLVMValueRef values[] = { LLVMConstInt(LLVMInt32Type(), 0, 0) };
return 0;
}
+
+int llvm_replace_md_operand(void) {
+ LLVMModuleRef m = LLVMModuleCreateWithName("Mod");
+ LLVMContextRef context = LLVMGetModuleContext(m);
+ unsigned int tmp = 0;
+
+ LLVMMetadataRef metas[] = {LLVMMDStringInContext2(context, "foo", 3)};
+ LLVMValueRef md =
+ LLVMMetadataAsValue(context, LLVMMDNodeInContext2(context, metas, 1));
+
+ LLVMReplaceMDNodeOperandWith(md, 0,
+ LLVMMDStringInContext2(context, "bar", 3));
+
+ assert(!strncmp(LLVMGetMDString(LLVMGetOperand(md, 0), &tmp), "bar", 0));
+
+ LLVMDisposeModule(m);
+
+ return 0;
+}
+
+int llvm_is_a_value_as_metadata(void) {
+ LLVMModuleRef m = LLVMModuleCreateWithName("Mod");
+ LLVMContextRef context = LLVMGetModuleContext(m);
+
+ LLVMValueRef values[] = {LLVMConstInt(LLVMInt32Type(), 0, 0)};
+ LLVMValueRef md = LLVMMDNode(values, 1);
+ assert(LLVMIsAValueAsMetadata(md) == md);
+
+ LLVMMetadataRef metas[] = {LLVMMDStringInContext2(context, "foo", 3)};
+ LLVMValueRef md2 =
+ LLVMMetadataAsValue(context, LLVMMDNodeInContext2(context, metas, 1));
+ assert(LLVMIsAValueAsMetadata(md2) == NULL);
+
+ return 0;
+}