<div dir="ltr"><div>Hi Robert,</div><div><br></div><div>this commit introduced a new warning in '
lib/IR/Core.cpp':<br></div><div>
<pre><span class="gmail-stdout">warning C4715: 'map_to_llvmModFlagBehavior': not all control paths return a value<br><br></span></pre>




</div><div>
<div>gcc also complains about it:</div><div>lib/IR/Core.cpp:293:1: warning: control reaches end of non-void function [-Wreturn-type]</div><div><br></div></div><div>I think you just forgot to add a llvm_unreachable at the end of that function.</div><div>Could you please fix it?</div><div><br></div><div>Thanks,</div><div>Andrea<br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, May 14, 2018 at 9:09 AM, Robert Widmann via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: codafi<br>
Date: Mon May 14 01:09:00 2018<br>
New Revision: 332219<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=332219&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project?rev=332219&view=rev</a><br>
Log:<br>
[LLVM-C] Add Bindings For Module Flags<br>
<br>
Summary:<br>
The first foray into merging debug info into the echo tests.<br>
<br>
- Add bindings to Module::<wbr>getModuleFlagsMetadata() in the form of LLVMCopyModuleFlagsMetadata<br>
- Add the opaque type LLVMModuleFlagEntry to represent Module::ModuleFlagEntry<br>
- Add accessors for LLVMModuleFlagEntry's behavior, key, and metadata node.<br>
<br>
Reviewers: whitequark, deadalnix<br>
<br>
Reviewed By: whitequark<br>
<br>
Subscribers: aprantl, JDevlieghere, llvm-commits, harlanhaskins<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D46792" rel="noreferrer" target="_blank">https://reviews.llvm.org/<wbr>D46792</a><br>
<br>
Modified:<br>
    llvm/trunk/include/llvm-c/<wbr>Core.h<br>
    llvm/trunk/include/llvm-c/<wbr>Types.h<br>
    llvm/trunk/lib/IR/Core.cpp<br>
    llvm/trunk/test/Bindings/llvm-<wbr>c/echo.ll<br>
    llvm/trunk/tools/llvm-c-test/<wbr>echo.cpp<br>
<br>
Modified: llvm/trunk/include/llvm-c/<wbr>Core.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm-c/Core.h?rev=332219&r1=332218&r2=332219&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/include/<wbr>llvm-c/Core.h?rev=332219&r1=<wbr>332218&r2=332219&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/include/llvm-c/<wbr>Core.h (original)<br>
+++ llvm/trunk/include/llvm-c/<wbr>Core.h Mon May 14 01:09:00 2018<br>
@@ -381,6 +381,57 @@ typedef enum {<br>
   LLVMInlineAsmDialectIntel<br>
 } LLVMInlineAsmDialect;<br>
