Don't use global (external) symbols for symbols that are used in only one translation unit #16092

pull practicalswift wants to merge 1 commits into bitcoin:master from practicalswift:reduce-global-namespacing changing 4 files +10 −7
  1. practicalswift commented at 9:14 AM on May 26, 2019: contributor

    Don't use global (external) symbols for symbols that are used in only one translation unit.

    Before:

    $ for SYMBOL in $(nm src/bitcoind | grep -E ' [BD] ' | c++filt | cut -f3- -d' ' | grep -v @ | grep -v : | sort | grep '[a-z]' | sort -u | grep -vE '(^_|typeinfo|vtable)'); do
          REFERENCES=$(git grep -lE "([^a-zA-Z]|^)${SYMBOL}([^a-zA-Z]|\$)" -- "*.cpp" "*.h")
          N_REFERENCES=$(wc -l <<< "${REFERENCES}")
          if [[ ${N_REFERENCES} > 1 ]]; then
              continue
          fi
          echo "Global symbol ${SYMBOL} is used in only one translation unit: ${REFERENCES}"
      done
    Global symbol g_chainstate is used in only one translation unit: src/validation.cpp
    Global symbol g_ui_signals is used in only one translation unit: src/ui_interface.cpp
    Global symbol instance_of_cmaincleanup is used in only one translation unit: src/validation.cpp
    Global symbol instance_of_cnetcleanup is used in only one translation unit: src/net.cpp
    Global symbol instance_of_cnetprocessingcleanup is used in only one translation unit: src/net_processing.cpp
    Global symbol pindexBestForkBase is used in only one translation unit: src/validation.cpp
    Global symbol pindexBestForkTip is used in only one translation unit: src/validation.cpp
    $ 
    

    After:

    $ for SYMBOL in $(nm src/bitcoind | grep -E ' [BD] ' | c++filt | cut -f3- -d' ' | grep -v @ | grep -v : | sort | grep '[a-z]' | sort -u | grep -vE '(^_|typeinfo|vtable)'); do
          REFERENCES=$(git grep -lE "([^a-zA-Z]|^)${SYMBOL}([^a-zA-Z]|\$)" -- "*.cpp" "*.h")
          N_REFERENCES=$(wc -l <<< "${REFERENCES}")
          if [[ ${N_REFERENCES} > 1 ]]; then
              continue
          fi
          echo "Global symbol ${SYMBOL} is used in only one translation unit: ${REFERENCES}"
      done
    $ 
    

    ♻️ Think about future generations: save the global namespace from unnecessary pollution! ♻️

  2. fanquake added the label Refactoring on May 26, 2019
  3. JosuGZ commented at 11:04 AM on May 26, 2019: contributor

    I whould say utACK b5476332cb5653552eefe25cc96131f04e2c8d8c

  4. DrahtBot commented at 12:03 PM on May 26, 2019: member

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #16194 (refactor: share blockmetadata with BlockManager by jamesob)
    • #15606 ([experimental] UTXO snapshots by jamesob)

    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.

  5. Empact commented at 8:20 PM on May 27, 2019: member

    Concept ACK. Could be better to use an anon namespace where possible for these as that will make the variables static and enforce local-only access to the associated class as well.

  6. practicalswift commented at 11:31 AM on June 3, 2019: contributor

    @Empact The end-result would be the same as using static, right?

    $ cat exporting.cpp
    static int _static_int = 1;
    namespace {
      int _namespace_int = 1;
      static int _namespace_static_int = 1;
    }
    int _int = 1;
    $ clang++ -c exporting.cpp
    $ nm exporting.o
    0000000000000000 D _int
    

    I don't have any strong preference -- I just want to minimise the diffs and make them super-trivial to review. Please read #15622 (comment) and other comments in that PR.

  7. Empact commented at 3:55 PM on June 3, 2019: member

    Sure, fine as is.

  8. DrahtBot added the label Needs rebase on Jun 5, 2019
  9. Don't use global (external) symbols for symbols that are used in only one translation unit 0959d37e3e
  10. practicalswift force-pushed on Jun 6, 2019
  11. practicalswift commented at 5:53 AM on June 6, 2019: contributor

    Rebased!

  12. DrahtBot removed the label Needs rebase on Jun 6, 2019
  13. MarcoFalke commented at 7:57 PM on June 10, 2019: member

    ACK 0959d37e3e0f80010a78d175e3846dabf5d35919

  14. hebasto commented at 12:58 PM on June 18, 2019: member

    ACK 0959d37e3e0f80010a78d175e3846dabf5d35919 I have not tested the code, but I have reviewed it and it LGTM. Ref: https://en.cppreference.com/w/cpp/language/storage_duration#Linkage

  15. promag commented at 1:16 PM on June 18, 2019: member

    ACK 0959d37.

  16. MarcoFalke referenced this in commit 0b68fca700 on Jun 18, 2019
  17. MarcoFalke merged this on Jun 18, 2019
  18. MarcoFalke closed this on Jun 18, 2019

  19. sidhujag referenced this in commit edd16402a6 on Jun 19, 2019
  20. deadalnix referenced this in commit 62cc9f5b0b on Jun 11, 2020
  21. practicalswift deleted the branch on Apr 10, 2021
  22. kittywhiskers referenced this in commit bbb7a39b9c on Oct 9, 2021
  23. kittywhiskers referenced this in commit 4a9d8617ff on Oct 10, 2021
  24. kittywhiskers referenced this in commit a5125f89b5 on Oct 16, 2021
  25. kittywhiskers referenced this in commit c3b13dd2d1 on Oct 16, 2021
  26. kittywhiskers referenced this in commit 0c13b1ce9a on Oct 16, 2021
  27. kittywhiskers referenced this in commit b837494790 on Oct 21, 2021
  28. kittywhiskers referenced this in commit 900c0100eb on Oct 22, 2021
  29. pravblockc referenced this in commit c8f2ddb0a6 on Nov 18, 2021
  30. DrahtBot locked this on Aug 16, 2022

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: 2026-04-16 15:14 UTC

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