Skip to content

Commit

Permalink
libdrgn: require semicolon after DEFINE_{HASH,VECTOR,BINARY_SEARCH_TR…
Browse files Browse the repository at this point in the history
…EE}*

The lack of a semicolon after these macros has always confused tooling
like cscope. We could add semicolons everywhere now, but let's enforce
it for the future, too. Let's add a dummy struct forward declaration at
the end of each macro that enforces this requirement and also provides a
useful error message.

Signed-off-by: Omar Sandoval <[email protected]>
  • Loading branch information
osandov committed Aug 2, 2023
1 parent 968abed commit c8406e1
Show file tree
Hide file tree
Showing 19 changed files with 82 additions and 74 deletions.
8 changes: 5 additions & 3 deletions libdrgn/binary_search_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ typedef typeof(entry_type) tree##_entry_type; \
\
struct tree { \
struct binary_tree_node *root; \
};
}; \
struct DEFINE_BINARY_SEARCH_TREE_needs_semicolon

/**
* Define the functions for a binary search tree.
Expand Down Expand Up @@ -559,7 +560,8 @@ static struct tree##_iterator tree##_next_post_order(struct tree##_iterator it)
tree##_node_to_entry(node->parent), \
}; \
} \
}
} \
struct DEFINE_BINARY_SEARCH_TREE_needs_semicolon

/**
* Define a binary search tree interface.
Expand All @@ -582,7 +584,7 @@ static struct tree##_iterator tree##_next_post_order(struct tree##_iterator it)
*/
#define DEFINE_BINARY_SEARCH_TREE(tree, entry_type, member, entry_to_key, \
cmp_func, variant) \
DEFINE_BINARY_SEARCH_TREE_TYPE(tree, entry_type) \
DEFINE_BINARY_SEARCH_TREE_TYPE(tree, entry_type); \
DEFINE_BINARY_SEARCH_TREE_FUNCTIONS(tree, member, entry_to_key, cmp_func, \
variant)

Expand Down
13 changes: 7 additions & 6 deletions libdrgn/debug_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ static inline Dwarf *drgn_elf_file_dwarf_key(struct drgn_elf_file * const *entry
return (*entry)->dwarf;
}
DEFINE_HASH_TABLE_FUNCTIONS(drgn_elf_file_dwarf_table, drgn_elf_file_dwarf_key,
ptr_key_hash_pair, scalar_key_eq)
DEFINE_VECTOR_FUNCTIONS(drgn_module_vector)
ptr_key_hash_pair, scalar_key_eq);
DEFINE_VECTOR_FUNCTIONS(drgn_module_vector);

struct drgn_module_key {
const void *build_id;
Expand Down Expand Up @@ -66,9 +66,10 @@ static inline bool drgn_module_key_eq(const struct drgn_module_key *a,
a->start == b->start && a->end == b->end);
}
DEFINE_HASH_TABLE_FUNCTIONS(drgn_module_table, drgn_module_key,
drgn_module_key_hash_pair, drgn_module_key_eq)
drgn_module_key_hash_pair, drgn_module_key_eq);

DEFINE_HASH_SET_FUNCTIONS(c_string_set, c_string_key_hash_pair, c_string_key_eq)
DEFINE_HASH_SET_FUNCTIONS(c_string_set, c_string_key_hash_pair,
c_string_key_eq);

