From fef0343476560770e2ce0267488d867c8587a769 Mon Sep 17 00:00:00 2001
From: Tristan Partin <tristan@partin.io>
Date: Wed, 23 Sep 2026 06:42:15 +0000
Subject: [PATCH v2 8/8] Map pg_attribute_counted_by() to _Field_size_() under
 MSVC

MSVC has no counted_by attribute, but its source annotation language has
_Field_size_(), which states the same property: the field is a buffer
whose writable size in elements is given by the named expression.

The static analyzer will make use of the information when enabling the
static analyzer with /analyze.

Author: Tristan Partin <tristan@partin.io>
Signed-off-by: Tristan Partin <tristan@partin.io>
---
 src/include/c.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/include/c.h b/src/include/c.h
index 6505a0d00ff..3ff6c104edf 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -76,6 +76,9 @@
 #if defined(WIN32) || defined(__CYGWIN__)
 #include <fcntl.h>				/* ensure O_BINARY is available */
 #endif
+#ifdef _MSC_VER
+#include <sal.h>
+#endif
 #include <locale.h>
 #ifdef HAVE_XLOCALE_H
 #include <xlocale.h>
@@ -310,10 +313,17 @@ extern "C++"
  *   including after either member is updated
  *
  * The attribute is ignored in C++ due to lack of compiler support.
+ *
+ * MSVC has no equivalent of its own, but its source annotation language spells
+ * the same property _Field_size_(), which its static analyzer understands.
+ * That only has an effect under /analyze; in an ordinary build it expands to
+ * nothing.
  */
 #ifndef __cplusplus
 #if __has_attribute (counted_by)
 #define pg_attribute_counted_by(count) __attribute__((counted_by(count)))
+#elif defined(_MSC_VER)
+#define pg_attribute_counted_by(count) _Field_size_(count)
 #else
 #define pg_attribute_counted_by(count)
 #endif
-- 
Tristan Partin
https://tristan.partin.io

