Skip to content

Commit

Permalink
gcc: Use __builtin_unreachable where appropriate.
Browse files Browse the repository at this point in the history
There are several instances of code, mostly functions
with a switch statement that either have a default or
enumerate all cases explicitly with no default, where
NULL (or some useless value) is returned to satisfy
the function's return value. However, this is dead
code; bbs_assert(0) was frequently used to indicate this.
Instead, use __builtin_unreachable, which is more
semantic, and possibly more performant.
  • Loading branch information
InterLinked1 committed Nov 24, 2023
1 parent 7b38c23 commit 576dc9a
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 17 deletions.
5 changes: 2 additions & 3 deletions bbs/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,7 @@ static const char *loglevel2str(enum bbs_log_level level, int term)
case LOG_DEBUG:
return COLOR_LOG_OR(" DEBUG", COLOR_GREEN, term);
}
bbs_assert(0);
return NULL;
__builtin_unreachable();
}

static const char *verbose_prefix(int level)
Expand All @@ -301,7 +300,7 @@ static const char *verbose_prefix(int level)
default:
bbs_assert(0);
}
return NULL;
__builtin_unreachable();
}

#define log_puts(msg) fprintf(logfp, "%s", msg); fflush(logfp);
Expand Down
3 changes: 1 addition & 2 deletions modules/mod_discord.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ static const char *status_str(enum user_status status)
case STATUS_NONE:
return "none";
}
bbs_assert(0);
return NULL;
__builtin_unreachable();
}

static void remove_user(struct discord_user *user)
Expand Down
2 changes: 1 addition & 1 deletion modules/mod_irc_relay.c
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ static void command_cb(const char *clientname, enum irc_callback_msg_type type,
}
break;
case CMD_QUIT: /* This is academic, as QUIT is handled above */
bbs_assert(0);
__builtin_unreachable();
#if 0
ourchan = MAP1_MATCH(cp, clientname, channel) ? cp->channel2 : cp->channel1;
snprintf(sysmsg, sizeof(sysmsg), ":%s/%s QUIT :%s", clientname, prefix, S_IF(msg));
Expand Down
3 changes: 1 addition & 2 deletions modules/mod_mail.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,7 @@ const char *mailbox_event_type_name(enum mailbox_event_type type)
return "UIDVALIDITYChange";
/* No default case */
}
bbs_assert(0);
return NULL;
__builtin_unreachable();
}

static pthread_mutex_t eventidlock = PTHREAD_MUTEX_INITIALIZER;
Expand Down
11 changes: 4 additions & 7 deletions nets/net_smtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ static int handle_auth(struct smtp_session *smtp, char *s)
free(pass);
goto logindone;
} else {
bbs_assert(0);
__builtin_unreachable();
}
return 0;

Expand Down Expand Up @@ -1005,8 +1005,7 @@ static const char *smtp_filter_type_name(enum smtp_filter_type type)
case SMTP_FILTER_PREPEND: return "PREPEND";
/* No default */
}
bbs_assert(0);
return NULL;
__builtin_unreachable();
}

int __smtp_filter_register(struct smtp_filter_provider *provider, enum smtp_filter_type type, enum smtp_filter_scope scope, enum smtp_direction dir, int priority, void *mod)
Expand Down Expand Up @@ -1417,8 +1416,7 @@ static const char *delivery_action_name(enum smtp_delivery_action action)
case DELIVERY_EXPANDED: return "expanded";
/* No default */
}
bbs_assert(0);
return NULL;
__builtin_unreachable();
}

static const char *delivery_subject_name(struct smtp_delivery_outcome **f, int n)
Expand All @@ -1436,8 +1434,7 @@ static const char *delivery_subject_name(struct smtp_delivery_outcome **f, int n
case DELIVERY_EXPANDED: return "Delivery Status Notification (Expanded)";
/* No default */
}
bbs_assert(0);
return NULL;
__builtin_unreachable();
}

/* Forward declaration */
Expand Down
3 changes: 1 addition & 2 deletions tests/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ static const char *loglevel2str(enum bbs_log_level level)
default:
break;
}
assert(0);
return NULL;
__builtin_unreachable();
}

/*! \brief Minimal implementation of __bbs_log, so we can use the same interface */
Expand Down

0 comments on commit 576dc9a

Please sign in to comment.