Quiet warnings for stricter compilers configurations (#84)
Fix warnings from -Wcast-qual and -Wstrict-prototypes.
A few other warning fixes and documentation improvements are added.
Also fixes Makefile so the before-release build script can invoke both gcc and clang. That script is here: laurencelundblade/qdv@536f083
This is an alternative to #79. It does not fix warnings for -Wbad-function-cast
because those fixes cause warnings with other compilers (MacOS clang). This doesn't enable GitHub automated build checks. The plan is to do that separately.
diff --git a/inc/qcbor/UsefulBuf.h b/inc/qcbor/UsefulBuf.h
index 7506c82..6dfacdf 100644
--- a/inc/qcbor/UsefulBuf.h
+++ b/inc/qcbor/UsefulBuf.h
@@ -41,6 +41,7 @@
when who what, where, why
-------- ---- --------------------------------------------------
+ 3/6/2021 mcr/llundblade Fix warnings related to --Wcast-qual
2/17/2021 llundblade Add method to go from a pointer to an offset.
1/25/2020 llundblade Add some casts so static anlyzers don't complain.
5/21/2019 llundblade #define configs for efficient endianness handling.
@@ -377,6 +378,14 @@
@param[in] UBC The @ref UsefulBuf to convert.
@return A non-const @ref UsefulBuf struct.
+
+ It is better to avoid use of this. The intended convention for
+ UsefulBuf is to make an empty buffer, some memory, as a UsefulBuf,
+ fill it in, and then make it a UsefulBufC. In that convension this
+ function is not needed.
+
+ This is an explicit way to quiet compiler warnings from
+ -Wcast-qual.
*/
static inline UsefulBuf UsefulBuf_Unconst(const UsefulBufC UBC);
@@ -606,7 +615,7 @@
static inline size_t UsefulBuf_PointerToOffset(UsefulBufC UB, const void *p);
-#if 1 // NOT_DEPRECATED
+#ifndef USEFULBUF_DISABLE_DEPRECATED
/** Deprecated macro; use @ref UsefulBuf_FROM_SZ_LITERAL instead */
#define SZLiteralToUsefulBufC(szString) \
((UsefulBufC) {(szString), sizeof(szString)-1})
@@ -623,9 +632,13 @@
/** Deprecated function; use UsefulBuf_Unconst() instead */
static inline UsefulBuf UsefulBufC_Unconst(const UsefulBufC UBC)
{
+ // See UsefulBuf_Unconst() implementation for comment on pragmas
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wcast-qual"
return (UsefulBuf){(void *)UBC.ptr, UBC.len};
+#pragma GCC diagnostic pop
}
-#endif
+#endif /* USEFULBUF_DISABLE_DEPRECATED */
@@ -1597,10 +1610,15 @@
return (UsefulBufC){UB.ptr, UB.len};
}
-
static inline UsefulBuf UsefulBuf_Unconst(const UsefulBufC UBC)
{
+ /* -Wcast-qual is a good warning flag to use in general. This is
+ * the one place in UsefulBuf where it needs to be quieted. Since
+ * clang supports GCC pragmas, this works for clang too. */
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wcast-qual"
return (UsefulBuf){(void *)UBC.ptr, UBC.len};
+#pragma GCC diagnostic pop
}
@@ -1647,7 +1665,7 @@
} else if(UB.ptr == NULL) {
ReturnValue = (UsefulBufC){NULL, UB.len - uAmount};
} else {
- ReturnValue = (UsefulBufC){(uint8_t *)UB.ptr + uAmount, UB.len - uAmount};
+ ReturnValue = (UsefulBufC){(const uint8_t *)UB.ptr + uAmount, UB.len - uAmount};
}
return ReturnValue;
@@ -1666,7 +1684,7 @@
}
// Cast to size_t (from ptrdiff_t) is OK because of check above
- const size_t uOffset = (size_t)((uint8_t *)p - (uint8_t *)UB.ptr);
+ const size_t uOffset = (size_t)((const uint8_t *)p - (const uint8_t *)UB.ptr);
if(uOffset >= UB.len) {
/* given pointer is off the end of the buffer */
@@ -2067,7 +2085,7 @@
// The ternery operator is subject to integer promotion, because the
// operands are smaller than int, so cast back to uint8_t is needed
// to be completely explicit about types (for static analyzers)
- return (uint8_t)(pResult ? *(uint8_t *)pResult : 0);
+ return (uint8_t)(pResult ? *(const uint8_t *)pResult : 0);
}
static inline uint16_t UsefulInputBuf_GetUint16(UsefulInputBuf *pMe)