Skip to content

Commit 5f1f1b3

Browse files
Merge pull request #4429 from Rastaban/update_examples
enable syntax highlighting in C/C++ examples
2 parents 3fd4684 + 5bf3c1d commit 5f1f1b3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+407
-391
lines changed

docs/code-quality/annotating-function-parameters-and-return-values.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,10 @@ For the annotations in the following table, when a pointer parameter is being an
179179

180180
A pointer to an array of `s` elements (resp. bytes) that will be written by the function. The array elements do not have to be valid in pre-state, and the number of elements that are valid in post-state is unspecified. If there are annotations on the parameter type, they are applied in post-state. For example, consider the following code.
181181

182-
`typedef _Null_terminated_ wchar_t *PWSTR; void MyStringCopy(_Out_writes_ (size) PWSTR p1, _In_ size_t size, _In_ PWSTR p2);`
182+
```cpp
183+
typedef _Null_terminated_ wchar_t *PWSTR;
184+
void MyStringCopy(_Out_writes_(size) PWSTR p1, _In_ size_t size, _In_ PWSTR p2);
185+
```
183186

184187
In this example, the caller provides a buffer of `size` elements for `p1`. `MyStringCopy` makes some of those elements valid. More importantly, the `_Null_terminated_` annotation on `PWSTR` means that `p1` is null-terminated in post-state. In this way, the number of valid elements is still well-defined, but a specific element count is not required.
185188

@@ -209,13 +212,14 @@ For the annotations in the following table, when a pointer parameter is being an
209212

210213
`_Out_writes_bytes_all_(s)`
211214

212-
A pointer to an array of `s` elements. The elements do not have to be valid in pre-state. In post-state, the elements up to the `c`-th element must be valid. If the size is known in bytes, scale `s` and `c` by the element size or use the `_bytes_` variant, which is defined as:
215+
A pointer to an array of `s` elements. The elements do not have to be valid in pre-state. In post-state, the elements up to the `c`-th element must be valid. The `_bytes_` variant can be used if the size is known in bytes rather than number of elements.
216+
217+
For example:
213218

214-
`_Out_writes_to_(_Old_(s), _Old_(s)) _Out_writes_bytes_to_(_Old_(s), _Old_(s))`
215-
216-
In other words, every element that exists in the buffer up to `s` in the pre-state is valid in the post-state. For example:
217-
218-
`void *memcpy(_Out_writes_bytes_all_(s) char *p1, _In_reads_bytes_(s) char *p2, _In_ int s); void * wordcpy(_Out_writes_all_(s) DWORD *p1, _In_reads_(s) DWORD *p2, _In_ int s);`
219+
```cpp
220+
void *memcpy(_Out_writes_bytes_all_(s) char *p1, _In_reads_bytes_(s) char *p2, _In_ int s);
221+
void *wordcpy(_Out_writes_all_(s) DWORD *p1, _In_reads_(s) DWORD *p2, _In_ int s);
222+
```
219223

220224
- `_Inout_updates_to_(s,c)`
221225

@@ -239,19 +243,24 @@ For the annotations in the following table, when a pointer parameter is being an
239243

240244
- `_In_reads_to_ptr_(p)`
241245

242-
A pointer to an array for which the expression `p` - `_Curr_` (that is, `p` minus `_Curr_`) is defined by the appropriate language standard. The elements prior to `p` must be valid in pre-state.
246+
A pointer to an array for which `p - _Curr_` (that is, `p` minus `_Curr_`) is a valid expression. The elements prior to `p` must be valid in pre-state.
247+
248+
For Example:
249+
```cpp
250+
int ReadAllElements(_In_reads_to_ptr_(EndOfArray) const int *Array, const int *EndOfArray);
251+
```
243252

244253
- `_In_reads_to_ptr_z_(p)`
245254

