build: ensure we aren’t using GNU extensions #18088

pull fanquake wants to merge 4 commits into bitcoin:master from fanquake:no_gnu_extensions changing 4 files +22 −23
  1. fanquake commented at 4:39 am on February 7, 2020: member

    Since we started using the ax_cxx_compile_stdcxx.m4 macro we’ve been passing [noext] to indicate that we don’t want to use an extended mode, i.e GNU extensions. Speaking to Cory he clarified that the intention was to “require only vanilla c++11 and turn off extension support so they would fail to compile”.

    However in the codebase we are currently making use of some GNU extensions. We should either remove there usage, or at least amend our CXX compiler checks. I’d prefer the former.

    anonymous structs

    0./prevector.h:153:9: warning: anonymous structs are a GNU extension [-Wgnu-anonymous-struct]
    1        struct {
    

    This is fixed in https://github.com/bitcoin/bitcoin/commit/b849212c1ec01cc8633b8cdcd390da9b1051be0d.

    variadic macros

    0./undo.h:57:50: warning: must specify at least one argument for '...' parameter of variadic macro [-Wgnu-zero-variadic-macro-arguments]
    1            ::Unserialize(s, VARINT(nVersionDummy));
    

    This is taken care of in #18087.

    The LOG_TIME_* macros introduced in #16805 make use of a GNU extension.

     0In file included from validation.cpp:22:
     1./logging/timer.h:99:99: warning: token pasting of ',' and __VA_ARGS__ is a GNU extension [-Wgnu-zero-variadic-macro-arguments]
     2    BCLog::Timer<std::chrono::milliseconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, ## __VA_ARGS__)
     3                                                                                                  ^
     4./logging/timer.h:99:99: warning: token pasting of ',' and __VA_ARGS__ is a GNU extension [-Wgnu-zero-variadic-macro-arguments]
     5./logging/timer.h:99:99: warning: token pasting of ',' and __VA_ARGS__ is a GNU extension [-Wgnu-zero-variadic-macro-arguments]
     6./logging/timer.h:99:99: warning: token pasting of ',' and __VA_ARGS__ is a GNU extension [-Wgnu-zero-variadic-macro-arguments]
     7./logging/timer.h:99:99: warning: token pasting of ',' and __VA_ARGS__ is a GNU extension [-Wgnu-zero-variadic-macro-arguments]
     8./logging/timer.h:101:92: warning: token pasting of ',' and __VA_ARGS__ is a GNU extension [-Wgnu-zero-variadic-macro-arguments]
     9    BCLog::Timer<std::chrono::seconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, ## __VA_ARGS__)
    10                                                                                           ^
    116 warnings generated.
    

    This is fixed in 081a0ab64eb442bc85c4d4a4d3bc2c8e97ac2a6d and 612e8e138b97fc5ad2f38847300132a8fc423c3f.

    prevention

    To ensure that usage doesn’t creep back in we can add -Wgnu to our compile time flags, which will make Clang warn whenever it encounters GNU extensions.

    This would close #14130. Also related to #17230, where it’s suggested we use a GNU extension, the gnu::pure attribute.

  2. fanquake added the label Brainstorming on Feb 7, 2020
  3. fanquake added the label Build system on Feb 7, 2020
  4. fanquake added the label Needs gitian build on Feb 7, 2020
  5. fanquake requested review from jamesob on Feb 7, 2020
  6. fanquake requested review from theuni on Feb 7, 2020
  7. fanquake requested review from sipa on Feb 7, 2020
  8. DrahtBot commented at 9:42 am on February 7, 2020: member

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

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #18857 (build: avoid repetitions when enabling warnings in configure.ac by vasild)

    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.

  9. laanwj commented at 12:21 pm on February 7, 2020: member
    Concept ACK. If we can do without compiler-specific extensions we should.
  10. Empact commented at 3:29 pm on February 7, 2020: member
    Concept ACK 👍
  11. Empact commented at 4:38 pm on February 7, 2020: member
    Here’s a set of changes to remove the Wgnu-zero-variadic-macro-arguments cases: https://github.com/fanquake/bitcoin/compare/no_gnu_extensions...Empact:2020-02-no_gnu_extensions
  12. DrahtBot removed the label Needs gitian build on Feb 8, 2020
  13. fanquake force-pushed on Feb 10, 2020
  14. fanquake commented at 12:11 pm on February 10, 2020: member

    Here’s a set of changes

    Thanks, I’ve taken some of those changes.

  15. MarcoFalke deleted a comment on Feb 10, 2020
  16. theuni commented at 7:52 pm on February 10, 2020: member
    Concept ACK, this was for sure the intent of [noext].
  17. practicalswift commented at 6:43 am on February 11, 2020: contributor
    Concept ACK – inside the standard is better than outside the standard
  18. fanquake force-pushed on Feb 11, 2020
  19. fanquake marked this as ready for review on Feb 11, 2020
  20. fanquake commented at 8:07 am on February 11, 2020: member
    Rebased on master now that #18087 is in, fixed up the sanitizer issue in prevector (bad rebase) and re-ordered some commits.
  21. practicalswift commented at 12:50 pm on February 11, 2020: contributor
    ACK 6af7c41771c2b3adeffe81ce06a70e0dbbef5360 – diff looks correct
  22. in src/logging.h:181 in 8ee10a2c3f outdated
    177@@ -178,7 +178,7 @@ static inline void LogPrintf(const char* fmt, const Args&... args)
    178 // evaluating arguments when logging for the category is not enabled.
    179 #define LogPrint(category, ...)              \
    180     do {                                     \
    181-        if (LogAcceptCategory((category))) { \
    182+        if ((category) == BCLog::LogFlags::ALL || LogAcceptCategory((category))) { \
    


    MarcoFalke commented at 12:55 pm on February 11, 2020:
    Why is this change needed?

    Empact commented at 12:58 pm on February 11, 2020:
    Added this to ensure the FlushStateToDisk call would be logged even if no categories were active, which I believe is consistent with the current behavior - though I am not certain it is necessary.

    laanwj commented at 5:15 pm on February 26, 2020:

    I am not convinced at the moment that this is necessary. Timer::Log has the following implementation:

    0    void Log(const std::string& msg)
    1    {
    2        const std::string full_msg = this->LogMsg(msg);
    3
    4        if (m_log_category == BCLog::LogFlags::ALL) {
    5            LogPrintf("%s\n", full_msg);
    6        } else {
    7            LogPrint(m_log_category, "%s\n", full_msg);
    8        }
    9    }
    

    To me, this implies that LogPrint(BCLog::LogFlags::ALL… is never called. And it shouldn’t, it’s not valid usage.


    fanquake commented at 4:07 am on March 20, 2020:
    I’ve removed this change.
  23. DrahtBot added the label Needs rebase on Feb 12, 2020
  24. fanquake force-pushed on Feb 12, 2020
  25. DrahtBot removed the label Needs rebase on Feb 13, 2020
  26. fanquake force-pushed on Mar 20, 2020
  27. fanquake commented at 4:07 am on March 20, 2020: member

    Isn’t COUNTER a GNU extension too?

    From what I can find, yes __COUNTER__ (added to GCC in 4.3) is an extension, however it’s supported by GCC, Clang, MSVC and -Wgnu doesn’t currently warn about it. Maybe someone can address this in a followup if they’d like.

    I’ve reverted the changes in logging.h and did some testing to check that the “FlushStateToDisk: write coins cache to disk” call was always being logged, regardless of logging categories/options.

  28. in src/validation.cpp:2330 in 45cbbdd6ee outdated
    2326@@ -2327,7 +2327,7 @@ bool CChainState::FlushStateToDisk(
    2327         // Flush best chain related state. This can only be done if the blocks / block index write was also done.
    2328         if (fDoFullFlush && !CoinsTip().GetBestBlock().IsNull()) {
    2329             LOG_TIME_SECONDS(strprintf("write coins cache to disk (%d coins, %.2fkB)",
    2330-                coins_count, coins_mem_usage / 1000));
    2331+                coins_count, coins_mem_usage / 1000), BCLog::ALL);
    


    MarcoFalke commented at 1:25 pm on March 20, 2020:

    I don’t really like leaking this private implementation detail of the log flags into the caller code. I’d slightly prefer a type-safe way, that also better documents what is happening.

    What about making this argument optional not with VA_ARGS, but with an Optional<BCLog::LogFlags>? Passing a nullopt or {} will unconditionally log, whereas passing a log flag will only log to that category (if enabled).


    Empact commented at 10:51 pm on March 20, 2020:

    We could drop it altogether as this is the only call to LOG_TIME_SECONDS:

     0diff --git a/src/logging.h b/src/logging.h
     1index 5d8ec33392..b2fde1b9ea 100644
     2--- a/src/logging.h
     3+++ b/src/logging.h
     4@@ -178,7 +178,7 @@ static inline void LogPrintf(const char* fmt, const Args&... args)
     5 // evaluating arguments when logging for the category is not enabled.
     6 #define LogPrint(category, ...)              \
     7     do {                                     \
     8-        if ((category) == BCLog::LogFlags::ALL || LogAcceptCategory((category))) { \
     9+        if (LogAcceptCategory((category))) { \
    10             LogPrintf(__VA_ARGS__);          \
    11         }                                    \
    12     } while (0)
    13diff --git a/src/logging/timer.h b/src/logging/timer.h
    14index 34a6e25185..6a40dc48ba 100644
    15--- a/src/logging/timer.h
    16+++ b/src/logging/timer.h
    17@@ -95,8 +95,8 @@ private:
    18 
    19 #define LOG_TIME_MILLIS(end_msg, log_category) \
    20     BCLog::Timer<std::chrono::milliseconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, log_category)
    21-#define LOG_TIME_SECONDS(end_msg, log_category) \
    22-    BCLog::Timer<std::chrono::seconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, log_category)
    23+#define LOG_TIME_SECONDS(end_msg) \
    24+    BCLog::Timer<std::chrono::seconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg)
    25 
    26 
    27 #endif // BITCOIN_LOGGING_TIMER_H
    28diff --git a/src/validation.cpp b/src/validation.cpp
    29index a0ac003029..bab04b8e34 100644
    30--- a/src/validation.cpp
    31+++ b/src/validation.cpp
    32@@ -2328,7 +2328,7 @@ bool CChainState::FlushStateToDisk(
    33         // Flush best chain related state. This can only be done if the blocks / block index write was also done.
    34         if (fDoFullFlush && !CoinsTip().GetBestBlock().IsNull()) {
    35             LOG_TIME_SECONDS(strprintf("write coins cache to disk (%d coins, %.2fkB)",
    36-                coins_count, coins_mem_usage / 1000), BCLog::ALL);
    37+                coins_count, coins_mem_usage / 1000));
    38 
    39             // Typical Coin structures on disk are around 48 bytes in size.
    40             // Pushing a new one to the database can cause it to be written
    


    fanquake commented at 4:13 am on March 23, 2020:

    We could drop it altogether

    I’ve updated to do this.

  29. fanquake force-pushed on Mar 23, 2020
  30. in src/logging/timer.h:99 in 322bcb9a63 outdated
    100-#define LOG_TIME_SECONDS(end_msg, ...) \
    101-    BCLog::Timer<std::chrono::seconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, ## __VA_ARGS__)
    102+#define LOG_TIME_MILLIS(end_msg, log_category) \
    103+    BCLog::Timer<std::chrono::milliseconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, log_category)
    104+#define LOG_TIME_SECONDS(end_msg) \
    105+    BCLog::Timer<std::chrono::seconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg)
    


    MarcoFalke commented at 12:26 pm on March 23, 2020:
    So while the macro name indicates the only difference is the precision, they are functionally also different to the logging behaviour: One always logs, the other must accept a category. Not sure if it makes sense to keep the names so similar then.

    fanquake commented at 7:16 am on April 27, 2020:
    Ok. I’ve renamed LOG_TIME_MILLIS to LOG_TIME_MILLIS_WITH_CATEGORY.
  31. fanquake force-pushed on Apr 27, 2020
  32. prevector: Avoid unnamed struct, which is a GNU extension 5d4999951e
  33. Drop unused LOG_TIME_MICROS helper 49f6178c3e
  34. Remove use of non-standard zero variadic macros
    These are a gnu extension warned against by: gnu-zero-variadic-macro-arguments
    3a0fd7726b
  35. build: add -Wgnu to compile flags
    When compiling with Clang, this will warn when GNU extensions are
    used.
    
    Info: https://clang.llvm.org/docs/DiagnosticsReference.html#wgnu
    0ae8f18dfe
  36. fanquake force-pushed on Apr 30, 2020
  37. fanquake commented at 10:04 am on April 30, 2020: member
    Rebased after #18591.
  38. vasild commented at 6:00 am on May 4, 2020: member

    utACK 0ae8f18df

    If we currently do not see any warnings due to the newly introduced -Wgnu then I would suggest to also add -Werror=gnu (if compiling with --enable-werror). So that a newly introduced warning in the future does not sneak - printed, but unnoticed in CI logs.

  39. dongcarl commented at 5:20 pm on May 4, 2020: member

    ACK 0ae8f18dfe143051fec6ae10ea7df10142e3ff2f


    Read relevant gcc docs, built, and ran tests.

  40. practicalswift commented at 6:24 pm on May 4, 2020: contributor

    ACK 0ae8f18dfe143051fec6ae10ea7df10142e3ff2f – diff looks correct

    Agree with @vasild about -Werror=gnu as part of --enable-werror. May also be done in a follow-up.

  41. MarcoFalke commented at 10:53 pm on May 4, 2020: member
    ACK 0ae8f18dfe143051fec6ae10ea7df10142e3ff2f
  42. fanquake merged this on May 4, 2020
  43. fanquake closed this on May 4, 2020

  44. fanquake deleted the branch on May 4, 2020
  45. sidhujag referenced this in commit fae55b66f9 on May 5, 2020
  46. practicalswift commented at 9:58 am on May 5, 2020: contributor
    @vasild Would you mind taking on the -Werror=gnu thing? :)
  47. vasild referenced this in commit a30b0a24e9 on May 5, 2020
  48. vasild commented at 12:52 pm on May 5, 2020: member

    @vasild Would you mind taking on the -Werror=gnu thing? :)

    Here it is: #18887 build: enable -Werror=gnu

  49. laanwj referenced this in commit 04c09553d8 on May 13, 2020
  50. sidhujag referenced this in commit ca5297cd76 on May 14, 2020
  51. janus referenced this in commit bb8147c591 on Nov 15, 2020
  52. Fabcien referenced this in commit 102f35022b on Jan 29, 2021
  53. PastaPastaPasta referenced this in commit 4085118d0b on Jun 27, 2021
  54. PastaPastaPasta referenced this in commit 7e392a9b4a on Jun 28, 2021
  55. PastaPastaPasta referenced this in commit 483c108cff on Jun 29, 2021
  56. PastaPastaPasta referenced this in commit 9802d1b7f1 on Jul 1, 2021
  57. PastaPastaPasta referenced this in commit 7cd14fe0a4 on Jul 1, 2021
  58. PastaPastaPasta referenced this in commit e30e39c0aa on Jul 14, 2021
  59. PastaPastaPasta referenced this in commit 35e1a2333e on Sep 17, 2021
  60. DrahtBot locked this on Feb 15, 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: 2024-11-17 09:12 UTC

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