Adopted case-as-label style in switch statements

So:

  switch (cond) {
  case 0:;
      // first case
      break;

  case 1:;
      // second case
      break;

  default:;
      // default case
      break;
  }

This basically adopts our current label style for the case statements in
switch statements. It initially looks like quite a monstrosity, but I
think it does a good job at highlighting that case statements in C are
no safer than labels and gotos.

I would not use this style in a language with better scoping in switch
statements.

I'd prefer not to use switch statements, their scoping rules in C are
just too error-prone, and the compiler usually optimizes things out
anyways, but there are some places where switch statements are clearly
the correct organization -- state machines such as lfsr_traversal_read
for example.

If you're curious about the ':;' ending, this is used in our current
style for labels to avoid "declaration is not a statement" warnings.
Which I think is just a bit of leftover from C historically not having
mixed statements/declarations.
This commit is contained in:
Christopher Haster
2024-01-20 22:00:59 -06:00
parent 6fc040db1a
commit 4ce582bf9b
3 changed files with 738 additions and 712 deletions
+6 -6
View File
@@ -9313,7 +9313,7 @@ int lfsr_file_opencfg(lfs_t *lfs, lfsr_file_t *file,
0, file->buffer, lfsr_bshrub_size(&file->bshrub));
if (d < 0) {
err = d;
goto failed_with_buffer;
goto failed;
}
// small files remain perpetually unflushed
@@ -9327,7 +9327,7 @@ int lfsr_file_opencfg(lfs_t *lfs, lfsr_file_t *file,
lfsr_addopened(lfs, &file->m);
return 0;
failed_with_buffer:;
failed:;
// clean up memory
if (!file->cfg->buffer) {
lfs_free(file->buffer);
@@ -14777,7 +14777,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
lfs->rcache.buffer = lfs_malloc(lfs->cfg->cache_size);
if (!lfs->rcache.buffer) {
err = LFS_ERR_NOMEM;
goto cleanup;
goto failed;
}
}
@@ -14788,7 +14788,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
lfs->pcache.buffer = lfs_malloc(lfs->cfg->cache_size);
if (!lfs->pcache.buffer) {
err = LFS_ERR_NOMEM;
goto cleanup;
goto failed;
}
}
@@ -14805,7 +14805,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
lfs->lookahead.buffer = lfs_malloc(lfs->cfg->lookahead_size);
if (!lfs->lookahead.buffer) {
err = LFS_ERR_NOMEM;
goto cleanup;
goto failed;
}
}
lfs->lookahead.start = 0;
@@ -14881,7 +14881,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
return 0;
cleanup:
failed:;
lfs_deinit(lfs);
return err;
}