llvm.org GIT mirror llvm / 8f32507
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@341085 91177308-0d34-0410-b5e6-96231b3b80d8 Robert Widmann 2 years ago
6 changed file(s) with 202 addition(s) and 21 deletion(s). Raw diff Collapse all Expand all
13151315 //===----------------------------------------------------------------------===//
13161316 /// A tuple of MDNodes.
13171317 ///
1318 /// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
1319 /// to modules, have names, and contain lists of MDNodes.
1320 ///
1321 /// TODO: Inherit from Metadata.
1318 /// Despite its name, a NamedMDNode isn't itself an MDNode.
1319 ///
1320 /// NamedMDNodes are named module-level entities that contain lists of MDNodes.
1321 ///
1322 /// It is illegal for a NamedMDNode to appear as an operand of an MDNode.
13221323 class NamedMDNode : public ilist_node {
13231324 friend class LLVMContextImpl;
13241325 friend class Module;
14191420 }
14201421 };
14211422
1423 // Create wrappers for C Binding types (see CBindingWrapping.h).
1424 DEFINE_ISA_CONVERSION_FUNCTIONS(NamedMDNode, LLVMNamedMDNodeRef)
1425
14221426 } // end namespace llvm
14231427
14241428 #endif // LLVM_IR_METADATA_H
840840 * Obtain a Type from a module by its registered name.
841841 */
842842 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name);
843
844 /**
845 * Obtain an iterator to the first NamedMDNode in a Module.
846 *
847 * @see llvm::Module::named_metadata_begin()
848 */
849 LLVMNamedMDNodeRef LLVMGetFirstNamedMetadata(LLVMModuleRef M);
850
851 /**
852 * Obtain an iterator to the last NamedMDNode in a Module.
853 *
854 * @see llvm::Module::named_metadata_end()
855 */
856 LLVMNamedMDNodeRef LLVMGetLastNamedMetadata(LLVMModuleRef M);
857
858 /**
859 * Advance a NamedMDNode iterator to the next NamedMDNode.
860 *
861 * Returns NULL if the iterator was already at the end and there are no more
862 * named metadata nodes.
863 */
864 LLVMNamedMDNodeRef LLVMGetNextNamedMetadata(LLVMNamedMDNodeRef NamedMDNode);
865
866 /**
867 * Decrement a NamedMDNode iterator to the previous NamedMDNode.
868 *
869 * Returns NULL if the iterator was already at the beginning and there are
870 * no previous named metadata nodes.
871 */
872 LLVMNamedMDNodeRef LLVMGetPreviousNamedMetadata(LLVMNamedMDNodeRef NamedMDNode);
873
874 /**
875 * Retrieve a NamedMDNode with the given name, returning NULL if no such
876 * node exists.
877 *
878 * @see llvm::Module::getNamedMetadata()
879 */
880 LLVMNamedMDNodeRef LLVMGetNamedMetadata(LLVMModuleRef M,
881 const char *Name, size_t NameLen);
882
883 /**
884 * Retrieve a NamedMDNode with the given name, creating a new node if no such
885 * node exists.
886 *
887 * @see llvm::Module::getOrInsertNamedMetadata()
888 */
889 LLVMNamedMDNodeRef LLVMGetOrInsertNamedMetadata(LLVMModuleRef M,
890 const char *Name,
891 size_t NameLen);
892
893 /**
894 * Retrieve the name of a NamedMDNode.
895 *
896 * @see llvm::NamedMDNode::getName()
897 */
898 const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NamedMD,
899 size_t *NameLen);
843900
844901 /**
845902 * Obtain the number of operands for named metadata in a module.
8989 typedef struct LLVMOpaqueMetadata *LLVMMetadataRef;
9090
9191 /**
92 * Represents an LLVM Named Metadata Node.
93 *
94 * This models llvm::NamedMDNode.
95 */
96 typedef struct LLVMOpaqueNamedMDNode *LLVMNamedMDNodeRef;
97
98 /**
9299 * Represents an LLVM basic block builder.
93100 *
94101 * This models llvm::IRBuilder.
10621062 if (isa(MD->getMetadata()))
10631063 return 1;
10641064 return cast(MD->getMetadata())->getNumOperands();
1065 }
1066
1067 LLVMNamedMDNodeRef LLVMGetFirstNamedMetadata(LLVMModuleRef M) {
1068 Module *Mod = unwrap(M);
1069 Module::named_metadata_iterator I = Mod->named_metadata_begin();
1070 if (I == Mod->named_metadata_end())
1071 return nullptr;
1072 return wrap(&*I);
1073 }
1074
1075 LLVMNamedMDNodeRef LLVMGetLastNamedMetadata(LLVMModuleRef M) {
1076 Module *Mod = unwrap(M);
1077 Module::named_metadata_iterator I = Mod->named_metadata_end();
1078 if (I == Mod->named_metadata_begin())
1079 return nullptr;
1080 return wrap(&*--I);
1081 }
1082
1083 LLVMNamedMDNodeRef LLVMGetNextNamedMetadata(LLVMNamedMDNodeRef NMD) {
1084 NamedMDNode *NamedNode = unwrap(NMD);
1085 Module::named_metadata_iterator I(NamedNode);
1086 if (++I == NamedNode->getParent()->named_metadata_end())
1087 return nullptr;
1088 return wrap(&*I);
1089 }
1090
1091 LLVMNamedMDNodeRef LLVMGetPreviousNamedMetadata(LLVMNamedMDNodeRef NMD) {
1092 NamedMDNode *NamedNode = unwrap(NMD);
1093 Module::named_metadata_iterator I(NamedNode);
1094 if (I == NamedNode->getParent()->named_metadata_begin())
1095 return nullptr;
1096 return wrap(&*--I);
1097 }
1098
1099 LLVMNamedMDNodeRef LLVMGetNamedMetadata(LLVMModuleRef M,
1100 const char *Name, size_t NameLen) {
1101 return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen)));
1102 }
1103
1104 LLVMNamedMDNodeRef LLVMGetOrInsertNamedMetadata(LLVMModuleRef M,
1105 const char *Name, size_t NameLen) {
1106 return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));
1107 }
1108
1109 const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
1110 NamedMDNode *NamedNode = unwrap(NMD);
1111 *NameLen = NamedNode->getName().size();
1112 return NamedNode->getName().data();
10651113 }
10661114
10671115 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
165165 ret void
166166 }
167167
168 !llvm.module.flags = !{!1}
168 !llvm.module.flags = !{!0}
169 !named = !{!1, !2, !3}
169170
170 !1 = !{i32 2, !"Debug Info Version", i32 3}
171 !0 = !{i32 2, !"Debug Info Version", i32 3}
172 !1 = distinct !{}
173 !2 = distinct !{}
174 !3 = !{!4}
175 !4 = distinct !{}
915915 if (!Begin) {
916916 if (End != nullptr)
917917 report_fatal_error("Range has an end but no beginning");
918 return;
918 goto NamedMDDecl;
919919 }
920920
921921 Cur = Begin;
942942
943943 Cur = Next;
944944 }
945
946 NamedMDDecl:
947 LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src);
948 LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src);
949 if (!BeginMD) {
950 if (EndMD != nullptr)
951 report_fatal_error("Range has an end but no beginning");
952 return;
953 }
954
955 LLVMNamedMDNodeRef CurMD = BeginMD;
956 LLVMNamedMDNodeRef NextMD = nullptr;
957 while (true) {
958 size_t NameLen;
959 const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen);
960 if (LLVMGetNamedMetadata(M, Name, NameLen))
961 report_fatal_error("Named Metadata Node already cloned");
962 LLVMGetOrInsertNamedMetadata(M, Name, NameLen);
963
964 NextMD = LLVMGetNextNamedMetadata(CurMD);
965 if (NextMD == nullptr) {
966 if (CurMD != EndMD)
967 report_fatal_error("");
968 break;
969 }
970
971 LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD);
972 if (PrevMD != CurMD)
973 report_fatal_error("Next.Previous global is not Current");
974
975 CurMD = NextMD;
976 }
945977 }
946978
947979 static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
10401072 if (!Begin) {
10411073 if (End != nullptr)
10421074 report_fatal_error("Range has an end but no beginning");
1043 return;
1075 goto NamedMDClone;
10441076 }
10451077
10461078 Cur = Begin;
10721104
10731105 Cur = Next;
10741106 }
1107
1108 NamedMDClone:
1109 LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src);
1110 LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src);
1111 if (!BeginMD) {
1112 if (EndMD != nullptr)
1113 report_fatal_error("Range has an end but no beginning");
1114 return;
1115 }
1116
1117 LLVMNamedMDNodeRef CurMD = BeginMD;
1118 LLVMNamedMDNodeRef NextMD = nullptr;
1119 while (true) {
1120 size_t NameLen;
1121 const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen);
1122 LLVMNamedMDNodeRef NamedMD = LLVMGetNamedMetadata(M, Name, NameLen);
1123 if (!NamedMD)
1124 report_fatal_error("Named MD Node must have been declared already");
1125
1126 unsigned OperandCount = LLVMGetNamedMetadataNumOperands(Src, Name);
1127 LLVMValueRef *OperandBuf = static_cast(
1128 safe_malloc(OperandCount * sizeof(LLVMValueRef)));
1129 LLVMGetNamedMetadataOperands(Src, Name, OperandBuf);
1130 for (unsigned i = 0, e = OperandCount; i != e; ++i) {
1131 LLVMAddNamedMetadataOperand(M, Name, OperandBuf[i]);
1132 }
1133 free(OperandBuf);
1134
1135 NextMD = LLVMGetNextNamedMetadata(CurMD);
1136 if (NextMD == nullptr) {
1137 if (CurMD != EndMD)
1138 report_fatal_error("Last Named MD Node does not match End");
1139 break;
1140 }
1141
1142 LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD);
1143 if (PrevMD != CurMD)
1144 report_fatal_error("Next.Previous Named MD Node is not Current");
1145
1146 CurMD = NextMD;
1147 }
10751148 }
10761149
10771150 int llvm_echo(void) {
10881161 LLVMSetSourceFileName(M, SourceFileName, SourceFileLen);
10891162 LLVMSetModuleIdentifier(M, ModuleName, ModuleIdentLen);
10901163
1091 size_t SourceFlagsLen;
1092 LLVMModuleFlagEntry *ModuleFlags =
1093 LLVMCopyModuleFlagsMetadata(Src, &SourceFlagsLen);
1094 for (unsigned i = 0; i < SourceFlagsLen; ++i) {
1095 size_t EntryNameLen;
1096 const char *EntryName =
1097 LLVMModuleFlagEntriesGetKey(ModuleFlags, i, &EntryNameLen);
1098 LLVMAddModuleFlag(M, LLVMModuleFlagEntriesGetFlagBehavior(ModuleFlags, i),
1099 EntryName, EntryNameLen,
1100 LLVMModuleFlagEntriesGetMetadata(ModuleFlags, i));
1101 }
1102
11031164 LLVMSetTarget(M, LLVMGetTarget(Src));
11041165 LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
11051166 if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
11141175 char *Str = LLVMPrintModuleToString(M);
11151176 fputs(Str, stdout);
11161177
1117 LLVMDisposeModuleFlagsMetadata(ModuleFlags);
11181178 LLVMDisposeMessage(Str);
11191179 LLVMDisposeModule(Src);
11201180 LLVMDisposeModule(M);