<br>
+typedef enum {<br>
+  /**<br>
+   * Emits an error if two values disagree, otherwise the resulting value is<br>
+   * that of the operands.<br>
+   *<br>
+   * @see Module::ModFlagBehavior::Error<br>
+   */<br>
+  LLVMModuleFlagBehaviorError,<br>
+  /**<br>
+   * Emits a warning if two values disagree. The result value will be the<br>
+   * operand for the flag from the first module being linked.<br>
+   *<br>
+   * @see Module::ModFlagBehavior::<wbr>Warning<br>
+   */<br>
+  LLVMModuleFlagBehaviorWarning,<br>
+  /**<br>
+   * Adds a requirement that another module flag be present and have a<br>
+   * specified value after linking is performed. The value must be a metadata<br>
+   * pair, where the first element of the pair is the ID of the module flag<br>
+   * to be restricted, and the second element of the pair is the value the<br>
+   * module flag should be restricted to. This behavior can be used to<br>
+   * restrict the allowable results (via triggering of an error) of linking<br>
+   * IDs with the **Override** behavior.<br>
+   *<br>
+   * @see Module::ModFlagBehavior::<wbr>Require<br>
+   */<br>
+  LLVMModuleFlagBehaviorRequire,<br>
+  /**<br>
+   * Uses the specified value, regardless of the behavior or value of the<br>
+   * other module. If both modules specify **Override**, but the values<br>
+   * differ, an error will be emitted.<br>
+   *<br>
+   * @see Module::ModFlagBehavior::<wbr>Override<br>
+   */<br>
+  LLVMModuleFlagBehaviorOverride<wbr>,<br>
+  /**<br>
+   * Appends the two values, which are required to be metadata nodes.<br>
+   *<br>
+   * @see Module::ModFlagBehavior::<wbr>Append<br>
+   */<br>
+  LLVMModuleFlagBehaviorAppend,<br>
+  /**<br>
+   * Appends the two values, which are required to be metadata<br>
+   * nodes. However, duplicate entries in the second list are dropped<br>
+   * during the append operation.<br>
+   *<br>
+   * @see Module::ModFlagBehavior::<wbr>AppendUnique<br>
+   */<br>
+  LLVMModuleFlagBehaviorAppendUn<wbr>ique,<br>
+} LLVMModuleFlagBehavior;<br>
+<br>
 /**<br>
  * Attribute index are either LLVMAttributeReturnIndex,<br>
  * LLVMAttributeFunctionIndex or a parameter number from 1 to N.<br>
@@ -666,6 +717,64 @@ const char *LLVMGetTarget(LLVMModuleRef<br>
 void LLVMSetTarget(LLVMModuleRef M, const char *Triple);<br>
<br>
 /**<br>
+ * Returns the module flags as an array of flag-key-value triples.  The caller<br>
+ * is responsible for freeing this array by calling<br>
+ * \c LLVMDisposeModuleFlagsMetadata<wbr>.<br>
+ *<br>
+ * @see Module::<wbr>getModuleFlagsMetadata()<br>
+ */<br>
+LLVMModuleFlagEntry *LLVMCopyModuleFlagsMetadata(<wbr>LLVMModuleRef M, size_t *Len);<br>
+<br>
+/**<br>
+ * Destroys module flags metadata entries.<br>
+ */<br>
+void LLVMDisposeModuleFlagsMetadata<wbr>(LLVMModuleFlagEntry *Entries);<br>
+<br>
+/**<br>
+ * Returns the flag behavior for a module flag entry at a specific index.<br>
+ *<br>
+ * @see Module::ModuleFlagEntry::<wbr>Behavior<br>
+ */<br>
+LLVMModuleFlagBehavior<br>
+<wbr>LLVMModuleFlagEntriesGetFlagBe<wbr>havior(LLVMModuleFlagEntry *Entries,<br>
+                                     unsigned Index);<br>
+<br>
+/**<br>
+ * Returns the key for a module flag entry at a specific index.<br>
+ *<br>
+ * @see Module::ModuleFlagEntry::Key<br>
+ */<br>
+const char *LLVMModuleFlagEntriesGetKey(<wbr>LLVMModuleFlagEntry *Entries,<br>
+                                        unsigned Index, size_t *Len);<br>
+<br>
+/**<br>
+ * Returns the metadata for a module flag entry at a specific index.<br>
+ *<br>
+ * @see Module::ModuleFlagEntry::Val<br>
+ */<br>
+LLVMMetadataRef LLVMModuleFlagEntriesGetMetada<wbr>ta(LLVMModuleFlagEntry *Entries,<br>
+                                                 unsigned Index);<br>
+<br>
+/**<br>
+ * Add a module-level flag to the module-level flags metadata if it doesn't<br>
+ * already exist.<br>
+ *<br>
+ * @see Module::getModuleFlag()<br>
+ */<br>
+LLVMMetadataRef LLVMGetModuleFlag(<wbr>LLVMModuleRef M,<br>
+                                  const char *Key, size_t KeyLen);<br>
+<br>
+/**<br>
+ * Add a module-level flag to the module-level flags metadata if it doesn't<br>
+ * already exist.<br>
+ *<br>
+ * @see Module::addModuleFlag()<br>
+ */<br>
+void LLVMAddModuleFlag(<wbr>LLVMModuleRef M, LLVMModuleFlagBehavior Behavior,<br>
+                       const char *Key, size_t KeyLen,<br>
+                       LLVMMetadataRef Val);<br>
+<br>
+/**<br>
  * Dump a representation of a module to stderr.<br>
  *<br>
  * @see Module::dump()<br>
<br>
Modified: llvm/trunk/include/llvm-c/<wbr>Types.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm-c/Types.h?rev=332219&r1=332218&r2=332219&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/include/<wbr>llvm-c/Types.h?rev=332219&r1=<wbr>332218&r2=332219&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/include/llvm-c/<wbr>Types.h (original)<br>
+++ llvm/trunk/include/llvm-c/<wbr>Types.h Mon May 14 01:09:00 2018<br>
@@ -140,6 +140,11 @@ typedef struct LLVMOpaqueDiagnosticInfo<br>
 typedef struct LLVMComdat *LLVMComdatRef;<br>
<br>
 /**<br>
+ * @see llvm::Module::ModuleFlagEntry<br>
+ */<br>
+typedef struct LLVMOpaqueModuleFlagEntry LLVMModuleFlagEntry;<br>
+<br>
+/**<br>
  * @}<br>
  */<br>
