Skip to content

Commit f5ad35e

Browse files
author
Laszlo Ersek
committed
must comment: add rule for documenting spurious variable assignments
Problem statement from Ard: > Sometimes, the GCC compiler warns about variables potentially being used > without having been initialized, while visual inspection reveals that > this is impossible. In such cases, we need to initialize such a variable > to an arbitrary value only to avoid breaking the build, given our policy > to treat warnings as errors. In such cases we generally use LocalIntegerVariable = 0; and LocalPointerVariable = NULL; which takes care of the incorrect warning. However, it also makes the human analysis of any subsequent logic harder, because it suggests that assigning that specific zero or NULL value to the local variable is *required* by the subsequent logic. In order to highlight such assignments, whose sole purpose is to suppress invalid "use before init" warnings from compilers or static analysis tools, we should mandate comments such as: // // set LocalVariable to suppress incorrect compiler/analyzer warnings // LocalVariable = 0; (Magic values such as 0xDEADBEEF, which would obviate the necessity of explicit comments, have been considered, and rejected for stylistic reasons.) Cc: Andrew Fish <[email protected]> Cc: Leif Lindholm <[email protected]> Cc: Michael D Kinney <[email protected]> Cc: Rebecca Cran <[email protected]> Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=607 Signed-off-by: Laszlo Ersek <[email protected]> Reviewed-by: Philippe Mathieu-Daude <[email protected]> Reviewed-by: Leif Lindholm <[email protected]> Reviewed-by: Michael D Kinney <[email protected]>
1 parent 1b4a4cc commit f5ad35e

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

6_documenting_software/64_what_you_must_comment.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,42 @@ instance differs.
5858

5959
When possible, you should also list the requirements that are satisfied by the
6060
code.
61+
62+
### 6.4.6 Comment spurious variable assignments.
63+
64+
A compiler or static code analyzer may warn that an object with automatic or
65+
allocated storage duration is read without having been initialized, while
66+
visual inspection reveals that this is impossible.
67+
68+
In order to suppress such a warning (which is emitted due to invalid data flow
69+
analysis), developers explicitly assign the affected object the value to which
70+
the same object would be initialized automatically, had the object static
71+
storage duration, and no initializer. (The value assigned could be arbitrary;
72+
the above-mentioned value is chosen for stylistic reasons.) For example:
73+
74+
```c
75+
UINTN LocalIntegerVariable;
76+
VOID *LocalPointerVariable;
77+
78+
LocalIntegerVariable = 0;
79+
LocalPointerVariable = NULL;
80+
```
81+
82+
This kind of assignment is difficult to distinguish from assignments where the
83+
initial value of an object is meaningful, and is consumed by other code without
84+
an intervening assignment. Therefore, each such assignment must be documented,
85+
as follows:
86+
87+
```c
88+
UINTN LocalIntegerVariable;
89+
VOID *LocalPointerVariable;
90+
91+
//
92+
// set LocalIntegerVariable to suppress incorrect compiler/analyzer warnings
93+
//
94+
LocalIntegerVariable = 0;
95+
//
96+
// set LocalPointerVariable to suppress incorrect compiler/analyzer warnings
97+
//
98+
LocalPointerVariable = NULL;
99+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,4 @@ Copyright (c) 2006-2017, Intel Corporation. All rights reserved.
113113
| 2.2 | Convert to Gitbook | June 2017 |
114114
| | [#425](https://bugzilla.tianocore.org/show_bug.cgi?id=425) [CCS] clarify line breaking and indentation requirements for multi-line function calls | |
115115
| | [#1656](https://bugzilla.tianocore.org/show_bug.cgi?id=1656) Update all Wiki pages for the BSD+Patent license change with SPDX identifiers | |
116+
| | [#607](https://bugzilla.tianocore.org/show_bug.cgi?id=607) Document code comment requirements for spurious variable assignments | |

0 commit comments

Comments
 (0)