Skip to content

Conversation

practicalswift
Copy link
Contributor

@practicalswift practicalswift commented Dec 12, 2017

This enforces parts of the project header include guidelines (added by @sipa in #10575).

Example run:

$ git diff
diff --git a/src/warnings.cpp b/src/warnings.cpp
index c52a1fd..d8994dd 100644
--- a/src/warnings.cpp
+++ b/src/warnings.cpp
@@ -5,6 +5,8 @@

 #include <sync.h>
 #include <clientversion.h>
+#include <string>
 #include <util.h>
 #include <warnings.h>
+#include <util.h>

diff --git a/src/warnings.h b/src/warnings.h
index e8e982c..8d2252e 100644
--- a/src/warnings.h
+++ b/src/warnings.h
@@ -7,6 +7,7 @@
 #define BITCOIN_WARNINGS_H

 #include <stdlib.h>
 #include <string>
+#include <stdlib.h>

 void SetMiscWarning(const std::string& strWarning);
$ contrib/devtools/lint-includes.sh
Duplicate include(s) in src/warnings.h:
#include <stdlib.h>

Include(s) from src/warnings.h duplicated in src/warnings.cpp:
#include <string>

Duplicate include(s) in src/warnings.cpp:
#include <util.h>
$ echo $?
1
$ git checkout .
$ contrib/devtools/lint-includes.sh
$ echo $?
0

@practicalswift practicalswift force-pushed the lint-includes branch 2 times, most recently from c614e4c to b1b9c43 Compare December 13, 2017 09:27
@practicalswift practicalswift changed the title Add Travis check for duplicate/redundant includes Add Travis check for duplicateincludes Dec 13, 2017
@practicalswift practicalswift changed the title Add Travis check for duplicateincludes Add Travis check for duplicate includes Dec 13, 2017
@promag
Copy link
Contributor

promag commented Dec 14, 2017

I just want to point that sometimes the include in the header is unnecessary if a forward declaration is possible. With this check the natural fix is to remove the include from the source.

@practicalswift have you checked Iwyu as referenced in #10575?

@ryanofsky
Copy link
Contributor

ryanofsky commented Dec 14, 2017

have you checked Iwyu as referenced in #10575?

I've used iwyu with bitcoin a few times before, though not since we switched from "" to <> style includes. It makes a lot of changes, so it's best to run it manually and selectively on individual source files, rather than in some automated place like a travis script.

Steps to install:

sudo apt install libclang-dev
cd ~/src
git clone github:include-what-you-use/include-what-you-use.git iwyu
cd iwyu
git checkout clang_3.8
mkdir build && cd build
cmake -DIWYU_LLVM_ROOT_PATH=/usr/lib/llvm-3.8 ..
make
sudo cp -aiv ~/src/iwyu/build/include-what-you-use /usr/lib/llvm-3.8/bin/

Steps to run:

cd ~/src/bitcoin/src
make
rm -v interface/libbitcoin_util_a-chain.o # run iwyu on interface/chain.cpp
make -k CXX="/usr/lib/llvm-3.8/bin/include-what-you-use -std=c++11" 2>&1 | tee /tmp/iwyu
~/src/iwyu/fix_includes.py < /tmp/iwyu

@practicalswift
Copy link
Contributor Author

practicalswift commented Dec 14, 2017

@promag Yes, iwyu is nice but I'm afraid it could be too slow to run as part of a Travis session?

@ryanofsky
Copy link
Contributor

@promag Yes, iwyu is nice but I'm afraid it could be too slow to run as part of a Travis session?

Even if it were fast, output isn't really useful or stable enough to run in an automated way. IWYU is most useful as a manual tool to remove unused includes after you add a new source file or split up a existing one. In any case, as instructions above indicate it is awkward to run, and it IMO would have to be a lot easier to run locally to even consider running it on travis.

@practicalswift
Copy link
Contributor Author

Any chance of getting this reviewed? Let me know if it is a NACK and I'll close :-)

FWIW, these types of duplicate includes have been recurring and this check is very quick so the costs of introducing it is hopefully near zero :-)

@luke-jr
Copy link
Member

luke-jr commented Feb 26, 2018

What? This contradicts the guidelines. If warnings.h references std::string and warnings.cpp uses std::string, both of them should include <string>

@practicalswift
Copy link
Contributor Author

practicalswift commented Feb 26, 2018

@luke-jr From the header include guideline:

