Repository navigation
docs: Add C++ guidelines #8688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kuznetsss
wants to merge
4
commits into
XRPLF:develop
Choose a base branch
from
kuznetsss:Cpp_guidelines
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+313
−233
Open
docs: Add C++ guidelines #8688
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -203,6 +203,7 @@ words: | |
| - multisign | ||
| - multisigned | ||
| - Nakamoto | ||
| - nbougalis | ||
| - nftid | ||
| - nftoffer | ||
| - nftoken | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| --- | ||
| applyTo: "**/*.h,**/*.hpp,**/*.cpp,**/*.ipp" | ||
| --- | ||
|
|
||
| # C++ | ||
|
|
||
| C++ code must follow [`docs/cpp/guidelines.md`](../../docs/cpp/guidelines.md), | ||
| and `XRPL_ASSERT` and `UNREACHABLE` contracts must follow | ||
| [`docs/cpp/antithesis.md`](../../docs/cpp/antithesis.md). | ||
|
|
||
| When writing code, follow these documents. | ||
|
|
||
| When reviewing a pull request: | ||
|
|
||
| - Check every C++ change against these documents, and report each violation | ||
| as a review comment that names the rule it breaks. | ||
| - Review only code the pull request adds or modifies; existing violations in | ||
| untouched code are out of scope. | ||
| - Do not comment on formatting or on anything clang-tidy reports; CI checks | ||
| those separately. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # Antithesis | ||
|
|
||
| We use [Antithesis](https://antithesis.com/) for continuous fuzzing, and keep | ||
| a copy of the [Antithesis C++ SDK](https://github.com/antithesishq/antithesis-sdk-cpp/) | ||
| in `external/antithesis-sdk`. One of the aims of fuzzing is to identify bugs | ||
| by finding external conditions that cause contract violations inside `xrpld`. | ||
| Contracts are expressed as `XRPL_ASSERT` or `UNREACHABLE` (defined in | ||
| `include/xrpl/beast/utility/instrumentation.h`), which outside of Antithesis | ||
| are effectively wrappers for `assert(...)` with an added name. The name gives | ||
| each contract a stable identity that does not rely on line numbers. | ||
|
|
||
| When `xrpld` is built with Antithesis instrumentation enabled (the `voidstar` | ||
| CMake option) and run on the Antithesis platform, the contracts become | ||
| [test properties](https://antithesis.com/docs/using_antithesis/properties.html); | ||
| otherwise they behave like a regular `assert`. To learn more about Antithesis, | ||
| see [How Antithesis Works](https://antithesis.com/docs/introduction/how_antithesis_works.html) | ||
| and the [C++ SDK](https://antithesis.com/docs/using_antithesis/sdk/cpp/overview.html). | ||
|
mathbunnyru marked this conversation as resolved.
Resolved
|
||
|
|
||
| We continue to use plain `assert` or `assert(false)` in certain locations where | ||
| reporting contract violations on the Antithesis platform is either not possible | ||
| or not useful. | ||
|
|
||
| For this reason: | ||
|
kuznetsss marked this conversation as resolved.
Resolved
|
||
|
|
||
| - `assert` and `assert(false)` should continue to be used in: | ||
| - `constexpr` functions; | ||
| - unit tests, i.e. files under `src/test`; | ||
|
kuznetsss marked this conversation as resolved.
Resolved
|
||
| - unit-test-related modules (files under `beast/test` and `beast/unit_test`). | ||
|
mathbunnyru marked this conversation as resolved.
Resolved
|
||
| - Outside of these locations, do not use `assert`; use `XRPL_ASSERT` instead, | ||
| giving it a unique name and a short description of the contract. | ||
| - Outside of these locations, do not use `assert(false)`; use `UNREACHABLE` | ||
| instead, giving it a unique name and a description of the condition being | ||
| violated. | ||
| - The contract name should start with the full name (including scope) of the | ||
| function, optionally followed by a named lambda, then a colon `:` and a brief | ||
| (typically at most five words) description. `UNREACHABLE` contracts can use | ||
| slightly longer descriptions. If the function has multiple overloads, use | ||
| common sense to balance brevity and unambiguity of the function name. | ||
| NOTE: the name exists to identify every contract uniquely and stably, so | ||
| avoid elements that are likely to change in obvious refactors or when the | ||
| condition is strengthened. | ||
| - The description of a contract (except `UNREACHABLE`) should describe the | ||
| _expected_ condition, as in "I assert that _expected_ is true". | ||
| - The description of an `UNREACHABLE` contract should describe the | ||
| _unexpected_ situation that caused the line to be reached. | ||
| - Example of a good `UNREACHABLE` name: | ||
| `"json::operator==(Value, Value) : invalid type"`; example of a good | ||
| `XRPL_ASSERT` name: `"json::Value::asCString : valid type"`. | ||
| - Example of a **bad** name: | ||
| `"RFC1751::insert(char* s, int x, int start, int length) : length is greater than or equal zero"` | ||
| (missing namespace, unnecessary full function signature, description too | ||
| verbose). Good name: `"xrpl::RFC1751::insert : minimum length"`. | ||
| - In a **few** well-justified cases a non-standard name can be used, in which | ||
| case add a comment explaining the rationale (see | ||
| `src/libxrpl/basics/contract.cpp` for an example). | ||
| - Do **not** rename a contract without a good reason (e.g. the name no longer | ||
| reflects the location or the condition being checked). | ||
| - Do not use `std::unreachable`. | ||
| - Do not put contracts where they can be violated by an external condition | ||
| (e.g. timing, or a data payload before mandatory validation), as this | ||
| creates bogus bug reports (and crashes Debug builds). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # C++ Formatting | ||
|
|
||
| All code must conform to `clang-format`, according to the settings in | ||
| [`.clang-format`](../../.clang-format), unless the result would be unreasonably | ||
| difficult to read or maintain. The `clang-format` version is pinned in | ||
| [`.pre-commit-config.yaml`](../../.pre-commit-config.yaml), so the | ||
| [`pre-commit`](../../CONTRIBUTING.md#pre-commit-hooks) hook always formats with the same version as | ||
| CI. To demarcate lines that should be left as-is, surround them with comments | ||
| like this: | ||
|
|
||
| ``` | ||
| // clang-format off | ||
| ... | ||
| // clang-format on | ||
| ``` | ||
|
kuznetsss marked this conversation as resolved.
Resolved
|
||
|
|
||
| The easiest way to format your changes is to let the `pre-commit` hook run | ||
| automatically on commit, or to run it manually: | ||
|
|
||
| ```bash | ||
| pre-commit run clang-format --all-files | ||
| ``` | ||
|
|
||
| You can also format individual files in place by running `clang-format -i <file>...` | ||
| from any directory within this project. | ||
|
|
||
| > [!NOTE] | ||
| > This uses whatever `clang-format` version is installed locally, which may | ||
| > differ from the pinned version used by `pre-commit` and CI, so the results | ||
| > can vary. | ||
|
kuznetsss marked this conversation as resolved.
Resolved
|
||
|
|
||
| There is a Continuous Integration job that runs clang-format on pull requests. If the code doesn't comply, a patch file that corrects auto-fixable formatting issues is generated. | ||
|
|
||
| To download the patch file: | ||
|
|
||
| 1. Next to `clang-format / check (pull_request) Failing after #s` -> click **Details** to open the details page. | ||
| 2. Left menu -> click **Summary** | ||
| 3. Scroll down to near the bottom-right under `Artifacts` -> click **clang-format.patch** | ||
| 4. Download the zip file and extract it to your local git repository. Run `git apply [patch-file-name]`. | ||
| 5. Commit and push. | ||
|
kuznetsss marked this conversation as resolved.
Resolved
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.