Skip to content

Commit 179eb1d

Browse files
committed
Merge branch 'ab/coding-guidelines-c99'
Update CodingGuidelines to clarify what features to use and avoid in C99. * ab/coding-guidelines-c99: CodingGuidelines: recommend against unportable C99 struct syntax CodingGuidelines: mention C99 features we can't use CodingGuidelines: allow declaring variables in for loops CodingGuidelines: mention dynamic C99 initializer elements CodingGuidelines: update for C99
2 parents 9c32cfb + 438c2f8 commit 179eb1d

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

Documentation/CodingGuidelines

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,19 @@ For C programs:
202202
by e.g. "echo DEVELOPER=1 >>config.mak".
203203

204204
- We try to support a wide range of C compilers to compile Git with,
205-
including old ones. You should not use features from newer C
205+
including old ones. As of Git v2.35.0 Git requires C99 (we check
206+
"__STDC_VERSION__"). You should not use features from a newer C
206207
standard, even if your compiler groks them.
207208

208-
There are a few exceptions to this guideline:
209+
New C99 features have been phased in gradually, if something's new
210+
in C99 but not used yet don't assume that it's safe to use, some
211+
compilers we target have only partial support for it. These are
212+
considered safe to use:
213+
214+
. since around 2007 with 2b6854c863a, we have been using
215+
initializer elements which are not computable at load time. E.g.:
216+
217+
const char *args[] = {"constant", variable, NULL};
209218

210219
. since early 2012 with e1327023ea, we have been using an enum
211220
definition whose last element is followed by a comma. This, like
@@ -221,18 +230,24 @@ For C programs:
221230
. since early 2021 with 765dc168882, we have been using variadic
222231
macros, mostly for printf-like trace and debug macros.
223232

224-
These used to be forbidden, but we have not heard any breakage
225-
report, and they are assumed to be safe.
233+
. since late 2021 with 44ba10d6, we have had variables declared in
234+
the for loop "for (int i = 0; i < 10; i++)".
235+
236+
New C99 features that we cannot use yet:
237+
238+
. %z and %zu as a printf() argument for a size_t (the %z being for
239+
the POSIX-specific ssize_t). Instead you should use
240+
printf("%"PRIuMAX, (uintmax_t)v). These days the MSVC version we
241+
rely on supports %z, but the C library used by MinGW does not.
242+
243+
. Shorthand like ".a.b = *c" in struct initializations is known to
244+
trip up an older IBM XLC version, use ".a = { .b = *c }" instead.
245+
See the 33665d98 (reftable: make assignments portable to AIX xlc
246+
v12.01, 2022-03-28).
226247

227248
- Variables have to be declared at the beginning of the block, before
228249
the first statement (i.e. -Wdeclaration-after-statement).
229250

230-
- Declaring a variable in the for loop "for (int i = 0; i < 10; i++)"
231-
is still not allowed in this codebase. We are in the process of
232-
allowing it by waiting to see that 44ba10d6 (revision: use C99
233-
declaration of variable in for() loop, 2021-11-14) does not get
234-
complaints. Let's revisit this around November 2022.
235-
236251
- NULL pointers shall be written as NULL, not as 0.
237252

238253
- When declaring pointers, the star sides with the variable

revision.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,6 @@ static inline int want_ancestry(const struct rev_info *revs);
4747
void show_object_with_name(FILE *out, struct object *obj, const char *name)
4848
{
4949
fprintf(out, "%s ", oid_to_hex(&obj->oid));
50-
/*
51-
* This "for (const char *p = ..." is made as a first step towards
52-
* making use of such declarations elsewhere in our codebase. If
53-
* it causes compilation problems on your platform, please report
54-
* it to the Git mailing list at [email protected]. In the meantime,
55-
* adding -std=gnu99 to CFLAGS may help if you are with older GCC.
56-
*/
5750
for (const char *p = name; *p && *p != '\n'; p++)
5851
fputc(*p, out);
5952
fputc('\n', out);

0 commit comments

Comments
 (0)