Skip to content

Commit 345f42a

Browse files
committed
Merge #14505: test: Add linter to make sure single parameter constructors are marked explicit
c4606b8 Add Travis check for single parameter constructors not marked "explicit" (practicalswift) Pull request description: Make single parameter constructors `explicit` (C++11). Rationale from the developer notes: > - By default, declare single-argument constructors `explicit`. > - *Rationale*: This is a precaution to avoid unintended conversions that might > arise when single-argument constructors are used as implicit conversion > functions. ACKs for top commit: laanwj: ACK c4606b8 Tree-SHA512: 3e6fd51935fd93b2604b2188664692973d0897469f814cd745b5147d71b99ea5d73c1081cfde9f6393f51f56969e412fcda35d2d54e938a3235b8d40945f31fd
2 parents 0a6ee97 + c4606b8 commit 345f42a

File tree

8 files changed

+145
-4
lines changed

8 files changed

+145
-4
lines changed

.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ cache:
3737
stages:
3838
- lint
3939
- test
40+
- extended-lint
4041
env:
4142
global:
4243
- MAKEJOBS=-j3
@@ -85,6 +86,19 @@ jobs:
8586
script:
8687
- set -o errexit; source .travis/lint_06_script.sh
8788

89+
- stage: extended-lint
90+
name: 'extended lint [runtime >= 60 seconds]'
91+
env:
92+
cache: false
93+
language: python
94+
python: '3.5'
95+
install:
96+
- set -o errexit; source .travis/extended_lint_04_install.sh
97+
before_script:
98+
- set -o errexit; source .travis/lint_05_before_script.sh
99+
script:
100+
- set -o errexit; source .travis/extended_lint_06_script.sh
101+
88102
- stage: test
89103
name: 'ARM [GOAL: install] [no unit or functional tests]'
90104
env: >-

.travis/extended_lint_04_install.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright (c) 2019 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+
export LC_ALL=C
8+
9+
CPPCHECK_VERSION=1.86
10+
curl -s https://codeload.github.com/danmar/cppcheck/tar.gz/${CPPCHECK_VERSION} | tar -zxf - --directory /tmp/
11+
(cd /tmp/cppcheck-${CPPCHECK_VERSION}/ && make CFGDIR=/tmp/cppcheck-${CPPCHECK_VERSION}/cfg/ > /dev/null)
12+
export PATH="$PATH:/tmp/cppcheck-${CPPCHECK_VERSION}/"

.travis/extended_lint_06_script.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright (c) 2019 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+
export LC_ALL=C
8+
9+
test/lint/extended-lint-all.sh

