diff options
author | Robert Widmann <devteam.codafi@gmail.com> | 2018-08-30 17:09:43 +0000 |
---|---|---|
committer | Robert Widmann <devteam.codafi@gmail.com> | 2018-08-30 17:09:43 +0000 |
commit | 0a35b7668bd605879f21ab16d80ac2f658ab5453 (patch) | |
tree | 8a2585620db561d212e9a127fa6ad43f5238af52 /llvm/tools/llvm-c-test | |
parent | 8d39ed895f4f1f15910ec1bcf3694b7a52a3e896 (diff) | |
download | bcm5719-llvm-0a35b7668bd605879f21ab16d80ac2f658ab5453.tar.gz bcm5719-llvm-0a35b7668bd605879f21ab16d80ac2f658ab5453.zip |
[LLVM-C] Add Bindings For Named Metadata
Summary: Add a new type for named metadata nodes. Use this to implement iterators and accessors for NamedMDNodes and extend the echo test to use them to copy module-level debug information.
Reviewers: whitequark, deadalnix, aprantl, dexonsmith
Reviewed By: whitequark
Subscribers: Wallbraker, JDevlieghere, llvm-commits, harlanhaskins
Differential Revision: https://reviews.llvm.org/D47179
llvm-svn: 341085
Diffstat (limited to 'llvm/tools/llvm-c-test')
-rw-r--r-- | llvm/tools/llvm-c-test/echo.cpp | 90 |
1 files changed, 75 insertions, 15 deletions
diff --git a/llvm/tools/llvm-c-test/echo.cpp b/llvm/tools/llvm-c-test/echo.cpp index a9cd4cf5cbb..60638ebbd00 100644 --- a/llvm/tools/llvm-c-test/echo.cpp +++ b/llvm/tools/llvm-c-test/echo.cpp @@ -916,7 +916,7 @@ AliasDecl: if (!Begin) { if (End != nullptr) report_fatal_error("Range has an end but no beginning"); - return; + goto NamedMDDecl; } Cur = Begin; @@ -943,6 +943,38 @@ AliasDecl: Cur = Next; } + +NamedMDDecl: + LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src); + LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src); + if (!BeginMD) { + if (EndMD != nullptr) + report_fatal_error("Range has an end but no beginning"); + return; + } + + LLVMNamedMDNodeRef CurMD = BeginMD; + LLVMNamedMDNodeRef NextMD = nullptr; + while (true) { + size_t NameLen; + const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen); + if (LLVMGetNamedMetadata(M, Name, NameLen)) + report_fatal_error("Named Metadata Node already cloned"); + LLVMGetOrInsertNamedMetadata(M, Name, NameLen); + + NextMD = LLVMGetNextNamedMetadata(CurMD); + if (NextMD == nullptr) { + if (CurMD != EndMD) + report_fatal_error(""); + break; + } + + LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD); + if (PrevMD != CurMD) + report_fatal_error("Next.Previous global is not Current"); + + CurMD = NextMD; + } } static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) { @@ -1041,7 +1073,7 @@ AliasClone: if (!Begin) { if (End != nullptr) report_fatal_error("Range has an end but no beginning"); - return; + goto NamedMDClone; } Cur = Begin; @@ -1073,6 +1105,47 @@ AliasClone: Cur = Next; } + +NamedMDClone: + LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src); + LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src); + if (!BeginMD) { + if (EndMD != nullptr) + report_fatal_error("Range has an end but no beginning"); + return; + } + + LLVMNamedMDNodeRef CurMD = BeginMD; + LLVMNamedMDNodeRef NextMD = nullptr; + while (true) { + size_t NameLen; + const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen); + LLVMNamedMDNodeRef NamedMD = LLVMGetNamedMetadata(M, Name, NameLen); + if (!NamedMD) + report_fatal_error("Named MD Node must have been declared already"); + + unsigned OperandCount = LLVMGetNamedMetadataNumOperands(Src, Name); + LLVMValueRef *OperandBuf = static_cast<LLVMValueRef *>( + safe_malloc(OperandCount * sizeof(LLVMValueRef))); + LLVMGetNamedMetadataOperands(Src, Name, OperandBuf); + for (unsigned i = 0, e = OperandCount; i != e; ++i) { + LLVMAddNamedMetadataOperand(M, Name, OperandBuf[i]); + } + free(OperandBuf); + + NextMD = LLVMGetNextNamedMetadata(CurMD); + if (NextMD == nullptr) { + if (CurMD != EndMD) + report_fatal_error("Last Named MD Node does not match End"); + break; + } + + LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD); + if (PrevMD != CurMD) + report_fatal_error("Next.Previous Named MD Node is not Current"); + + CurMD = NextMD; + } } int llvm_echo(void) { @@ -1089,18 +1162,6 @@ int llvm_echo(void) { LLVMSetSourceFileName(M, SourceFileName, SourceFileLen); LLVMSetModuleIdentifier(M, ModuleName, ModuleIdentLen); - size_t SourceFlagsLen; - LLVMModuleFlagEntry *ModuleFlags = - LLVMCopyModuleFlagsMetadata(Src, &SourceFlagsLen); - for (unsigned i = 0; i < SourceFlagsLen; ++i) { - size_t EntryNameLen; - const char *EntryName = - LLVMModuleFlagEntriesGetKey(ModuleFlags, i, &EntryNameLen); - LLVMAddModuleFlag(M, LLVMModuleFlagEntriesGetFlagBehavior(ModuleFlags, i), - EntryName, EntryNameLen, - LLVMModuleFlagEntriesGetMetadata(ModuleFlags, i)); - } - LLVMSetTarget(M, LLVMGetTarget(Src)); LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src)); if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src))) @@ -1115,7 +1176,6 @@ int llvm_echo(void) { char *Str = LLVMPrintModuleToString(M); fputs(Str, stdout); - LLVMDisposeModuleFlagsMetadata(ModuleFlags); LLVMDisposeMessage(Str); LLVMDisposeModule(Src); LLVMDisposeModule(M); |