enable fallthrough warnings on clang

From: Peter Eisentraut <peter(at)eisentraut(dot)org>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: enable fallthrough warnings on clang
Date: 2026-01-20 11:16:31
Message-ID: 76a8efcd-925a-4eaf-bdd1-d972cd1a32ff@eisentraut.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

The warnings about fallthrough in switch statements currently only work
on gcc. It would be nice if they also worked on clang, and in the long
run other compilers.

gcc has a convention of annotation with comments, like /*fallthrough*/,
but that is not available in other compilers. The standard for this is
using attributes, like [[fallthrough]] in C23 and C++, and
__attribute__((fallthrough)) as extensions in gcc and clang. (MSVC
doesn't have anything like this in C mode, but it supports
[[fallthrough]] in C++, so there is a plausible path to get this into C
sometime as well.)

So my proposal is that we wrap the appropriate attribute into a
pg_fallthrough macro, and replace the current comments with that.

There is a bit of fiddliness in setting the right compiler warning
option. You need -Wimplicit-fallthrough=5 with gcc, but
-Wimplicit-fallthrough with clang, but you don't want the latter with
gcc, because that doesn't enforce the use of the attribute.

Also, I found some places where clang warns but gcc does not, so there
are patches to also fix those. (These appear to be bugs in gcc.)

Attachment Content-Type Size
0001-Remove-useless-fallthrough-annotation.patch text/plain 1.0 KB
0002-Use-fallthrough-attribute-instead-of-comment.patch text/plain 70.0 KB
0003-Enable-Wimplicit-fallthrough-option-for-clang.patch text/plain 7.5 KB
0004-Fix-additional-fallthrough-warnings-from-clang.patch text/plain 4.4 KB
0005-Fix-additional-fallthrough-warning.patch text/plain 973 bytes

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Dean Rasheed 2026-01-20 11:31:22 Re: commented out code
Previous Message Soumya S Murali 2026-01-20 11:09:13 Re: Allowing ALTER COLUMN TYPE for columns in publication column lists