/**
* @c Dwfl_Callbacks::find_elf() implementation.
Expand Down Expand Up @@ -598,11 +599,11 @@ struct drgn_mapped_file_segment {
uint64_t file_offset;
};

DEFINE_VECTOR(drgn_mapped_file_segment_vector, struct drgn_mapped_file_segment)
DEFINE_VECTOR(drgn_mapped_file_segment_vector, struct drgn_mapped_file_segment);

DEFINE_HASH_MAP(drgn_mapped_files, const char *,
struct drgn_mapped_file_segment_vector, c_string_key_hash_pair,
c_string_key_eq)
c_string_key_eq);

struct userspace_core_report_state {
struct drgn_mapped_files files;
Expand Down
8 changes: 4 additions & 4 deletions libdrgn/debug_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ enum drgn_module_state {
DRGN_DEBUG_INFO_MODULE_INDEXED,
} __attribute__((__packed__));

DEFINE_HASH_TABLE_TYPE(drgn_elf_file_dwarf_table, struct drgn_elf_file *)
DEFINE_HASH_TABLE_TYPE(drgn_elf_file_dwarf_table, struct drgn_elf_file *);

/**
* A module reported to a @ref drgn_debug_info.
Expand Down Expand Up @@ -124,9 +124,9 @@ struct drgn_module {
struct drgn_module *next;
};

DEFINE_HASH_TABLE_TYPE(drgn_module_table, struct drgn_module *)
DEFINE_HASH_TABLE_TYPE(drgn_module_table, struct drgn_module *);

DEFINE_HASH_SET_TYPE(c_string_set, const char *)
DEFINE_HASH_SET_TYPE(c_string_set, const char *);

/** Cache of debugging information. */
struct drgn_debug_info {
Expand Down Expand Up @@ -155,7 +155,7 @@ struct drgn_error *drgn_debug_info_create(struct drgn_program *prog,
/** Destroy a @ref drgn_debug_info. */
void drgn_debug_info_destroy(struct drgn_debug_info *dbinfo);

DEFINE_VECTOR_TYPE(drgn_module_vector, struct drgn_module *)
DEFINE_VECTOR_TYPE(drgn_module_vector, struct drgn_module *);

/** State tracked while loading debugging information. */
struct drgn_debug_info_load_state {
Expand Down
33 changes: 17 additions & 16 deletions libdrgn/dwarf_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ drgn_dwarf_specification_to_key(const struct drgn_dwarf_specification *entry)
}
DEFINE_HASH_TABLE_FUNCTIONS(drgn_dwarf_specification_map,
drgn_dwarf_specification_to_key, int_key_hash_pair,
scalar_key_eq)
scalar_key_eq);

/**
* Placeholder for drgn_dwarf_index_cu::file_name_hashes if the CU has no
Expand Down Expand Up @@ -150,9 +150,10 @@ struct drgn_dwarf_index_cu {
const char *str_offsets;
};

DEFINE_VECTOR_FUNCTIONS(drgn_dwarf_index_cu_vector)
DEFINE_VECTOR_FUNCTIONS(drgn_dwarf_index_cu_vector);

DEFINE_HASH_MAP_FUNCTIONS(drgn_dwarf_type_map, ptr_key_hash_pair, scalar_key_eq)
DEFINE_HASH_MAP_FUNCTIONS(drgn_dwarf_type_map, ptr_key_hash_pair,
scalar_key_eq);

/** DIE which needs to be indexed. */
struct drgn_dwarf_index_pending_die {
Expand All @@ -164,7 +165,7 @@ struct drgn_dwarf_index_pending_die {
uintptr_t addr;
};

DEFINE_VECTOR_FUNCTIONS(drgn_dwarf_index_pending_die_vector)
DEFINE_VECTOR_FUNCTIONS(drgn_dwarf_index_pending_die_vector);

/** DIE indexed in a @ref drgn_namespace_dwarf_index. */
struct drgn_dwarf_index_die {
Expand Down Expand Up @@ -200,8 +201,8 @@ struct drgn_dwarf_index_die {
};

DEFINE_HASH_MAP(drgn_dwarf_index_die_map, struct nstring, uint32_t,
nstring_hash_pair, nstring_eq)
DEFINE_VECTOR(drgn_dwarf_index_die_vector, struct drgn_dwarf_index_die)
nstring_hash_pair, nstring_eq);
DEFINE_VECTOR(drgn_dwarf_index_die_vector, struct drgn_dwarf_index_die);

#define DRGN_DWARF_INDEX_SHARD_BITS 8
static const size_t DRGN_DWARF_INDEX_NUM_SHARDS = 1 << DRGN_DWARF_INDEX_SHARD_BITS;
Expand Down Expand Up @@ -427,9 +428,9 @@ enum drgn_dwarf_index_abbrev_insn {
static_assert(NUM_INSNS - 1 == UINT8_MAX,
"maximum DWARF index instruction is invalid");

DEFINE_VECTOR(uint8_vector, uint8_t)
DEFINE_VECTOR(uint32_vector, uint32_t)
DEFINE_VECTOR(uint64_vector, uint64_t)
DEFINE_VECTOR(uint8_vector, uint8_t);
DEFINE_VECTOR(uint32_vector, uint32_t);
DEFINE_VECTOR(uint64_vector, uint64_t);

struct drgn_dwarf_index_cu_buffer {
struct binary_buffer bb;
Expand Down Expand Up @@ -926,7 +927,7 @@ struct path_hash_chunk {
struct path_hash_chunk *next;
};

DEFINE_VECTOR(path_hash_vector, const struct path_hash *)
DEFINE_VECTOR(path_hash_vector, const struct path_hash *);

struct lnp_entry_format {
uint64_t content_type;
Expand Down Expand Up @@ -3025,7 +3026,7 @@ drgn_debug_info_main_language(struct drgn_debug_info *dbinfo,
* DIE iteration.
*/

DEFINE_VECTOR(dwarf_die_vector, Dwarf_Die)
DEFINE_VECTOR(dwarf_die_vector, Dwarf_Die);

/** Iterator over DWARF DIEs in a @ref drgn_module. */
struct drgn_dwarf_die_iterator {
Expand Down Expand Up @@ -6442,7 +6443,7 @@ struct array_dimension {
bool is_complete;
};

DEFINE_VECTOR(array_dimension_vector, struct array_dimension)
DEFINE_VECTOR(array_dimension_vector, struct array_dimension);

static struct drgn_error *subrange_length(Dwarf_Die *die,
struct array_dimension *dimension)
Expand Down Expand Up @@ -7063,10 +7064,10 @@ struct drgn_dwarf_cie {
size_t initial_instructions_size;
};

DEFINE_VECTOR(drgn_dwarf_fde_vector, struct drgn_dwarf_fde)
DEFINE_VECTOR(drgn_dwarf_cie_vector, struct drgn_dwarf_cie)
DEFINE_VECTOR(drgn_dwarf_fde_vector, struct drgn_dwarf_fde);
DEFINE_VECTOR(drgn_dwarf_cie_vector, struct drgn_dwarf_cie);
DEFINE_HASH_MAP(drgn_dwarf_cie_map, size_t, size_t, int_key_hash_pair,
scalar_key_eq)
scalar_key_eq);

static struct drgn_error *
drgn_dwarf_cfi_next_encoded(struct drgn_elf_file_section_buffer *buffer,
Expand Down Expand Up @@ -7616,7 +7617,7 @@ drgn_dwarf_cfi_next_block(struct drgn_elf_file_section_buffer *buffer,
return NULL;
}

DEFINE_VECTOR(drgn_cfi_row_vector, struct drgn_cfi_row *)
DEFINE_VECTOR(drgn_cfi_row_vector, struct drgn_cfi_row *);

static struct drgn_error *
drgn_eval_dwarf_cfi(struct drgn_elf_file *file, enum drgn_section_index scn,
Expand Down
8 changes: 4 additions & 4 deletions libdrgn/dwarf_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ struct drgn_module_dwarf_info {
void drgn_module_dwarf_info_deinit(struct drgn_module *module);

DEFINE_VECTOR_TYPE(drgn_dwarf_index_pending_die_vector,
struct drgn_dwarf_index_pending_die)
struct drgn_dwarf_index_pending_die);

/**
* Index of DWARF information for a namespace by entity name.
Expand Down Expand Up @@ -108,9 +108,9 @@ struct drgn_dwarf_specification {
};

DEFINE_HASH_TABLE_TYPE(drgn_dwarf_specification_map,
struct drgn_dwarf_specification)
struct drgn_dwarf_specification);

DEFINE_VECTOR_TYPE(drgn_dwarf_index_cu_vector, struct drgn_dwarf_index_cu)
DEFINE_VECTOR_TYPE(drgn_dwarf_index_cu_vector, struct drgn_dwarf_index_cu);

/** Cached type in a @ref drgn_debug_info. */
struct drgn_dwarf_type {
Expand All @@ -125,7 +125,7 @@ struct drgn_dwarf_type {
bool is_incomplete_array;
};

DEFINE_HASH_MAP_TYPE(drgn_dwarf_type_map, const void *, struct drgn_dwarf_type)
DEFINE_HASH_MAP_TYPE(drgn_dwarf_type_map, const void *, struct drgn_dwarf_type);

/** DWARF debugging information for a program/@ref drgn_debug_info. */
struct drgn_dwarf_info {
Expand Down
14 changes: 8 additions & 6 deletions libdrgn/hash_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
* key_type entry_to_key(const entry_type *entry);
* struct hash_pair hash_func(const key_type *key);
* bool eq_func(const key_type *a, const key_type *b);
* DEFINE_HASH_TABLE(hash_table, entry_type, entry_to_key, hash_func, eq_func)
* DEFINE_HASH_TABLE(hash_table, entry_type, entry_to_key, hash_func, eq_func);
* @endcode
*
* @sa BinarySearchTrees
Expand Down Expand Up @@ -406,7 +406,8 @@ struct table { \
size_t size; \
uintptr_t first_packed; \
} basic[!table##_vector_policy]; \
};
}; \
struct DEFINE_HASH_TABLE_needs_semicolon

/*
* Common search function implementation returning an item iterator. This is
Expand Down Expand Up @@ -1406,7 +1407,8 @@ static struct table##_iterator table##_next(struct table##_iterator it) \
} else { \
return table##_next_impl(it, false); \
} \
}
} \
struct DEFINE_HASH_TABLE_needs_semicolon

/**
* Define a hash table interface.
Expand All @@ -1425,7 +1427,7 @@ static struct table##_iterator table##_next(struct table##_iterator it) \
* *</tt> and returns a @c bool.
*/
#define DEFINE_HASH_TABLE(table, entry_type, entry_to_key, hash_func, eq_func) \
DEFINE_HASH_TABLE_TYPE(table, entry_type) \
DEFINE_HASH_TABLE_TYPE(table, entry_type); \
DEFINE_HASH_TABLE_FUNCTIONS(table, entry_to_key, hash_func, eq_func)

/**
Expand Down Expand Up @@ -1479,7 +1481,7 @@ DEFINE_HASH_TABLE_FUNCTIONS(table, HASH_MAP_ENTRY_TO_KEY, hash_func, eq_func)
* @param[in] eq_func See @ref DEFINE_HASH_TABLE().
*/
#define DEFINE_HASH_MAP(table, key_type, value_type, hash_func, eq_func) \
DEFINE_HASH_MAP_TYPE(table, key_type, value_type) \
DEFINE_HASH_MAP_TYPE(table, key_type, value_type); \
DEFINE_HASH_MAP_FUNCTIONS(table, hash_func, eq_func)

/**
Expand Down Expand Up @@ -1520,7 +1522,7 @@ DEFINE_HASH_TABLE_FUNCTIONS(table, HASH_SET_ENTRY_TO_KEY, hash_func, eq_func)
* @param[in] eq_func See @ref DEFINE_HASH_TABLE().
*/
#define DEFINE_HASH_SET(table, key_type, hash_func, eq_func) \
DEFINE_HASH_SET_TYPE(table, key_type) \
DEFINE_HASH_SET_TYPE(table, key_type); \
DEFINE_HASH_SET_FUNCTIONS(table, hash_func, eq_func)

/**
Expand Down
2 changes: 1 addition & 1 deletion libdrgn/language_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ struct compound_initializer_state {
uint64_t bit_offset;
};

DEFINE_VECTOR(compound_initializer_stack, struct compound_initializer_state)
DEFINE_VECTOR(compound_initializer_stack, struct compound_initializer_state);

struct compound_initializer_iter {
struct initializer_iter iter;
Expand Down
2 changes: 1 addition & 1 deletion libdrgn/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "drgn.h"
#include "lexer.h"

DEFINE_VECTOR_FUNCTIONS(drgn_token_vector)
DEFINE_VECTOR_FUNCTIONS(drgn_token_vector);

void drgn_lexer_init(struct drgn_lexer *lexer, drgn_lexer_func func,
const char *str)
Expand Down
2 changes: 1 addition & 1 deletion libdrgn/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct drgn_token {
size_t len;
};

DEFINE_VECTOR_TYPE(drgn_token_vector, struct drgn_token)
DEFINE_VECTOR_TYPE(drgn_token_vector, struct drgn_token);

/**
* Lexer instance.
Expand Down
4 changes: 2 additions & 2 deletions libdrgn/linux_kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ static struct drgn_error *identify_kernel_elf(Elf *elf,
}

DEFINE_HASH_MAP(elf_scn_name_map, const char *, Elf_Scn *,
c_string_key_hash_pair, c_string_key_eq)
c_string_key_hash_pair, c_string_key_eq);

static struct drgn_error *
cache_kernel_module_sections(struct kernel_module_iterator *kmod_it, Elf *elf)
Expand Down Expand Up @@ -1348,7 +1348,7 @@ kernel_module_table_key(struct kernel_module_file * const *entry)
}

DEFINE_HASH_TABLE(kernel_module_table, struct kernel_module_file *,
kernel_module_table_key, nstring_hash_pair, nstring_eq)
kernel_module_table_key, nstring_hash_pair, nstring_eq);

static struct drgn_error *
report_loaded_kernel_module(struct drgn_debug_info_load_state *load,
Expand Down
2 changes: 1 addition & 1 deletion libdrgn/memory_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ drgn_memory_segment_to_key(const struct drgn_memory_segment *entry)

DEFINE_BINARY_SEARCH_TREE_FUNCTIONS(drgn_memory_segment_tree, node,
drgn_memory_segment_to_key,
binary_search_tree_scalar_cmp, splay)
binary_search_tree_scalar_cmp, splay);

void drgn_memory_reader_init(struct drgn_memory_reader *reader)
{
Expand Down
2 changes: 1 addition & 1 deletion libdrgn/memory_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/

DEFINE_BINARY_SEARCH_TREE_TYPE(drgn_memory_segment_tree,
struct drgn_memory_segment)
struct drgn_memory_segment);

/**
* Memory reader.
Expand Down
8 changes: 4 additions & 4 deletions libdrgn/program.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ static inline uint32_t drgn_thread_to_key(const struct drgn_thread *entry)
return entry->tid;
}

DEFINE_VECTOR_FUNCTIONS(drgn_prstatus_vector)
DEFINE_VECTOR_FUNCTIONS(drgn_prstatus_vector);
DEFINE_HASH_TABLE_FUNCTIONS(drgn_thread_set, drgn_thread_to_key,
int_key_hash_pair, scalar_key_eq)
int_key_hash_pair, scalar_key_eq);

struct drgn_thread_iterator {
struct drgn_program *prog;
Expand Down Expand Up @@ -1603,7 +1603,7 @@ drgn_program_read_memory(struct drgn_program *prog, void *buf, uint64_t address,
return NULL;
}

DEFINE_VECTOR(char_vector, char)
DEFINE_VECTOR(char_vector, char);

LIBDRGN_PUBLIC struct drgn_error *
drgn_program_read_c_string(struct drgn_program *prog, uint64_t address,
Expand Down Expand Up @@ -1771,7 +1771,7 @@ drgn_program_find_symbol_by_address(struct drgn_program *prog, uint64_t address,
return NULL;
}

DEFINE_VECTOR(symbolp_vector, struct drgn_symbol *)
DEFINE_VECTOR(symbolp_vector, struct drgn_symbol *);

enum {
SYMBOLS_SEARCH_NAME = (1 << 0),
Expand Down
6 changes: 3 additions & 3 deletions libdrgn/program.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ struct drgn_thread {
struct drgn_object object;
};

DEFINE_VECTOR_TYPE(drgn_typep_vector, struct drgn_type *)
DEFINE_VECTOR_TYPE(drgn_prstatus_vector, struct nstring)
DEFINE_HASH_TABLE_TYPE(drgn_thread_set, struct drgn_thread)
DEFINE_VECTOR_TYPE(drgn_typep_vector, struct drgn_type *);
DEFINE_VECTOR_TYPE(drgn_prstatus_vector, struct nstring);
DEFINE_HASH_TABLE_TYPE(drgn_thread_set, struct drgn_thread);

struct drgn_program {
/** @privatesection */
Expand Down
Loading

0 comments on commit c8406e1

Please sign in to comment.