alnet
alnet

Reputation: 1233

How to apply indent correctly on a line containing BEGIN_C_DECLS macro?

I'm using indent utility to apply code styling on my code. I have problem with the following:

#ifdef __cplusplus
#define BEGIN_C_DECLS extern "C" {
#define END_C_DECLS   }
#else /* !__cplusplus */
#define BEGIN_C_DECLS
#define END_C_DECLS
#endif /* __cplusplus */

BEGIN_C_DECLS 

int x;
...
END_C_DECLS

and after applying indent -npro -kr -i8 -ts8 -sob -l80 -ss -ncs I get: BEGIN_C_DECLS int x on the same line.

Any ideas how to keep them on separate lines?

Upvotes: 3

Views: 321

Answers (1)

Jens
Jens

Reputation: 72619

Since indent isn't a C parser (it can't be because it can't process #include directives), it uses heuristics to determine which tokens are identifiers or typedef names and which kind of syntactic unit these form. This obviously guesses wrong for what you are doing--mangling C syntax.

What you can do is to wrap the macros like so

/* *INDENT-OFF* */
BEGIN_C_DECLS
/* *INDENT-ON* */

int x;

/* *INDENT-OFF* */
END_C_DECLS
/* *INDENT-ON* */

Another way is through judicious use of the empty preprocessor directive, # (finally it becomes useful for something!):

#
BEGIN_C_DECLS
#
int     x;

#
END_C_DECLS
#

Upvotes: 3

Related Questions