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
Let GNU99 use the GNU extensions
- Commit
72b5b4ac317d892e47ca9161af99158eee442a60- Author
- Leitwolf11 ·
- Parents
0c53b12aaee6
Changes
CHANGELOG.md | 7 ++-- .../Common/LANGUAGE.md | 2 +- .../Common/PRINCIPLES.md | 12 +++--- .../Common/TOOLING.md | 2 - .../Gnu99/LANGUAGE.md | 44 +++++----------------- Progmasoft_Core_C_Coding_Guidelines/index.json | 2 +- README.md | 4 +- 7 files changed, 22 insertions(+), 51 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71048eb..2e13303 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,10 +9,9 @@ A revision of Progmasoft C/C++ Styles is named by its date. A change that requir 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`. +version, and Language (chapter 9) says how a project chooses. GNU99 is `-std=gnu99` with the GNU extensions, and a +table gives the 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 diff --git a/Progmasoft_Core_C_Coding_Guidelines/Common/LANGUAGE.md b/Progmasoft_Core_C_Coding_Guidelines/Common/LANGUAGE.md index 6e15007..327d14d 100644 --- a/Progmasoft_Core_C_Coding_Guidelines/Common/LANGUAGE.md +++ b/Progmasoft_Core_C_Coding_Guidelines/Common/LANGUAGE.md @@ -7,7 +7,7 @@ The guidelines cover two versions of C. A project names its version in its contr - **[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. + or that uses GNU extensions. It is C99 with the GNU extensions. The rules below hold in both. A version's page says how it is built and which features it uses. diff --git a/Progmasoft_Core_C_Coding_Guidelines/Common/PRINCIPLES.md b/Progmasoft_Core_C_Coding_Guidelines/Common/PRINCIPLES.md index c0ee5cc..ee2feeb 100644 --- a/Progmasoft_Core_C_Coding_Guidelines/Common/PRINCIPLES.md +++ b/Progmasoft_Core_C_Coding_Guidelines/Common/PRINCIPLES.md @@ -10,14 +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**, 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 language is ISO C11**, or GNU99 for code that joins a codebase built as GNU99 or uses GNU 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)). GNU99 adds the closed list of - extensions on [its page](../Gnu99/LANGUAGE.md#the-extensions-it-allows). +- **C11 uses 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)). GNU99 may use the GNU + extensions ([GNU99](../Gnu99/LANGUAGE.md#extensions)). ## 1.2 How the rules are written diff --git a/Progmasoft_Core_C_Coding_Guidelines/Common/TOOLING.md b/Progmasoft_Core_C_Coding_Guidelines/Common/TOOLING.md index 56dba9b..accb3eb 100644 --- a/Progmasoft_Core_C_Coding_Guidelines/Common/TOOLING.md +++ b/Progmasoft_Core_C_Coding_Guidelines/Common/TOOLING.md @@ -58,8 +58,6 @@ 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 index 528d13f..7eddffa 100644 --- a/Progmasoft_Core_C_Coding_Guidelines/Gnu99/LANGUAGE.md +++ b/Progmasoft_Core_C_Coding_Guidelines/Gnu99/LANGUAGE.md @@ -3,9 +3,8 @@ # 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. +GNU99 is C99 with the GNU extensions. It is for code that joins a codebase built as GNU99, such as firmware or kernel +code, or that uses GNU extensions. 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. @@ -13,26 +12,16 @@ 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)). + `-pedantic-errors` is not used, because it rejects the extensions. - **`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 +## Extensions -- **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. +- **GNU extensions may be used.** Attributes, `__typeof__`, statement expressions, the built-in functions, inline + assembly and the other extensions of GNU C are part of the language of a GNU99 project. +- **An extension follows the chapters of Common like any other code**: a statement expression in a macro still + evaluates each parameter once ([Preprocessor](../Common/PREPROCESSOR.md#72-macros)), and its variables carry the name + of the macro, so they cannot hide a variable of the caller. ```c #define EXAMPLE_LIST_MAX(dA_First, dA_Second) \ @@ -49,15 +38,6 @@ 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 | @@ -70,9 +50,3 @@ if(__builtin_mul_overflow(aCount, sizeof(*pValues), &bytes)) | `_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 c9d1a9f..50b267d 100644 --- a/Progmasoft_Core_C_Coding_Guidelines/index.json +++ b/Progmasoft_Core_C_Coding_Guidelines/index.json @@ -14,7 +14,7 @@ "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", + "for": "code that joins a codebase built as GNU99, such as firmware or kernel code, or that uses GNU extensions", "documents": ["Gnu99/LANGUAGE.md"] } ], diff --git a/README.md b/README.md index 39834d6..e6dd678 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ 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 | +| [GNU99](Progmasoft_Core_C_Coding_Guidelines/Gnu99/LANGUAGE.md) | for code that joins a GNU99 codebase or uses GNU extensions: `-std=gnu99`, the 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 @@ -113,7 +113,7 @@ 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"`; or GNU99 with a closed list of GNU +- ISO C11 with `-pedantic-errors`, no extensions but `#pragma once` and `extern "C"`; or GNU99 with the 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 ]`.