Skip to content

Commit bc2eee7

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#25092: doc: various developer notes updates
6542842 Add clang lifetimebound section to developer notes (Jon Atack) e66b321 Add C++ functions and methods section to developer notes (Jon Atack) 5fca70f Link in developer notes style to internal interface exception (Jon Atack) fc4cb85 Prefer Python for scripts in developer notes (Jon Atack) 370120e Remove obsolete BDB ENABLE_WALLET section in developer notes (Jon Atack) Pull request description: A few updates noticed while working on a lifetimebound section. - Remove obsolete BDB ENABLE_WALLET section (only one file, src/wallet/bdb.h, still has a `db_cxx.h` BDB header) - Prefer Python for scripts in developer notes (and a few miscellaneous touch-ups) - In the code style section, add a link to the internal interface exception so that people are aware of it - Add a "C++ functions and methods" section - Add a Clang `lifetimebound` attribute section ACKs for top commit: laanwj: ACK 6542842 jarolrod: code review ACK bitcoin/bitcoin@6542842 Tree-SHA512: d2ae335cac899451d5c7bdc8e94fd82053a5f44ac1ba444bdde71abaaa24a519713c1078f3a8f07ec8466b181788a613fd3c68061e54b3fdc8cd6f3e3f9791ec
2 parents b74a6dd + 6542842 commit bc2eee7

File tree

1 file changed

+47
-24
lines changed

1 file changed

+47
-24
lines changed

doc/developer-notes.md

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Developer Notes
3232
- [C++ data structures](#c-data-structures)
3333
- [Strings and formatting](#strings-and-formatting)
3434
- [Shadowing](#shadowing)
35+
- [Lifetimebound](#lifetimebound)
3536
- [Threads and synchronization](#threads-and-synchronization)
3637
- [Scripts](#scripts)
3738
- [Shebang](#shebang)
@@ -96,7 +97,10 @@ code.
9697
Guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-caps),
9798
which recommend using `snake_case`. Please use what seems appropriate.
9899
- Class names, function names, and method names are UpperCamelCase
99-
(PascalCase). Do not prefix class names with `C`.
100+
(PascalCase). Do not prefix class names with `C`. See [Internal interface
101+
naming style](#internal-interface-naming-style) for an exception to this
102+
convention.
103+
100104
- Test suite naming convention: The Boost test suite in file
101105
`src/test/foo_tests.cpp` should be named `foo_tests`. Test suite names
102106
must be unique.
@@ -138,6 +142,27 @@ public:
138142
} // namespace foo
139143
```
140144

145+
Coding Style (C++ functions and methods)
146+
--------------------
147+
148+
- When ordering function parameters, place input parameters first, then any
149+
in-out parameters, followed by any output parameters.
150+
151+
- *Rationale*: API consistency.
152+
153+
- Prefer returning values directly to using in-out or output parameters. Use
154+
`std::optional` where helpful for returning values.
155+
156+
- *Rationale*: Less error-prone (no need for assumptions about what the output
157+
is initialized to on failure), easier to read, and often the same or better
158+
performance.
159+
160+
- Generally, use `std::optional` to represent optional by-value inputs (and
161+
instead of a magic default value, if there is no real default). Non-optional
162+
input parameters should usually be values or const references, while
163+
non-optional in-out and output parameters should usually be references, as
164+
they cannot be null.
165+
141166
Coding Style (C++ named arguments)
142167
------------------------------
143168

@@ -657,10 +682,6 @@ Wallet
657682

658683
- Make sure that no crashes happen with run-time option `-disablewallet`.
659684

660-
- Include `db_cxx.h` (BerkeleyDB header) only when `ENABLE_WALLET` is set.
661-
662-
- *Rationale*: Otherwise compilation of the disable-wallet build will fail in environments without BerkeleyDB.
663-
664685
General C++
665686
-------------
666687

@@ -863,12 +884,22 @@ from using a different variable with the same name),
863884
please name variables so that their names do not shadow variables defined in the source code.
864885
865886
When using nested cycles, do not name the inner cycle variable the same as in
866-
the upper cycle, etc.
887+
the outer cycle, etc.
888+
889+
Lifetimebound
890+
--------------
891+
892+
The [Clang `lifetimebound`
893+
attribute](https://clang.llvm.org/docs/AttributeReference.html#lifetimebound)
894+
can be used to tell the compiler that a lifetime is bound to an object and
895+
potentially see a compile-time warning if the object has a shorter lifetime from
896+
the invalid use of a temporary. You can use the attribute by adding a `LIFETIMEBOUND`
897+
annotation defined in `src/attributes.h`; please grep the codebase for examples.
867898
868899
Threads and synchronization
869900
----------------------------
870901
871-
- Prefer `Mutex` type to `RecursiveMutex` one
902+
- Prefer `Mutex` type to `RecursiveMutex` one.
872903
873904
- Consistently use [Clang Thread Safety Analysis](https://clang.llvm.org/docs/ThreadSafetyAnalysis.html) annotations to
874905
get compile-time warnings about potential race conditions in code. Combine annotations in function declarations with
@@ -947,6 +978,8 @@ TRY_LOCK(cs_vNodes, lockNodes);
947978
Scripts
948979
--------------------------
949980
981+
Write scripts in Python rather than bash, when possible.
982+
950983
### Shebang
951984
952985
- Use `#!/usr/bin/env bash` instead of obsolete `#!/bin/bash`.
@@ -1389,22 +1422,9 @@ communication:
13891422
virtual boost::signals2::scoped_connection connectTipChanged(TipChangedFn fn) = 0;
13901423
```
13911424
1392-
- For consistency and friendliness to code generation tools, interface method
1393-
input and inout parameters should be ordered first and output parameters
1394-
should come last.
1425+
- Interface methods should not be overloaded.
13951426
1396-
Example:
1397-
1398-
```c++
1399-
// Good: error output param is last
1400-
virtual bool broadcastTransaction(const CTransactionRef& tx, CAmount max_fee, std::string& error) = 0;
1401-
1402-
// Bad: error output param is between input params
1403-
virtual bool broadcastTransaction(const CTransactionRef& tx, std::string& error, CAmount max_fee) = 0;
1404-
```
1405-
1406-
- For friendliness to code generation tools, interface methods should not be
1407-
overloaded:
1427+
*Rationale*: consistency and friendliness to code generation tools.
14081428
14091429
Example:
14101430
@@ -1418,10 +1438,13 @@ communication:
14181438
virtual bool disconnect(NodeId id) = 0;
14191439
```
14201440

1421-
- For consistency and friendliness to code generation tools, interface method
1422-
names should be `lowerCamelCase` and standalone function names should be
1441+
### Internal interface naming style
1442+
1443+
- Interface method names should be `lowerCamelCase` and standalone function names should be
14231444
`UpperCamelCase`.
14241445

1446+
*Rationale*: consistency and friendliness to code generation tools.
1447+
14251448
Examples:
14261449

14271450
```c++

0 commit comments

Comments
 (0)