Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse how can I indent C++ preprocessor macros

Tags:

c++

eclipse

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?

like image 660
chasep255 Avatar asked Aug 29 '15 14:08

chasep255


People also ask

Should preprocessor directives be indented?

C static code analysis: Preprocessor directives should not be indented.

How do I enable macros in Eclipse?

Use the Discovery Tab on Build preference panel to define global scanner discovery options. In the Discovery tab you can see a list of Language Settings Providers. Language Settings Providers supply Language Settings such as include paths and preprocessor macros for the indexer and other components of CDT.

How to get C/C++ preprocessor macros and preprocessor include paths?

Include paths and preprocessor macros for C/C++ indexer and other Language Settings are supplied by Language Settings Providers using Scanner Discovery mechanism. The entries can be inspected and set up on project properties page "Preprocessor Include Paths, Macros, etc." - Entries tab .

How to set up include paths and macros for C/C++ indexer?

Setting Up Include Paths and Macros for C/C++ Indexer 1 Scanner Discovery. ... 2 C/C++ Project properties: Preprocessor Include Paths, Macros, etc. ... 3 Adding and Editing entries. ... 4 C/C++ Project properties: Preprocessor Include Paths, Macros, etc. ...

What is a preprocessor in C?

The C preprocessor is a macro preprocessor (allows you to define macros) that transforms your program before it is compiled. These transformations can be the inclusion of header file, macro expansions etc. All preprocessing directives begin with a # symbol. For example,

How do I find include paths and preprocessor symbols?

Often, include paths are supplied to the compiler with -I options, and macros with -D options. That relies on verbose build output of your build where all these options are actually printed by make. Scanner Discovery uses Language Settings Providers to find include paths and preprocessor symbols.


2 Answers

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"

like image 129
Oleg Gopkolov Avatar answered Oct 04 '22 09:10

Oleg Gopkolov


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.

like image 38
Misgevolution Avatar answered Oct 04 '22 09:10

Misgevolution