specstrings.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*****************************************************************************\
  2. * *
  3. * SpecStrings.h - additional markers for documenting the semantics of APIs *
  4. * *
  5. * Version 1.0 *
  6. * *
  7. * Copyright (c) Microsoft Corporation. All rights reserved. *
  8. * *
  9. \*****************************************************************************/
  10. #if _MSC_VER > 1000
  11. #pragma once
  12. #endif // #if _MSC_VER > 1000
  13. #ifdef __cplusplus
  14. #ifndef __nothrow
  15. # define __nothrow __declspec(nothrow)
  16. #endif
  17. extern "C" {
  18. #else
  19. #ifndef __nothrow
  20. # define __nothrow
  21. #endif
  22. #endif // #ifdef __cplusplus
  23. #if (_MSC_VER >= 1000) && !defined(MIDL_PASS) && defined(_PREFAST_)
  24. // ----------------------------------------------------------------------------
  25. // The primitive MACRO definitations. More complicated semantics such as
  26. // the meaning of an __in parameter are express in SpecStrings composed from
  27. // these primives.
  28. //
  29. // In the primitive __declspec("SAL_*") annotations "SAL" stands for SpecString
  30. // Annotation Language. These __declspec("SAL_*") annotations are the
  31. // primivates the compiler understands and all higher-level SpecString MACROs
  32. // will decompose into these primivates.
  33. // ----------------------------------------------------------------------------
  34. // helper macro, do not use directly
  35. #define SPECSTRINGIZE( x ) #x
  36. //
  37. // __null p
  38. // __notnull p
  39. // __maybenull p
  40. //
  41. // Annotates a pointer p. States that pointer p is null. Commonly used
  42. // in the negated form __notnull or the possibly null form __maybenull.
  43. //
  44. #define __null __declspec("SAL_null")
  45. #define __notnull __declspec("SAL_notnull")
  46. #define __maybenull __declspec("SAL_maybenull")
  47. //
  48. // __reserved p or v
  49. //
  50. // Annotates a field that must be 0/null for future use.
  51. //
  52. #define __reserved __declspec("SAL_pre") __declspec("SAL_null")
  53. //
  54. // __readonly l
  55. // __notreadonly l
  56. // __mabyereadonly l
  57. //
  58. // Annotates a location l. States that location l is not modified after
  59. // this point. If the annotation is placed on the precondition state of
  60. // a function, the restriction only applies until the postcondition state
  61. // of the function. __maybereadonly states that the annotated location
  62. // may be modified, whereas __notreadonly states that a location must be
  63. // modified.
  64. //
  65. #define __readonly __declspec("SAL_readonly")
  66. #define __notreadonly __declspec("SAL_notreadonly")
  67. #define __maybereadonly __declspec("SAL_maybereadonly")
  68. //
  69. // __valid v
  70. // __notvalid v
  71. // __maybevalid v
  72. //
  73. // Annotates any value v. States that the value satisfies all properties of
  74. // valid values of its type. For example, for a string buffer, valid means
  75. // that the buffer pointer is not null, and the buffer is null terminated.
  76. //
  77. #define __valid __declspec("SAL_valid")
  78. #define __notvalid __declspec("SAL_notvalid")
  79. #define __maybevalid __declspec("SAL_maybevalid")
  80. //
  81. // __checkReturn v
  82. //
  83. // Annotates a return value v. States that the caller should not ignore
  84. // this value.
  85. //
  86. #define __checkReturn __declspec("SAL_checkReturn")
  87. //
  88. // __readableTo(size) p
  89. //
  90. // Annotates a buffer pointer p. If the buffer can be read, size describes
  91. // how much of the buffer is readable. For a reader of the buffer, this is
  92. // an explicit permission to read upto size, rather than a restriction to
  93. // read only upto size.
  94. //
  95. #define __readableTo(size) __declspec("SAL_readableTo("SPECSTRINGIZE(size)")")
  96. //
  97. // __writableTo(size) p
  98. //
  99. // Annotates a buffer pointer p. If the buffer can be modified, size
  100. // describes how much of the buffer is writable (usually the allocation
  101. // size). For a writer of the buffer, this is an explicit permission to
  102. // write upto size, rather than a restriction to write only upto size.
  103. //
  104. #define __writableTo(size) __declspec("SAL_writableTo("SPECSTRINGIZE(size)")")
  105. //
  106. // __typefix(ctype) p
  107. //
  108. // Annotates any value. States that if the value is annotated as valid,
  109. // then it satisfies all of the properties of valid ctype values. ctype
  110. // must be a visible C/C++ type at the program point where the annotation
  111. // is placed.
  112. //
  113. #define __typefix(ctype) __declspec("SAL_typefix("SPECSTRINGIZE(ctype)")")
  114. //
  115. // __deref p
  116. //
  117. // Annotates a pointer p. The next annotation applies one dereference down
  118. // in the type. If readableTo(p, size) then the next annotation applies to
  119. // all elements *(p+i) for which i satisfies the size. If p is a pointer
  120. // to a struct, the next annotation applies to all fields of the struct.
  121. //
  122. #define __deref __declspec("SAL_deref")
  123. //
  124. // __pre __next_annotation
  125. //
  126. // The next annotation applies in the precondition state
  127. //
  128. #define __pre __declspec("SAL_pre")
  129. //
  130. // __post __next_annotation
  131. //
  132. // The next annotation applies in the postcondition state
  133. //
  134. #define __post __declspec("SAL_post")
  135. //
  136. // __exceptthat
  137. //
  138. // Given a set of annotations Q containing __exceptthat maybeP, the effect of
  139. // the except clause is to erase any P or notP annotations (explicit or
  140. // implied) within Q at the same level of dereferencing that the except
  141. // clause appears, and to replace it with maybeP.
  142. //
  143. // Example 1: __valid __exceptthat __maybenull on a pointer p means that the
  144. // pointer may be null, and is otherwise valid, thus overriding
  145. // the implicit notnull annotation implied by __valid on
  146. // pointers.
  147. //
  148. // Example 2: __valid __deref __exceptthat __maybenull on an int **p means
  149. // that p is not null (implied by valid), but the elements
  150. // pointed to by p could be null, and are otherwise valid.
  151. //
  152. #define __exceptthat __declspec("SAL_except")
  153. //
  154. // __override
  155. //
  156. // Use __override to specify C#-style 'override' behaviour for
  157. // overriding virtual methods. It improves documentation of code and
  158. // helps to identify bugs caused by changes to overridden method
  159. // signatures.
  160. //
  161. #define __override __declspec("__override")
  162. //
  163. // __fallthrough
  164. //
  165. // Use __fallthrough before switch statement labels where fall-through
  166. // to distinguish from forgotten break statements.
  167. //
  168. __inline __nothrow void __FallThrough() {}
  169. #define __fallthrough __FallThrough();
  170. //
  171. // __callback
  172. //
  173. // Use __callback for functions that used as function pointers, the
  174. // keyword will silence various OACR checks on formal parameters of the
  175. // marked functions
  176. //
  177. #define __callback __declspec("__callback")
  178. // ----------------------------------------------------------------------------
  179. // The high level MACRO definitations. These are compose from the above
  180. // primitives and express common semantics in a compact MACRO.
  181. // ----------------------------------------------------------------------------
  182. //
  183. // __in p
  184. //
  185. // Specifices that the pointer p is a IN parameter. It has the
  186. // precondition of being a valid pointer and the memory that is pointed
  187. // to is initialized in a valid manner for the datatype. It has the
  188. // precondition that the memory it points to is readonly.
  189. //
  190. #define __in __pre __valid \
  191. __pre __deref __readonly
  192. //
  193. // __out p
  194. //
  195. // Specifices that the pointer p is an OUT paramters. It has the
  196. // precodition that the pointer is not null. The purpose of the
  197. // "__exceptthat __mabyereadonly" statement is to enable the common
  198. // annotation of "__in __out p". The postcondition is the pointer
  199. // must be valid and the memory the pointer references must be
  200. // valid.
  201. //
  202. #define __out __pre __notnull \
  203. __pre __deref __exceptthat __maybereadonly \
  204. __post __valid
  205. //
  206. // __inout p
  207. //
  208. // Precondition that p and *p are valid.
  209. // Postcondition that p and *p are valid.
  210. //
  211. // This is shorthand as the same annotation as "__in __out p" and
  212. // the annotations "__in __out p" and "__inout p" behave identically.
  213. //
  214. #define __inout __pre __valid \
  215. __post __valid
  216. //
  217. // __opt p
  218. //
  219. // Adds the condition that it is valid for the pointer p to be NULL
  220. // It should be used in combination with a preceeding __in or __out
  221. // annotation as:
  222. // __in __opt p // means if p is not null then p and *p have the
  223. // precondition of being valid.
  224. // __out __opt p // means if p is not null then p and *p have the
  225. // postcondition of being valid.
  226. //
  227. #define __opt __pre __exceptthat __maybenull \
  228. __post __exceptthat __maybenull
  229. //
  230. // __out_ecount(size) p
  231. //
  232. // Pointer p is an OUT parameter. The size of p is given in element
  233. // counts by size.
  234. //
  235. #define __out_ecount(size) __pre __notnull \
  236. __pre __deref __exceptthat __maybereadonly \
  237. __pre __declspec("SAL_writableTo(elementCount("SPECSTRINGIZE(size)"))") \
  238. __post __valid \
  239. __post __declspec("SAL_readableTo(elementCount("SPECSTRINGIZE(size)"))")
  240. //
  241. // __in_ecount(size) p
  242. //
  243. // Pointer p is an IN parameter. The size of p is given by in element
  244. // counts by size.
  245. //
  246. #define __in_ecount(size) __pre __valid \
  247. __pre __deref __readonly \
  248. __pre __declspec("SAL_readableTo(elementCount("SPECSTRINGIZE(size)"))") \
  249. __post __declspec("SAL_readableTo(elementCount("SPECSTRINGIZE(size)"))")
  250. //
  251. // __inout_ecount(size) p
  252. //
  253. // Is shorthand for: "__in_ecount(size) __out_ecount(size) p"
  254. //
  255. #define __inout_ecount(size) __pre __valid \
  256. __post __valid \
  257. __pre __declspec("SAL_readableTo(elementCount("SPECSTRINGIZE(size)"))") \
  258. __post __declspec("SAL_readableTo(elementCount("SPECSTRINGIZE(size)"))") \
  259. __pre __declspec("SAL_writableTo(elementCount("SPECSTRINGIZE(size)"))")
  260. //
  261. // __out_bcount(size) p
  262. //
  263. // Pointer p is an OUT parameter. The size of p is given in byte counts
  264. // by size.
  265. //
  266. #define __out_bcount(size) __pre __notnull \
  267. __pre __deref __exceptthat __maybereadonly \
  268. __pre __declspec("SAL_writableTo(byteCount("SPECSTRINGIZE(size)"))") \
  269. __post __valid \
  270. __post __declspec("SAL_readableTo(byteCount("SPECSTRINGIZE(size)"))")
  271. //
  272. // __in_bcount(size) p
  273. //
  274. // Pointer p is an IN parameter. The size of p is given in byte counts
  275. // by size.
  276. //
  277. #define __in_bcount(size) __pre __valid \
  278. __pre __deref __readonly \
  279. __pre __declspec("SAL_readableTo(byteCount("SPECSTRINGIZE(size)"))") \
  280. __post __declspec("SAL_readableTo(byteCount("SPECSTRINGIZE(size)"))")
  281. //
  282. // __inout_bcount(size) p
  283. //
  284. // Is shorthand for: "__in_bcount(size) __out_bcount(size) p"
  285. //
  286. #define __inout_bcount(size) __pre __valid \
  287. __post __valid \
  288. __pre __declspec("SAL_readableTo(byteCount("SPECSTRINGIZE(size)"))") \
  289. __post __declspec("SAL_readableTo(byteCount("SPECSTRINGIZE(size)"))") \
  290. __pre __declspec("SAL_writableTo(byteCount("SPECSTRINGIZE(size)"))")
  291. //
  292. // __format_string
  293. //
  294. #define __format_string
  295. #else
  296. #define __null
  297. #define __notnull
  298. #define __maybenull
  299. #define __reserved
  300. #define __readonly
  301. #define __notreadonly
  302. #define __maybereadonly
  303. #define __valid
  304. #define __novalid
  305. #define __maybevalid
  306. #define __checkReturn
  307. #define __readableTo(size)
  308. #define __writableTo(size)
  309. #define __typefix(ctype)
  310. #define __deref
  311. #define __pre
  312. #define __post
  313. #define __exceptthat
  314. #define __override
  315. #define __fallthrough
  316. #define __callback
  317. #define __in
  318. #define __out
  319. #define __inout
  320. #define __opt
  321. #define __out_ecount(size)
  322. #define __in_ecount(size)
  323. #define __inout_ecount(size)
  324. #define __out_bcount(size)
  325. #define __in_bcount(size)
  326. #define __inout_bcount(size)
  327. #define __format_string
  328. #endif
  329. #ifdef __cplusplus
  330. }
  331. #endif