Skip to content

Commit c72f2fe

Browse files
committed
Merge branch 'ab/coding-guidelines-c99' into maint-2.38
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 d5b4139 + 438c2f8 commit c72f2fe

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
@@ -204,10 +204,19 @@ For C programs:
204204
by e.g. "echo DEVELOPER=1 >>config.mak".
205205

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

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

212221
. since early 2012 with e1327023ea, we have been using an enum
213222
definition whose last element is followed by a comma. This, like
@@ -223,18 +232,24 @@ For C programs:
223232
. since early 2021 with 765dc168882, we have been using variadic
224233
macros, mostly for printf-like trace and debug macros.
225234

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

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

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

240255
- 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)