Skip to content

Commit e727c2b

Browse files
committed
Merge #18088: build: ensure we aren't using GNU extensions
0ae8f18 build: add -Wgnu to compile flags (fanquake) 3a0fd77 Remove use of non-standard zero variadic macros (Ben Woosley) 49f6178 Drop unused LOG_TIME_MICROS helper (Ben Woosley) 5d49999 prevector: Avoid unnamed struct, which is a GNU extension (DesWurstes) Pull request description: Since we [started using](bitcoin/bitcoin#7165) 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 ```bash ./prevector.h:153:9: warning: anonymous structs are a GNU extension [-Wgnu-anonymous-struct] struct { ``` This is fixed in bitcoin/bitcoin@b849212. #### variadic macros ```bash ./undo.h:57:50: warning: must specify at least one argument for '...' parameter of variadic macro [-Wgnu-zero-variadic-macro-arguments] ::Unserialize(s, VARINT(nVersionDummy)); ``` This is taken care of in #18087. The `LOG_TIME_*` macros introduced in #16805 make use of a [GNU extension](https://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html). ```bash In file included from validation.cpp:22: ./logging/timer.h:99:99: warning: token pasting of ',' and __VA_ARGS__ is a GNU extension [-Wgnu-zero-variadic-macro-arguments] BCLog::Timer<std::chrono::milliseconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, ## __VA_ARGS__) ^ ./logging/timer.h:99:99: warning: token pasting of ',' and __VA_ARGS__ is a GNU extension [-Wgnu-zero-variadic-macro-arguments] ./logging/timer.h:99:99: warning: token pasting of ',' and __VA_ARGS__ is a GNU extension [-Wgnu-zero-variadic-macro-arguments] ./logging/timer.h:99:99: warning: token pasting of ',' and __VA_ARGS__ is a GNU extension [-Wgnu-zero-variadic-macro-arguments] ./logging/timer.h:99:99: warning: token pasting of ',' and __VA_ARGS__ is a GNU extension [-Wgnu-zero-variadic-macro-arguments] ./logging/timer.h:101:92: warning: token pasting of ',' and __VA_ARGS__ is a GNU extension [-Wgnu-zero-variadic-macro-arguments] BCLog::Timer<std::chrono::seconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, ## __VA_ARGS__) ^ 6 warnings generated. ``` This is fixed in 081a0ab64eb442bc85c4d4a4d3bc2c8e97ac2a6d and 612e8e138b97fc5ad2f38847300132a8fc423c3f. #### prevention To ensure that usage doesn't creep back in we can add [`-Wgnu`](https://clang.llvm.org/docs/DiagnosticsReference.html#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. ACKs for top commit: practicalswift: ACK 0ae8f18 -- diff looks correct MarcoFalke: ACK 0ae8f18 vasild: utACK 0ae8f18 dongcarl: ACK 0ae8f18 Tree-SHA512: c517404681ef8edf04c785731d26105bac9f3c9c958605aa24cbe399c649e7c5ee0c4aa8e714fd2b2d335e2fbea4d571e09b0dec36678ef871f0a6683ba6bb7f
2 parents b549cb1 + 0ae8f18 commit e727c2b

File tree

4 files changed

+22
-23
lines changed

4 files changed

+22
-23
lines changed

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ fi
349349
if test "x$CXXFLAGS_overridden" = "xno"; then
350350
AX_CHECK_COMPILE_FLAG([-Wall],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wall"],,[[$CXXFLAG_WERROR]])
351351
AX_CHECK_COMPILE_FLAG([-Wextra],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wextra"],,[[$CXXFLAG_WERROR]])
352+
AX_CHECK_COMPILE_FLAG([-Wgnu],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wgnu"],,[[$CXXFLAG_WERROR]])
352353
AX_CHECK_COMPILE_FLAG([-Wformat],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wformat"],,[[$CXXFLAG_WERROR]])
353354
AX_CHECK_COMPILE_FLAG([-Wvla],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wvla"],,[[$CXXFLAG_WERROR]])
354355
AX_CHECK_COMPILE_FLAG([-Wswitch],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wswitch"],,[[$CXXFLAG_WERROR]])

src/logging/timer.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,10 @@ class Timer
9393
} // namespace BCLog
9494

9595

96-
#define LOG_TIME_MICROS(end_msg, ...) \
97-
BCLog::Timer<std::chrono::microseconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, ## __VA_ARGS__)
98-
#define LOG_TIME_MILLIS(end_msg, ...) \
99-
BCLog::Timer<std::chrono::milliseconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, ## __VA_ARGS__)
100-
#define LOG_TIME_SECONDS(end_msg, ...) \
101-
BCLog::Timer<std::chrono::seconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, ## __VA_ARGS__)
96+
#define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category) \
97+
BCLog::Timer<std::chrono::milliseconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, log_category)
98+
#define LOG_TIME_SECONDS(end_msg) \
99+
BCLog::Timer<std::chrono::seconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg)
102100

103101

104102
#endif // BITCOIN_LOGGING_TIMER_H

src/prevector.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class prevector {
152152
struct {
153153
char* indirect;
154154
size_type capacity;
155-
};
155+
} indirect_contents;
156156
};
157157
#pragma pack(pop)
158158
alignas(char*) direct_or_indirect _union = {};
@@ -163,8 +163,8 @@ class prevector {
163163

164164
T* direct_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.direct) + pos; }
165165
const T* direct_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.direct) + pos; }
166-
T* indirect_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.indirect) + pos; }
167-
const T* indirect_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.indirect) + pos; }
166+
T* indirect_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.indirect_contents.indirect) + pos; }
167+
const T* indirect_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.indirect_contents.indirect) + pos; }
168168
bool is_direct() const { return _size <= N; }
169169

