Skip to content

Commit 3eb7918

Browse files
tniessenRafaelGSS
authored andcommitted
src: make minor improvements to EnabledDebugList
Change the underlying type of DebugCategory to unsigned int so that it can be safely cast to an unsigned type without having to worry about negative values, removing the need for the DCHECK_GE statements. To fully benefit from type safety, remove DebugCategory::CATEGORY_COUNT and instead add a constexpr kDebugCategoryCount. Remove the second argument from EnabledDebugList::set_enabled() and EnabledDebugList::Parse() because it was always set to true. PR-URL: #44350 Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Darshan Sen <[email protected]>
1 parent 2563401 commit 3eb7918

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

src/debug_utils.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ void EnabledDebugList::Parse(std::shared_ptr<KVStore> env_vars,
6363
v8::Isolate* isolate) {
6464
std::string cats;
6565
credentials::SafeGetenv("NODE_DEBUG_NATIVE", &cats, env_vars, isolate);
66-
Parse(cats, true);
66+
Parse(cats);
6767
}
6868

69-
void EnabledDebugList::Parse(const std::string& cats, bool enabled) {
69+
void EnabledDebugList::Parse(const std::string& cats) {
7070
std::string debug_categories = cats;
7171
while (!debug_categories.empty()) {
7272
std::string::size_type comma_pos = debug_categories.find(',');
@@ -76,7 +76,7 @@ void EnabledDebugList::Parse(const std::string& cats, bool enabled) {
7676
{ \
7777
static const std::string available_category = ToLower(#name); \
7878
if (available_category.find(wanted) != std::string::npos) \
79-
set_enabled(DebugCategory::name, enabled); \
79+
set_enabled(DebugCategory::name); \
8080
}
8181

8282
DEBUG_CATEGORY_NAMES(V)

src/debug_utils.h

+13-14
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,21 @@ void NODE_EXTERN_PRIVATE FWrite(FILE* file, const std::string& str);
5151
V(WASI) \
5252
V(MKSNAPSHOT)
5353

54-
enum class DebugCategory {
54+
enum class DebugCategory : unsigned int {
5555
#define V(name) name,
5656
DEBUG_CATEGORY_NAMES(V)
5757
#undef V
58-
CATEGORY_COUNT
5958
};
6059

60+
#define V(name) +1
61+
constexpr unsigned int kDebugCategoryCount = DEBUG_CATEGORY_NAMES(V);
62+
#undef V
63+
6164
class NODE_EXTERN_PRIVATE EnabledDebugList {
6265
public:
63-
bool enabled(DebugCategory category) const {
64-
DCHECK_GE(static_cast<int>(category), 0);
65-
DCHECK_LT(static_cast<int>(category),
66-
static_cast<int>(DebugCategory::CATEGORY_COUNT));
67-
return enabled_[static_cast<int>(category)];
66+
bool FORCE_INLINE enabled(DebugCategory category) const {
67+
DCHECK_LT(static_cast<unsigned int>(category), kDebugCategoryCount);
68+
return enabled_[static_cast<unsigned int>(category)];
6869
}
6970

7071
// Uses NODE_DEBUG_NATIVE to initialize the categories. The env_vars variable
@@ -74,16 +75,14 @@ class NODE_EXTERN_PRIVATE EnabledDebugList {
7475
v8::Isolate* isolate = nullptr);
7576

7677
private:
77-
// Set all categories matching cats to the value of enabled.
78-
void Parse(const std::string& cats, bool enabled);
79-
void set_enabled(DebugCategory category, bool enabled) {
80-
DCHECK_GE(static_cast<int>(category), 0);
81-
DCHECK_LT(static_cast<int>(category),
82-
static_cast<int>(DebugCategory::CATEGORY_COUNT));
78+
// Enable all categories matching cats.
79+
void Parse(const std::string& cats);
80+
void set_enabled(DebugCategory category) {
81+
DCHECK_LT(static_cast<unsigned int>(category), kDebugCategoryCount);
8382
enabled_[static_cast<int>(category)] = true;
8483
}
8584

86-
bool enabled_[static_cast<int>(DebugCategory::CATEGORY_COUNT)] = {false};
85+
bool enabled_[kDebugCategoryCount] = {false};
8786
};
8887

8988
template <typename... Args>

0 commit comments

Comments
 (0)