<br>
<br>
Modified: llvm/trunk/lib/IR/Core.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Core.cpp?rev=332219&r1=332218&r2=332219&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/lib/IR/<wbr>Core.cpp?rev=332219&r1=332218&<wbr>r2=332219&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/lib/IR/Core.cpp (original)<br>
+++ llvm/trunk/lib/IR/Core.cpp Mon May 14 01:09:00 2018<br>
@@ -266,6 +266,110 @@ void LLVMSetTarget(LLVMModuleRef M, cons<br>
   unwrap(M)->setTargetTriple(<wbr>Triple);<br>
 }<br>
<br>
+/*--.. Module flags ..............................<wbr>..........................--*/<br>
+struct LLVMOpaqueModuleFlagEntry {<br>
+  LLVMModuleFlagBehavior Behavior;<br>
+  const char *Key;<br>
+  size_t KeyLen;<br>
+  LLVMMetadataRef Metadata;<br>
+};<br>
+<br>
+static Module::ModFlagBehavior<br>
+map_to_llvmModFlagBehavior(<wbr>LLVMModuleFlagBehavior Behavior) {<br>
+  switch (Behavior) {<br>
+  case LLVMModuleFlagBehaviorError:<br>
+    return Module::ModFlagBehavior::<wbr>Error;<br>
+  case LLVMModuleFlagBehaviorWarning:<br>
+    return Module::ModFlagBehavior::<wbr>Warning;<br>
+  case LLVMModuleFlagBehaviorRequire:<br>
+    return Module::ModFlagBehavior::<wbr>Require;<br>
+  case LLVMModuleFlagBehaviorOverride<wbr>:<br>
+    return Module::ModFlagBehavior::<wbr>Override;<br>
+  case LLVMModuleFlagBehaviorAppend:<br>
+    return Module::ModFlagBehavior::<wbr>Append;<br>
+  case LLVMModuleFlagBehaviorAppendUn<wbr>ique:<br>
+    return Module::ModFlagBehavior::<wbr>AppendUnique;<br>
+  }<br>
+}<br>
+<br>
+static LLVMModuleFlagBehavior<br>
+map_from_llvmModFlagBehavior(<wbr>Module::ModFlagBehavior Behavior) {<br>
+  switch (Behavior) {<br>
+  case Module::ModFlagBehavior::<wbr>Error:<br>
+    return LLVMModuleFlagBehaviorError;<br>
+  case Module::ModFlagBehavior::<wbr>Warning:<br>
+    return LLVMModuleFlagBehaviorWarning;<br>
+  case Module::ModFlagBehavior::<wbr>Require:<br>
+    return LLVMModuleFlagBehaviorRequire;<br>
+  case Module::ModFlagBehavior::<wbr>Override:<br>
+    return LLVMModuleFlagBehaviorOverride<wbr>;<br>
+  case Module::ModFlagBehavior::<wbr>Append:<br>
+    return LLVMModuleFlagBehaviorAppend;<br>
+  case Module::ModFlagBehavior::<wbr>AppendUnique:<br>
+    return LLVMModuleFlagBehaviorAppendUn<wbr>ique;<br>
+  default:<br>
+    llvm_unreachable("Unhandled Flag Behavior");<br>
+  }<br>
+}<br>
+<br>
+LLVMModuleFlagEntry *LLVMCopyModuleFlagsMetadata(<wbr>LLVMModuleRef M, size_t *Len) {<br>
+  SmallVector<Module::<wbr>ModuleFlagEntry, 8> MFEs;<br>
+  unwrap(M)-><wbr>getModuleFlagsMetadata(MFEs);<br>
+<br>
+  LLVMOpaqueModuleFlagEntry *Result = static_cast<<wbr>LLVMOpaqueModuleFlagEntry *>(<br>
+      safe_malloc(MFEs.size() * sizeof(<wbr>LLVMOpaqueModuleFlagEntry)));<br>
+  for (unsigned i = 0; i < MFEs.size(); ++i) {<br>
+    const auto &ModuleFlag = MFEs[i];<br>
+    Result[i].Behavior = map_from_llvmModFlagBehavior(<wbr>ModuleFlag.Behavior);<br>
+    Result[i].Key = ModuleFlag.Key->getString().<wbr>data();<br>
+    Result[i].KeyLen = ModuleFlag.Key->getString().<wbr>size();<br>
+    Result[i].Metadata = wrap(ModuleFlag.Val);<br>
+  }<br>
+  *Len = MFEs.size();<br>
+  return Result;<br>
+}<br>
+<br>
+void LLVMDisposeModuleFlagsMetadata<wbr>(LLVMModuleFlagEntry *Entries) {<br>
+  free(Entries);<br>
+}<br>
+<br>
+LLVMModuleFlagBehavior<br>
+<wbr>LLVMModuleFlagEntriesGetFlagBe<wbr>havior(LLVMModuleFlagEntry *Entries,<br>
+                                     unsigned Index) {<br>
+  LLVMOpaqueModuleFlagEntry MFE =<br>
+      static_cast<<wbr>LLVMOpaqueModuleFlagEntry>(<wbr>Entries[Index]);<br>
+  return MFE.Behavior;<br>
+}<br>
+<br>
+const char *LLVMModuleFlagEntriesGetKey(<wbr>LLVMModuleFlagEntry *Entries,<br>
+                                        unsigned Index, size_t *Len) {<br>
+  LLVMOpaqueModuleFlagEntry MFE =<br>
+      static_cast<<wbr>LLVMOpaqueModuleFlagEntry>(<wbr>Entries[Index]);<br>
+  *Len = MFE.KeyLen;<br>
+  return MFE.Key;<br>
+}<br>
+<br>
+LLVMMetadataRef LLVMModuleFlagEntriesGetMetada<wbr>ta(LLVMModuleFlagEntry *Entries,<br>
+                                                 unsigned Index) {<br>
+  LLVMOpaqueModuleFlagEntry MFE =<br>
+      static_cast<<wbr>LLVMOpaqueModuleFlagEntry>(<wbr>Entries[Index]);<br>
+  return MFE.Metadata;<br>
+}<br>
+<br>
+LLVMMetadataRef LLVMGetModuleFlag(<wbr>LLVMModuleRef M,<br>
+                                  const char *Key, size_t KeyLen) {<br>
+  return wrap(unwrap(M)->getModuleFlag(<wbr>{Key, KeyLen}));<br>
+}<br>
+<br>
+void LLVMAddModuleFlag(<wbr>LLVMModuleRef M, LLVMModuleFlagBehavior Behavior,<br>
+                       const char *Key, size_t KeyLen,<br>
+                       LLVMMetadataRef Val) {<br>
+  unwrap(M)->addModuleFlag(map_<wbr>to_llvmModFlagBehavior(<wbr>Behavior),<br>
+                           {Key, KeyLen}, unwrap(Val));<br>
+}<br>
+<br>
+/*--.. Printing modules ..............................<wbr>......................--*/<br>
+<br>
 void LLVMDumpModule(LLVMModuleRef M) {<br>
   unwrap(M)->print(errs(), nullptr,<br>
                    /*ShouldPreserveUseListOrder=*<wbr>/false, /*IsForDebug=*/true);<br>
<br>
Modified: llvm/trunk/test/Bindings/llvm-<wbr>c/echo.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bindings/llvm-c/echo.ll?rev=332219&r1=332218&r2=332219&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/test/<wbr>Bindings/llvm-c/echo.ll?rev=<wbr>332219&r1=332218&r2=332219&<wbr>view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/test/Bindings/llvm-<wbr>c/echo.ll (original)<br>
+++ llvm/trunk/test/Bindings/llvm-<wbr>c/echo.ll Mon May 14 01:09:00 2018<br>
@@ -159,3 +159,7 @@ cleanup:<br>
 exit:<br>
   ret void<br>
 }<br>
+<br>
+!llvm.module.flags = !{!1}<br>
+<br>
+!1 = !{i32 2, !"Debug Info Version", i32 3}<br>
<br>
Modified: llvm/trunk/tools/llvm-c-test/<wbr>echo.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-c-test/echo.cpp?rev=332219&r1=332218&r2=332219&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/tools/llvm-<wbr>c-test/echo.cpp?rev=332219&r1=<wbr>332218&r2=332219&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/tools/llvm-c-test/<wbr>echo.cpp (original)<br>
+++ llvm/trunk/tools/llvm-c-test/<wbr>echo.cpp Mon May 14 01:09:00 2018<br>
@@ -997,6 +997,18 @@ int llvm_echo(void) {<br>
   LLVMSetSourceFileName(M, SourceFileName, SourceFileLen);<br>
   LLVMSetModuleIdentifier(M, ModuleName, ModuleIdentLen);<br>
<br>
+  size_t SourceFlagsLen;<br>
+  LLVMModuleFlagEntry *ModuleFlags =<br>
+      LLVMCopyModuleFlagsMetadata(<wbr>Src, &SourceFlagsLen);<br>
+  for (unsigned i = 0; i < SourceFlagsLen; ++i) {<br>
+    size_t EntryNameLen;<br>
+    const char *EntryName =<br>
+        LLVMModuleFlagEntriesGetKey(<wbr>ModuleFlags, i, &EntryNameLen);<br>
+    LLVMAddModuleFlag(M, LLVMModuleFlagEntriesGetFlagBe<wbr>havior(ModuleFlags, i),<br>
+                      EntryName, EntryNameLen,<br>
+                      LLVMModuleFlagEntriesGetMetada<wbr>ta(ModuleFlags, i));<br>
+  }<br>
+<br>
   LLVMSetTarget(M, LLVMGetTarget(Src));<br>
   LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));<br>
   if (strcmp(LLVMGetDataLayoutStr(<wbr>M), LLVMGetDataLayoutStr(Src)))<br>
@@ -1011,6 +1023,7 @@ int llvm_echo(void) {<br>
   char *Str = LLVMPrintModuleToString(M);<br>
   fputs(Str, stdout);<br>
<br>
+  LLVMDisposeModuleFlagsMetadata<wbr>(ModuleFlags);<br>
   LLVMDisposeMessage(Str);<br>
   LLVMDisposeModule(Src);<br>
   LLVMDisposeModule(M);<br>
<br>
<br>
______________________________<wbr>_________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>