Indent C preprocessor directives

B

Bogdan

Can anyone recommend a program for indentation of C preprocessor
directives. My file looks like this:
#ifdef a
#define b
#else
#define c
#endif

int main()
{
return 0;
}
and I want a program able to automatically indent the preprocessor
directives.

I have already tried astyle, but with no success.

Bogdan
 
P

Pietro Cerutti

Bogdan said:
Can anyone recommend a program for indentation of C preprocessor
directives. My file looks like this:
#ifdef a
#define b
#else
#define c
#endif

int main()
{
return 0;
}
and I want a program able to automatically indent the preprocessor
directives.

I have already tried astyle, but with no success.
indent(1) and vim both say that the source you provided is already
indented in a good way...

If what you want is something similar to this

#ifdef a
#define b
#else
#define c
#endif

, then I think you've got quite an unusual habit...
 
D

David Tiktin

indent(1) and vim both say that the source you provided is already
indented in a good way...

If what you want is something similar to this

#ifdef a
#define b
#else
#define c
#endif

, then I think you've got quite an unusual habit...

Maybe unusual, but at least *I* don't feel so lonely anymore ;-)

I think some compilers long ago didn't support preprocessor
directives with the # other than in the first column, but none I've
used in that past few years have that restriction. Seriously, isn't
the indented clause version *way* easier to read? If we indent if
clauses, why shouldn't we indent #if clauses for the same reason?

BTW, I don't use a separate program for this. My editor handles most
of it for me with it's autoindent feature.

Dave
 
K

Kenneth Brody

David said:
Bogdan said:
Can anyone recommend a program for indentation of C preprocessor
directives. My file looks like this:
#ifdef a
#define b
#else
#define c
#endif [...]
and I want a program able to automatically indent the preprocessor
directives.
[...]
If what you want is something similar to this

#ifdef a
#define b
#else
#define c
#endif

, then I think you've got quite an unusual habit...

Maybe unusual, but at least *I* don't feel so lonely anymore ;-)

I think some compilers long ago didn't support preprocessor
directives with the # other than in the first column, but none I've
used in that past few years have that restriction.

I've seen programs keep the '#' in the first column, and insert
whitespace between it and the directive. (Does the Standard say
that this is allowed?) Using the same example:

#ifdef a
# define b
#else
# define c
#endif
Seriously, isn't the indented clause version *way* easier to read?
If we indent if clauses, why shouldn't we indent #if clauses for the
same reason?

When you have complex nested #if/#ifdef's, it is certainly easier
to read for me. However, I've been programming long enough that I've
used compilers that required the '#' in column 1, and no whitespace
following it, so I got in the habit of not indenting such things.
If the Standard guarantees both/either of the above, I just may
retrain myself.

[...]

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
E

Eric Sosman

Kenneth Brody wrote On 05/21/07 16:56,:
David said:
Bogdan wrote:

Can anyone recommend a program for indentation of C preprocessor
directives. My file looks like this:
#ifdef a
#define b
#else
#define c
#endif
[...]
and I want a program able to automatically indent the preprocessor
directives.
[...]
If what you want is something similar to this

#ifdef a
#define b
#else
#define c
#endif

, then I think you've got quite an unusual habit...

Maybe unusual, but at least *I* don't feel so lonely anymore ;-)

I think some compilers long ago didn't support preprocessor
directives with the # other than in the first column, but none I've
used in that past few years have that restriction.


I've seen programs keep the '#' in the first column, and insert
whitespace between it and the directive. (Does the Standard say
that this is allowed?) Using the same example:

#ifdef a
# define b
#else
# define c
#endif

Seriously, isn't the indented clause version *way* easier to read?
If we indent if clauses, why shouldn't we indent #if clauses for the
same reason?


When you have complex nested #if/#ifdef's, it is certainly easier
to read for me. However, I've been programming long enough that I've
used compilers that required the '#' in column 1, and no whitespace
following it, so I got in the habit of not indenting such things.
If the Standard guarantees both/either of the above, I just may
retrain myself.

White space is allowed both before and after the #.
What's more, it can be any species of white space:

/*#ifdef A*/#/*include <stdio.h>*/else/*include "stdio.h"*/

As with most of C, good sense is not enforced.
 
K

keith

White space is allowed both before and after the #.
What's more, it can be any species of white space:

/*#ifdef A*/#/*include <stdio.h>*/else/*include "stdio.h"*/

As with most of C, good sense is not enforced.

Really?

