r/cpp • • Feb 05 '20

Fall through on switch case - warnings?

So, for years, in both c and c++, I've used a technique where a case: does a bunch of stuff, call it X, and the case: above it needs to do exactly that, plus Y stuff, so I put the Y in the upper case: and let it fall through to the X case. It's always worked great, never had a problem.

So I went to port a relatively large app (this one) from OSX, where I developed it, to Windows. Installed Qt, built it, the qt install picked a compiler (mingw), etc., and off I went to cross-compile.

The new compiler, predictably, went at things a little differently, and TBH, it caught a few things I was glad to have caught. So far so good.

But it also pushed warnings about my fall-through case: statements, and looking at them, they were doing just what I had intended. I hadn't yet found a way to switch the warnings off (laptop is off getting more RAM and an SSD, so I can't even look at it today) but probably there's a way.

So what I'm asking, though, is what folks think of this technique. Apparently, the compiler authors think I shouldn't be doing this. Anyone agree? Disagree? Other? If I can turn this off, should I? Yes? No? Other?

TIA.

[EDIT: End goal is zero errors, zero warnings, always. I can just code this differently, two cases plus an if inside the case, for instance... it just seems inelegant.]

7 Upvotes

23 comments sorted by

View all comments

Show parent comments

3

u/fyngyrz Feb 05 '20

Ah, very good. I'll see if the OSX compiler understands this, and if not, I'll #ifdef it out unless building under windows.

Presuming the mingw compiler understands it, of course. I'll have the lappy back tomorrow so I'll know then.

7

u/MrPotatoFingers Feb 06 '20

Shouldn't be a problem for OSX, since it uses clang. No idea what mingw uses - but since it's likely either gcc or clang it should work fine.

2

u/fyngyrz Feb 06 '20 edited Feb 06 '20

Turns out that the OSX compiler (yes, CLANG) likes it, but Qt-Creator does not...

qtcreator [[fallthrough]]; error

...so I did end up wrapping it with...

#ifdef _WIN64
[[fallthough]];
#endif

...in order to silence qt-creator's (visual) whining. I found a page that says you can edit the clang model, but that's disabled in my qt-creator install, which tells me I can't change the model because "the plug-in is not loaded" and doesn't offer any insight at all into loading or finding said plug-in. Nor do I really want to go fiddling with my setup, which has worked well for many moons, sigh.

To be clear, CLANG isn't generating a warning on this anyway; it is fine with a case: fallthrough, intentional or otherwise. It's just the mingw build under Windows that is complaining.

7

u/NotUniqueOrSpecial Feb 06 '20

You can change the Qt-creator settings under Settings -> C++ -> Diagnostic Configurations.

Cluttering up your code just to avoid squigglies in a specific IDE is icky, especially when that IDE is cross-platform and you're only guarding Windows.

1

u/fyngyrz Feb 06 '20

I can't seem to find where you're talking about... Settings?

Do you mean Preferences? If so, under c++, there is no "Diagnostic Configurations"

2

u/NotUniqueOrSpecial Feb 06 '20

Tools -> Options, in my case. I don't have a "Preferences" entry.

It is a setting specific to the Clang Code Model, I just realized. Have you enabled that? That's under Help -> About Plugins.

2

u/fyngyrz Feb 06 '20

I don't have a Help -> About Plugins menu entry. This is qt-creator 4.2.4 OSX.

Is it somewhere other than in the menus?

um... no Tools -> Options, either.

2

u/NotUniqueOrSpecial Feb 06 '20

Hm. Not sure, then. Even the official docs say that's where to look. I don't have my laptop with me to check.

3

u/fyngyrz Feb 06 '20 edited Feb 06 '20

okay... built the same, no compiler warnings or errors, but in the IDE, the message has changed: now the IDE (not the compiler) is warning that [[fallthrough]]; is a "c++1z extension", and the underline is yellow instead of red.

Still haven't found anything to let OSX qt-creator's IDE know this is okay. It may be too old a version, and moving to a later version is definitely not an option.

[EDIT: ...for all this trouble, it certainly is a lot simpler just to put an if in the lower case:

Barring some new insight, I think I'll go that way. It'll be broadly compatible, and at least in this case, efficiency isn't an issue. Thank you for trying. :) ]

2

u/fyngyrz Feb 06 '20

Even the official docs say that's where to look.

I found it - it's under the first menu, "Qt Creator", not "Help."

However, under preferences/c++, there is no "Diagnostic Configurations", even after enabling the CLANG plugin and restarting.

I'm rebuilding the entire project right now (about an 8-minute build on this machine, which is 2.93 GHz/64GB) to see if CLANG will burp out any warnings, etc., from the codebase now that the invocation might be different, etc. Baby steps. :)