Skip to content

Commit 536695c

Browse files
committed
Merge branch 'ps/doc-more-c-coding-guidelines'
Some project conventions have been added to CodingGuidelines. * ps/doc-more-c-coding-guidelines: Documentation: consistently use spaces inside initializers Documentation: document idiomatic function names Documentation: document naming schema for structs and their functions Documentation: clarify indentation style for C preprocessor directives clang-format: fix indentation width for preprocessor directives
2 parents 984ab11 + 6cda597 commit 536695c

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

.clang-format

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,13 @@ BreakStringLiterals: false
100100
# Switch statement body is always indented one level more than case labels.
101101
IndentCaseLabels: false
102102

103-
# Indents directives before the hash.
103+
# Indents directives before the hash. Each level uses a single space for
104+
# indentation.
104105
# #if FOO
105-
# # include <foo>
106+
# # include <foo>
106107
# #endif
107108
IndentPPDirectives: AfterHash
109+
PPIndentWidth: 1
108110

109111
# Don't indent a function definition or declaration if it is wrapped after the
110112
# type

Documentation/CodingGuidelines

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,16 @@ For C programs:
241241
- We use tabs to indent, and interpret tabs as taking up to
242242
8 spaces.
243243

244+
- Nested C preprocessor directives are indented after the hash by one
245+
space per nesting level.
246+
247+
#if FOO
248+
# include <foo.h>
249+
# if BAR
250+
# include <bar.h>
251+
# endif
252+
#endif
253+
244254
- We try to keep to at most 80 characters per line.
245255

246256
- As a Git developer we assume you have a reasonably modern compiler
@@ -261,7 +271,7 @@ For C programs:
261271
. since around 2007 with 2b6854c863a, we have been using
262272
initializer elements which are not computable at load time. E.g.:
263273

264-
const char *args[] = {"constant", variable, NULL};
274+
const char *args[] = { "constant", variable, NULL };
265275

266276
. since early 2012 with e1327023ea, we have been using an enum
267277
definition whose last element is followed by a comma. This, like
@@ -558,6 +568,42 @@ For C programs:
558568
use your own debugger and arguments. Example: `GIT_DEBUGGER="ddd --gdb"
559569
./bin-wrappers/git log` (See `wrap-for-bin.sh`.)
560570

571+
- The primary data structure that a subsystem 'S' deals with is called
572+
`struct S`. Functions that operate on `struct S` are named
573+
`S_<verb>()` and should generally receive a pointer to `struct S` as
574+
first parameter. E.g.
575+
576+
struct strbuf;
577+
578+
void strbuf_add(struct strbuf *buf, ...);
579+
580+
void strbuf_reset(struct strbuf *buf);
581+
582+
is preferred over:
583+
584+
struct strbuf;
585+
586+
void add_string(struct strbuf *buf, ...);
587+
588+
void reset_strbuf(struct strbuf *buf);
589+
590+
- There are several common idiomatic names for functions performing
591+
specific tasks on a structure `S`:
592+
593+
- `S_init()` initializes a structure without allocating the
594+
structure itself.
595+
596+
- `S_release()` releases a structure's contents without freeing the
597+
structure.
598+
599+
- `S_clear()` is equivalent to `S_release()` followed by `S_init()`
600+
such that the structure is directly usable after clearing it. When
601+
`S_clear()` is provided, `S_init()` shall not allocate resources
602+
that need to be released again.
603+
604+
- `S_free()` releases a structure's contents and frees the
605+
structure.
606+
561607
For Perl programs:
562608

563609
- Most of the C guidelines above apply.

0 commit comments

Comments
 (0)