246-
A pointer to a null-terminated array for which the expression `p` - `_Curr_` (that is, `p` minus `_Curr_`) is defined by the appropriate language standard. The elements prior to `p` must be valid in pre-state.
255+
A pointer to a null-terminated array for which expression `p - _Curr_` (that is, `p` minus `_Curr_`) is a valid expression. The elements prior to `p` must be valid in pre-state.
247256

248257
- `_Out_writes_to_ptr_(p)`
249258

250-
A pointer to an array for which the expression `p` - `_Curr_` (that is, `p` minus `_Curr_`) is defined by the appropriate language standard. The elements prior to `p` do not have to be valid in pre-state and must be valid in post-state.
259+
A pointer to an array for which `p - _Curr_` (that is, `p` minus `_Curr_`) is a valid expression. The elements prior to `p` do not have to be valid in pre-state and must be valid in post-state.
251260

252261
- `_Out_writes_to_ptr_z_(p)`
253262

254-
A pointer to a null-terminated array for which the expression `p` - `_Curr_` (that is, `p` minus `_Curr_`) is defined by the appropriate language standard. The elements prior to `p` do not have to be valid in pre-state and must be valid in post-state.
263+
A pointer to a null-terminated array for which `p - _Curr_` (that is, `p` minus `_Curr_`) is a valid expression. The elements prior to `p` do not have to be valid in pre-state and must be valid in post-state.
255264

256265
## Optional Pointer Parameters
257266

@@ -355,7 +364,7 @@ Output pointer parameters require special notation to disambiguate null-ness on
355364

356365
## Output Reference Parameters
357366

358-
A common use of the reference parameter is for output parameters. For simple output reference parameters—for example, `int&``_Out_` provides the correct semantics. However, when the output value is a pointer—for example `int *&`the equivalent pointer annotations like `_Outptr_ int **` don't provide the correct semantics. To concisely express the semantics of output reference parameters for pointer types, use these composite annotations:
367+
A common use of the reference parameter is for output parameters. For simple output reference parameters such as `int&`, `_Out_` provides the correct semantics. However, when the output value is a pointer such as `int *&`, the equivalent pointer annotations like `_Outptr_ int **` don't provide the correct semantics. To concisely express the semantics of output reference parameters for pointer types, use these composite annotations:
359368

360369
**Annotations and Descriptions**
361370

docs/code-quality/annotating-locking-behavior.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ The following table lists the locking annotations.
6767
|`_Create_lock_level_(name)`|A statement that declares the symbol `name` to be a lock level so that it may be used in the annotations `_Has_Lock_level_` and `_Lock_level_order_`.|
6868
|`_Has_lock_kind_(kind)`|Annotates any object to refine the type information of a resource object. Sometimes a common type is used for different kinds of resources and the overloaded type is not sufficient to distinguish the semantic requirements among various resources. Here's a list of pre-defined `kind` parameters:<br /><br /> `_Lock_kind_mutex_`<br /> Lock kind ID for mutexes.<br /><br /> `_Lock_kind_event_`<br /> Lock kind ID for events.<br /><br /> `_Lock_kind_semaphore_`<br /> Lock kind ID for semaphores.<br /><br /> `_Lock_kind_spin_lock_`<br /> Lock kind ID for spin locks.<br /><br /> `_Lock_kind_critical_section_`<br /> Lock kind ID for critical sections.|
6969
|`_Has_lock_level_(name)`|Annotates a lock object and gives it the lock level of `name`.|
70-
|`_Lock_level_order_(name1, name2)`|A statement that gives the lock ordering between `name1` and `name2`.|
70+
|`_Lock_level_order_(name1, name2)`|A statement that gives the lock ordering between `name1` and `name2`. Locks that have level `name1` must be acquired before locks that have level `name2`|
7171
|`_Post_same_lock_(expr1, expr2)`|Annotates a function and indicates that in post state the two locks, `expr1` and `expr2`, are treated as if they are the same lock object.|
7272
|`_Releases_exclusive_lock_(expr)`|Annotates a function and indicates that in post state the function decrements by one the exclusive lock count of the lock object that's named by `expr`.|
7373
|`_Releases_lock_(expr)`|Annotates a function and indicates that in post state the function decrements by one the lock count of the lock object that's named by `expr`.|

