Nun clang hat -Wimplicit-fallthrough
, die ich nicht kannte, aber mit -Weverything
gefunden. Also für diesen Code mir gibt es die Warnung folgende (see it live):
warning: unannotated fall-through between switch labels [-Wimplicit-fallthrough]
case 2:
^
note: insert '[[clang::fallthrough]];' to silence this warning
case 2:
^
[[clang::fallthrough]];
note: insert 'break;' to avoid fall-through
case 2:
^
break;
Die einzige Dokumentation ich für diese Flagge finden im Attribute Reference ist, die sagt:
The clang::fallthrough attribute is used along with the -Wimplicit-fallthrough argument to annotate intentional fall-through between switch labels. It can only be applied to a null statement placed at a point of execution between any statement and the next switch label. It is common to mark these places with a specific comment, but this attribute is meant to replace comments with a more strict annotation, which can be checked by the compiler.
und liefert ein Beispiel
case 44: // warning: unannotated fall-through
g();
[[clang::fallthrough]];
case 55: // no warning
Diese Verwendung eines attribute zu markieren: wie explizite durch~~POS=TRUNC markieren Der explizite Durchgriff hat den Nachteil, dass er nicht tragbar ist. Visual Studio
einen Fehler erzeugen und gcc
generiert die folgende Warnung:
warning: attributes at the beginning of statement are ignored [-Wattributes]
was ein Problem ist, wenn Sie -Werror
verwenden möchten.
habe ich versucht, dies mit gcc 4.9
und es sieht aus wie gcc
diese Warnung nicht unterstützt:
error: unrecognized command line option '-Wimplicit-fallthrough'
Ab GCC 7, -Wimplicit-fallthrough
unterstützt wird und __attribute__((fallthrough))
verwendet werden kann, die Warnungen zu unterdrücken, wenn fallthrough beabsichtigt ist. GCC erkennt in bestimmten Szenarien "Fall-through" -Kommentare, aber es kann verwechselt werden fairly easily.
Ich sehe keine Möglichkeit, eine solche Warnung für Visual Studio
zu generieren.
Hinweis, Chandler Carruth erklärt, dass -Weverything
nicht für den produktiven Einsatz ist:
This is an insane group that literally enables every warning in Clang. Don't use this on your code. It is intended strictly for Clang developers or for exploring what warnings exist.
aber es ist nützlich, um herauszufinden, was Warnungen vorhanden sind.
Welchen Compiler benutzen Sie? Ich weiß, dass ich [Diskussion] gesehen habe (http://stackoverflow.com/questions/7703358/how-can-i-tell-gcc-to-warn-or-fail-on-switch-case-statements-without- a-break) des Hinzufügens einer solchen optionalen Warnung zu gcc. Nicht sicher, was daraus geworden ist. – CoryKramer
klingt wie eine Änderung, die möglicherweise Legacy-Code brechen könnte, wenn es sich um etwas anderes als eine Warnung handelt. –
@Cyber ich gcc bin mit 4.7 – Barry