Eclipse how can I indent C++ preprocessor macros

2020-08-26 04:29发布

问题:

I can't find a setting in eclipse so that I can have it automatically indent my preprocessor macros the same way it indents code. For example eclipse tries to format code like this.

int main()
{
#ifdef SOMETHING
     cout << "Something Defined" << endl;
#endif
    return 0;
}

Whereas I want it to look like...

int main()
{
    #ifdef SOMETHING
     cout << "Something Defined" << endl;
    #endif
    return 0;
}

Any ideas to make eclipse do it how I want?

回答1:

Pre-ANSI C preprocessor did not allow for space between the start of a line and the "#" character; the leading "#" had to always be placed in the first column.

Pre-ANSI C compilers are non-existent these days. Use which ever style (space before "#" or space between "#" and the identifier) you prefer.

But I suggest you do this:

Just use Find/Replace dialog and the push "Replace all"



回答2:

I think there is no option for macro indentation. But I see clangformat seems to have option for macro indentation so you can customize your own clang format (http://clang.llvm.org/docs/ClangFormatStyleOptions.html) and configure eclipse to use clangformat instead of the default.



回答3:

To indent the preprocessor you might need to use Neatbens instead. It's formatter disregard the preANSIc.



回答4:

The Eclipse indentation is correct. Preprocessor directives should be on the leftmost column, regardless of the indentation of the surrounding code.



回答5:

Like others already pointed out compiler directives # have to start in the first column to be standard conform. Nevertheless it is allowed to put spaces behind them. So my preferred solution looks as follows and then it should no longer be an eclipse issue.

int main()
{
#  ifdef SOMETHING
     cout << "Something Defined" << endl;
#  endif
}


标签: c++ eclipse