progmasoft-cpp
The C++ style guide of Progmasoft: naming, formatting, language rules, errors, ownership, ABI, testing and tooling.
Clonegit clone https://git.progmasoft.com/progmasoft-cpp
Cover GNU99 in Progmasoft Core C Coding Guidelines
- Commit
0c53b12aaee6dca7c1b2fbacc54fcb717d03f76d- Author
- Leitwolf11 ·
- Parents
5718e0151da4
Changes
ADOPTING.md | 7 +- CHANGELOG.md | 7 ++ .../C11/LANGUAGE.md | 28 ++++---- .../{C11 => Common}/COMMENTS-AND-DOCUMENTATION.md | 0 .../{C11 => Common}/CONTROL-FLOW.md | 0 .../DECLARATIONS-AND-INITIALIZERS.md | 2 +- .../{C11 => Common}/FILES-AND-HEADERS.md | 0 .../{C11 => Common}/FORMATTING.md | 0 .../Common/LANGUAGE.md | 30 +++++++++ .../{C11 => Common}/MEMORY-AND-OWNERSHIP.md | 0 .../{C11 => Common}/NAMING.md | 0 .../{C11 => Common}/PREPROCESSOR.md | 0 .../{C11 => Common}/PRINCIPLES.md | 7 +- .../{C11 => Common}/TOOLING.md | 5 ++ .../Gnu99/LANGUAGE.md | 78 ++++++++++++++++++++++ Progmasoft_Core_C_Coding_Guidelines/index.json | 45 ++++++------- README.md | 46 +++++++------ 17 files changed, 190 insertions(+), 65 deletions(-) diff --git a/ADOPTING.md b/ADOPTING.md index 07579bb..05c221c 100644 --- a/ADOPTING.md +++ b/ADOPTING.md @@ -61,9 +61,10 @@ mixed with a change in behavior, so that review and `git blame` stay useful. ## C projects -A C project adopts [Progmasoft Core C Coding Guidelines](Progmasoft_Core_C_Coding_Guidelines/C11/PRINCIPLES.md) the same way: it points to them in its -contributing guide, copies [the `.clang-format` profile](Progmasoft_Core_C_Coding_Guidelines/C11/FORMATTING.md#the-profile) and [the `.clang-tidy` -profile](Progmasoft_Core_C_Coding_Guidelines/C11/TOOLING.md#111-the-clang-tidy-profile), and records its deviations. +A C project adopts [Progmasoft Core C Coding Guidelines](Progmasoft_Core_C_Coding_Guidelines/Common/PRINCIPLES.md) the same way: it points to them in its +contributing guide, copies [the `.clang-format` profile](Progmasoft_Core_C_Coding_Guidelines/Common/FORMATTING.md#the-profile) and [the `.clang-tidy` +profile](Progmasoft_Core_C_Coding_Guidelines/Common/TOOLING.md#111-the-clang-tidy-profile), and records its deviations. +It names its version, C11 or GNU99, in the same guide; without one, it is C11. ## Projects that follow the guidelines diff --git a/CHANGELOG.md b/CHANGELOG.md index e392109..71048eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ A revision of Progmasoft C/C++ Styles is named by its date. A change that requir ## Unreleased +Progmasoft Core C Coding Guidelines cover GNU99 next to C11. The chapters that apply to both moved to +`Progmasoft_Core_C_Coding_Guidelines/Common/`; `C11/LANGUAGE.md` and `Gnu99/LANGUAGE.md` hold what applies to one +version, and Language (chapter 9) says how a project chooses. GNU99 is `-std=gnu99` with a closed list of GNU +extensions (attributes, `__typeof__` and statement expressions inside macros, overflow built-ins, `__builtin_expect`, +inline assembly in `static inline` functions), no variable-length arrays, and a GNU99 form of each C11 feature. No C11 +rule changed; links to `Progmasoft_Core_C_Coding_Guidelines/C11/` move to `Common/`, except `C11/LANGUAGE.md`. + Progmasoft Core C Coding Guidelines are written, for C11 with LLVM 23, in `Progmasoft_Core_C_Coding_Guidelines/C11/`: eleven chapters, a `.clang-format` and a `.clang-tidy` profile, and an `index.json`. They are a style of their own; the C interfaces of C++ libraries keep following the C++ guidelines. diff --git a/Progmasoft_Core_C_Coding_Guidelines/C11/LANGUAGE.md b/Progmasoft_Core_C_Coding_Guidelines/C11/LANGUAGE.md index a2c78e8..bee7939 100644 --- a/Progmasoft_Core_C_Coding_Guidelines/C11/LANGUAGE.md +++ b/Progmasoft_Core_C_Coding_Guidelines/C11/LANGUAGE.md @@ -1,29 +1,25 @@ <!-- SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> --> <!-- SPDX-License-Identifier: CC-BY-4.0 --> -# 9. Language +# C11 -The guidelines are written for ISO C11: code compiles with `-std=c11 -pedantic-errors`. +C11 is the default, for every C project. A C11 project follows every chapter of [Common](../Common/PRINCIPLES.md) +unchanged and uses the features below. -## 9.1 Extensions +## Building -- **No language extensions.** Two constructs that are not ISO C11 are part of every header and are allowed: - `#pragma once`, and `extern "C"` inside `#ifdef __cplusplus`, which a C compiler never sees - ([Files and headers](FILES-AND-HEADERS.md#42-the-start-of-a-header)). -- **`{}` is not C11**: an empty initializer is `{ 0 }`. +- **Code compiles with `-std=c11 -pedantic-errors`**, next to the warnings of + [Tooling](../Common/TOOLING.md#112-commands). +- **No language extensions** but the two that every header uses + ([Language](../Common/LANGUAGE.md#91-what-every-version-allows)). -## 9.2 C11 features +## Features -- **`bool`, `true` and `false` come from `<stdbool.h>`** and should be used for every truth value. -- **`restrict` may be used only where the aliasing contract has been checked**; in a public interface, the - documentation states what it forbids the caller. -- **An `_Atomic` variable is named like any other variable.** `_Atomic(Type)` should be preferred to the qualifier form. -- **`_Thread_local` adds no prefix to a name.** +- **`_Atomic(Type)` should be preferred to the `_Atomic` qualifier**, and `<stdatomic.h>` gives the operations. +- **`_Thread_local` makes a variable local to a thread.** - **`_Alignas`, `_Alignof`, `_Static_assert`, `_Generic` and `_Noreturn` are used where they mean something.** `_Static_assert` always has a message that says what failed; `_Noreturn` marks only functions that never return. -- **`volatile` does not synchronize threads.** Shared state between threads uses `_Atomic` or the functions of - `<threads.h>` or the platform. -- **`auto` and `register` should not be written.** +- **Threads and locks come from `<threads.h>` or the platform.** ```c _Static_assert(EXAMPLE_LIST_MAX_VALUES <= SIZE_MAX / sizeof(int), diff --git a/Progmasoft_Core_C_Coding_Guidelines/C11/COMMENTS-AND-DOCUMENTATION.md b/Progmasoft_Core_C_Coding_Guidelines/Common/COMMENTS-AND-DOCUMENTATION.md similarity index 100% rename from Progmasoft_Core_C_Coding_Guidelines/C11/COMMENTS-AND-DOCUMENTATION.md rename to Progmasoft_Core_C_Coding_Guidelines/Common/COMMENTS-AND-DOCUMENTATION.md diff --git a/Progmasoft_Core_C_Coding_Guidelines/C11/CONTROL-FLOW.md b/Progmasoft_Core_C_Coding_Guidelines/Common/CONTROL-FLOW.md similarity index 100% rename from Progmasoft_Core_C_Coding_Guidelines/C11/CONTROL-FLOW.md rename to Progmasoft_Core_C_Coding_Guidelines/Common/CONTROL-FLOW.md diff --git a/Progmasoft_Core_C_Coding_Guidelines/C11/DECLARATIONS-AND-INITIALIZERS.md b/Progmasoft_Core_C_Coding_Guidelines/Common/DECLARATIONS-AND-INITIALIZERS.md similarity index 97% rename from Progmasoft_Core_C_Coding_Guidelines/C11/DECLARATIONS-AND-INITIALIZERS.md rename to Progmasoft_Core_C_Coding_Guidelines/Common/DECLARATIONS-AND-INITIALIZERS.md index c01e9b3..8ac1b30 100644 --- a/Progmasoft_Core_C_Coding_Guidelines/C11/DECLARATIONS-AND-INITIALIZERS.md +++ b/Progmasoft_Core_C_Coding_Guidelines/Common/DECLARATIONS-AND-INITIALIZERS.md @@ -63,7 +63,7 @@ const char* pText = "First part " `clang-format` runs, and adding an item then changes one line of a diff, not two. - **Designated initializers are used only where choosing the member is the point**, such as a `union` or a sparse table. -- **Zero initialization is `{ 0 }`.** `{}` is not C11. +- **Zero initialization is `{ 0 }`.** `{}` is not C11, and GNU99 accepts it only as an extension. ```c int values[ 3 ] = { 1, 2, 3 }; diff --git a/Progmasoft_Core_C_Coding_Guidelines/C11/FILES-AND-HEADERS.md b/Progmasoft_Core_C_Coding_Guidelines/Common/FILES-AND-HEADERS.md similarity index 100% rename from Progmasoft_Core_C_Coding_Guidelines/C11/FILES-AND-HEADERS.md rename to Progmasoft_Core_C_Coding_Guidelines/Common/FILES-AND-HEADERS.md diff --git a/Progmasoft_Core_C_Coding_Guidelines/C11/FORMATTING.md b/Progmasoft_Core_C_Coding_Guidelines/Common/FORMATTING.md similarity index 100% rename from Progmasoft_Core_C_Coding_Guidelines/C11/FORMATTING.md rename to Progmasoft_Core_C_Coding_Guidelines/Common/FORMATTING.md diff --git a/Progmasoft_Core_C_Coding_Guidelines/Common/LANGUAGE.md b/Progmasoft_Core_C_Coding_Guidelines/Common/LANGUAGE.md new file mode 100644 index 0000000..6e15007 --- /dev/null +++ b/Progmasoft_Core_C_Coding_Guidelines/Common/LANGUAGE.md @@ -0,0 +1,30 @@ +<!-- SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> --> +<!-- SPDX-License-Identifier: CC-BY-4.0 --> + +# 9. Language + +The guidelines cover two versions of C. A project names its version in its contributing guide; without one, it is C11. + +- **[C11](../C11/LANGUAGE.md)** is the default, for every C project: ISO C11, with no language extensions. +- **[GNU99](../Gnu99/LANGUAGE.md)** is for code that joins a codebase built as GNU99, such as firmware or kernel code, + or that needs one of the GNU extensions its page lists. It is C99 with a short, closed list of GNU extensions. + +The rules below hold in both. A version's page says how it is built and which features it uses. + +## 9.1 What every version allows + +- **Two constructs outside ISO C are part of every header and are allowed:** `#pragma once`, and `extern "C"` inside + `#ifdef __cplusplus`, which a C compiler never sees + ([Files and headers](FILES-AND-HEADERS.md#42-the-start-of-a-header)). +- **An empty initializer is `{ 0 }`.** `{}` is not C11, and GNU99 accepts it only as an extension. + +## 9.2 Features of every version + +- **`bool`, `true` and `false` come from `<stdbool.h>`** and should be used for every truth value. +- **`restrict` may be used only where the aliasing contract has been checked**; in a public interface, the + documentation states what it forbids the caller. +- **`volatile` does not synchronize threads.** Shared state between threads uses the atomics of the version + ([C11](../C11/LANGUAGE.md#features), [GNU99](../Gnu99/LANGUAGE.md#the-gnu99-form-of-the-c11-features)) or the + threads of the platform. +- **A variable that is atomic or local to a thread is named like any other variable**; its storage adds no prefix. +- **`auto` and `register` should not be written.** diff --git a/Progmasoft_Core_C_Coding_Guidelines/C11/MEMORY-AND-OWNERSHIP.md b/Progmasoft_Core_C_Coding_Guidelines/Common/MEMORY-AND-OWNERSHIP.md similarity index 100% rename from Progmasoft_Core_C_Coding_Guidelines/C11/MEMORY-AND-OWNERSHIP.md rename to Progmasoft_Core_C_Coding_Guidelines/Common/MEMORY-AND-OWNERSHIP.md diff --git a/Progmasoft_Core_C_Coding_Guidelines/C11/NAMING.md b/Progmasoft_Core_C_Coding_Guidelines/Common/NAMING.md similarity index 100% rename from Progmasoft_Core_C_Coding_Guidelines/C11/NAMING.md rename to Progmasoft_Core_C_Coding_Guidelines/Common/NAMING.md diff --git a/Progmasoft_Core_C_Coding_Guidelines/C11/PREPROCESSOR.md b/Progmasoft_Core_C_Coding_Guidelines/Common/PREPROCESSOR.md similarity index 100% rename from Progmasoft_Core_C_Coding_Guidelines/C11/PREPROCESSOR.md rename to Progmasoft_Core_C_Coding_Guidelines/Common/PREPROCESSOR.md diff --git a/Progmasoft_Core_C_Coding_Guidelines/C11/PRINCIPLES.md b/Progmasoft_Core_C_Coding_Guidelines/Common/PRINCIPLES.md similarity index 86% rename from Progmasoft_Core_C_Coding_Guidelines/C11/PRINCIPLES.md rename to Progmasoft_Core_C_Coding_Guidelines/Common/PRINCIPLES.md index 24a7b49..c0ee5cc 100644 --- a/Progmasoft_Core_C_Coding_Guidelines/C11/PRINCIPLES.md +++ b/Progmasoft_Core_C_Coding_Guidelines/Common/PRINCIPLES.md @@ -10,11 +10,14 @@ layout and comments differ on purpose. A C interface that a C++ library exposes ## 1.1 The language and the tools -- **The language is ISO C11.** What C11 offers and how it is used is in [Language](LANGUAGE.md). +- **The language is ISO C11**, or GNU99 for code that joins a codebase built as GNU99 or needs one of its listed + extensions. [Language](LANGUAGE.md) says how to choose, and the [C11](../C11/LANGUAGE.md) and + [GNU99](../Gnu99/LANGUAGE.md) pages what each version uses. - **The toolchain is LLVM 23**: Clang compiles, and `clang-format` and `clang-tidy` 23 run the profiles in [Formatting](FORMATTING.md#the-profile) and [Tooling](TOOLING.md#111-the-clang-tidy-profile). - **No language extensions**, with two exceptions that every header uses: `#pragma once`, and the conditional - `extern "C"` block of a public header ([Files and headers](FILES-AND-HEADERS.md)). + `extern "C"` block of a public header ([Files and headers](FILES-AND-HEADERS.md)). GNU99 adds the closed list of + extensions on [its page](../Gnu99/LANGUAGE.md#the-extensions-it-allows). ## 1.2 How the rules are written diff --git a/Progmasoft_Core_C_Coding_Guidelines/C11/TOOLING.md b/Progmasoft_Core_C_Coding_Guidelines/Common/TOOLING.md similarity index 90% rename from Progmasoft_Core_C_Coding_Guidelines/C11/TOOLING.md rename to Progmasoft_Core_C_Coding_Guidelines/Common/TOOLING.md index 2f24577..56dba9b 100644 --- a/Progmasoft_Core_C_Coding_Guidelines/C11/TOOLING.md +++ b/Progmasoft_Core_C_Coding_Guidelines/Common/TOOLING.md @@ -44,6 +44,9 @@ clang-tidy Example.List.c -- -std=c11 -I. clang -std=c11 -pedantic-errors -Wall -Wextra -I. -fsyntax-only Example.List.c ``` +The commands are those of a C11 project. A GNU99 project writes `-std=gnu99` in place of `-std=c11 -pedantic-errors` +([GNU99](../Gnu99/LANGUAGE.md#building)). + `clang-format --dry-run --Werror` reports the places where the formatter's output differs from the file, including the few where the guidelines are right and the formatter is not ([Formatting](FORMATTING.md#what-the-tool-leaves-to-you)). @@ -55,6 +58,8 @@ few where the guidelines are right and the formatter is not ([Formatting](FORMAT - A valid value before the first read; each macro parameter evaluated once. - `realloc` through a temporary pointer, no allocation of zero bytes, overflow checks on sizes, ownership. - Module prefixes of public names, include paths, and file and directory names that match the module. +- In GNU99, only [the listed extensions](../Gnu99/LANGUAGE.md#the-extensions-it-allows), and none in a public header but + the listed attributes. - The line breaks the formatter cannot make, the blank lines around `goto` labels, and the forms in [What the tool leaves to you](FORMATTING.md#what-the-tool-leaves-to-you). diff --git a/Progmasoft_Core_C_Coding_Guidelines/Gnu99/LANGUAGE.md b/Progmasoft_Core_C_Coding_Guidelines/Gnu99/LANGUAGE.md new file mode 100644 index 0000000..528d13f --- /dev/null +++ b/Progmasoft_Core_C_Coding_Guidelines/Gnu99/LANGUAGE.md @@ -0,0 +1,78 @@ +<!-- SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> --> +<!-- SPDX-License-Identifier: CC-BY-4.0 --> + +# GNU99 + +GNU99 is for code that joins a codebase built as GNU99, such as firmware or kernel code, or that needs one of the GNU +extensions below. It is C99 with a short, closed list of GNU extensions; every extension not listed here is forbidden, +as in C11. + +A GNU99 project follows every chapter of [Common](../Common/PRINCIPLES.md) unchanged and uses the forms below where +the [C11 page](../C11/LANGUAGE.md) uses a C11 feature. + +## Building + +- **Code compiles with `-std=gnu99`**, next to the warnings of [Tooling](../Common/TOOLING.md#112-commands). + `-pedantic-errors` is not used, because it rejects the allowed extensions; review checks that only the listed ones + appear ([What review checks](../Common/TOOLING.md#113-what-review-checks)). +- **`clang-tidy` runs with the same flag**: `clang-tidy Example.List.c -- -std=gnu99 -I.` +- **Clang and GCC both build it.** Every extension below is one that both compilers support, so the code does not + depend on which of them a codebase uses. +- **Extensions are written in their double-underscore form**, `__attribute__`, `__typeof__`, `__asm__`, which also + compiles under `-std=c99` and `-std=c11`, where `typeof` and `asm` are not keywords. + +## The extensions it allows + +- **Function and variable attributes**, `__attribute__((...))`, with these names: `noreturn`, `format`, `nonnull`, + `warn_unused_result`, `unused`, `aligned`, and `packed` only for a layout that an external format or device fixes. +- **`__typeof__` and statement expressions, `({ ... })`, only inside a macro**, so that the macro evaluates each + parameter once ([Preprocessor](../Common/PREPROCESSOR.md#72-macros)). The variables of a statement expression carry + the name of the macro, so they cannot hide a variable of the caller. +- **`__builtin_add_overflow`, `__builtin_sub_overflow` and `__builtin_mul_overflow` for size computations**, which the + [check against `SIZE_MAX`](../Common/MEMORY-AND-OWNERSHIP.md#82-allocating) may use instead of a division. +- **`__builtin_expect` only where a measurement shows that it helps**, with the measurement named next to it. +- **Inline assembly, `__asm__ __volatile__`, only inside a `static inline` function** whose name says what the + instruction does, in a header of its own for each architecture. + +```c +#define EXAMPLE_LIST_MAX(dA_First, dA_Second) \ + ({ \ + __typeof__(dA_First) exampleListMaxFirst = (dA_First); \ + __typeof__(dA_Second) exampleListMaxSecond = (dA_Second); \ + exampleListMaxFirst > exampleListMaxSecond \ + ? exampleListMaxFirst \ + : exampleListMaxSecond; \ + }) + +size_t bytes = 0; +if(__builtin_mul_overflow(aCount, sizeof(*pValues), &bytes)) + return (false); +``` + +## What it does not allow + +- **No other extension**: no nested functions, zero-length arrays (C99 has the flexible array member), case ranges, + labels as values, `__label__`, the conditional with an omitted middle operand (`a ?: b`), or anonymous structures + and unions. +- **Variable-length arrays must not be used.** Their size is not checked against the stack, and C11 makes them + optional, so the code keeps building where it moves to C11. A buffer whose size is known only at run time is + allocated. + +## The GNU99 form of the C11 features + +| C11 | GNU99 | +| --- | --- | +| `_Static_assert(condition, "message")` | the same: Clang and GCC accept it in GNU99, and it keeps its message | +| `_Noreturn` | `__attribute__((noreturn))` | +| `_Alignas(N)` | `__attribute__((aligned(N)))` | +| `_Alignof(Type)` | `__alignof__(Type)` | +| `_Thread_local` | `__thread` | +| `_Atomic` and `<stdatomic.h>` | the `__atomic_*` built-ins on an integer or a pointer, each with its memory order (`__ATOMIC_ACQUIRE`, ...) written out | +| `_Generic` | none: a function for each type | +| `<threads.h>` | the threads of the platform | + +## Public headers + +A public header of a GNU99 library is included by C11 and C++ code. It uses no extension but `__attribute__` with the +names above, which Clang and GCC accept in C11 with `-pedantic-errors` and in C++, and it declares nothing that only +GNU99 can compile. diff --git a/Progmasoft_Core_C_Coding_Guidelines/index.json b/Progmasoft_Core_C_Coding_Guidelines/index.json index 0587fb0..c9d1a9f 100644 --- a/Progmasoft_Core_C_Coding_Guidelines/index.json +++ b/Progmasoft_Core_C_Coding_Guidelines/index.json @@ -8,32 +8,29 @@ "name": "C11", "default": true, "for": "every C project", - "documents": [ - "C11/PRINCIPLES.md", - "C11/FORMATTING.md", - "C11/NAMING.md", - "C11/FILES-AND-HEADERS.md", - "C11/CONTROL-FLOW.md", - "C11/DECLARATIONS-AND-INITIALIZERS.md", - "C11/PREPROCESSOR.md", - "C11/MEMORY-AND-OWNERSHIP.md", - "C11/LANGUAGE.md", - "C11/COMMENTS-AND-DOCUMENTATION.md", - "C11/TOOLING.md" - ] + "documents": ["C11/LANGUAGE.md"] + }, + { + "id": "gnu99", + "name": "GNU99", + "default": false, + "for": "code that joins a codebase built as GNU99, such as firmware or kernel code, or needs one of its listed GNU extensions", + "documents": ["Gnu99/LANGUAGE.md"] } ], "documents": [ - { "id": "principles", "chapter": 1, "title": "Principles", "path": "C11/PRINCIPLES.md", "standards": ["c11"] }, - { "id": "formatting", "chapter": 2, "title": "Formatting", "path": "C11/FORMATTING.md", "standards": ["c11"] }, - { "id": "naming", "chapter": 3, "title": "Naming", "path": "C11/NAMING.md", "standards": ["c11"] }, - { "id": "files-and-headers", "chapter": 4, "title": "Files and headers", "path": "C11/FILES-AND-HEADERS.md", "standards": ["c11"] }, - { "id": "control-flow", "chapter": 5, "title": "Control flow", "path": "C11/CONTROL-FLOW.md", "standards": ["c11"] }, - { "id": "declarations-and-initializers", "chapter": 6, "title": "Declarations and initializers", "path": "C11/DECLARATIONS-AND-INITIALIZERS.md", "standards": ["c11"] }, - { "id": "preprocessor", "chapter": 7, "title": "Preprocessor", "path": "C11/PREPROCESSOR.md", "standards": ["c11"] }, - { "id": "memory-and-ownership", "chapter": 8, "title": "Memory and ownership", "path": "C11/MEMORY-AND-OWNERSHIP.md", "standards": ["c11"] }, - { "id": "language", "chapter": 9, "title": "Language", "path": "C11/LANGUAGE.md", "standards": ["c11"] }, - { "id": "comments-and-documentation", "chapter": 10, "title": "Comments and documentation", "path": "C11/COMMENTS-AND-DOCUMENTATION.md", "standards": ["c11"] }, - { "id": "tooling", "chapter": 11, "title": "Tooling", "path": "C11/TOOLING.md", "standards": ["c11"] } + { "id": "principles", "chapter": 1, "title": "Principles", "path": "Common/PRINCIPLES.md", "standards": ["c11", "gnu99"] }, + { "id": "formatting", "chapter": 2, "title": "Formatting", "path": "Common/FORMATTING.md", "standards": ["c11", "gnu99"] }, + { "id": "naming", "chapter": 3, "title": "Naming", "path": "Common/NAMING.md", "standards": ["c11", "gnu99"] }, + { "id": "files-and-headers", "chapter": 4, "title": "Files and headers", "path": "Common/FILES-AND-HEADERS.md", "standards": ["c11", "gnu99"] }, + { "id": "control-flow", "chapter": 5, "title": "Control flow", "path": "Common/CONTROL-FLOW.md", "standards": ["c11", "gnu99"] }, + { "id": "declarations-and-initializers", "chapter": 6, "title": "Declarations and initializers", "path": "Common/DECLARATIONS-AND-INITIALIZERS.md", "standards": ["c11", "gnu99"] }, + { "id": "preprocessor", "chapter": 7, "title": "Preprocessor", "path": "Common/PREPROCESSOR.md", "standards": ["c11", "gnu99"] }, + { "id": "memory-and-ownership", "chapter": 8, "title": "Memory and ownership", "path": "Common/MEMORY-AND-OWNERSHIP.md", "standards": ["c11", "gnu99"] }, + { "id": "language", "chapter": 9, "title": "Language", "path": "Common/LANGUAGE.md", "standards": ["c11", "gnu99"] }, + { "id": "comments-and-documentation", "chapter": 10, "title": "Comments and documentation", "path": "Common/COMMENTS-AND-DOCUMENTATION.md", "standards": ["c11", "gnu99"] }, + { "id": "tooling", "chapter": 11, "title": "Tooling", "path": "Common/TOOLING.md", "standards": ["c11", "gnu99"] }, + { "id": "c11", "title": "C11", "path": "C11/LANGUAGE.md", "standards": ["c11"] }, + { "id": "gnu99", "title": "GNU99", "path": "Gnu99/LANGUAGE.md", "standards": ["gnu99"] } ] } diff --git a/README.md b/README.md index a4f70ed..39834d6 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ conventions each time. | Guidelines | Languages | State | | --- | --- | --- | | [Progmasoft Core C++ Coding Guidelines](#progmasoft-core-c-coding-guidelines) | C++17, C++20 | written | -| [Progmasoft Core C Coding Guidelines](#progmasoft-core-c-coding-guidelines) | C11 | written | +| [Progmasoft Core C Coding Guidelines](#progmasoft-core-c-coding-guidelines) | C11, GNU99 | written | They are written for anyone who wants a complete, opinionated rule set to adopt. Progmasoft's own C++ projects follow them, among them [Catch3](https://github.com/Progmasoft/catch3) and [Tazelish](https://github.com/Progmasoft/tazelish). @@ -81,32 +81,40 @@ records a deviation. [Contributing](CONTRIBUTING.md) explains how to propose a c ## Progmasoft Core C Coding Guidelines -The guidelines for C11, in `Progmasoft_Core_C_Coding_Guidelines/`. They are a style of their own, not the C part of the -C++ guidelines: a C interface of a C++ library follows the C++ guidelines, a C project follows these. +The guidelines for C11 and GNU99, in `Progmasoft_Core_C_Coding_Guidelines/`. They are a style of their own, not the C +part of the C++ guidelines: a C interface of a C++ library follows the C++ guidelines, a C project follows these. ### Contents of the C guidelines | Chapter | Covers | | --- | --- | -| [1. Principles](Progmasoft_Core_C_Coding_Guidelines/C11/PRINCIPLES.md) | C11 and LLVM 23, how the rules are worded, what tools check and what review checks | -| [2. Formatting](Progmasoft_Core_C_Coding_Guidelines/C11/FORMATTING.md) | the `.clang-format` profile, and the forms it cannot produce | -| [3. Naming](Progmasoft_Core_C_Coding_Guidelines/C11/NAMING.md) | prefixes for scope, storage, pointers and parameters; modules; what function names mean | -| [4. Files and headers](Progmasoft_Core_C_Coding_Guidelines/C11/FILES-AND-HEADERS.md) | file and directory names, `#pragma once`, `extern "C"`, includes | -| [5. Control flow](Progmasoft_Core_C_Coding_Guidelines/C11/CONTROL-FLOW.md) | conditions, `return`, loops, `switch`, `goto` | -| [6. Declarations and initializers](Progmasoft_Core_C_Coding_Guidelines/C11/DECLARATIONS-AND-INITIALIZERS.md) | declarations, `typedef`, literals, initializers | -| [7. Preprocessor](Progmasoft_Core_C_Coding_Guidelines/C11/PREPROCESSOR.md) | conditions and macros | -| [8. Memory and ownership](Progmasoft_Core_C_Coding_Guidelines/C11/MEMORY-AND-OWNERSHIP.md) | ownership contracts, `malloc` and `realloc`, growing and freeing | -| [9. Language](Progmasoft_Core_C_Coding_Guidelines/C11/LANGUAGE.md) | extensions, `bool`, `restrict`, `_Atomic`, `_Static_assert` and the other C11 features | -| [10. Comments and documentation](Progmasoft_Core_C_Coding_Guidelines/C11/COMMENTS-AND-DOCUMENTATION.md) | comments, Doxygen and the order of its parts | -| [11. Tooling](Progmasoft_Core_C_Coding_Guidelines/C11/TOOLING.md) | the `.clang-tidy` profile, commands, what review checks | - -With one version, the chapters are all in `C11/`. [`index.json`](Progmasoft_Core_C_Coding_Guidelines/index.json) lists -them in the same form as the C++ index. +| [1. Principles](Progmasoft_Core_C_Coding_Guidelines/Common/PRINCIPLES.md) | C11 or GNU99 and LLVM 23, how the rules are worded, what tools check and what review checks | +| [2. Formatting](Progmasoft_Core_C_Coding_Guidelines/Common/FORMATTING.md) | the `.clang-format` profile, and the forms it cannot produce | +| [3. Naming](Progmasoft_Core_C_Coding_Guidelines/Common/NAMING.md) | prefixes for scope, storage, pointers and parameters; modules; what function names mean | +| [4. Files and headers](Progmasoft_Core_C_Coding_Guidelines/Common/FILES-AND-HEADERS.md) | file and directory names, `#pragma once`, `extern "C"`, includes | +| [5. Control flow](Progmasoft_Core_C_Coding_Guidelines/Common/CONTROL-FLOW.md) | conditions, `return`, loops, `switch`, `goto` | +| [6. Declarations and initializers](Progmasoft_Core_C_Coding_Guidelines/Common/DECLARATIONS-AND-INITIALIZERS.md) | declarations, `typedef`, literals, initializers | +| [7. Preprocessor](Progmasoft_Core_C_Coding_Guidelines/Common/PREPROCESSOR.md) | conditions and macros | +| [8. Memory and ownership](Progmasoft_Core_C_Coding_Guidelines/Common/MEMORY-AND-OWNERSHIP.md) | ownership contracts, `malloc` and `realloc`, growing and freeing | +| [9. Language](Progmasoft_Core_C_Coding_Guidelines/Common/LANGUAGE.md) | choosing the version, what every version allows, `bool`, `restrict`, `volatile` | +| [10. Comments and documentation](Progmasoft_Core_C_Coding_Guidelines/Common/COMMENTS-AND-DOCUMENTATION.md) | comments, Doxygen and the order of its parts | +| [11. Tooling](Progmasoft_Core_C_Coding_Guidelines/Common/TOOLING.md) | the `.clang-tidy` profile, commands, what review checks | + +Each version has a page of its own with the rules that use its features: + +| Version | Covers | +| --- | --- | +| [C11](Progmasoft_Core_C_Coding_Guidelines/C11/LANGUAGE.md) | the default: `-std=c11 -pedantic-errors`, no extensions, `_Atomic`, `_Static_assert` and the other C11 features | +| [GNU99](Progmasoft_Core_C_Coding_Guidelines/Gnu99/LANGUAGE.md) | for code that joins a GNU99 codebase: `-std=gnu99`, the closed list of GNU extensions, the GNU99 form of each C11 feature | + +As in the C++ guidelines, `Common/` holds what applies to both versions, `C11/` and `Gnu99/` what applies to one. +[`index.json`](Progmasoft_Core_C_Coding_Guidelines/index.json) lists every document with its chapter and versions, in +the same form as the C++ index. ### The C guidelines in short -- ISO C11 with `-pedantic-errors`, no extensions but `#pragma once` and `extern "C"`; Clang, `clang-format` and - `clang-tidy` 23. +- ISO C11 with `-pedantic-errors`, no extensions but `#pragma once` and `extern "C"`; or GNU99 with a closed list of GNU + extensions. Clang, `clang-format` and `clang-tidy` 23. - Eight spaces, 100 columns, the return type on its own line, braces on their own line, and no braces around a body of one statement. `if(`, `return (value);`, `int* pValue`, `values[ index ]`. - `camelCase` locals with `g`, `s` and one `p` per pointer level as prefixes (`gspUser`); parameters `aCount`,