Skip to content

Commit 0c5f67b

Browse files
author
MarcoFalke
committed
Merge #12757: Clarify include guard naming convention
3bcc005 Add lint-include-guards.sh which checks include guard consistency (practicalswift) 8fd6af8 Fix missing or inconsistent include guards (practicalswift) 8af65d9 Document include guard convention (practicalswift) Pull request description: * **Documentation**: Document include guard convention * **Fix**: Fix missing or inconsistent include guards * **Regression test**: Add `lint-include-guards.sh` which checks include guard consistency Tree-SHA512: 8171878f60fd08ccbea943a11e835195750592abb9d7ab74eaa4265ae7fac523b1da9d31ca13d6ab73dd596e49986bfb7593c696e5f39567c93e610165bc2acc
2 parents 9beded5 + 3bcc005 commit 0c5f67b

19 files changed

+94
-46
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
#
3+
# Copyright (c) 2018 The Bitcoin Core developers
4+
# Distributed under the MIT software license, see the accompanying
5+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
6+
#
7+
# Check include guards.
8+
9+
HEADER_ID_PREFIX="BITCOIN_"
10+
HEADER_ID_SUFFIX="_H"
11+
12+
REGEXP_EXCLUDE_FILES_WITH_PREFIX="src/(crypto/ctaes/|leveldb/|secp256k1/|tinyformat.h|univalue/)"
13+
14+
EXIT_CODE=0
15+
for HEADER_FILE in $(git ls-files -- "*.h" | grep -vE "^${REGEXP_EXCLUDE_FILES_WITH_PREFIX}")
16+
do
17+
HEADER_ID_BASE=$(cut -f2- -d/ <<< "${HEADER_FILE}" | sed "s/\.h$//g" | tr / _ | tr "[:lower:]" "[:upper:]")
18+
HEADER_ID="${HEADER_ID_PREFIX}${HEADER_ID_BASE}${HEADER_ID_SUFFIX}"
19+
if [[ $(grep -cE "^#(ifndef|define) ${HEADER_ID}" "${HEADER_FILE}") != 2 ]]; then
20+
echo "${HEADER_FILE} seems to be missing the expected include guard:"
21+
echo " #ifndef ${HEADER_ID}"
22+
echo " #define ${HEADER_ID}"
23+
echo " ..."
24+
echo " #endif // ${HEADER_ID}"
25+
echo
26+
EXIT_CODE=1
27+
fi
28+
done
29+
exit ${EXIT_CODE}

doc/developer-notes.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,16 @@ namespace {
606606
source file into account. This allows quoted includes to stand out more when
607607
the location of the source file actually is relevant.
608608

609+
- Use include guards to avoid the problem of double inclusion. The header file
610+
`foo/bar.h` should use the include guard identifier `BITCOIN_FOO_BAR_H`, e.g.
611+
612+
```c++
613+
#ifndef BITCOIN_FOO_BAR_H
614+
#define BITCOIN_FOO_BAR_H
615+
...
616+
#endif // BITCOIN_FOO_BAR_H
617+
```
618+
609619
GUI
610620
-----
611621

src/bech32.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
//
1010
// For more information, see BIP 173.
1111

12+
#ifndef BITCOIN_BECH32_H
13+
#define BITCOIN_BECH32_H
14+
1215
#include <stdint.h>
1316
#include <string>
1417
#include <vector>
@@ -23,3 +26,5 @@ std::string Encode(const std::string& hrp, const std::vector<uint8_t>& values);
2326
std::pair<std::string, std::vector<uint8_t>> Decode(const std::string& str);
2427

2528
} // namespace bech32
29+
30+
#endif // BITCOIN_BECH32_H

src/bench/perf.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
/** Functions for measurement of CPU cycles */
6-
#ifndef H_PERF
7-
#define H_PERF
6+
#ifndef BITCOIN_BENCH_PERF_H
7+
#define BITCOIN_BENCH_PERF_H
88

99
#include <stdint.h>
1010

@@ -34,4 +34,4 @@ uint64_t perf_cpucycles(void);
3434
void perf_init(void);
3535
void perf_fini(void);
3636

37-
#endif // H_PERF
37+
#endif // BITCOIN_BENCH_PERF_H