src/index/blockfilterindex.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct DBHeightKey {
5353
int height;
5454

5555
DBHeightKey() : height(0) {}
56-
DBHeightKey(int height_in) : height(height_in) {}
56+
explicit DBHeightKey(int height_in) : height(height_in) {}
5757

5858
template<typename Stream>
5959
void Serialize(Stream& s) const
@@ -76,7 +76,7 @@ struct DBHeightKey {
7676
struct DBHashKey {
7777
uint256 hash;
7878

79-
DBHashKey(const uint256& hash_in) : hash(hash_in) {}
79+
explicit DBHashKey(const uint256& hash_in) : hash(hash_in) {}
8080

8181
ADD_SERIALIZE_METHODS;
8282

src/interfaces/chain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class NotificationsHandlerImpl : public Handler, CValidationInterface
205205
class RpcHandlerImpl : public Handler
206206
{
207207
public:
208-
RpcHandlerImpl(const CRPCCommand& command) : m_command(command), m_wrapped_command(&command)
208+
explicit RpcHandlerImpl(const CRPCCommand& command) : m_command(command), m_wrapped_command(&command)
209209
{
210210
m_command.actor = [this](const JSONRPCRequest& request, UniValue& result, bool last_handler) {
211211
if (!m_wrapped_command) return false;

src/rpc/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ struct RPCResults {
226226

227227
struct RPCExamples {
228228
const std::string m_examples;
229-
RPCExamples(
229+
explicit RPCExamples(
230230
std::string examples)
231231
: m_examples(std::move(examples))
232232
{

test/lint/extended-lint-all.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright (c) 2019 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+
# This script runs all contrib/devtools/extended-lint-*.sh files, and fails if
8+
# any exit with a non-zero status code.
9+
10+
# This script is intentionally locale dependent by not setting "export LC_ALL=C"
11+
# in order to allow for the executed lint scripts to opt in or opt out of locale
12+
# dependence themselves.
13+
14+
set -u
15+
16+
SCRIPTDIR=$(dirname "${BASH_SOURCE[0]}")
17+
LINTALL=$(basename "${BASH_SOURCE[0]}")
18+
19+
for f in "${SCRIPTDIR}"/extended-lint-*.sh; do
20+
if [ "$(basename "$f")" != "$LINTALL" ]; then
21+
if ! "$f"; then
22+
echo "^---- failure generated from $f"
23+
exit 1
24+
fi
25+
fi
26+
done

test/lint/extended-lint-cppcheck.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright (c) 2019 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+
8+
export LC_ALL=C
9+
10+
ENABLED_CHECKS=(
11+
"Class '.*' has a constructor with 1 argument that is not explicit."
12+
"Struct '.*' has a constructor with 1 argument that is not explicit."
13+
)
14+
15+
IGNORED_WARNINGS=(
16+
"src/arith_uint256.h:.* Class 'arith_uint256' has a constructor with 1 argument that is not explicit."
17+
"src/arith_uint256.h:.* Class 'base_uint < 256 >' has a constructor with 1 argument that is not explicit."
18+
"src/arith_uint256.h:.* Class 'base_uint' has a constructor with 1 argument that is not explicit."
19+
"src/coins.h:.* Class 'CCoinsViewBacked' has a constructor with 1 argument that is not explicit."
20+
"src/coins.h:.* Class 'CCoinsViewCache' has a constructor with 1 argument that is not explicit."
21+
"src/coins.h:.* Class 'CCoinsViewCursor' has a constructor with 1 argument that is not explicit."
22+
"src/net.h:.* Class 'CNetMessage' has a constructor with 1 argument that is not explicit."
23+
"src/policy/feerate.h:.* Class 'CFeeRate' has a constructor with 1 argument that is not explicit."
24+
"src/prevector.h:.* Class 'const_iterator' has a constructor with 1 argument that is not explicit."
25+
"src/prevector.h:.* Class 'const_reverse_iterator' has a constructor with 1 argument that is not explicit."
26+
"src/prevector.h:.* Class 'iterator' has a constructor with 1 argument that is not explicit."
27+
"src/prevector.h:.* Class 'reverse_iterator' has a constructor with 1 argument that is not explicit."
28+
"src/primitives/block.h:.* Class 'CBlock' has a constructor with 1 argument that is not explicit."
29+
"src/primitives/transaction.h:.* Class 'CTransaction' has a constructor with 1 argument that is not explicit."
30+
"src/protocol.h:.* Class 'CMessageHeader' has a constructor with 1 argument that is not explicit."
31+
"src/qt/guiutil.h:.* Class 'ItemDelegate' has a constructor with 1 argument that is not explicit."
32+
"src/rpc/util.h:.* Struct 'RPCResults' has a constructor with 1 argument that is not explicit."
33+
"src/rpc/util.h:.* style: Struct 'UniValueType' has a constructor with 1 argument that is not explicit."
34+
"src/script/descriptor.cpp:.* Class 'AddressDescriptor' has a constructor with 1 argument that is not explicit."
35+
"src/script/descriptor.cpp:.* Class 'ComboDescriptor' has a constructor with 1 argument that is not explicit."
36+
"src/script/descriptor.cpp:.* Class 'ConstPubkeyProvider' has a constructor with 1 argument that is not explicit."
37+
"src/script/descriptor.cpp:.* Class 'PKDescriptor' has a constructor with 1 argument that is not explicit."
38+
"src/script/descriptor.cpp:.* Class 'PKHDescriptor' has a constructor with 1 argument that is not explicit."
39+
"src/script/descriptor.cpp:.* Class 'RawDescriptor' has a constructor with 1 argument that is not explicit."
40+
"src/script/descriptor.cpp:.* Class 'SHDescriptor' has a constructor with 1 argument that is not explicit."
41+
"src/script/descriptor.cpp:.* Class 'WPKHDescriptor' has a constructor with 1 argument that is not explicit."
42+
"src/script/descriptor.cpp:.* Class 'WSHDescriptor' has a constructor with 1 argument that is not explicit."
43+
"src/script/script.h:.* Class 'CScript' has a constructor with 1 argument that is not explicit."
44+
"src/script/standard.h:.* Class 'CScriptID' has a constructor with 1 argument that is not explicit."
45+
"src/support/allocators/secure.h:.* Struct 'secure_allocator < char >' has a constructor with 1 argument that is not explicit."
46+
"src/support/allocators/secure.h:.* Struct 'secure_allocator < RNGState >' has a constructor with 1 argument that is not explicit."
47+
"src/support/allocators/secure.h:.* Struct 'secure_allocator < unsigned char >' has a constructor with 1 argument that is not explicit."
48+
"src/support/allocators/zeroafterfree.h:.* Struct 'zero_after_free_allocator < char >' has a constructor with 1 argument that is not explicit."
49+
"src/test/checkqueue_tests.cpp:.* Struct 'FailingCheck' has a constructor with 1 argument that is not explicit."
50+
"src/test/checkqueue_tests.cpp:.* Struct 'MemoryCheck' has a constructor with 1 argument that is not explicit."
51+
"src/test/checkqueue_tests.cpp:.* Struct 'UniqueCheck' has a constructor with 1 argument that is not explicit."
52+
"src/wallet/db.h:.* Class 'BerkeleyEnvironment' has a constructor with 1 argument that is not explicit."
53+
)
54+
55+
if ! command -v cppcheck > /dev/null; then
56+
echo "Skipping cppcheck linting since cppcheck is not installed. Install by running \"apt install cppcheck\""
57+
exit 0
58+
fi
59+
60+
function join_array {
61+
local IFS="$1"
62+
shift
63+
echo "$*"
64+
}
65+
66+
ENABLED_CHECKS_REGEXP=$(join_array "|" "${ENABLED_CHECKS[@]}")
67+
IGNORED_WARNINGS_REGEXP=$(join_array "|" "${IGNORED_WARNINGS[@]}")
68+
WARNINGS=$(git ls-files -- "*.cpp" "*.h" ":(exclude)src/leveldb/" ":(exclude)src/secp256k1/" ":(exclude)src/univalue/" | \
69+
xargs cppcheck --enable=all -j "$(getconf _NPROCESSORS_ONLN)" --language=c++ --std=c++11 --template=gcc -D__cplusplus -DCLIENT_VERSION_BUILD -DCLIENT_VERSION_IS_RELEASE -DCLIENT_VERSION_MAJOR -DCLIENT_VERSION_MINOR -DCLIENT_VERSION_REVISION -DCOPYRIGHT_YEAR -DDEBUG -DHAVE_WORKING_BOOST_SLEEP_FOR -I src/ -q 2>&1 | sort -u | \
70+
grep -E "${ENABLED_CHECKS_REGEXP}" | \
71+
grep -vE "${IGNORED_WARNINGS_REGEXP}")
72+
if [[ ${WARNINGS} != "" ]]; then
73+
echo "${WARNINGS}"
74+
echo
75+
echo "Advice not applicable in this specific case? Add an exception by updating"
76+
echo "IGNORED_WARNINGS in $0"
77+
# Uncomment to enforce the developer note policy "By default, declare single-argument constructors `explicit`"
78+
# exit 1
79+
fi
80+
exit 0

0 commit comments

Comments
 (0)