sigmd 0.1.0

Windows API signature metadata
Documentation
#pragma once

//
// Always implied definitions.
//

#if !defined(_MSC_VER)
#   define _MSC_VER 1939
#endif

#if !defined(_MSC_FULL_VER)
#   define _MSC_FULL_VER 193933519
#endif

#if !defined(_WIN32)
#   define _WIN32
#endif

#if !defined(WIN32)
#   define WIN32
#endif

//
// Definitions implied by _M_IX86.
//

#if defined(_M_IX86)
#   if !defined(_M_IX86_FP)
#       define _M_IX86_FP 0
#   endif
#   if !defined(_X86_)
#       define _X86_
#   endif
#endif

//
// Definitions implied by _M_X64.
//

#if defined(_M_X64)
#   if !defined(_M_AMD64)
#       define _M_AMD64
#   endif
#   if !defined(_AMD64_)
#       define _AMD64_
#   endif
#   if !defined(_WIN64)
#       define _WIN64
#   endif
#endif

//
// Forced definitions.
//

#define WINVER                    0x0A00
#define _WIN32_WINNT              0x0A00

//
// Define wchar_t.
//

#define _WCHAR_T_DEFINED          1
#define _NATIVE_WCHAR_T_DEFINED   1
#if !defined(__cplusplus)
typedef __wchar_t wchar_t;
#endif

//
// UCRT pragma-warning macros referenced by some UCRT headers.
//

#define _UCRT_DISABLE_CLANG_WARNINGS
#define _UCRT_RESTORE_CLANG_WARNINGS

//
// General predefined macros.
//

#define __RPC_FAR

//
// Special macros.
//

#undef DECLSPEC_UUID
#define DECLSPEC_UUID(x)    __attribute__((annotate("__UUID|" x)))

#undef __OVERRIDE
#define __OVERRIDE          __attribute__((annotate("__OVERRIDE")))

//
// Check for __VA_OPT__ support.
//
// ref: https://stackoverflow.com/a/48045656
//

#define __SIGMD_VA_OPT_PROBE_IMPL(_1, _2, _3, ...) _3
#define __SIGMD_VA_OPT_PROBE(...)                  __SIGMD_VA_OPT_PROBE_IMPL(__VA_OPT__(,), 1, 0,)
#define __SIGMD_HAS_VA_OPT                         __SIGMD_VA_OPT_PROBE(?)

#if !__SIGMD_HAS_VA_OPT
#   error "__VA_OPT__ is required for SAL annotation expansion."
#endif

//
// SAL annotation re-encoder.
//
// Each SAL site is rewritten into a sequence of clang annotate()
// attributes carrying a per-site counter that defeats clang's
// AnnotateAttr content-based dedup across redeclarations:
//
//   __attribute__((annotate("__SAL:<c>:<name>")))           open marker
//   __attribute__((annotate("__SAL:<c>:<i>:<value>")))      arg i
//
// <c> is __COUNTER__ captured once per site so the open marker and
// every arg of one site share the same counter. The decoder routes
// args to their owning open marker by counter, so the physical
// order of attributes after clang's redeclaration-merge dedup does
// not matter.
//
// Without the __COUNTER__, clang's redeclaration-merge dedup folds
// AnnotateAttr lists with identical content - the `gethostname` case
// in winsock2.h, where two redeclarations carry identical SAL
// strings and clang collapses them, dropping per-decl argument data.
//

#define __SPECSTRINGS_STRICT_LEVEL 0
#include <sal.h>
#include <rpcsal.h>

#define __SIGMD_SAL_STR(x)              __SIGMD_SAL_STR_IMPL(x)
#define __SIGMD_SAL_STR_IMPL(x)         #x

//
// Strip one set of parens.
//
// args = (a, b, c)
// __SIGMD_SAL_STRIP args -> a, b, c
//

#define __SIGMD_SAL_STRIP(...)          __VA_ARGS__

//
// Open marker.
//
// __SAL:<c>:<name>
//

