Skip to content

Commit

Permalink
build: ensure we aren't using GNU extensions
Browse files Browse the repository at this point in the history
Summary:
Remove the use of gnu extensions:
 - anonymous structs
 - variadic macros with no argument
And add a Clang warning for detecting their future use.

Backport of core [[bitcoin/bitcoin#18088 | PR18088]].

Test Plan:
With clang:
  ninja all check
Check there is no warning.

Reviewers: #bitcoin_abc, PiRK

Reviewed By: #bitcoin_abc, PiRK

Differential Revision: https://reviews.bitcoinabc.org/D9109
  • Loading branch information
DesWurstes authored and Fabcien committed Jan 29, 2021
1 parent a952590 commit 102f350
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ add_compiler_flags(
-Wall
-Wextra
-Wformat
-Wgnu
-Wvla
-Wcast-align
-Wunused-parameter
Expand Down
13 changes: 5 additions & 8 deletions src/logging/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,11 @@ template <typename TimeType> class Timer {

} // namespace BCLog

#define LOG_TIME_MICROS(end_msg, ...) \
BCLog::Timer<std::chrono::microseconds> PASTE2( \
logging_timer, __COUNTER__)(__func__, end_msg, ##__VA_ARGS__)
#define LOG_TIME_MILLIS(end_msg, ...) \
#define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category) \
BCLog::Timer<std::chrono::milliseconds> PASTE2( \
logging_timer, __COUNTER__)(__func__, end_msg, ##__VA_ARGS__)
#define LOG_TIME_SECONDS(end_msg, ...) \
BCLog::Timer<std::chrono::seconds> PASTE2(logging_timer, __COUNTER__)( \
__func__, end_msg, ##__VA_ARGS__)
logging_timer, __COUNTER__)(__func__, end_msg, log_category)
#define LOG_TIME_SECONDS(end_msg) \
BCLog::Timer<std::chrono::seconds> PASTE2(logging_timer, \
__COUNTER__)(__func__, end_msg)

#endif // BITCOIN_LOGGING_TIMER_H
28 changes: 15 additions & 13 deletions src/prevector.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class prevector {
struct {
char *indirect;
size_type capacity;
};
} indirect_contents;
};
#pragma pack(pop)
alignas(char *) direct_or_indirect _union = {};
Expand All @@ -258,10 +258,11 @@ class prevector {
return reinterpret_cast<const T *>(_union.direct) + pos;
}
T *indirect_ptr(difference_type pos) {
return reinterpret_cast<T *>(_union.indirect) + pos;
return reinterpret_cast<T *>(_union.indirect_contents.indirect) + pos;
}
const T *indirect_ptr(difference_type pos) const {
return reinterpret_cast<const T *>(_union.indirect) + pos;
return reinterpret_cast<const T *>(_union.indirect_contents.indirect) +
pos;
}
bool is_direct() const { return _size <= N; }

Expand All @@ -282,19 +283,20 @@ class prevector {
// allocator or new/delete so that handlers are called as
// necessary, but performance would be slightly degraded by
// doing so.
_union.indirect = static_cast<char *>(realloc(
_union.indirect, ((size_t)sizeof(T)) * new_capacity));
assert(_union.indirect);
_union.capacity = new_capacity;
_union.indirect_contents.indirect = static_cast<char *>(
realloc(_union.indirect_contents.indirect,
((size_t)sizeof(T)) * new_capacity));
assert(_union.indirect_contents.indirect);
_union.indirect_contents.capacity = new_capacity;
} else {
char *new_indirect = static_cast<char *>(
malloc(((size_t)sizeof(T)) * new_capacity));
assert(new_indirect);
T *src = direct_ptr(0);
T *dst = reinterpret_cast<T *>(new_indirect);
memcpy(dst, src, size() * sizeof(T));
_union.indirect = new_indirect;
_union.capacity = new_capacity;
_union.indirect_contents.indirect = new_indirect;
_union.indirect_contents.capacity = new_capacity;
_size += N + 1;
}
}
Expand Down Expand Up @@ -403,7 +405,7 @@ class prevector {
if (is_direct()) {
return N;
} else {
return _union.capacity;
return _union.indirect_contents.capacity;
}
}

Expand Down Expand Up @@ -548,8 +550,8 @@ class prevector {
clear();
}
if (!is_direct()) {
free(_union.indirect);
_union.indirect = nullptr;
free(_union.indirect_contents.indirect);
_union.indirect_contents.indirect = nullptr;
}
}

Expand Down Expand Up @@ -601,7 +603,7 @@ class prevector {
if (is_direct()) {
return 0;
} else {
return ((size_t)(sizeof(T))) * _union.capacity;
return ((size_t)(sizeof(T))) * _union.indirect_contents.capacity;
}
}

Expand Down
17 changes: 10 additions & 7 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2111,12 +2111,13 @@ bool CChainState::FlushStateToDisk(const CChainParams &chainparams,
if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) &&
!fReindex) {
if (nManualPruneHeight > 0) {
LOG_TIME_MILLIS("find files to prune (manual)",
BCLog::BENCH);
LOG_TIME_MILLIS_WITH_CATEGORY(
"find files to prune (manual)", BCLog::BENCH);
FindFilesToPruneManual(g_chainman, setFilesToPrune,
nManualPruneHeight);
} else {
LOG_TIME_MILLIS("find files to prune", BCLog::BENCH);
LOG_TIME_MILLIS_WITH_CATEGORY("find files to prune",
BCLog::BENCH);
FindFilesToPrune(g_chainman, setFilesToPrune,
chainparams.PruneAfterHeight());
fCheckForPruning = false;
Expand Down Expand Up @@ -2166,8 +2167,8 @@ bool CChainState::FlushStateToDisk(const CChainParams &chainparams,
}

{
LOG_TIME_MILLIS("write block and undo data to disk",
BCLog::BENCH);
LOG_TIME_MILLIS_WITH_CATEGORY(
"write block and undo data to disk", BCLog::BENCH);

// First make sure all block and undo data is flushed to
// disk.
Expand All @@ -2176,7 +2177,8 @@ bool CChainState::FlushStateToDisk(const CChainParams &chainparams,
// Then update all block file information (which may refer to
// block and undo files).
{
LOG_TIME_MILLIS("write block index to disk", BCLog::BENCH);
LOG_TIME_MILLIS_WITH_CATEGORY("write block index to disk",
BCLog::BENCH);

std::vector<std::pair<int, const CBlockFileInfo *>> vFiles;
vFiles.reserve(setDirtyFileInfo.size());
Expand All @@ -2203,7 +2205,8 @@ bool CChainState::FlushStateToDisk(const CChainParams &chainparams,

// Finally remove any pruned files
if (fFlushForPrune) {
LOG_TIME_MILLIS("unlink pruned files", BCLog::BENCH);
LOG_TIME_MILLIS_WITH_CATEGORY("unlink pruned files",
BCLog::BENCH);

UnlinkPrunedFiles(setFilesToPrune);
}
Expand Down

0 comments on commit 102f350

Please sign in to comment.