170170
void change_capacity(size_type new_capacity) {
@@ -182,17 +182,17 @@ class prevector {
182182
/* FIXME: Because malloc/realloc here won't call new_handler if allocation fails, assert
183183
success. These should instead use an allocator or new/delete so that handlers
184184
are called as necessary, but performance would be slightly degraded by doing so. */
185-
_union.indirect = static_cast<char*>(realloc(_union.indirect, ((size_t)sizeof(T)) * new_capacity));
186-
assert(_union.indirect);
187-
_union.capacity = new_capacity;
185+
_union.indirect_contents.indirect = static_cast<char*>(realloc(_union.indirect_contents.indirect, ((size_t)sizeof(T)) * new_capacity));
186+
assert(_union.indirect_contents.indirect);
187+
_union.indirect_contents.capacity = new_capacity;
188188
} else {
189189
char* new_indirect = static_cast<char*>(malloc(((size_t)sizeof(T)) * new_capacity));
190190
assert(new_indirect);
191191
T* src = direct_ptr(0);
192192
T* dst = reinterpret_cast<T*>(new_indirect);
193193
memcpy(dst, src, size() * sizeof(T));
194-
_union.indirect = new_indirect;
195-
_union.capacity = new_capacity;
194+
_union.indirect_contents.indirect = new_indirect;
195+
_union.indirect_contents.capacity = new_capacity;
196196
_size += N + 1;
197197
}
198198
}
@@ -301,7 +301,7 @@ class prevector {
301301
if (is_direct()) {
302302
return N;
303303
} else {
304-
return _union.capacity;
304+
return _union.indirect_contents.capacity;
305305
}
306306
}
307307

@@ -468,8 +468,8 @@ class prevector {
468468
clear();
469469
}
470470
if (!is_direct()) {
471-
free(_union.indirect);
472-
_union.indirect = nullptr;
471+
free(_union.indirect_contents.indirect);
472+
_union.indirect_contents.indirect = nullptr;
473473
}
474474
}
475475

@@ -521,7 +521,7 @@ class prevector {
521521
if (is_direct()) {
522522
return 0;
523523
} else {
524-
return ((size_t)(sizeof(T))) * _union.capacity;
524+
return ((size_t)(sizeof(T))) * _union.indirect_contents.capacity;
525525
}
526526
}
527527

src/validation.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2279,11 +2279,11 @@ bool CChainState::FlushStateToDisk(
22792279
LOCK(cs_LastBlockFile);
22802280
if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && !fReindex) {
22812281
if (nManualPruneHeight > 0) {
2282-
LOG_TIME_MILLIS("find files to prune (manual)", BCLog::BENCH);
2282+
LOG_TIME_MILLIS_WITH_CATEGORY("find files to prune (manual)", BCLog::BENCH);
22832283

22842284
FindFilesToPruneManual(setFilesToPrune, nManualPruneHeight);
22852285
} else {
2286-
LOG_TIME_MILLIS("find files to prune", BCLog::BENCH);
2286+
LOG_TIME_MILLIS_WITH_CATEGORY("find files to prune", BCLog::BENCH);
22872287

22882288
FindFilesToPrune(setFilesToPrune, chainparams.PruneAfterHeight());
22892289
fCheckForPruning = false;
@@ -2321,15 +2321,15 @@ bool CChainState::FlushStateToDisk(
23212321
return AbortNode(state, "Disk space is too low!", _("Error: Disk space is too low!").translated, CClientUIInterface::MSG_NOPREFIX);
23222322
}
23232323
{
2324-
LOG_TIME_MILLIS("write block and undo data to disk", BCLog::BENCH);
2324+
LOG_TIME_MILLIS_WITH_CATEGORY("write block and undo data to disk", BCLog::BENCH);
23252325

23262326
// First make sure all block and undo data is flushed to disk.
23272327
FlushBlockFile();
23282328
}
23292329

23302330
// Then update all block file information (which may refer to block and undo files).
23312331
{
2332-
LOG_TIME_MILLIS("write block index to disk", BCLog::BENCH);
2332+
LOG_TIME_MILLIS_WITH_CATEGORY("write block index to disk", BCLog::BENCH);
23332333

23342334
std::vector<std::pair<int, const CBlockFileInfo*> > vFiles;
23352335
vFiles.reserve(setDirtyFileInfo.size());
@@ -2349,7 +2349,7 @@ bool CChainState::FlushStateToDisk(
23492349
}
23502350
// Finally remove any pruned files
23512351
if (fFlushForPrune) {
2352-
LOG_TIME_MILLIS("unlink pruned files", BCLog::BENCH);
2352+
LOG_TIME_MILLIS_WITH_CATEGORY("unlink pruned files", BCLog::BENCH);
23532353

23542354
UnlinkPrunedFiles(setFilesToPrune);
23552355
}

0 commit comments

Comments
 (0)