#define __SIGMD_SAL_OPEN(c, name) \
    __attribute__((annotate("__SAL:" __SIGMD_SAL_STR(c) ":" #name)))

//
// One indexed arg.
//
// __SAL:<c>:<i>:<value>
//

#define __SIGMD_SAL_ARG(c, i, value) \
    __attribute__((annotate("__SAL:" __SIGMD_SAL_STR(c) ":" #i ":" #value)))

//
// Arity counter.
//
// __SIGMD_SAL_NARG(a, b, c) -> 3
// __SIGMD_SAL_NARG(a, b) -> 2
// ...
//

#define __SIGMD_SAL_NARG(...)           __SIGMD_SAL_NARG_IMPL(__VA_ARGS__, 6, 5, 4, 3, 2, 1)
#define __SIGMD_SAL_NARG_IMPL(_1, _2, _3, _4, _5, _6, N, ...)  N

//
// Per-arity unrolls.
//
// __SIGMD_SAL_EMIT_<N>(c, x0, ..., xN-1)
//     -> __SIGMD_SAL_ARG(c, 0, x0) ... __SIGMD_SAL_ARG(c, N-1, xN-1)
//

#define __SIGMD_SAL_EMIT_1(c, x0)                       __SIGMD_SAL_ARG(c, 0, x0)
#define __SIGMD_SAL_EMIT_2(c, x0, x1)                   __SIGMD_SAL_EMIT_1(c, x0)                 __SIGMD_SAL_ARG(c, 1, x1)
#define __SIGMD_SAL_EMIT_3(c, x0, x1, x2)               __SIGMD_SAL_EMIT_2(c, x0, x1)             __SIGMD_SAL_ARG(c, 2, x2)
#define __SIGMD_SAL_EMIT_4(c, x0, x1, x2, x3)           __SIGMD_SAL_EMIT_3(c, x0, x1, x2)         __SIGMD_SAL_ARG(c, 3, x3)
#define __SIGMD_SAL_EMIT_5(c, x0, x1, x2, x3, x4)       __SIGMD_SAL_EMIT_4(c, x0, x1, x2, x3)     __SIGMD_SAL_ARG(c, 4, x4)
#define __SIGMD_SAL_EMIT_6(c, x0, x1, x2, x3, x4, x5)   __SIGMD_SAL_EMIT_5(c, x0, x1, x2, x3, x4) __SIGMD_SAL_ARG(c, 5, x5)

//
// Pick __SIGMD_SAL_EMIT_<N> by arity and apply with bound counter.
//

#define __SIGMD_SAL_DISPATCH(c, ...) \
    __VA_OPT__(__SIGMD_SAL_DISPATCH_EXPAND(c, __SIGMD_SAL_NARG(__VA_ARGS__), __VA_ARGS__))

#define __SIGMD_SAL_DISPATCH_EXPAND(c, n, ...) \
    __SIGMD_SAL_DISPATCH_IMPL(c, n, __VA_ARGS__)

#define __SIGMD_SAL_DISPATCH_IMPL(c, n, ...) \
    __SIGMD_SAL_EMIT_##n(c, __VA_ARGS__)

//
// One-hop counter capture.
//
// __SIGMD_SAL_BIND(c, name, (x, y, z))
//     -> __SIGMD_SAL_OPEN(c, name) __SIGMD_SAL_DISPATCH(c, x, y, z)
//

#define __SIGMD_SAL_BIND(c, name, args) \
    __SIGMD_SAL_OPEN(c, name) __SIGMD_SAL_DISPATCH(c, __SIGMD_SAL_STRIP args)

//
// SAL annotation re-encoder entry point.
//

#define __SIGMD_SAL(name, args)    __SIGMD_SAL_BIND(__COUNTER__, name, args)

//
// SDK hooks.
//
// _SAL1_Source_ is a pass-through. The real content lives in
// `annotes`, which expands into nested _SAL?_*_Source_ calls that
// we capture below. SAL1 annotations are the legacy annotations
// like __in or __in_bcount(size).
//

#undef _SAL1_Source_
#undef _SAL1_1_Source_
#undef _SAL1_2_Source_
#undef _SAL2_Source_
#undef _SAL_L_Source_

#define _SAL1_Source_(Name, args, annotes)      annotes
#define _SAL1_1_Source_(Name, args, annotes)    __SIGMD_SAL(Name, args)
#define _SAL1_2_Source_(Name, args, annotes)    __SIGMD_SAL(Name, args)
#define _SAL2_Source_(Name, args, annotes)      __SIGMD_SAL(Name, args)
#define _SAL_L_Source_(Name, args, annotes)     __SIGMD_SAL(Name, args)

//
// _At_ and _When_ need special handling.
//

#undef _At_
#undef _When_

#define _At_(expr, annotes)             annotes
#define _When_(expr, annotes)           annotes

//
// Legacy uppercase argless flags. Map to their SAL2 equivalents.
//

#undef IN
#undef OUT

#define IN     __SIGMD_SAL_OPEN(__COUNTER__, _In_)
#define OUT    __SIGMD_SAL_OPEN(__COUNTER__, _Out_)

//
// Forced includes.
//

#include <basetyps.h>
#include <windef.h>
#include <winbase.h>
#include <wingdi.h>         // needed for gl/gl.h
#include <clfsw32.h>        // needed for clfsmgmtw32.h