Skip to content

Commit

Permalink
libdrgn: use if instead of switch for dw_foo_str
Browse files Browse the repository at this point in the history
Some constant types (e.g. DW_OP) have duplicate values for vendor
specific extensions, so generating the dw_foo_str function for them
results in "duplicate case value" compiler errors. Use if statements
instead and use the first match. This doesn't change code generated for
dw_tag_str with either GCC or Clang.

Signed-off-by: Omar Sandoval <[email protected]>
  • Loading branch information
osandov authored and Asphaltt committed Oct 4, 2023
1 parent 659c446 commit 2fd4af2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
9 changes: 3 additions & 6 deletions libdrgn/dwarf_constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@

#include "dwarf_constants.h"

#define X(name, value) case name: return #name;
#define X(name, _) if (value == name) return #name;

const char *dw_tag_str(int value, char buf[static DW_TAG_STR_BUF_LEN])
{
switch (value) {
DW_TAG_DEFINITIONS
default:
snprintf(buf, DW_TAG_STR_BUF_LEN, DW_TAG_STR_UNKNOWN_FORMAT, value);
return buf;
}
snprintf(buf, DW_TAG_STR_BUF_LEN, DW_TAG_STR_UNKNOWN_FORMAT, value);
return buf;
}

#undef X
9 changes: 3 additions & 6 deletions scripts/gen_dwarf_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def gen_dwarf_constants_c(
#include "dwarf_constants.h"
#define X(name, value) case name: return #name;
#define X(name, _) if (value == name) return #name;
"""
)
for constant_type in dwarf_constants:
Expand All @@ -211,12 +211,9 @@ def gen_dwarf_constants_c(
f"""
const char *{constant_type.name.lower()}_str(int value, char buf[static {constant_type.name}_STR_BUF_LEN])
{{
switch (value) {{
{constant_type.name}_DEFINITIONS
default:
snprintf(buf, {constant_type.name}_STR_BUF_LEN, {constant_type.name}_STR_UNKNOWN_FORMAT, value);
return buf;
}}
snprintf(buf, {constant_type.name}_STR_BUF_LEN, {constant_type.name}_STR_UNKNOWN_FORMAT, value);
return buf;
}}
"""
)
Expand Down

0 comments on commit 2fd4af2

Please sign in to comment.