docs/code-quality/annotating-structs-and-classes.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,9 @@ You can annotate struct and class members by using annotations that act like inv
7575

7676
```cpp
7777
#include <sal.h>
78-
// For FIELD_OFFSET macro
79-
#include <windows.h>
8078

8179
// This _Struct_size_bytes_ is equivalent to what below _Field_size_ means.
82-
_Struct_size_bytes_(FIELD_OFFSET(MyBuffer, buffer) + bufferSize * sizeof(int))
80+
_Struct_size_bytes_(__builtin_offsetof(MyBuffer, buffer) + bufferSize * sizeof(int))
8381
struct MyBuffer
8482
{
8583
static int MaxBufferSize;
@@ -93,8 +91,9 @@ struct MyBuffer
9391

9492
_Field_range_(1, MaxBufferSize)
9593
int bufferSize;
94+
9695
_Field_size_(bufferSize) // Prefered way - easier to read and maintain.
97-
int buffer[0];
96+
int buffer[]; // Using C99 Flexible array member
9897
};
9998
```
10099

docs/code-quality/c26100.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ warning C26100: Race condition. Variable \<var> should be protected by lock \<lo
2121
## Example
2222
The following example generates warning C26100 because there is a violation of the `_Guarded_by_` contract.
2323

24-
```
24+
```cpp
2525
CRITICAL_SECTION gCS;
2626

