1

I am encountering an issue with clang-format. Consider the following example header file:


#ifdef __cplusplus
extern "C" {
#endif

    void foo(int x);
    void foo2(int y);

#define FOO 2
#define BAR 3
  
    void bar(int z);

#ifdef __cplusplus
}
#endif


I would like to indent the defines (FOO and BAR) to the same level as the declarations. Is there a way to do this with clang-format?


Desired Result:


#ifdef __cplusplus
extern "C" {
#endif

    void foo(int x);
    void foo2(int y);

    #define FOO 2
    #define BAR 3
  
    void bar(int z);

#ifdef __cplusplus
}
#endif



mame98
  • 1,271
  • 12
  • 26
  • I believe some tools (??... can't remember any right now) only want the `#` on column 1 – pmg Oct 22 '20 at 20:20
  • Interesting, never encountered such an issue. Would be curious to learn more if someone has more details about that – mame98 Oct 22 '20 at 20:39
  • In early C standards, the `#` was mandated to be on the left. – Weather Vane Oct 22 '20 at 21:43
  • 1
    Went to sleep with the `#` on my mind, woke up without any idea about why I keep it on column 1... just the nagging feeling about some tool (maybe it was a badly written script?)... when I want to indent preprocessing lines I put the spaces between the `#` and the rest ... `# define OCTOTHORPE 35` – pmg Oct 23 '20 at 06:34
  • @pmg This looks like an acceptable alternative! Is there a way to achive this in clang-format? – mame98 Oct 23 '20 at 11:26
  • Maybe "IndentPDirectives"?? https://stackoverflow.com/a/48610210/25324 – pmg Oct 23 '20 at 11:31
  • IIRC IndentPDirectives does only indent nested preprocessor directives but not preprocessor directives nestend inside "regular" code. But I can try it in a few hours – mame98 Oct 23 '20 at 11:33
  • @pmg I tested your suggestion and it does ignore preprocessor directives in cases like the one above. So sadly this option wont work here – mame98 Oct 23 '20 at 14:58

0 Answers0