summaryrefslogtreecommitdiffstats
path: root/source
diff options
context:
space:
mode:
authorEllie Hermaszewska <ellieh@nvidia.com>2023-01-04 20:01:42 +0800
committerGitHub <noreply@github.com>2023-01-04 20:01:42 +0800
commit57e9786d69f7d7846a289f5d2ce8a34c5f177e93 (patch)
treeba76411d2925c72ce682d437c2af7069df83264a /source
parent6dbdb74dbdc20783a0429229c21604a3d08d28f8 (diff)
Add format checking attributes on printf-like functions (#2570)
* Add format checking attributes on printf-like functions * Don't use printf format attributes on msvc Where they are not supported
Diffstat (limited to 'source')
-rw-r--r--source/core/slang-common.h6
-rw-r--r--source/core/slang-secure-crt.h4
-rw-r--r--source/core/slang-writer.h1
3 files changed, 11 insertions, 0 deletions
diff --git a/source/core/slang-common.h b/source/core/slang-common.h
index 7fddb067e..cd1490a20 100644
--- a/source/core/slang-common.h
+++ b/source/core/slang-common.h
@@ -120,6 +120,12 @@ template<typename T> void slang_use_obj(T&) {}
#endif
#endif
+#if defined(_MSC_VER)
+# define SLANG_ATTR_PRINTF(string_index, varargs_index)
+#else
+# define SLANG_ATTR_PRINTF(string_index, varargs_index) __attribute__((format(printf, string_index, varargs_index)))
+#endif
+
#ifndef SLANG_RT_API
#define SLANG_RT_API
#endif
diff --git a/source/core/slang-secure-crt.h b/source/core/slang-secure-crt.h
index 991fe939e..b6f6671dd 100644
--- a/source/core/slang-secure-crt.h
+++ b/source/core/slang-secure-crt.h
@@ -47,6 +47,7 @@ inline size_t strnlen_s(const char * str, size_t numberOfElements)
#endif
}
+__attribute__((format(printf, 3, 4)))
inline int sprintf_s(char * buffer, size_t sizeOfBuffer, const char * format, ...)
{
va_list argptr;
@@ -56,6 +57,9 @@ inline int sprintf_s(char * buffer, size_t sizeOfBuffer, const char * format, ..
return rs;
}
+// A patch was submitted to GCC wchar_t support in 2001, so I'm sure we can
+// enable this any day now...
+// __attribute__((format(wprintf, 3, 4)))
inline int swprintf_s(wchar_t * buffer, size_t sizeOfBuffer, const wchar_t * format, ...)
{
va_list argptr;
diff --git a/source/core/slang-writer.h b/source/core/slang-writer.h
index ccdcb3747..2a86af629 100644
--- a/source/core/slang-writer.h
+++ b/source/core/slang-writer.h
@@ -16,6 +16,7 @@ namespace Slang
class WriterHelper
{
public:
+ SLANG_ATTR_PRINTF(2, 3)
SlangResult print(const char* format, ...);
SlangResult put(const char* text);
SlangResult put(const UnownedStringSlice& text);