5

I've activated automatic code formatting in Visual Studio 2015 for a legacy C++ project.

The problem is that code lines after specific macros calls (not closing with a semicolon since its inside the macro) are always indented. I'm searching for a setting in Visual Studio 2015 to prevent this, without me having to install another extension, the need to change the macro definition itself or to ask everybody to add an additional semicolon to the call (which would fix it).

Here is an minimal example of how the code and the resulting problem looks like:

#include <iostream>

#ifdef _DEBUG
# define MY_TRACE(X) do { std::cout << X << std::endl; } while (0);
#else
# define MY_TRACE(X) {}
#endif

int main()
{
    MY_TRACE( "Hello World!" )
        return 0;
}

So I have the following settings which does automatically indent e.g. on <Enter> or with } :

C++ Autoformat Settings

And the following settings for indentation:

C++ Indentation Settings

I've tried all possible and impossible combinations of the settings there and elsewhere (including e.g. turning of "Tabs/Indenting/Smart"). Every time Visual Studio C++ editor did continue to indent the line after the macro.

I also couldn't find anything on SO or the internet. The closest I got, but didn't had an answer or where for an different editor/language:

Florian
  • 39,996
  • 9
  • 133
  • 149
  • 4
    Why not having `MY_TRACE` require a semicolon at the end? – Vittorio Romeo Mar 31 '17 at 13:27
  • 2
    Seconded. Having a line not terminated with a semicolon looks really weird. – NathanOliver Mar 31 '17 at 13:29
  • 1
    @VittorioRomeo That's a possibility, but would need to go through a lot of legacy code. It was also discussed to abandon this macro and just add another one that does require a semicolon, but I think that would be sort of confusing. – Florian Mar 31 '17 at 13:30
  • @NathanOliver I admit that it looks weird, even after 20 years of looking at this code. But that's the point: it was a decision made long back and since used in a lot of code. And yes, maybe that's already the answer: how could a modern C++ editor know or care about coding sins made in the past. – Florian Mar 31 '17 at 13:34
  • 1
    Adding a (extra) semicolon to the end of the macro won't hurt anything. It's just an empty statement. You don't have to change the macro or risk breaking anything. – Cody Gray - on strike Mar 31 '17 at 15:50
  • @CodyGray Yes, the extra semicolon at the end of the macro call would be the best solution (both visually and for code compatibility). At the moment it just has some problems with legacy compilers we still support that give an "extra ';'" warning in such cases (mostly like described in ["GHS C++: extra semicolon diagnostic message - purpose?"](http://stackoverflow.com/questions/30327682)). If I understand ["Semicolon after Function"](http://stackoverflow.com/questions/9997895) correctly `;` empty statements came officially with C++11. – Florian Mar 31 '17 at 20:00
  • In my case, where I have a define for specific loop start (#define forever for(;;)) and thus I cannot require a semicolon, this is also an issue. However, I agree that there should be a semicolon in the case above. – Lukas Kalinski Mar 15 '18 at 11:27

0 Answers0