Incorrect named args are source of bugs, like #22979.
Fix that by correcting them and adjust the format, so that clang-tidy can check it.
Incorrect named args are source of bugs, like #22979.
Fix that by correcting them and adjust the format, so that clang-tidy can check it.
Setup clang-tidy:
apt install clang-tidy bear clang
./autogen.sh && ./configure --with-incompatible-bdb CC=clang CXX=clang++
make clean && bear make -j $(nproc) # For bear 2.x
make clean && bear -- make -j $(nproc) # For bear 3.x
Full test:
( cd ./src/ && run-clang-tidy -j $(nproc) )
Diff test:
git diff | ( cd ./src/ && clang-tidy-diff -p2 -j $(nproc) )
Concept ACK, being able to check these automatically with a tool is a good idea.
<!--e57a25ab6845829454e8d69fc972939a-->
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
<!--174a7506f384e20aa4161008e828411d-->
Reviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.
69 | @@ -70,7 +70,7 @@ int main(int argc, char* argv[]) 70 | #if defined(WIN32) 71 | if (getenv("QT_QPA_PLATFORM") == nullptr) _putenv_s("QT_QPA_PLATFORM", "minimal"); 72 | #else 73 | - setenv("QT_QPA_PLATFORM", "minimal", /* overwrite */ 0); 74 | + setenv("QT_QPA_PLATFORM", "minimal", 0 /* overwrite */);
setenv("QT_QPA_PLATFORM", "minimal", /* overwrite */0);
I assume this isn't updated to /*overwrite=*/ because that will fail the check:
/bitcoin/src/qt/test/test_main.cpp:73:46: error: argument name 'overwrite' in comment does not match parameter name '__replace' [bugprone-argument-comment,-warnings-as-errors]
setenv("QT_QPA_PLATFORM", "minimal", /*overwrite=*/0);
^
/usr/include/stdlib.h:653:65: note: '__replace' declared here
extern int setenv (const char *__name, const char *__value, int __replace)
If that's the case, why move the argument to before the comment, clang-tidy still works when it's trailing.
I think for consistency named args that are not enforced should be trailing. If it was kept in the beginning, it could confuse devs and trick them into adding an =, which again would fail clang-tidy. See also the comment in src/test/fuzz/tx_pool.cpp, which I moved to the end.
maybe /* don't overwrite */ ? if it doesn't look like an argument name, any confusion is gone
It is the correct argument name according to man, but the headers shipped on the system may pick a different name (implementation detail).
$ man setenv | head -9
SETENV(3) Linux Programmer's Manual SETENV(3)
NAME
setenv - change or add an environment variable
SYNOPSIS
#include <stdlib.h>
int setenv(const char *name, const char *value, int overwrite);
Concept ACK - Tested using the steps below. If we are going to start making more of these changes we should be integrating this into the CI at some point soon.
docker run -it --rm ubuntu:hirsute
apt update && apt upgrade
apt install git build-essential libtool autotools-dev automake pkg-config bsdmainutils python3 clang-12 clang-tidy-12 bear libevent-dev libboost-dev libboost-system-dev libboost-filesystem-dev libboost-test-dev libdb-dev libdb++-dev
clang-12 --version
Ubuntu clang version 12.0.0-3ubuntu1~21.04.2
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
clang-tidy-12 --version
LLVM (http://llvm.org/):
LLVM version 12.0.0
Optimized build.
Default target: x86_64-pc-linux-gnu
Host CPU: skylake-avx512
bear --version
bear 3.0.8
git clone https://github.com/bitcoin/bitcoin
cd bitcoin
./autogen
./configure --with-incompatible-bdb CC=clang-12 CXX=clang++-12
make clean && bear -- make -j $(nproc)
( cd ./src/ && run-clang-tidy-12 -j $(nproc) )
Enabled checks:
bugprone-argument-comment
clang-tidy-12 --use-color -p=/bitcoin /bitcoin/src/blockfilter.cpp
clang-tidy-12 --use-color -p=/bitcoin /bitcoin/src/chain.cpp
clang-tidy-12 --use-color -p=/bitcoin /bitcoin/src/banman.cpp
<snipp>
git diff | ( cd ./src/ && clang-tidy-diff-12.py -p2 -j $(nproc) )
No relevant changes found.
we should be integrating this into the CI at some point soon.
Happy to review a pull doing this. Though, I think it is also acceptable to run it only once before every major release.
11 | @@ -12,7 +12,7 @@ 12 | static void AddTx(const CTransactionRef& tx, const CAmount& fee, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs) 13 | { 14 | LockPoints lp; 15 | - pool.addUnchecked(CTxMemPoolEntry(tx, fee, /* time */ 0, /* height */ 1, /* spendsCoinbase */ false, /* sigOpCost */ 4, lp)); 16 | + pool.addUnchecked(CTxMemPoolEntry(tx, fee, /*_nTime=*/0, /*_entryHeight=*/1, /*spendsCoinbase=*/false, /*nSigOpsCost=*/4, lp));
It's unfortunate in these cases that these argument names are so ugly (e.g. _nTime doesn't just have an underscore, but also the long deprecated syntax)
Happy to review a follow-up to fixup the name of the args, or should I do it here?
I don't know. It does make me reconsider whether automatically checking this is a good idea.
Not checking them makes it harder to catch bugs, like #22979. So I think this is overall a good idea.
Fixed in #23054
222 | @@ -223,7 +223,7 @@ void UpdatePSBTOutput(const SigningProvider& provider, PartiallySignedTransactio 223 | // Construct a would-be spend of this output, to update sigdata with. 224 | // Note that ProduceSignature is used to fill in metadata (not actual signatures), 225 | // so provider does not need to provide any private keys (it can be a HidingSigningProvider). 226 | - MutableTransactionSignatureCreator creator(&tx, /* index */ 0, out.nValue, SIGHASH_ALL); 227 | + MutableTransactionSignatureCreator creator(&tx, /*nInIn=*/0, out.nValue, SIGHASH_ALL);
Same here. I'm not sure this is an improvement in readability. What is a nInIn? Might as well strip the comment out.
nInIn is used as comment elsewhere, so this is a pre-existing issue. I think every literal numeric type passed to a function should have a name.
I can offer to rename nInIn here or in a follow-up.
Done here in a new commit
I think we should first make sure our argument names make sense, then do this.
Concept ACK
Concept ACK. Should we document the required comment style somewhere in the developer docs?
Happy to document the style in a follow-up. I have one follow-up myself that adjusts the style of existing args. The goal of this pull is to replace wrong names with correct names.
tACK fa7efa0
The commands described in #22981 (comment) ran successfully.
And after clang-tidy-10 returned without an error, I changed a commented named argument in a function, reran it, and the error was identified.
Should compile_commands.json be added to .gitignore ?
Should compile_commands.json be added to .gitignore ?
Yeah, will do on the next force push or the next follow-up pull, unless someone beats me to it.
Concept ACK.
/usr/local/opt/llvm/bin/clang-tidy --use-color -p=/Users/michael/github/bitcoin-merge-tree /Users/michael/github/bitcoin-merge-tree/src/psbt.cpp
psbt.cpp:226:53: error: argument name 'input_idx' in comment does not match parameter name 'nInIn' [bugprone-argument-comment,-warnings-as-errors]
MutableTransactionSignatureCreator creator(&tx, /*input_idx=*/0, out.nValue, SIGHASH_ALL);
^
./script/sign.h:48:88: note: 'nInIn' declared here
MutableTransactionSignatureCreator(const CMutableTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, int nHashTypeIn);
^
1 warning generated.
1 warning treated as error
Good catch. Fixed in https://github.com/bitcoin/bitcoin/pull/23525
Rebased to get rid of already fixed instances. Only 29 left in this pull for now.
ACK fac49470ca36ff944a613f4358386bf8e0967427 - run-clang-tidy works for me now.