One exception is that a .cpp file does not need to re-include the includes already included in its corresponding .h file.

@practicalswift
Copy link
Contributor Author

@MarcoFalke Would you be willing to review (context: I saw that you mentioned this PR in #11884)? :-)

@sipa
Copy link
Member

sipa commented Mar 9, 2018

utACK. Why is this not included in Travis?

@practicalswift
Copy link
Contributor Author

@sipa contrib/devtools/lint-all.sh which is included in Travis runs contrib/devtools/lint-*.sh, so no need to include this script explicitly in the Travis conf :-)

@maflcko
Copy link
Member

maflcko commented Mar 11, 2018

utACK 4dbfc1a83f79e2208890501b75253b85730e56cc

Copy link
Contributor

@eklitzke eklitzke left a comment

Choose a reason for hiding this comment

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

Besides the pedantic shell comments I gave, I don't think this is right because you're not going to be able to handle preprocessor statements adequately with this approach.


EXIT_CODE=0
for HEADER_FILE in $(git ls-files | grep -E '^src/.*\.h$' | grep -Ev '/(leveldb|secp256k1|univalue)/'); do
DUPLICATE_INCLUDES_IN_HEADER_FILE=$(grep -E '^#include ' < "${HEADER_FILE}" | sort | uniq -d)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are you using redirection here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean why I'm storing the output in DUPLICATE_INCLUDES_IN_HEADER_FILE? :-) That is the only redirection taking place here, right? :-)

I need the output to print it below. I'm guessing that's not what you meant? :-)

if [[ ! -e $CPP_FILE ]]; then
continue
fi
DUPLICATE_INCLUDES_IN_HEADER_AND_CPP_FILES=$(grep -hE '^#include ' <(sort -u < "${HEADER_FILE}") <(sort -u < "${CPP_FILE}") | grep -E '^#include ' | sort | uniq -d)
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment wrt redirection

done
for CPP_FILE in $(git ls-files | grep -E '^src/.*\.cpp$' | grep -Ev '/(leveldb|secp256k1|univalue)/'); do
DUPLICATE_INCLUDES_IN_CPP_FILE=$(grep -E '^#include ' < "${CPP_FILE}" | sort | uniq -d)
if [[ ${DUPLICATE_INCLUDES_IN_CPP_FILE} != "" ]]; then
Copy link
Contributor

Choose a reason for hiding this comment

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

You're using '' in some places and "" in others

@practicalswift
Copy link
Contributor Author

@eklitzke Please re-review :-)

Copy link
Contributor

@eklitzke eklitzke left a comment

Choose a reason for hiding this comment

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

Honestly looks good, sorry for the bash nits. For the nit-feedbackness also feel free to ping me on freenode my handle is eklitzke. Sometimes GitHub code reviews can get queued up, but for minor changes pinging on IRC works.

@@ -0,0 +1,39 @@
#!/bin/bash
#
# Copyright (c) 2017 The Bitcoin Core developers
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: 2018

# Check for duplicate includes.

EXIT_CODE=0
for HEADER_FILE in $(git ls-files | grep -E "^src/.*\.h$" | grep -Ev "/(leveldb|secp256k1|univalue)/"); do
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: you could do this and the change below as a function, and pass in the .h or .cpp'ness in a bash function, e.g.

filter_suffix() { git ls-files | grep -E "^src/.*\.${1}"'$' | grep -Ev "/(leveldb|secp256k1|univalue)/"; }

And then below would become:

for HEADER_FILE in $(filter_suffix h); do
...

for CPP_FILE in $(filter_suffix cpp); do
...

@practicalswift
Copy link
Contributor Author

@eklitzke Good points. Now updated. Please re-review :-)

@eklitzke
Copy link
Contributor

utACK 745513ecb7a825a8129104eda382cbc82d35c097

@maflcko
Copy link
Member

maflcko commented Apr 9, 2018

Needs rebase if still relevant

This enforces parts of the project header include guidelines (added by @sipa in bitcoin#10575).
@practicalswift
Copy link
Contributor Author

Rebased and removed 5+ few recently introduced duplicate includes. Still very much relevant :-)

Please re-review :-)

@maflcko maflcko merged commit c36b720 into bitcoin:master Apr 9, 2018
maflcko pushed a commit that referenced this pull request Apr 9, 2018
c36b720 Add Travis check for duplicate includes (practicalswift)
280023f Remove duplicate includes (practicalswift)

