/ test / lint / test_runner / src / lint_cpp.rs
lint_cpp.rs
  1  // Copyright (c) The Bitcoin Core developers
  2  // Distributed under the MIT software license, see the accompanying
  3  // file COPYING or https://opensource.org/license/mit/.
  4  
  5  use std::process::Command;
  6  
  7  use crate::util::{check_output, get_pathspecs_default_excludes, git, LintResult};
  8  
  9  pub fn lint_includes_build_config() -> LintResult {
 10      let config_path = "./cmake/bitcoin-build-config.h.in";
 11      let defines_regex = format!(
 12          r"^\s*(?!//).*({})",
 13          check_output(Command::new("grep").args(["define", "--", config_path]))
 14              .expect("grep failed")
 15              .lines()
 16              .map(|line| {
 17                  line.split_whitespace()
 18                      .nth(1)
 19                      .unwrap_or_else(|| panic!("Could not extract name in line: {line}"))
 20              })
 21              .collect::<Vec<_>>()
 22              .join("|")
 23      );
 24      let print_affected_files = |mode: bool| {
 25          // * mode==true: Print files which use the define, but lack the include
 26          // * mode==false: Print files which lack the define, but use the include
 27          let defines_files = check_output(
 28              git()
 29                  .args([
 30                      "grep",
 31                      "--perl-regexp",
 32                      if mode {
 33                          "--files-with-matches"
 34                      } else {
 35                          "--files-without-match"
 36                      },
 37                      &defines_regex,
 38                      "--",
 39                      "*.cpp",
 40                      "*.h",
 41                  ])
 42                  .args(get_pathspecs_default_excludes()),
 43          )
 44          .expect("grep failed");
 45          git()
 46              .args([
 47                  "grep",
 48                  if mode {
 49                      "--files-without-match"
 50                  } else {
 51                      "--files-with-matches"
 52                  },
 53                  if mode {
 54                      "^#include <bitcoin-build-config.h> // IWYU pragma: keep$"
 55                  } else {
 56                      "#include <bitcoin-build-config.h>" // Catch redundant includes with and without the IWYU pragma
 57                  },
 58                  "--",
 59              ])
 60              .args(defines_files.lines())
 61              .status()
 62              .expect("command error")
 63              .success()
 64      };
 65      let missing = print_affected_files(true);
 66      if missing {
 67          return Err(format!(
 68              r#"
 69  One or more files use a symbol declared in the bitcoin-build-config.h header. However, they are not
 70  including the header. This is problematic, because the header may or may not be indirectly
 71  included. If the indirect include were to be intentionally or accidentally removed, the build could
 72  still succeed, but silently be buggy. For example, a slower fallback algorithm could be picked,
 73  even though bitcoin-build-config.h indicates that a faster feature is available and should be used.
 74  
 75  If you are unsure which symbol is used, you can find it with this command:
 76  git grep --perl-regexp '{defines_regex}' -- file_name
 77  
 78  Make sure to include it with the IWYU pragma. Otherwise, IWYU may falsely instruct to remove the
 79  include again.
 80  
 81  #include <bitcoin-build-config.h> // IWYU pragma: keep
 82              "#
 83          )
 84          .trim()
 85          .to_string());
 86      }
 87      let redundant = print_affected_files(false);
 88      if redundant {
 89          return Err(r#"
 90  None of the files use a symbol declared in the bitcoin-build-config.h header. However, they are including
 91  the header. Consider removing the unused include.
 92              "#
 93          .to_string());
 94      }
 95      Ok(())
 96  }
 97  
 98  pub fn lint_std_filesystem() -> LintResult {
 99      let found = git()
100          .args([
101              "grep",
102              "--line-number",
103              "std::filesystem",
104              "--",
105              "./src/",
106              ":(exclude)src/ipc/libmultiprocess/",
107              ":(exclude)src/util/fs.h",
108              ":(exclude)src/bitcoin-chainstate.cpp",
109          ])
110          .status()
111          .expect("command error")
112          .success();
113      if found {
114          Err(r#"
115  Direct use of std::filesystem may be dangerous and buggy. Please include <util/fs.h> and use the
116  fs:: namespace, which has unsafe filesystem functions marked as deleted.
117              "#
118          .trim()
119          .to_string())
120      } else {
121          Ok(())
122      }
123  }
124  
125  pub fn lint_rpc_assert() -> LintResult {
126      let found = git()
127          .args([
128              "grep",
129              "--line-number",
130              "--extended-regexp",
131              r"\<(A|a)ss(ume|ert)\(",
132              "--",
133              "src/rpc/",
134              "src/wallet/rpc*",
135              ":(exclude)src/rpc/server.cpp",
136              // src/rpc/server.cpp is excluded from this check since it's mostly meta-code.
137          ])
138          .status()
139          .expect("command error")
140          .success();
141      if found {
142          Err(r#"
143  CHECK_NONFATAL(condition) or NONFATAL_UNREACHABLE should be used instead of assert for RPC code.
144  
145  Aborting the whole process is undesirable for RPC code. So nonfatal
146  checks should be used over assert. See: src/util/check.h
147              "#
148          .trim()
149          .to_string())
150      } else {
151          Ok(())
152      }
153  }
154  
155  pub fn lint_boost_assert() -> LintResult {
156      let found = git()
157          .args([
158              "grep",
159              "--line-number",
160              "--extended-regexp",
161              r"BOOST_ASSERT\(",
162              "--",
163              "*.cpp",
164              "*.h",
165          ])
166          .status()
167          .expect("command error")
168          .success();
169      if found {
170          Err(r#"
171  BOOST_ASSERT must be replaced with Assert, BOOST_REQUIRE, or BOOST_CHECK to avoid an unnecessary
172  include of the boost/assert.hpp dependency.
173              "#
174          .trim()
175          .to_string())
176      } else {
177          Ok(())
178      }
179  }