2727
_Guarded_by_(gCS) int gData;
@@ -52,8 +52,7 @@ void Unsafe(DATA* p) {
5252
## Example
5353
Occasionally a shared variable only has to be guarded for write access but not for read access. In that case, use the `_Write_guarded_by_` annotation, as shown in the following example.
5454
55-
```
56-
55+
```cpp
5756
CRITICAL_SECTION gCS;
5857
5958
_Guarded_by_(gCS) int gData;

docs/code-quality/c26101.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ warning C26101: Failing to use interlocked operation properly for variable \<var
2121
## Example
2222
The following example generates warning C26101 because there is a violation of the `_Interlocked_` contract.
2323

24-
```
25-
24+
```cpp
2625
CRITICAL_SECTION cs;
2726
typedef struct _DATA
2827
{

docs/code-quality/c26105.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ warning C26105: Lock order violation. Acquiring lock \<lock> with level \<level>
2121
## Example
2222
The following example generates warning C26105 because there is a lock order inversion in the function `OrderInversion`.
2323

24-
```
25-
24+
```cpp
2625
_Create_lock_level_(MutexLockLevel);
2726
_Create_lock_level_(TunnelLockLevel);
2827
_Create_lock_level_(ChannelLockLevel);

docs/code-quality/c26110.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ warning C26110: Caller failing to hold lock \<lock> before calling function \<fu
2121
## Example
2222
In the following example, warning C26110 is generated because the annotation `_Requires_lock_held_` on function `LockRequired` states that the caller of `LockRequired` must acquire the lock before it calls `LockRequired`. Without this annotation, `LockRequired` has to acquire the lock before it accesses any shared data protected by the lock.
2323

24-
```
25-
24+
```cpp
2625
typedef struct _DATA
2726
{
2827
CRITICAL_SECTION cs;

docs/code-quality/c26111.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ warning C26111: Caller failing to release lock \<lock> before calling function \
2121
## Example
2222
The following example generates warning C26111 because the `_Requires_lock_not_held_` precondition is violated by the call to `DoNotLock` within the locked section.
2323

24-
```
25-
24+
```cpp
2625
typedef struct _DATA
2726
{
2827
CRITICAL_SECTION cs;

docs/code-quality/c26112.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ warning C26112: Caller cannot hold any lock before calling \<func>.
2121
## Example
2222
The following example generates warning C26112 because the `_Requires_no_locks_held_` precondition is violated by the call to `NoLocksAllowed` within the locked section.
2323

24-
```
25-
24+
```cpp
2625
typedef struct _DATA
2726
{
2827
CRITICAL_SECTION cs;

docs/code-quality/c26115.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ warning C26115: Failing to release lock \<lock> in function \<func>.
2323
## Example
2424
The following example generates warning C26115 because there is an orphaned lock in a function that is not annotated with `_Acquires_lock_`.
2525

26-
```
27-
26+
```cpp
2827
typedef struct _DATA
2928
{
3029
CRITICAL_SECTION cs;

docs/code-quality/c26116.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ warning C26116: Failing to acquire or to hold lock \<lock> in \<func>.
2121
## Example
2222
The following example generates warning C26116 because the function `DoesNotLock` was annotated with `_Acquires_lock_` but does not acquire it. The function `DoesNotHoldLock` generates the warning because it is annotated with `_Requires_lock_held` and does not hold it.
2323

24-
```
25-
24+
```cpp
2625
typedef struct _DATA
2726
{
2827
CRITICAL_SECTION cs;

docs/code-quality/c26130.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ warning C26130: Missing annotation \_Requires\_lock\_held\_(\<lock>) or \_No\_co
2121
## Example
2222
In the following example, warning C26130 is generated because a `_Guarded_by_` member is being modified without a lock.
2323

24-
```
25-
24+
```cpp
2625
typedef struct _DATA
2726
{
2827
CRITICAL_SECTION cs;
@@ -38,8 +37,7 @@ void Init(DATA* p)
3837
## Example
3938
If the previous code is guaranteed to be operated in a single threaded mode, annotate the function by using `_No_competing_thread_`, as shown in the following example.
4039
41-
```
42-
40+
```cpp
4341
typedef struct _DATA
4442
{
4543
CRITICAL_SECTION cs;
@@ -55,8 +53,7 @@ _No_competing_thread_ void Init(DATA* p)
5553
## Example
5654
Alternatively, you can annotate a code fragment by using `_No_competing_thread_begin_` and `_No_competing_thread_end_`, as follows.
5755

58-
```
59-
56+
```cpp
6057
typedef struct _DATA
6158
{
6259
CRITICAL_SECTION cs;

docs/code-quality/c26135.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ warning C26135: Missing annotation \<annotation> at function \<func>.
2121
## Example
2222
The following example generates warning C26135 because an appropriate side effect annotation is missing.
2323

24-
```
25-
24+
```cpp
2625
typedef struct _DATA
2726
{
2827
CRITICAL_SECTION cs;
@@ -46,8 +45,7 @@ void MyLeave(DATA* p)
4645
## Example
4746
Warning C26135 is also issued when a conditional locking side effect is detected. To annotate a conditional effect, use the `_When_(ConditionExpr, LockAnnotation)` annotation, where `LockAnnotation` is either `_Acquires_lock_` or `_Releases_lock_` and the predicate expression `ConditionExpr` is a Boolean conditional expression. The side effects of other annotations on the same function only occur when `ConditionExpr` evaluates to true. Because `ConditionExpr` is used to relay the condition back to the caller, it must involve variables that are recognized in the calling context. These include function parameters, global or class member variables, or the return value. To see the return value, use a special keyword in the annotation, `return`, as shown in the following example.
4847
49-
```
50-
48+
```cpp
5149
typedef struct _DATA
5250
{
5351
CRITICAL_SECTION cs;

docs/code-quality/c26140.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ warning C26140: Undefined lock kind \<lock> in annotation \<annotation> on lock
1818

1919
## Example
2020

21-
```
22-
21+
```cpp
2322
_Has_lock_kind_(MUTEXa) HANDLE gMutex;
2423

2524
struct CorrectExample

docs/code-quality/c28103.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ warning C28103: Leaking resource
2121
## Example
2222
The following code example generates this warning:
2323

24-
```
24+
```cpp
2525
res = KeSaveFloatingPointState(buffer);
2626
```
2727

2828
The following code example avoids this warning:
2929

30-
```
30+
```cpp
3131
res = KeSaveFloatingPointState(buffer);
3232
if (NT_SUCCESS(res))
3333
{

docs/code-quality/c28104.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ warning C28104: Resource that should have been acquired before function exit was
2222

2323
## Example
2424

25-
```
25+
```cpp
2626
__drv_acquireResourceGlobal(HWLock, lockid)
2727
void GetHardwareLock(lockid)
2828
#pragma warning (suppress: 28104)

docs/code-quality/c28105.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Warning C28105: Leaking resource due to an exception
2121
## Example
2222
The following code example generates this warning:
2323

24-
```
24+
```cpp
2525
res = KeSaveFloatingPointState(buffer);
2626

2727
res = AllocateResource(Resource);
@@ -33,7 +33,7 @@ FreeResource(Resource)
3333
3434
The following code example avoids this warning:
3535
36-
```
36+
```cpp
3737
res = AllocateResource(Resource);
3838
char *p2;
3939

docs/code-quality/c28106.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ Warning C28106: Variable already holds resource possibly causing leak
2121
## Example
2222
The following code example generates this warning:
2323

24-
```
24+
```cpp
2525
ExAcquireResourceLite(resource, true);
26-
...
26+
//...
2727
ExAcquireResourceLite(resource, true);
2828
```
2929
3030
The following code example avoids this warning:
3131
32-
```
32+
```cpp
3333
ExAcquireResourceLite(resource1, true);
34-
...
34+
//...
3535
ExAcquireResourceLite(resource2, true);
3636
```

docs/code-quality/c28107.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ warning C28107: Resource must be held when calling function
2121
## Example
2222
The following code example generates this warning:
2323

24-
```
24+
```cpp
2525
ExAcquireResourceLite(resource, true);
2626
ExReleaseResourceLite(resource);
2727
```
2828
2929
The following code example avoids this warning:
3030
31-
```
31+
```cpp
3232
KeEnterCriticalRegion();
3333
ExAcquireResourceLite(resource, true);
3434
ExReleaseResourceLite(resource);

docs/code-quality/c28108.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ warning C28108: Variable holds an unexpected resource
2121
## Example
2222
The following code example generates this warning:
2323

24-
```
24+
```cpp
2525
KeAcquireInStackSpinLock(spinLock, lockHandle);
26-
...
26+
//...
2727
KeReleaseSpinLock(spinLock, 0);
2828
```
2929
3030
The following code example avoids this warning:
3131
32-
```
32+
```cpp
3333
KeAcquireInStackSpinLock(spinLock, lockHandle);
34-
...
34+
//...
3535
KeReleaseInStackSpinLock(lockHandle);
3636
```

docs/code-quality/c28109.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ warning C28109: Variable cannot be held at the time function is called
2121
## Example
2222
The following code example generates this warning:
2323

24-
```
24+
```cpp
2525
ExAcquireResourceLite(resource, true);
26-
...
26+
//...
2727
ExAcquireResourceLite(resource, true);
2828
```
2929
3030
The following code example avoids this warning:
3131
32-
```
32+
```cpp
3333
ExAcquireResourceLite(resource, true);
3434
```

docs/code-quality/c28113.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ warning C28113: Accessing a local variable via an Interlocked function
2323
## Example
2424
Typically, the return value of an Interlocked executive support routine is used in subsequent computations, instead of the input arguments. Also, the Interlocked routines only protect the first (leftmost) argument. Using an Interlocked routine in the following way does not protect the value of global and often serves no purpose.
2525

26-
```
26+
```cpp
2727
InterlockedExchange(&local, global)
2828
```
2929
3030
The following form has the same effect on the data and safely accesses the global variable.
3131
32-
```
32+
```cpp
3333
local = InterllockedExchange(&global, global)
3434
```

0 commit comments

Comments
 (0)