Eclipse how can I indent C++ preprocessor macros

2020-08-26 04:21发布

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?

标签: c++ eclipse
5条回答
Viruses.
2楼-- · 2020-08-26 04:39

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: enter image description here

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

查看更多
Deceive 欺骗
3楼-- · 2020-08-26 04:41

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.

查看更多
Root(大扎)
4楼-- · 2020-08-26 04:41

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

查看更多
贼婆χ
5楼-- · 2020-08-26 04:48

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
}
查看更多
ゆ 、 Hurt°
6楼-- · 2020-08-26 04:51

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

查看更多
登录 后发表回答