utils/: Split compiler-version macros into a separate header.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 26 May 2018 15:18:43 +0000 (16:18 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 26 May 2018 15:19:34 +0000 (16:19 +0100)
This way we can use them in other headers without the namespace mess of
`macros.h'.

Also, actually document `CLANG_VERSION_P'.

utils/Makefile.am
utils/compiler.3 [new file with mode: 0644]
utils/compiler.h [new file with mode: 0644]
utils/macros.3
utils/macros.h

index b52e7f6..2f6ed7c 100644 (file)
@@ -32,6 +32,10 @@ libutils_la_SOURCES   =
 ###--------------------------------------------------------------------------
 ### Component files.
 
+## Compiler checking.
+pkginclude_HEADERS     += compiler.h
+LIBMANS                        += compiler.3
+
 ## Utility macros.
 pkginclude_HEADERS     += macros.h
 LIBMANS                        += macros.3
diff --git a/utils/compiler.3 b/utils/compiler.3
new file mode 100644 (file)
index 0000000..1920be4
--- /dev/null
@@ -0,0 +1,35 @@
+.\" -*-nroff-*-
+.TH compiler 3 "26 May 2018" "Straylight/Edgeware" "mLib utilities library"
+.SH NAME
+compiler \- detect compiler version
+.\" @GCC_VERSION_P
+.\" @CLANG_VERSION_P
+.SH SYNOPSIS
+.nf
+.B "#include <mLib/compiler.h>"
+
+.BI "int GCC_VERSION_P(" maj ", " min ");"
+.BI "int CLANG_VERSION_P(" maj ", " min ");"
+.fi
+.SH DESCRIPTION
+The macro invocation
+.BI GCC_VERSION_P( maj ", " min )
+expands to a compile-time constant nonzero value if the present compiler
+is GCC version
+.IR maj . min
+or better, or claims compatibility with it.
+This is frequently imperfect, as many compilers claim compatibility
+without implementing all of the necessary features, but it works
+adequately if one takes care.
+.PP
+The macro invocation
+.BI CLANG_VERSION_P( maj ", " min )
+expands to a compile-time constant nonzero value if the present compiler
+is Clang version
+.IR maj . min
+or better (or claims compatibility with it, but this is less likely
+than for GCC.
+.SH "SEE ALSO"
+.BR mLib (3).
+.SH "AUTHOR"
+Mark Wooding, <mdw@distorted.org.uk>
diff --git a/utils/compiler.h b/utils/compiler.h
new file mode 100644 (file)
index 0000000..7e66d12
--- /dev/null
@@ -0,0 +1,58 @@
+/* -*-c-*-
+ *
+ * Macros for determining the compiler version
+ *
+ * (c) 2018 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of the mLib utilities library.
+ *
+ * mLib is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU Library General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * mLib is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with mLib.  If not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+ * USA.
+ */
+
+#ifndef MLIB_COMPILER_H
+#define MLIB_COMPILER_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+/*----- Macros ------------------------------------------------------------*/
+
+#if defined(__GNUC__)
+#  define GCC_VERSION_P(maj, min)                                      \
+       (__GNUC__ > (maj) || (__GNUC__ == (maj) && __GNUC_MINOR__ >= (min)))
+#else
+#  define GCC_VERSION_P(maj, min) 0
+#endif
+
+#ifdef __clang__
+#  define CLANG_VERSION_P(maj, min)                                    \
+       (__clang_major__ > (maj) || (__clang_major__ == (maj) &&        \
+                                    __clang_minor__ >= (min)))
+#else
+#  define CLANG_VERSION_P(maj, min) 0
+#endif
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif
index bd8cbb6..a2a9003 100644 (file)
@@ -16,7 +16,6 @@ macros \- useful macros
 .\" @MUFFLE_WARNINGS_DECL
 .\" @MUFFLE_WARNINGS_EXPR
 .\" @MUFFLE_WARNINGS_STMT
-.\" @GCC_VERSION_P
 .\" @GCC_WARNING
 .SH SYNOPSIS
 .nf
@@ -40,7 +39,6 @@ macros \- useful macros
 .BI "MUFFLE_WARNINGS_EXPR(" warns ", " expr ")"
 .BI "MUFFLE_WARNINGS_STMT(" warns ", " stmt ")"
 
-.BI "int GCC_VERSION_P(" maj ", " min ");"
 .BI "GCC_WARNING(" option ")"
 .BI "CLANG_WARNING(" option ")"
 .fi
@@ -168,7 +166,15 @@ naming a GCC warning option, e.g.,
 The
 .B CLANG_WARNING
 is similar, except that it works with the Clang compiler.
+.PP
+Note that including
+.B <mLib/macros.h>
+also defines the compiler-test macros in
+.BR <mLib/compiler.h>;
+see
+.BR compiler (3).
 .SH "SEE ALSO"
-.BR mLib (3).
+.BR mLib (3),
+.BR compiler (3).
 .SH "AUTHOR"
 Mark Wooding, <mdw@distorted.org.uk>
index 02d820e..bcceecd 100644 (file)
   extern "C" {
 #endif
 
+/*----- Header files ------------------------------------------------------*/
+
+#ifndef MLIB_COMPILER_H
+#  include "compiler.h"
+#endif
+
 /*----- Miscellaneous utility macros --------------------------------------*/
 
 #define N(v) (sizeof(v)/sizeof(*v))
 
 /* --- Compiler-specific definitions --- */
 
-#if defined(__GNUC__)
-#  define GCC_VERSION_P(maj, min)                                      \
-       (__GNUC__ > (maj) || (__GNUC__ == (maj) && __GNUC_MINOR__ >= (min)))
-#else
-#  define GCC_VERSION_P(maj, min) 0
-#endif
-
-#ifdef __clang__
-#  define CLANG_VERSION_P(maj, min)                                    \
-       (__clang_major__ > (maj) || (__clang_major__ == (maj) &&        \
-                                    __clang_minor__ >= (min)))
-#else
-#  define CLANG_VERSION_P(maj, min) 0
-#endif
-
 #if GCC_VERSION_P(2, 5) || CLANG_VERSION_P(3, 3)
 #  define NORETURN __attribute__((noreturn))
 #  define PRINTF_LIKE(fix, aix) __attribute__((format(printf, fix, aix)))