aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2024-08-08 10:41:19 -0700
committerJunio C Hamano <gitster@pobox.com>2024-08-08 10:41:20 -0700
commit536695cabefc8638f4e1b0bbd38f6bd37a3142a0 (patch)
tree87239cad7d6f63ce03e9b2f65e9fab3c2ad83833
parentMerge branch 'rs/grep-omit-blank-lines-after-function-at-eof' (diff)
parentDocumentation: consistently use spaces inside initializers (diff)
downloadgit-536695cabefc8638f4e1b0bbd38f6bd37a3142a0.tar.gz
git-536695cabefc8638f4e1b0bbd38f6bd37a3142a0.zip
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
-rw-r--r--.clang-format6
-rw-r--r--Documentation/CodingGuidelines48
2 files changed, 51 insertions, 3 deletions
diff --git a/.clang-format b/.clang-format
index 16fd12253e..0b82f3c776 100644
--- a/.clang-format
+++ b/.clang-format
@@ -100,11 +100,13 @@ BreakStringLiterals: false
# Switch statement body is always indented one level more than case labels.
IndentCaseLabels: false
-# Indents directives before the hash.
+# Indents directives before the hash. Each level uses a single space for
+# indentation.
# #if FOO
-# # include <foo>
+# # include <foo>
# #endif
IndentPPDirectives: AfterHash
+PPIndentWidth: 1
# Don't indent a function definition or declaration if it is wrapped after the
# type
diff --git a/Documentation/CodingGuidelines b/Documentation/CodingGuidelines
index 52afb2725f..2f35b6691f 100644
--- a/Documentation/CodingGuidelines
+++ b/Documentation/CodingGuidelines
@@ -241,6 +241,16 @@ For C programs:
- We use tabs to indent, and interpret tabs as taking up to
8 spaces.
+ - Nested C preprocessor directives are indented after the hash by one
+ space per nesting level.
+
+ #if FOO
+ # include <foo.h>
+ # if BAR
+ # include <bar.h>
+ # endif
+ #endif
+
- We try to keep to at most 80 characters per line.
- As a Git developer we assume you have a reasonably modern compiler
@@ -261,7 +271,7 @@ For C programs:
. since around 2007 with 2b6854c863a, we have been using
initializer elements which are not computable at load time. E.g.:
- const char *args[] = {"constant", variable, NULL};
+ const char *args[] = { "constant", variable, NULL };
. since early 2012 with e1327023ea, we have been using an enum
definition whose last element is followed by a comma. This, like
@@ -558,6 +568,42 @@ For C programs:
use your own debugger and arguments. Example: `GIT_DEBUGGER="ddd --gdb"
./bin-wrappers/git log` (See `wrap-for-bin.sh`.)
+ - The primary data structure that a subsystem 'S' deals with is called
+ `struct S`. Functions that operate on `struct S` are named
+ `S_<verb>()` and should generally receive a pointer to `struct S` as
+ first parameter. E.g.
+
+ struct strbuf;
+
+ void strbuf_add(struct strbuf *buf, ...);
+
+ void strbuf_reset(struct strbuf *buf);
+
+ is preferred over:
+
+ struct strbuf;
+
+ void add_string(struct strbuf *buf, ...);
+
+ void reset_strbuf(struct strbuf *buf);
+
+ - There are several common idiomatic names for functions performing
+ specific tasks on a structure `S`:
+
+ - `S_init()` initializes a structure without allocating the
+ structure itself.
+
+ - `S_release()` releases a structure's contents without freeing the
+ structure.
+
+ - `S_clear()` is equivalent to `S_release()` followed by `S_init()`
+ such that the structure is directly usable after clearing it. When
+ `S_clear()` is provided, `S_init()` shall not allocate resources
+ that need to be released again.
+
+ - `S_free()` releases a structure's contents and frees the
+ structure.
+
For Perl programs:
- Most of the C guidelines above apply.