src/blockencodings.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#ifndef BITCOIN_BLOCK_ENCODINGS_H
6-
#define BITCOIN_BLOCK_ENCODINGS_H
5+
#ifndef BITCOIN_BLOCKENCODINGS_H
6+
#define BITCOIN_BLOCKENCODINGS_H
77

88
#include <primitives/block.h>
99

@@ -206,4 +206,4 @@ class PartiallyDownloadedBlock {
206206
ReadStatus FillBlock(CBlock& block, const std::vector<CTransactionRef>& vtx_missing);
207207
};
208208

209-
#endif
209+
#endif // BITCOIN_BLOCKENCODINGS_H

src/consensus/merkle.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#ifndef BITCOIN_MERKLE
6-
#define BITCOIN_MERKLE
5+
#ifndef BITCOIN_CONSENSUS_MERKLE_H
6+
#define BITCOIN_CONSENSUS_MERKLE_H
77

88
#include <stdint.h>
99
#include <vector>
@@ -35,4 +35,4 @@ uint256 BlockWitnessMerkleRoot(const CBlock& block, bool* mutated = nullptr);
3535
*/
3636
std::vector<uint256> BlockMerkleBranch(const CBlock& block, uint32_t position);
3737

38-
#endif
38+
#endif // BITCOIN_CONSENSUS_MERKLE_H

src/key_io.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

6-
#ifndef BITCOIN_KEYIO_H
7-
#define BITCOIN_KEYIO_H
6+
#ifndef BITCOIN_KEY_IO_H
7+
#define BITCOIN_KEY_IO_H
88

99
#include <chainparams.h>
1010
#include <key.h>
@@ -26,4 +26,4 @@ CTxDestination DecodeDestination(const std::string& str);
2626
bool IsValidDestinationString(const std::string& str);
2727
bool IsValidDestinationString(const std::string& str, const CChainParams& params);
2828

29-
#endif // BITCOIN_KEYIO_H
29+
#endif // BITCOIN_KEY_IO_H

src/policy/fees.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Copyright (c) 2009-2017 The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5-
#ifndef BITCOIN_POLICYESTIMATOR_H
6-
#define BITCOIN_POLICYESTIMATOR_H
5+
#ifndef BITCOIN_POLICY_FEES_H
6+
#define BITCOIN_POLICY_FEES_H
77

88
#include <amount.h>
99
#include <policy/feerate.h>
@@ -294,4 +294,4 @@ class FeeFilterRounder
294294
FastRandomContext insecure_rand;
295295
};
296296

297-
#endif /*BITCOIN_POLICYESTIMATOR_H */
297+
#endif // BITCOIN_POLICY_FEES_H

src/qt/test/paymentrequestdata.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5+
#ifndef BITCOIN_QT_TEST_PAYMENTREQUESTDATA_H
6+
#define BITCOIN_QT_TEST_PAYMENTREQUESTDATA_H
7+
58
//
69
// Data for paymentservertests.cpp
710
//
@@ -458,3 +461,5 @@ iEBFUrBDJZU+UEezGwr7/zoECjo5ZY3PmtZcM2sILNjyweJF6XVzGqTxUw6pN6sW\
458461
XR2T3Gy2LzRvhVA25QgGqpz0/juS2BtmNbsZPkN9gMMwKimgzc+PuCzmEKwPK9cQ\
459462
YQ==\
460463
";
464+
465+
#endif // BITCOIN_QT_TEST_PAYMENTREQUESTDATA_H

src/qt/test/rpcnestedtests.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#ifndef BITCOIN_QT_TEST_RPC_NESTED_TESTS_H
6-
#define BITCOIN_QT_TEST_RPC_NESTED_TESTS_H
5+
#ifndef BITCOIN_QT_TEST_RPCNESTEDTESTS_H
6+
#define BITCOIN_QT_TEST_RPCNESTEDTESTS_H
77

88
#include <QObject>
99
#include <QTest>
@@ -19,4 +19,4 @@ class RPCNestedTests : public QObject
1919
void rpcNestedTests();
2020
};
2121

22-
#endif // BITCOIN_QT_TEST_RPC_NESTED_TESTS_H
22+
#endif // BITCOIN_QT_TEST_RPCNESTEDTESTS_H

0 commit comments

Comments
 (0)