refactor(argparse): 将null替换为nullptr以提高C++兼容性

- 在argparse库中将所有null指针常量替换为nullptr
- 更新头文件和源文件中的指针初始化和比较操作
- 修改测试文件中的相关断言检查
- 更新AST定义文件中的注释说明
This commit is contained in:
zzy
2026-04-05 20:18:09 +08:00
parent 27d86d5685
commit 4144f7841c
76 changed files with 1430 additions and 998 deletions

View File

@@ -88,7 +88,7 @@ extern logger_t __default_logger_root;
/**
* @brief 初始化日志实例 其余参数设置为默认值
* @param[in] logger 日志器实例指针
* @param[in] name 日志器名称(NULL表示获取默认日志器名称)
* @param[in] name 日志器名称(nullptr表示获取默认日志器名称)
*/
void init_logger(logger_t *logger, const char *name);
@@ -102,7 +102,7 @@ void log_set_level(logger_t *logger, int level);
/**
* @brief 设置自定义日志处理器
* @param[in] logger 目标日志器实例
* @param[in] handler 自定义处理函数(NULL恢复默认处理)
* @param[in] handler 自定义处理函数(nullptr恢复默认处理)
*/
void log_set_handler(logger_t *logger, log_handler handler);
@@ -115,7 +115,7 @@ void log_set_handler(logger_t *logger, log_handler handler);
#define SCC_LOG_IMPL(_module_, _level_, _fmt_, ...) \
do { \
/* TODO check _module_ is NULL */ \
/* TODO check _module_ is nullptr */ \
if ((_module_)->handler && ((_module_)->level & (_level_))) \
(_module_)->handler( \
SCC_LOG_HANDLE_ARGS(_module_, _level_, _fmt_, ##__VA_ARGS__)); \

View File

@@ -57,7 +57,7 @@
break; \
} \
usize phys_tail = _scc_ring_phys(ring, (ring).tail); \
if ((ring).fill == null || \
if ((ring).fill == nullptr || \
!(ring).fill(&((ring).data[phys_tail]), (ring).userdata)) { \
ok = 0; \
break; \
@@ -97,14 +97,14 @@
* @brief 初始化环形缓冲区
* @param ring 环形缓冲区变量
* @param cap 容量
* @param fill_func 填充回调函数 (可传 NULL) 返回true表示成功
* @param fill_func 填充回调函数 (可传 nullptr) 返回true表示成功
*
* 内存分配失败由 scc_malloc 内部处理 (如 LOG_FATAL)
*/
#define scc_ring_init(ring, _cap, fill_func, _userdata) \
do { \
(ring).data = \
(_cap) != 0 ? scc_malloc((_cap) * sizeof(*(ring).data)) : null; \
(_cap) != 0 ? scc_malloc((_cap) * sizeof(*(ring).data)) : nullptr; \
(ring).cap = (_cap); \
(ring).head = 0; \
(ring).probe = 0; \
@@ -120,8 +120,8 @@
(ring).head = 0; \
(ring).probe = 0; \
(ring).tail = (size); \
(ring).fill = null; \
(ring).userdata = null; \
(ring).fill = nullptr; \
(ring).userdata = nullptr; \
} while (0)
/**
@@ -131,7 +131,7 @@
#define scc_ring_free(ring) \
do { \
scc_free((ring).data); \
(ring).data = NULL; \
(ring).data = nullptr; \
(ring).cap = (ring).head = (ring).probe = (ring).tail = 0; \
} while (0)

View File

@@ -22,12 +22,12 @@ typedef struct scc_str {
* @return cstring_t 初始化后的对象
*/
static inline scc_str_t scc_str_empty(void) {
return (scc_str_t){.data = null, .size = 0, .cap = 0};
return (scc_str_t){.data = nullptr, .size = 0, .cap = 0};
}
static inline void scc_str_init(scc_str_t *string) {
Assert(string != null);
string->data = null;
Assert(string != nullptr);
string->data = nullptr;
string->size = 0;
string->cap = 0;
}
@@ -39,7 +39,7 @@ static inline void scc_str_init(scc_str_t *string) {
* @return cstring_t 新建对象,包含输入字符串的副本
*/
static inline scc_str_t scc_str_from_cstr(const char *s) {
if (s == null) {
if (s == nullptr) {
return scc_str_empty();
}
@@ -49,7 +49,7 @@ static inline scc_str_t scc_str_from_cstr(const char *s) {
len++;
char *data = (char *)scc_malloc(len + 1);
Assert(data != null);
Assert(data != nullptr);
scc_memcpy(data, s, len);
data[len] = '\0';
@@ -66,12 +66,12 @@ static inline scc_str_t scc_str_copy(const scc_str_t *s) {
* @param str 要被释放的字符串指针
*/
static inline void scc_str_drop(scc_str_t *str) {
if (str == null) {
if (str == nullptr) {
return;
}
if (str->data != null) {
if (str->data != nullptr) {
scc_free(str->data);
str->data = null;
str->data = nullptr;
}
str->size = 0;
str->cap = 0;
@@ -86,7 +86,7 @@ static inline void scc_str_drop(scc_str_t *str) {
*/
static inline void scc_str_append_cstr(scc_str_t *str, const char *data,
usize len) {
if (str == null || data == null || len == 0) {
if (str == nullptr || data == nullptr || len == 0) {
return;
}
@@ -109,7 +109,7 @@ static inline void scc_str_append_cstr(scc_str_t *str, const char *data,
}
char *new_data = (char *)scc_realloc(str->data, new_cap);
Assert(new_data != null);
Assert(new_data != nullptr);
str->data = new_data;
str->cap = new_cap;
@@ -117,7 +117,7 @@ static inline void scc_str_append_cstr(scc_str_t *str, const char *data,
scc_memcpy(str->data + str->size - 1, data, len);
str->size += len;
Assert(str->data != null);
Assert(str->data != nullptr);
str->data[str->size - 1] = '\0'; // 保证 C 字符串兼容性
}
@@ -136,7 +136,7 @@ static inline void scc_str_append(scc_str_t *str, const scc_str_t *other) {
static inline void scc_str_append_fmt(scc_str_t *str, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
usize needed = scc_vsnprintf(null, 0, fmt, args);
usize needed = scc_vsnprintf(nullptr, 0, fmt, args);
va_end(args);
if (needed == 0)
return;
@@ -148,7 +148,7 @@ static inline void scc_str_append_fmt(scc_str_t *str, const char *fmt, ...) {
new_cap = new_cap ? new_cap * 2 : (needed + 16);
}
char *new_data = (char *)scc_realloc(str->data, new_cap);
Assert(new_data != NULL);
Assert(new_data != nullptr);
str->data = new_data;
str->cap = new_cap;
}
@@ -178,7 +178,7 @@ static inline void scc_str_append_ch(scc_str_t *str, char ch) {
* @return usize 字符串实际长度
*/
static inline usize scc_str_len(const scc_str_t *str) {
if (str == null) {
if (str == nullptr) {
return 0;
}
if (str->size == 0) {
@@ -194,7 +194,7 @@ static inline usize scc_str_len(const scc_str_t *str) {
* @return cbool
*/
static inline cbool scc_str_is_empty(const scc_str_t *str) {
return str == null || str->size == 0;
return str == nullptr || str->size == 0;
}
/**
@@ -218,8 +218,8 @@ static inline void scc_str_clear(scc_str_t *str) {
* @return char* 返回指向内部缓冲区的 C 风格字符串指针
*/
static inline char *scc_str_as_cstr(const scc_str_t *str) {
if (str == null || str->data == null) {
return null;
if (str == nullptr || str->data == nullptr) {
return nullptr;
}
return str->data;
}
@@ -229,11 +229,11 @@ static inline int scc_str_equal(const scc_str_t *str1, const scc_str_t *str2) {
}
static inline char *scc_str_move_cstr(scc_str_t *str) {
if (str == null || str->data == null) {
return null;
if (str == nullptr || str->data == nullptr) {
return nullptr;
}
char *ret = str->data;
str->data = null;
str->data = nullptr;
str->cap = 0;
str->size = 0;
return ret;

View File

@@ -26,14 +26,14 @@ typedef float f32;
typedef double f64;
typedef bool cbool;
/// void / null
#define null NULL
/// void / nullptr
#ifndef nullptr
#define nullptr NULL
#endif
/* clang-format on */
static_assert(sizeof(cbool) == 1, "cbool size must 1");
#else
/* clang-format off */
#else
#include <stdarg.h>
#include <stdbool.h>
@@ -58,8 +58,8 @@ typedef double f64;
typedef bool cbool;
#ifndef null
#define null NULL
#ifndef nullptr
#define nullptr nullptr
#endif
/* clang-format on */

View File

@@ -106,7 +106,7 @@ typedef size_t usize;
int cap = (vec).cap ? (vec).cap * 2 : 4; \
scc_vec_realloc(vec, cap); \
} \
Assert((vec).data != null); \
Assert((vec).data != nullptr); \
(vec).data[(vec).size++] = value; \
} while (0)
@@ -147,7 +147,7 @@ typedef size_t usize;
#define scc_vec_free(vec) \
do { \
__scc_vec_free((vec).data); \
(vec).data = NULL; \
(vec).data = nullptr; \
(vec).size = (vec).cap = 0; \
} while (0)

View File

@@ -51,7 +51,7 @@ void reset_token_fill(void) { token_id = 0; }
void free_token(test_token_t *tok) {
if (tok->data) {
scc_free(tok->data);
tok->data = NULL;
tok->data = nullptr;
}
}
@@ -323,4 +323,4 @@ TEST_LIST = {{"test_char_ring_basic", test_char_ring_basic},
{"test_char_ring_wrap", test_char_ring_wrap},
{"test_token_ring_basic", test_token_ring_basic},
{"test_token_ring_full", test_token_ring_full},
{NULL, NULL}};
{nullptr, nullptr}};

View File

@@ -10,9 +10,9 @@
#ifndef __KLLIST_H__
#define __KLLIST_H__
#ifndef NULL
#define NULL (0)
#define __NULL_KLIST_DEFINED__
#ifndef nullptr
#define nullptr (0)
#define __nullptr_KLIST_DEFINED__
#endif
#ifndef container_of
@@ -88,8 +88,8 @@ static inline void __list_del(struct list_head *prev, struct list_head *next) {
static inline void list_del(struct list_head *entry) {
__list_del(entry->prev, entry->next);
entry->next = NULL;
entry->prev = NULL;
entry->next = nullptr;
entry->prev = nullptr;
}
/**
@@ -159,8 +159,9 @@ typedef int (*list_cmp_func_t)(void *, const struct list_head *,
static void list_sort(void *priv, struct list_head *head, list_cmp_func_t cmp);
#endif
#if defined(__NULL_KLIST_DEFINED__) && !defined(__NULL_KLIST_DEFINED_NOMOVE__)
#undef NULL
#if defined(__nullptr_KLIST_DEFINED__) && \
!defined(__nullptr_KLIST_DEFINED_NOMOVE__)
#undef nullptr
#endif
#endif

View File

@@ -76,7 +76,7 @@ void scc_hashtable_init(scc_hashtable_t *ht,
* @param ht 哈希表实例指针
* @param key 键指针
* @param value 值指针
* @return 被替换的旧值指针(无替换返回NULL
* @return 被替换的旧值指针(无替换返回nullptr
*/
void *scc_hashtable_set(scc_hashtable_t *ht, const void *key, void *value);
@@ -84,7 +84,7 @@ void *scc_hashtable_set(scc_hashtable_t *ht, const void *key, void *value);
* @brief 查找键对应值
* @param ht 哈希表实例指针
* @param key 查找键指针
* @return 找到返回值指针,未找到返回NULL
* @return 找到返回值指针,未找到返回nullptr
*/
void *scc_hashtable_get(scc_hashtable_t *ht, const void *key);
@@ -92,7 +92,7 @@ void *scc_hashtable_get(scc_hashtable_t *ht, const void *key);
* @brief 删除键值对
* @param ht 哈希表实例指针
* @param key 要删除的键指针
* @return 被删除的值指针(不存在返回NULL
* @return 被删除的值指针(不存在返回nullptr
*
* @note 实际采用墓碑标记方式删除
*/

View File

@@ -13,7 +13,7 @@ void scc_hashtable_init(scc_hashtable_t *ht,
ht->tombstone_count = 0;
ht->cmp_func = cmp_func;
ht->hash_func = hash_func;
Assert(ht->cmp_func != null && ht->hash_func != null);
Assert(ht->cmp_func != nullptr && ht->hash_func != nullptr);
}
static int next_power_of_two(int n) {
@@ -29,12 +29,12 @@ static int next_power_of_two(int n) {
static scc_hashtable_entry_t *find_entry(scc_hashtable_t *ht, const void *key,
u32 hash) {
if (ht->entries.cap == 0)
return NULL;
return nullptr;
u32 index = hash & (ht->entries.cap - 1); // 容量是2的幂
u32 probe = 0;
scc_hashtable_entry_t *tombstone = NULL;
scc_hashtable_entry_t *tombstone = nullptr;
while (1) {
scc_hashtable_entry_t *entry = &scc_vec_at(ht->entries, index);
@@ -56,7 +56,7 @@ static scc_hashtable_entry_t *find_entry(scc_hashtable_t *ht, const void *key,
break;
}
LOG_ERROR("hashset_find: hash table is full");
return NULL;
return nullptr;
}
static void adjust_capacity(scc_hashtable_t *ht, usize new_cap) {
@@ -71,7 +71,7 @@ static void adjust_capacity(scc_hashtable_t *ht, usize new_cap) {
ht->entries.size = new_cap;
ht->entries.cap = new_cap;
ht->entries.data =
scc_realloc(NULL, new_cap * sizeof(scc_hashtable_entry_t));
scc_realloc(nullptr, new_cap * sizeof(scc_hashtable_entry_t));
scc_memset(ht->entries.data, 0, new_cap * sizeof(scc_hashtable_entry_t));
// rehash the all of the old data
@@ -99,7 +99,7 @@ void *scc_hashtable_set(scc_hashtable_t *ht, const void *key, void *value) {
u32 hash = ht->hash_func(key);
scc_hashtable_entry_t *entry = find_entry(ht, key, hash);
void *old_value = NULL;
void *old_value = nullptr;
if (entry->state == ENTRY_ACTIVE) {
old_value = entry->value;
} else {
@@ -117,22 +117,22 @@ void *scc_hashtable_set(scc_hashtable_t *ht, const void *key, void *value) {
void *scc_hashtable_get(scc_hashtable_t *ht, const void *key) {
if (ht->entries.cap == 0)
return NULL;
return nullptr;
u32 hash = ht->hash_func(key);
scc_hashtable_entry_t *entry = find_entry(ht, key, hash);
return (entry && entry->state == ENTRY_ACTIVE) ? entry->value : NULL;
return (entry && entry->state == ENTRY_ACTIVE) ? entry->value : nullptr;
}
void *scc_hashtable_del(scc_hashtable_t *ht, const void *key) {
if (ht->entries.cap == 0)
return NULL;
return nullptr;
u32 hash = ht->hash_func(key);
scc_hashtable_entry_t *entry = find_entry(ht, key, hash);
if (entry == NULL || entry->state != ENTRY_ACTIVE)
return NULL;
if (entry == nullptr || entry->state != ENTRY_ACTIVE)
return nullptr;
void *value = entry->value;
entry->state = ENTRY_TOMBSTONE;

View File

@@ -15,7 +15,7 @@ const char *scc_strpool_intern(scc_strpool_t *pool, const char *str) {
char *new_str = scc_malloc(len);
if (!new_str) {
LOG_ERROR("strpool: Failed to allocate memory for string");
return NULL;
return nullptr;
}
scc_memcpy(new_str, str, len);