Pull request description:

  This enforces parts of the project header include guidelines (added by @sipa in #10575).

  Example run:

  ```
  $ git diff
  diff --git a/src/warnings.cpp b/src/warnings.cpp
  index c52a1fd..d8994dd 100644
  --- a/src/warnings.cpp
  +++ b/src/warnings.cpp
  @@ -5,6 +5,8 @@

   #include <sync.h>
   #include <clientversion.h>
  +#include <string>
   #include <util.h>
   #include <warnings.h>
  +#include <util.h>

  diff --git a/src/warnings.h b/src/warnings.h
  index e8e982c..8d2252e 100644
  --- a/src/warnings.h
  +++ b/src/warnings.h
  @@ -7,6 +7,7 @@
   #define BITCOIN_WARNINGS_H

   #include <stdlib.h>
   #include <string>
  +#include <stdlib.h>

   void SetMiscWarning(const std::string& strWarning);
  $ contrib/devtools/lint-includes.sh
  Duplicate include(s) in src/warnings.h:
  #include <stdlib.h>

  Include(s) from src/warnings.h duplicated in src/warnings.cpp:
  #include <string>

  Duplicate include(s) in src/warnings.cpp:
  #include <util.h>
  $ echo $?
  1
  $ git checkout .
  $ contrib/devtools/lint-includes.sh
  $ echo $?
  0
  ```

Tree-SHA512: f653d23c58ebc024dfc5b1fb8570698fd3c515c75b60b5cabbc43595548c488fca92349fa4c8b64460edbe61c879ff1d24f37f959e18552e202a7342460ddbf1
@practicalswift
Copy link
Contributor Author

@MarcoFalke Thanks for merging! :-)

PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 17, 2020
c36b720 Add Travis check for duplicate includes (practicalswift)
280023f Remove duplicate includes (practicalswift)