gcc gives me "`#else' not within a conditional" for that line, with or
without the "-ansi -pedantic" flags.
 
C

Chris Dollin

Really.

gcc gives me "`#else' not within a conditional" for that line, with or
without the "-ansi -pedantic" flags.

That's nice. But that only reinforces Eric's point: the line
above is a #else, but written in a diabolical fashion devoid
of good sense.

[To be fair, most programming languages allow similar exhibitions
of sense without sensibility, since they're blind to the meaning
of names & natural language.]
 
T

Tor Rustad

Bogdan said:
Can anyone recommend a program for indentation of C preprocessor
directives. My file looks like this:
#ifdef a
#define b
#else
#define c
#endif

int main()
{
return 0;
}
and I want a program able to automatically indent the preprocessor
directives.

I have already tried astyle, but with no success.


$ indent -kr -ppi 3 -st your_file.c
#ifdef a
# define b
#else
# define c
#endif

int main()
{
return 0;
}
 
P

Pietro Cerutti

Tor said:
$ indent -kr -ppi 3 -st your_file.c

Which version of indent are you using?
Mine, on FreeBSD-6.2-STABLE doesn't have any of the options you're using...

Thanks
 
T

Tor Rustad

Pietro said:
Which version of indent are you using?
Mine, on FreeBSD-6.2-STABLE doesn't have any of the options you're
using...

$ indent --version
GNU indent 2.2.9

The laptop is running Ubuntu 7.04 Feisty.
 
T

Tor Rustad

CBFalconer said:
Tor Rustad wrote:

I can't find the -ppi directive in the manuals anywhere. What is
its description?

The man page of indent say this (see INDENTATION section):


ANSI C allows white space to be placed on preprocessor command lines
between the character ?#? and the command name. By default, indent
removes this space, but specifying the ?-lps? option directs indent to
leave this space unmodified. The option ?-ppi? overrides ?-nlps?
and ?-lps?.

This option can be used to request that preprocessor conditional
statements can be indented by to given number of spaces, for example with
the
option ?-ppi 3?

#if X
#if Y
#define Z 1
#else
#define Z 0
#endif
#endif
becomes
#if X
# if Y
# define Z 1
# else
# define Z 0
# endif
#endif
 
T

Tor Rustad

CBFalconer said:
I'm using 2.2.9, and the manual never mentions ppi. What does it
do?

TRINITY: A deja vu is usually a glitch in the Matrix. It happens when they
change something.
 
G

Giorgos Keramidas

I'm using 2.2.9, and the manual never mentions ppi.
What does it do?

That's odd. Are you sure you are looking at the right manpage?
The manpage of GNU indent 2.2.9 here mentionds -ppi:

: ANSI C allows white space to be placed on preprocessor command
: lines between the character `#' and the command name. By
: default, indent removes this space, but specifying the `-lps'
: option directs indent to leave this space unmodified. The
: option `-ppi' overrides `-nlps' and `-lps'.
:
: This option can be used to request that preprocessor
: conditional state- ments can be indented by to given number of
: spaces, for example with the option `-ppi 3'
:
: #if X
: #if Y
: #define Z 1
: #else
: #define Z 0
: #endif
: #endif
: becomes
: #if X
: # if Y
: # define Z 1
: # else
: # define Z 0
: # endif
: #endif
 
H

Hallvard B Furuseth

David said:
I think some compilers long ago didn't support preprocessor
directives with the # other than in the first column, but none I've
used in that past few years have that restriction.

I've encountered a few. Don't remember how long ago, or if they still
are in use. But they did accept space _after_ the '#', so I write

#ifdef FOO
# define BAR
#endif
Seriously, isn't
the indented clause version *way* easier to read? If we indent if
clauses, why shouldn't we indent #if clauses for the same reason?

In preprocessor-only code that is nice. With #ifdefs in the middle
of other code it gets wrong either way. Well, it's possible but
programs like indent or editors' C mode will mess up code like this:

#ifdef FOO
int hmm()
{
int i;
# ifdef BAR
i = 3;
# else
i = 5;
# endif
...
}
#endif
 
G

Giorgos Keramidas

That exact information was posted 3 days ago. :)

You're right, of course. Unfortunately, I saw the 3-days ago post
later, after I had posted mine already :-/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Preprocessor 0
preprocessor bug? 2
Preprocessor commands usage. 11
preprocessor 2
Preprocessor Directives 5
Preprocessor problem 1
Different behavior of C/C++-preprocessor 7
Linux: using "clone3" and "waitid" 0

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top