[Mlir-commits] [mlir] 9be3c01 - Undef the `DEFINE_C_API_STRUCT` macro after using it in the MLIR C API header (NFC)

Mehdi Amini llvmlistbot at llvm.org
Mon Nov 2 11:18:52 PST 2020


Author: Mehdi Amini
Date: 2020-11-02T19:18:32Z
New Revision: 9be3c01eb989a1729ea34ebe102b3716acfe5223

URL: https://github.com/llvm/llvm-project/commit/9be3c01eb989a1729ea34ebe102b3716acfe5223
DIFF: https://github.com/llvm/llvm-project/commit/9be3c01eb989a1729ea34ebe102b3716acfe5223.diff

LOG: Undef the `DEFINE_C_API_STRUCT` macro after using it in the MLIR C API header (NFC)

Leaking macros isn't a good practice when defining headers. This
requires to duplicate the macro definition in every header though, but
that seems like a better tradeoff right now.

Differential Revision: https://reviews.llvm.org/D90633

Added: 
    

Modified: 
    mlir/include/mlir-c/AffineExpr.h
    mlir/include/mlir-c/AffineMap.h
    mlir/include/mlir-c/IR.h

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir-c/AffineExpr.h b/mlir/include/mlir-c/AffineExpr.h
index fb00c70b9ba5..62700ada031f 100644
--- a/mlir/include/mlir-c/AffineExpr.h
+++ b/mlir/include/mlir-c/AffineExpr.h
@@ -17,8 +17,30 @@
 extern "C" {
 #endif
 
+/*============================================================================*/
+/** Opaque type declarations.
+ *
+ * Types are exposed to C bindings as structs containing opaque pointers. They
+ * are not supposed to be inspected from C. This allows the underlying
+ * representation to change without affecting the API users. The use of structs
+ * instead of typedefs enables some type safety as structs are not implicitly
+ * convertible to each other.
+ *
+ * Instances of these types may or may not own the underlying object. The
+ * ownership semantics is defined by how an instance of the type was obtained.
+ */
+/*============================================================================*/
+
+#define DEFINE_C_API_STRUCT(name, storage)                                     \
+  struct name {                                                                \
+    storage *ptr;                                                              \
+  };                                                                           \
+  typedef struct name name
+
 DEFINE_C_API_STRUCT(MlirAffineExpr, const void);
 
+#undef DEFINE_C_API_STRUCT
+
 /** Gets the context that owns the affine expression. */
 MlirContext mlirAffineExprGetContext(MlirAffineExpr affineExpr);
 

diff  --git a/mlir/include/mlir-c/AffineMap.h b/mlir/include/mlir-c/AffineMap.h
index a5d99185eaf4..4793407db0ab 100644
--- a/mlir/include/mlir-c/AffineMap.h
+++ b/mlir/include/mlir-c/AffineMap.h
@@ -16,8 +16,30 @@
 extern "C" {
 #endif
 
+/*============================================================================*/
+/** Opaque type declarations.
+ *
+ * Types are exposed to C bindings as structs containing opaque pointers. They
+ * are not supposed to be inspected from C. This allows the underlying
+ * representation to change without affecting the API users. The use of structs
+ * instead of typedefs enables some type safety as structs are not implicitly
+ * convertible to each other.
+ *
+ * Instances of these types may or may not own the underlying object. The
+ * ownership semantics is defined by how an instance of the type was obtained.
+ */
+/*============================================================================*/
+
+#define DEFINE_C_API_STRUCT(name, storage)                                     \
+  struct name {                                                                \
+    storage *ptr;                                                              \
+  };                                                                           \
+  typedef struct name name
+
 DEFINE_C_API_STRUCT(MlirAffineMap, const void);
 
+#undef DEFINE_C_API_STRUCT
+
 /** Gets the context that the given affine map was created with*/
 MlirContext mlirAffineMapGetContext(MlirAffineMap affineMap);
 

diff  --git a/mlir/include/mlir-c/IR.h b/mlir/include/mlir-c/IR.h
index 77c66e027c22..eea7c51b1a1b 100644
--- a/mlir/include/mlir-c/IR.h
+++ b/mlir/include/mlir-c/IR.h
@@ -61,6 +61,8 @@ DEFINE_C_API_STRUCT(MlirModule, const void);
 DEFINE_C_API_STRUCT(MlirType, const void);
 DEFINE_C_API_STRUCT(MlirValue, const void);
 
+#undef DEFINE_C_API_STRUCT
+
 /** Named MLIR attribute.
  *
  * A named attribute is essentially a (name, attribute) pair where the name is


        


More information about the Mlir-commits mailing list