Skip to content
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

ci: Integrate bitcoin-tidy clang-tidy plugin #26296

Merged
merged 5 commits into from
Aug 7, 2023

Conversation

fanquake
Copy link
Member

@fanquake fanquake commented Oct 12, 2022

Demo of integrating the bitcoin-tidy, clang-tidy plugin written by theuni into our tidy CI job.

The plugin currently has a single check, bitcoin-unterminated-logprintf. This would replace our current Python driven, git-grep-based, .cpp file only, lint-logs linter.

@DrahtBot
Copy link
Contributor

DrahtBot commented Oct 12, 2022

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK theuni, MarcoFalke, TheCharlatan
Concept ACK dergoegge
Approach ACK stickies-v

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #28087 (ci: Use qemu-user through container engine by MarcoFalke)
  • #27976 (ci: Start with clean env by MarcoFalke)
  • #24230 (indexes: Stop using node internal types and locking cs_main, improve sync logic by ryanofsky)

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.

@maflcko
Copy link
Member

maflcko commented Oct 12, 2022

If this requires clang-15, I'd prefer to wait until that is backported to Ubuntu Jammy instead of hopping the short-term releases.

@fanquake
Copy link
Member Author

If this requires clang-15, I'd prefer to wait until that is backported to Ubuntu Jammy instead of hopping the short-term releases.

This doesn't actually require LLVM 15, it's just that the run-clang-tidy script shipped with LLVM 15 has support for passing -load arguments through to clang-tidy.

I've dropped the Kinetic / LLVM 15 bump, in favour of a different approach; patching -load support into the llvm 14 run-clang-tidy script.

@theuni
Copy link
Member

theuni commented Oct 24, 2022

Concept ACK (obviously). Thought dump below.

A few things worth considering when it comes to these plugins:

  • They do exactly what you tell them to do - no more and no less.
  • Tests are really important
  • We may want different checks for certain parts of the code.

Elaborating:

c++ is complicated, so defining compiler rules is also complicated. Taking this code for a trivial example:

struct A{};
void func()
{
    A foo;
    const A bar;
}

In testing, one may find that the following query works to find all declarations of type A:
varDecl(hasType(asString("struct A")))
However, this will find only the non-const-qualified foo and not bar.

Instead, we'd want something more like: varDecl(hasType(cxxRecordDecl(hasName("A"))))

I'm mentioning this to try to demonstrate what I meant by "they do exactly what you tell them to". It's quite common (especially when just getting started) to end up with an overly-broad or unnecessarily specific query. The first query would appear to be fine if a const A is not used anywhere. The best defense against this from what I can tell is adding a test for each failing corner-case.

So I think we'll likely want some pretty thorough documentation that states exactly what the check is intended to do. Additionally, we'll want tests that verify those intentions. In the case of the plugin here, I have added some loose checks here but nothing automated: https://github.com/theuni/bitcoin-tidy/blob/main/test_desig_init.cpp . An example of a corner-case that wasn't caught at first with this check is included here: nested structs with uninitialized members.

tl;dr: before merge, I think we'll want to add a mini test suite to the plugin repo that allows us to verify that checks are working as defined/intended. Additionally, there will need to be some understanding that checks will likely require refinement over time as new corner cases are exposed.

  • We may want different checks for certain parts of the code.

This isn't important for now, but I suspect that the infrastructure for this will end up being more complicated eventually. The obvious outlier (imo) is libbitcoinkernel, where we may want to add more strict checks that other parts of the code (bitcoin-qt for ex) aren't required to abide by.

@dergoegge
Copy link
Member

Concept ACK

@maflcko
Copy link
Member

maflcko commented Mar 21, 2023

Would be good to explain how this is different from https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/pro-type-member-init.html .

Edit: I guess one is for constructors, the other is for aggregates, so they are orthogonal, and go hand-in-hand.

See also #26762 (comment)

@fanquake
Copy link
Member Author

Would be good to explain how this is different from https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/pro-type-member-init.html .

Last I checked, turning that on was essentially unusable, due to false-positives. Will take another look.

@maflcko
Copy link
Member

maflcko commented Mar 21, 2023

It should only hit on classes that have a SetNull method, no? In those cases the SetNull method can be removed, or the initializers duplicated from the method?

@hebasto
Copy link
Member

hebasto commented Mar 21, 2023

Last I checked, turning that on was essentially unusable, due to false-positives.

Indeed.

fanquake and others added 3 commits August 3, 2023 17:52
Enable `bitcoin-unterminated-logprintf`.

Co-authored-by: Cory Fields <cory-nospam-@coryfields.com>
@fanquake
Copy link
Member Author

fanquake commented Aug 3, 2023

Dropped the test commit back out.

Copy link
Member

@theuni theuni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 1c976c6

@DrahtBot DrahtBot requested a review from maflcko August 3, 2023 18:03
Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-ACK 1c976c6 👠

Show signature

Signature:

untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
trusted comment: re-ACK 1c976c691cc4b20f43071aabf36c7afed1571057  👠
AUAtgJBmTgwGLADs8IgqfpOQg2nQfQ2+D/7P8lwnKkrIGJwTNAjXOYnonfN9oAT1L5+CTeUJzKaPpGHVi5t7Cw==

@DrahtBot DrahtBot removed the CI failed label Aug 4, 2023
@TheCharlatan
Copy link
Contributor

ACK 1c976c6

@maflcko
Copy link
Member

maflcko commented Aug 7, 2023

rfm or is anything left to be done here for this test-only pull?

@fanquake fanquake marked this pull request as ready for review August 7, 2023 15:06
@fanquake
Copy link
Member Author

fanquake commented Aug 7, 2023

is anything left to be done here

I missed that Cory had already ACK'd, and was waiting for him to re-ACK. Given that's already happened, I don't think there's anything left here, and docs can be fixed in the followup.

@fanquake fanquake merged commit 6243334 into bitcoin:master Aug 7, 2023
15 checks passed
@fanquake fanquake deleted the integrate_cory_tidy branch August 7, 2023 15:15
Example Usage:

```bash
cmake -S . -B build -DLLVM_DIR=/path/to/lib/cmake/llvm -DCMAKE_BUILD_TYPE=Release
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also add an example for common systems, Ubuntu/Debian-based ones, and Fedora/CentOS-based ones, what DLLVM_DIR should be for them?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #28245.

cxxMemberCallExpr(
thisPointerType(qualType(hasDeclaration(cxxRecordDecl(hasName("CWallet"))))),
callee(cxxMethodDecl(hasName("WalletLogPrintf"))),
hasArgument(0, stringLiteral(unterminated()).bind("logstring"))),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. The argument of WalletLogPrintf is never a string literal (the parameter is).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #28237

*/
finder->addMatcher(
cxxMemberCallExpr(
thisPointerType(qualType(hasDeclaration(cxxRecordDecl(hasName("CWallet"))))),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong, too. ScriptPubKeyMan has a log statement, too.

My recommendation would be to just remove this line completely, unless there is a reason to have it. The other lines should be exact enough to match everything that is needed, without under- or over-matching.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, I was inclined to argue with you because I think the tests should be as tight as possible. But realistically, it's more likely for a class to be forgotten in the checks (as has happened here) than a false-positive in some future class. So I begrudgingly agree.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind creating a pull with the outstanding feedback? :)

If not, I'll try to do it next week.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #28237

@theuni
Copy link
Member

theuni commented Aug 9, 2023

Yep, will do. I was just waiting for the others to settle.

sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Aug 9, 2023
fanquake added a commit to bitcoin-core/gui that referenced this pull request Aug 18, 2023
…alletLogPrintf()

fa60fa3 bitcoin-tidy: Apply bitcoin-unterminated-logprintf to spkm as well (MarcoFalke)
faa1143 refactor: Enable all clang-tidy plugin bitcoin tests (MarcoFalke)
fa6dc57 refactor: Enforce C-str fmt strings in WalletLogPrintf() (MarcoFalke)
fa244f3 doc: Fix bitcoin-unterminated-logprintf tidy comments (MarcoFalke)

Pull request description:

  All fmt functions only accept a raw C-string as argument.

  There should never be a need to pass a format string that is not a compile-time string literal, so disallow it in `WalletLogPrintf()` to avoid accidentally introducing it.

  Apart from consistency, this also fixes the clang-tidy plugin bug bitcoin/bitcoin#26296 (comment).

ACKs for top commit:
  theuni:
    ACK fa60fa3

Tree-SHA512: fa6f4984c50f9b34e850bdfee7236706af586e512d866cc869cf0cdfaf9aa707029c210ca72d91f85e75fcbd8efe0d77084701de8c3d2004abfd7e46b6fa9072
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Oct 24, 2024
fa67b81 Refactor: Remove unused FlatFilePos::SetNull (MarcoFalke)

Pull request description:

  This is unused outside of tests and the default constructor. With C++11, it can be replaced by C++11 member initializers in the default constructor.

  Beside removing unused code, this also makes it less fragile in light of uninitialized memory. (See also bitcoin#26296 (comment))

  If new code needs to set this to null, it can use `std::optional`, or in the worst case re-introduce this method.

ACKs for top commit:
  dergoegge:
    Code review ACK fa67b81
  TheCharlatan:
    ACK fa67b81
  john-moffett:
    ACK fa67b81

Tree-SHA512: 465c5e3eb4625405c445695d33e09a1fc5185c7dd1e766ba06034fb093880bfc65441d5334f7d9b20e2e417c2075557d86059f59d9648ca0e62a54c699c029b9
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Oct 24, 2024
fa67b81 Refactor: Remove unused FlatFilePos::SetNull (MarcoFalke)

Pull request description:

  This is unused outside of tests and the default constructor. With C++11, it can be replaced by C++11 member initializers in the default constructor.

  Beside removing unused code, this also makes it less fragile in light of uninitialized memory. (See also bitcoin#26296 (comment))

  If new code needs to set this to null, it can use `std::optional`, or in the worst case re-introduce this method.

ACKs for top commit:
  dergoegge:
    Code review ACK fa67b81
  TheCharlatan:
    ACK fa67b81
  john-moffett:
    ACK fa67b81

Tree-SHA512: 465c5e3eb4625405c445695d33e09a1fc5185c7dd1e766ba06034fb093880bfc65441d5334f7d9b20e2e417c2075557d86059f59d9648ca0e62a54c699c029b9
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Oct 24, 2024
fa67b81 Refactor: Remove unused FlatFilePos::SetNull (MarcoFalke)

Pull request description:

  This is unused outside of tests and the default constructor. With C++11, it can be replaced by C++11 member initializers in the default constructor.

  Beside removing unused code, this also makes it less fragile in light of uninitialized memory. (See also bitcoin#26296 (comment))

  If new code needs to set this to null, it can use `std::optional`, or in the worst case re-introduce this method.

ACKs for top commit:
  dergoegge:
    Code review ACK fa67b81
  TheCharlatan:
    ACK fa67b81
  john-moffett:
    ACK fa67b81

Tree-SHA512: 465c5e3eb4625405c445695d33e09a1fc5185c7dd1e766ba06034fb093880bfc65441d5334f7d9b20e2e417c2075557d86059f59d9648ca0e62a54c699c029b9
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Oct 24, 2024
fa67b81 Refactor: Remove unused FlatFilePos::SetNull (MarcoFalke)

Pull request description:

  This is unused outside of tests and the default constructor. With C++11, it can be replaced by C++11 member initializers in the default constructor.

  Beside removing unused code, this also makes it less fragile in light of uninitialized memory. (See also bitcoin#26296 (comment))

  If new code needs to set this to null, it can use `std::optional`, or in the worst case re-introduce this method.

ACKs for top commit:
  dergoegge:
    Code review ACK fa67b81
  TheCharlatan:
    ACK fa67b81
  john-moffett:
    ACK fa67b81

Tree-SHA512: 465c5e3eb4625405c445695d33e09a1fc5185c7dd1e766ba06034fb093880bfc65441d5334f7d9b20e2e417c2075557d86059f59d9648ca0e62a54c699c029b9
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Oct 24, 2024
fa67b81 Refactor: Remove unused FlatFilePos::SetNull (MarcoFalke)

Pull request description:

  This is unused outside of tests and the default constructor. With C++11, it can be replaced by C++11 member initializers in the default constructor.

  Beside removing unused code, this also makes it less fragile in light of uninitialized memory. (See also bitcoin#26296 (comment))

  If new code needs to set this to null, it can use `std::optional`, or in the worst case re-introduce this method.

ACKs for top commit:
  dergoegge:
    Code review ACK fa67b81
  TheCharlatan:
    ACK fa67b81
  john-moffett:
    ACK fa67b81

Tree-SHA512: 465c5e3eb4625405c445695d33e09a1fc5185c7dd1e766ba06034fb093880bfc65441d5334f7d9b20e2e417c2075557d86059f59d9648ca0e62a54c699c029b9
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Oct 24, 2024
fa67b81 Refactor: Remove unused FlatFilePos::SetNull (MarcoFalke)

Pull request description:

  This is unused outside of tests and the default constructor. With C++11, it can be replaced by C++11 member initializers in the default constructor.

  Beside removing unused code, this also makes it less fragile in light of uninitialized memory. (See also bitcoin#26296 (comment))

  If new code needs to set this to null, it can use `std::optional`, or in the worst case re-introduce this method.

ACKs for top commit:
  dergoegge:
    Code review ACK fa67b81
  TheCharlatan:
    ACK fa67b81
  john-moffett:
    ACK fa67b81

Tree-SHA512: 465c5e3eb4625405c445695d33e09a1fc5185c7dd1e766ba06034fb093880bfc65441d5334f7d9b20e2e417c2075557d86059f59d9648ca0e62a54c699c029b9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants