[PATCH] D139381: Added a C-API binding to query the LLVM version

wenzel.jakob@epfl.ch via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 5 17:12:32 PST 2022


wjakob created this revision.
Herald added a reviewer: deadalnix.
Herald added a subscriber: hiraditya.
Herald added a project: All.
wjakob requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The LLVM C bindings currently offer no way to query the version string
dynamically. This is a useful feature in situations where a program
isn't compiled against a specific version of LLVM but rather loads it
dynamically (e.g. using `dlopen()`).

In situations where the shared library filename doesn't reveal the
version (e.g. LLVM-C.dll) and to adapt to version-specific API
differences, it is then useful to be able to query the version string by
calling the proposed `LLVMGetVersion` function.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139381

Files:
  llvm/include/llvm-c/Core.h
  llvm/lib/IR/Core.cpp


Index: llvm/lib/IR/Core.cpp
===================================================================
--- llvm/lib/IR/Core.cpp
+++ llvm/lib/IR/Core.cpp
@@ -61,6 +61,17 @@
   llvm_shutdown();
 }
 
+/*===-- Version query -----------------------------------------------------===*/
+
+void LLVMGetVersion(unsigned *major, unsigned *minor, unsigned *patch) {
+    if (major)
+        *major = LLVM_VERSION_MAJOR;
+    if (minor)
+        *minor = LLVM_VERSION_MINOR;
+    if (patch)
+        *patch = LLVM_VERSION_PATCH;
+}
+
 /*===-- Error handling ----------------------------------------------------===*/
 
 char *LLVMCreateMessage(const char *Message) {
Index: llvm/include/llvm-c/Core.h
===================================================================
--- llvm/include/llvm-c/Core.h
+++ llvm/include/llvm-c/Core.h
@@ -479,6 +479,16 @@
     @see ManagedStatic */
 void LLVMShutdown(void);
 
+/*===-- Version query -----------------------------------------------------===*/
+
+/**
+ * Return the major, minor, and patch version of LLVM
+ *
+ * The version components are returned via the function's three output
+ * parameters or skipped if a NULL pointer was supplied.
+ */
+void LLVMGetVersion(unsigned *major, unsigned *minor, unsigned *patch);
+
 /*===-- Error handling ----------------------------------------------------===*/
 
 char *LLVMCreateMessage(const char *Message);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139381.480287.patch
Type: text/x-patch
Size: 1376 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221206/608006b4/attachment.bin>


More information about the llvm-commits mailing list