lint: Check for missing bitcoin-config.h includes #29408

pull maflcko wants to merge 5 commits into bitcoin:master from maflcko:2402-lint-missing-includes- changing 4 files +126 −12
  1. maflcko commented at 12:56 pm on February 8, 2024: member

    Missing bitcoin-config.h includes are problematic, because the build could silently pass, but produce an unintended result. For example, a slower fallback algorithm could be picked, even though bitcoin-config.h indicates that a faster feature is available and should be used.

    As the build succeeds silently, this problem is not possible to detect with iwyu.

    Thus, fix this by using a linter based on grepping the source code.

  2. DrahtBot commented at 12:56 pm on February 8, 2024: contributor

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

    Code Coverage

    For detailed information about the code coverage, see the test coverage report.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK theuni, TheCharlatan, hebasto
    Concept ACK epiccurious

    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:

    • #29119 (refactor: Use std::span over Span by maflcko)

    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.

  3. DrahtBot renamed this:
    lint: Check for missing bitcoin-config.h includes
    lint: Check for missing bitcoin-config.h includes
    on Feb 8, 2024
  4. DrahtBot added the label Tests on Feb 8, 2024
  5. maflcko marked this as a draft on Feb 8, 2024
  6. maflcko commented at 12:58 pm on February 8, 2024: member
  7. DrahtBot added the label CI failed on Feb 8, 2024
  8. maflcko force-pushed on Feb 8, 2024
  9. maflcko force-pushed on Feb 8, 2024
  10. theuni commented at 5:07 pm on February 8, 2024: member

    Concept ACK.

    The linter output matches my list, with the additions of:

    0src/crypto/sha256_arm_shani.cpp
    1src/crypto/sha256_avx2.cpp
    2src/crypto/sha256_sse41.cpp
    3src/crypto/sha256_x86_shani.cpp
    4src/interfaces/wallet.h
    

    The crypto defines actually come from our Makefile. Those have always been wonky, maybe now is a good time to do something about those.

    src/interfaces/wallet.h however is a false-positive :(

  11. maflcko commented at 5:57 pm on February 8, 2024: member

    src/interfaces/wallet.h however is a false-positive :(

    Yeah, not sure how to solve this. Maybe rewrite everything as a clang-tidy plugin?

  12. in test/lint/test_runner/src/main.rs:106 in f4db3546c0 outdated
    101+        assert!(Command::new("./autogen.sh")
    102+            .status()
    103+            .expect("command error")
    104+            .success());
    105+    }
    106+    let defines_regex = check_output(Command::new("grep").args(["undef ", "--", config_path]))
    


    TheCharlatan commented at 6:29 pm on February 8, 2024:

    How about this to at least filter out the easy commented lines?

     0diff --git a/test/lint/test_runner/src/main.rs b/test/lint/test_runner/src/main.rs
     1index d9950c3fba..fa5ac0a597 100644
     2--- a/test/lint/test_runner/src/main.rs
     3+++ b/test/lint/test_runner/src/main.rs
     4@@ -106,10 +106,16 @@ fn lint_includes_build_config() -> LintResult {
     5-    let defines_regex = check_output(Command::new("grep").args(["undef ", "--", config_path]))
     6-        .expect("grep failed")
     7-        .lines()
     8-        .map(|line| {
     9-            line.split("undef ")
    10-                .nth(1)
    11-                .expect(&format!("Could not extract name in line: {line}"))
    12-        })
    13-        .collect::<Vec<_>>()
    14-        .join("|");
    15+    let defines_regex = format!(
    16+        "^(?!//).*({})",
    17+        check_output(Command::new("grep").args(["undef ", "--", config_path]))
    18+            .expect("grep failed")
    19+            .lines()
    20+            .map(|line| {
    21+                line.split("undef ")
    22+                    .nth(1)
    23+                    .expect(&format!("Could not extract name in line: {line}"))
    24+            })
    25+            .collect::<Vec<_>>()
    26+            .join("|")
    27+    );
    28+
    29+    println!("{}", defines_regex);
    30+
    31@@ -120 +126 @@ fn lint_includes_build_config() -> LintResult {
    32-                "--extended-regexp",
    33+                "--perl-regexp",
    

    theuni commented at 8:20 pm on February 8, 2024:

    I think this is a reasonable enough escape hatch. Given that we only have 1 currently, requiring a // FOO rather than a /* FOO */ seems ok to me.

    I don’t see how we could do it as a clang-tidy plugin unfortunately. Its preprocessor stuff kinda sucks, and I believe we’d also have to keep a hard-coded list because any actual bitcoin-config.h would be guaranteed to leave some things undef’d. Hmm, I suppose the plugin itself could read the .h.in for each invocation. That’s pretty ugly though :(


    maflcko commented at 9:47 am on February 9, 2024:
    Nice, done. TIL about regex lookahead assertions.
  13. epiccurious commented at 3:41 am on February 9, 2024: none
    Concept ACK f4db3546c003822e485f1597701f278eef197619.
  14. maflcko force-pushed on Feb 9, 2024
  15. maflcko force-pushed on Feb 12, 2024
  16. theuni commented at 8:18 pm on February 15, 2024: member

    What’s the status on this? Just waiting on a change for the crypto defines?

    It’d be nice to get it in with/soonafter #29404.

  17. maflcko force-pushed on Feb 16, 2024
  18. maflcko commented at 8:09 am on February 16, 2024: member

    Ok, this (The signed, previous to last commit, fa9c71aa914c683a6f4ee6aa91b8673e8eefd2e5) is ready for review.

    I’ll drop the last commit (unsigned merge commit) once and if it is merged.

  19. DrahtBot removed the label CI failed on Feb 16, 2024
  20. hebasto commented at 12:24 pm on February 16, 2024: member

    Tested 48562917172accd911e4c86f1032e4282fd321f5 on Ubuntu 23.10.

    Missing #include <config/bitcoin-config.h> are reported correctly.

    What are we supposed to do to automatically avoid redundant / unneeded includes?

  21. maflcko commented at 12:48 pm on February 16, 2024: member

    What are we supposed to do to automatically avoid redundant / unneeded includes?

    Iwyu is a tool to detect redundant and unneeded includes, so it can be used. However, there may be false warnings in regard to this specific header. Thus, an iwyu pragma can be used to avoid incorrect removal of needed includes. Should I push a commit here?

  22. hebasto commented at 2:14 pm on February 16, 2024: member

    What are we supposed to do to automatically avoid redundant / unneeded includes?

    Iwyu is a tool to detect redundant and unneeded includes, so it can be used. However, there may be false warnings in regard to this specific header. Thus, an iwyu pragma can be used to avoid incorrect removal of needed includes. Should I push a commit here?

    I assume that both macros HAVE_TIMINGSAFE_BCMP and HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR are commented out in config/bitcoin-config.h in the CI environment.

    I’m pretty sure that the same happens to the CHAR_EQUALS_INT8 macro as well. However, IWYU does not suggest to drop #include <config/bitcoin-config.h> from the serialize.h. That seems fragile as IWYU behavior might change in the future..

    I suggest to add // IWYU pragma: keep everywhere and put this rule to the Developer Notes.

  23. hebasto commented at 2:20 pm on February 16, 2024: member

    https://api.cirrus-ci.com/v1/task/5330278671974400/logs/ci.log:

    0crypto/sha256.cpp should remove these lines:
    1- #include <config/bitcoin-config.h>  // lines 6-6
    

    More // IWYU pragma: keep?

  24. theuni commented at 5:35 pm on February 16, 2024: member

    It’s not clear to me what the benefit of using IWYU is here. The missing defines will (by definition) vary per-platform, so I’m not sure what we’re chasing here exactly.

    Or is it more-so just to keep IWYU usable without warnings for our c-i configs specifically?

  25. maflcko commented at 11:05 am on February 17, 2024: member
    Yeah, good point. So I guess the only way to remove unused includes would be to write another check in the linter. I’ll drop the last commit and leave everything else for a follow-up.
  26. maflcko force-pushed on Feb 17, 2024
  27. DrahtBot added the label CI failed on Feb 17, 2024
  28. DrahtBot commented at 12:47 pm on February 17, 2024: contributor

    🚧 At least one of the CI tasks failed. Make sure to run all tests locally, according to the documentation.

    Possibly this is due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch.

    Leave a comment here, if you need help tracking down a confusing failure.

    Debug: https://github.com/bitcoin/bitcoin/runs/21683771354

  29. hebasto commented at 2:59 pm on February 19, 2024: member

    Or is it more-so just to keep IWYU usable without warnings for our c-i configs specifically?

    Yes. At some point in the future we might want to treat IWYU warnings as errors.

  30. maflcko force-pushed on Feb 19, 2024
  31. maflcko force-pushed on Feb 19, 2024
  32. maflcko marked this as ready for review on Feb 20, 2024
  33. lint: Add get_subtrees() helper
    This is needed for a future change.
    fa10051267
  34. doc: Add missing RUST_BACKTRACE=1
    This will print a backtrace when an internal code error happened.
    fa770fd368
  35. maflcko commented at 2:02 pm on February 20, 2024: member
    Added the check to error on redundant (harmless) includes as well, since it was trivial. Should be trivial to re-ACK.
  36. lint: Make lint error easier to spot in output fa63b0e351
  37. lint: Check for missing or redundant bitcoin-config.h includes fa31908ea8
  38. maflcko force-pushed on Feb 20, 2024
  39. maflcko commented at 2:05 pm on February 20, 2024: member
    Rebased, for silent merge conflict in src/bench/wallet_ismine.cpp. I guess this immediately shows that the check is useful :)
  40. refactor: Add missing include for USE_BDB, USE_SQLITE to bench/wallet_ismine.cpp
    It was included indirectly via src/wallet/test/util.h, however it is
    better to include what you use.
    fa58ae74ea
  41. maflcko requested review from hebasto on Feb 20, 2024
  42. DrahtBot removed the label CI failed on Feb 20, 2024
  43. theuni approved
  44. theuni commented at 6:07 pm on February 20, 2024: member

    Weak ACK fa58ae74ea67485495dbc2d003adfbca68d6869b.

    My python-fu is weak, but I think this is a necessary check and it seems to work as advertised.

  45. TheCharlatan approved
  46. TheCharlatan commented at 10:31 am on February 22, 2024: contributor
    ACK fa58ae74ea67485495dbc2d003adfbca68d6869b
  47. maflcko added this to the milestone 27.0 on Feb 22, 2024
  48. maflcko commented at 11:25 am on February 22, 2024: member
    Added milestone, since this test can catch bugs, and it was reviewed.
  49. hebasto approved
  50. hebasto commented at 4:30 pm on February 22, 2024: member
    ACK fa58ae74ea67485495dbc2d003adfbca68d6869b, tested on Ubuntu 23.10 – it catches bugs properly. I didn’t review rust code changes.
  51. fanquake merged this on Feb 26, 2024
  52. fanquake closed this on Feb 26, 2024

  53. maflcko deleted the branch on Feb 26, 2024

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-07-01 10:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me