Pull request description:

  This enforces parts of the project header include guidelines (added by @sipa in bitcoin#10575).

  Example run:

  ```
  $ git diff
  diff --git a/src/warnings.cpp b/src/warnings.cpp
  index c52a1fd..d8994dd 100644
  --- a/src/warnings.cpp
  +++ b/src/warnings.cpp
  @@ -5,6 +5,8 @@

   #include <sync.h>
   #include <clientversion.h>
  +#include <string>
   #include <util.h>
   #include <warnings.h>
  +#include <util.h>

  diff --git a/src/warnings.h b/src/warnings.h
  index e8e982c..8d2252e 100644
  --- a/src/warnings.h
  +++ b/src/warnings.h
  @@ -7,6 +7,7 @@
   #define BITCOIN_WARNINGS_H

   #include <stdlib.h>
   #include <string>
  +#include <stdlib.h>

   void SetMiscWarning(const std::string& strWarning);
  $ contrib/devtools/lint-includes.sh
  Duplicate include(s) in src/warnings.h:
  #include <stdlib.h>

  Include(s) from src/warnings.h duplicated in src/warnings.cpp:
  #include <string>

  Duplicate include(s) in src/warnings.cpp:
  #include <util.h>
  $ echo $?
  1
  $ git checkout .
  $ contrib/devtools/lint-includes.sh
  $ echo $?
  0
  ```

Tree-SHA512: f653d23c58ebc024dfc5b1fb8570698fd3c515c75b60b5cabbc43595548c488fca92349fa4c8b64460edbe61c879ff1d24f37f959e18552e202a7342460ddbf1
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 2, 2020
c36b720 Add Travis check for duplicate includes (practicalswift)
280023f Remove duplicate includes (practicalswift)

Pull request description:

  This enforces parts of the project header include guidelines (added by @sipa in bitcoin#10575).

  Example run:

  ```
  $ git diff
  diff --git a/src/warnings.cpp b/src/warnings.cpp
  index c52a1fd..d8994dd 100644
  --- a/src/warnings.cpp
  +++ b/src/warnings.cpp
  @@ -5,6 +5,8 @@

   #include <sync.h>
   #include <clientversion.h>
  +#include <string>
   #include <util.h>
   #include <warnings.h>
  +#include <util.h>

  diff --git a/src/warnings.h b/src/warnings.h
  index e8e982c..8d2252e 100644
  --- a/src/warnings.h
  +++ b/src/warnings.h
  @@ -7,6 +7,7 @@
   #define BITCOIN_WARNINGS_H

   #include <stdlib.h>
   #include <string>
  +#include <stdlib.h>

   void SetMiscWarning(const std::string& strWarning);
  $ contrib/devtools/lint-includes.sh
  Duplicate include(s) in src/warnings.h:
  #include <stdlib.h>

  Include(s) from src/warnings.h duplicated in src/warnings.cpp:
  #include <string>

  Duplicate include(s) in src/warnings.cpp:
  #include <util.h>
  $ echo $?
  1
  $ git checkout .
  $ contrib/devtools/lint-includes.sh
  $ echo $?
  0
  ```

Tree-SHA512: f653d23c58ebc024dfc5b1fb8570698fd3c515c75b60b5cabbc43595548c488fca92349fa4c8b64460edbe61c879ff1d24f37f959e18552e202a7342460ddbf1
@practicalswift practicalswift deleted the lint-includes branch April 10, 2021 19:34
random-zebra added a commit to PIVX-Project/PIVX that referenced this pull request Feb 9, 2022
ad5717d Lint: Add lint-includes.sh (Fuzzbawls)
faba3f6 [tests] Use FastRandomContext in scheduler_tests.cpp (practicalswift)
2034bf6 Remove unused boost includes in reverselock_tests.cpp (Fuzzbawls)
3256d16 bench: Use non-throwing ParseDouble() (Fuzzbawls)
3c984d1 Remove duplicate includes (Fuzzbawls)
b54e396 Declare TorReply parsing functions in torcontrol_tests (Fuzzbawls)

Pull request description:

  This brings in the `lint-includes.sh` shell script from upstream (first introduced in bitcoin#11878) to automatically check for duplicate includes and expanded in bitcoin#13301 and bitcoin#13385 to check for the inclusion of `.cpp` files and the introduction of new boost includes, respectively.

  The check for enforcing bracket include syntax (bitcoin#13230) is also included, but currently disabled, as we have yet to systematically switch to that syntax preference.

  Three other upstream PRs are backported here as they are directly related to the removal of some boost dependencies and are very straight forward:
  - bitcoin#10547
  - bitcoin#13291
  - bitcoin#13383

  **NOTE: #2711 removes the dependency on `boost/tuple/tuple.hpp`, so it makes sense to merge that first. submitting this now so the general concept/functionality can be reviewed prior to that PR being merged**

ACKs for top commit:
  furszy:
    good ACK ad5717d
  random-zebra:
    utACK ad5717d and merging...

Tree-SHA512: d9d32d6d5122dac52c9601cda75612d87c4e027c6f196a2382206b227fcfd2bb61d4f72df7cbf5e572d94150ad8ca6db6301bd99b0da647b9627fe342b66873f
gades pushed a commit to cosanta/cosanta-core that referenced this pull request Feb 21, 2022
c36b720 Add Travis check for duplicate includes (practicalswift)
280023f Remove duplicate includes (practicalswift)

Pull request description:

  This enforces parts of the project header include guidelines (added by @sipa in bitcoin#10575).

  Example run:

  ```
  $ git diff
  diff --git a/src/warnings.cpp b/src/warnings.cpp
  index c52a1fd..d8994dd 100644
  --- a/src/warnings.cpp
  +++ b/src/warnings.cpp
  @@ -5,6 +5,8 @@

   #include <sync.h>
   #include <clientversion.h>
  +#include <string>
   #include <util.h>
   #include <warnings.h>
  +#include <util.h>

  diff --git a/src/warnings.h b/src/warnings.h
  index e8e982c..8d2252e 100644
  --- a/src/warnings.h
  +++ b/src/warnings.h
  @@ -7,6 +7,7 @@
   #define BITCOIN_WARNINGS_H

   #include <stdlib.h>
   #include <string>
  +#include <stdlib.h>

   void SetMiscWarning(const std::string& strWarning);
  $ contrib/devtools/lint-includes.sh
  Duplicate include(s) in src/warnings.h:
  #include <stdlib.h>

  Include(s) from src/warnings.h duplicated in src/warnings.cpp:
  #include <string>

  Duplicate include(s) in src/warnings.cpp:
  #include <util.h>
  $ echo $?
  1
  $ git checkout .
  $ contrib/devtools/lint-includes.sh
  $ echo $?
  0
  ```

Tree-SHA512: f653d23c58ebc024dfc5b1fb8570698fd3c515c75b60b5cabbc43595548c488fca92349fa4c8b64460edbe61c879ff1d24f37f959e18552e202a7342460ddbf1
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants