Handmade Hero»Forums»Code
Ben Smith
6 posts
clang-format for formatting C/C++ code
Hi everyone,

Apologies if you all have seen clang-format before, but I did a search through the forums and only found a comment I made a year ago. :)

At the end of day 336, Casey talked about an upcoming feature in 4coder that will automatically reflow your code as you're typing it. I just wanted to mention that I've been using clang-format to do something very similar. I have it hooked up to Ctrl+K in vim, so I can just reformat a line of text as I'm typing it. You can also reformat entire files, or regions of text too.

Here's an animated gif from vim-clang-format showing how it works:



It's made working on C++ code significantly more satisfying. It's just like Casey said; when you don't have to think about proper spacing or indentation, you can type much more quickly then just reformat and everything looks great.

And having it reformat a multi-line macro is a thing of beauty...

before:
1
2
3
4
5
6
7
#define FOO(x, y, z) \
do { \
frob(y,z); \
while(1) \
{ \
 blast (z); \
} } while(0)


after:
1
2
3
4
5
6
7
#define FOO(x, y, z) \
  do {               \
    frob(y, z);      \
    while (1) {      \
      blast(z);      \
    }                \
  } while (0)


Happy formatting!
Ameen Sayegh
51 posts
clang-format for formatting C/C++ code
I used to use clang-format for code generation kind of stuff.
Instead of using templates I would use macros to generate code like: sort functions or linked-list functions.
Then I would run the compiler preprocessor on the macros and take the output and feed it to clang-format.
It is the poor man approach for meta-programming. It is slow but it is handy and does the job.

However, what you are showing is not similar to what 4coder is trying to do from what _I understand_.
The idea in the long terms is to have code editing rather than text editing meaning the program understand whether the cursor is on statement, expression, argument and then you would say for example: "make for loop", "wrap selection in for loop", "delete statement" or "remove parameter" and you will map these commands to your own shortcuts. Also because it understand your code it will render it as "blocks" rather than text meaning it doesn't matter how many spaces there are or how many newlines there are or whether the statement is broken into two lines it will just be rendered according to the user configurations. Also moving the cursor is now more clever as Casey was saying.

The source code representation inside the editor will be something like the AST (what the compiler make when it parses the code) not text buffer. And when you save the file it will convert it back to text.

This is _my understanding_ of Allen's goals for 4coder from watching his streams. And I am not/cannot speak for him.
His streams and demos are on YouTube.
Ben Smith
6 posts
clang-format for formatting C/C++ code
Ah, interesting. I was wondering if I was missing something, perhaps I should have paid attention to Casey's description more closely! I have to admit it sounds like these higher-level manipulations of structure would be difficult to work with for me; with Vim I can already manipulate the text in a pretty intelligent way, and I certainly don't feel like my editor is slowing me down. But I haven't watched any of Allen's videos so I should reserve judgment until I do. :)

Anyway, you mention "feeding the output to clang format", which I'm guessing means you ran clang-format from the command line I'd really suggest using the editor integration, if you can. It makes a huge difference if you can just hit a key and format the line you're currently on.
511 posts
clang-format for formatting C/C++ code
binji
Ah, interesting. I was wondering if I was missing something, perhaps I should have paid attention to Casey's description more closely! I have to admit it sounds like these higher-level manipulations of structure would be difficult to work with for me; with Vim I can already manipulate the text in a pretty intelligent way, and I certainly don't feel like my editor is slowing me down. But I haven't watched any of Allen's videos so I should reserve judgment until I do. :)

Anyway, you mention "feeding the output to clang format", which I'm guessing means you ran clang-format from the command line I'd really suggest using the editor integration, if you can. It makes a huge difference if you can just hit a key and format the line you're currently on.


running clan-format on generated code on the command line is a valid approach. It lets you debug something sane looking without having to spend time correcting indentation in the generator code.
Ameen Sayegh
51 posts
clang-format for formatting C/C++ code
Edited by Ameen Sayegh on
This is how I use it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// templates.h
#define DEPLOY_SINGLY_LINKEDLIST_PUSH_FIRST(func_name, list_type, type, \
                                    next_m, first_m, last_m)    \
    void func_name(list_type *List, type *Entry)                \
    {                                                           \
        Assert(!List->first_m == !List->last_m);                \
        Assert(!List->last_m || !List->last_m->next_m);         \
        Entry->next_m = List->first_m;                          \
        List->first_m = Entry;                                  \
        if(!List->last_m)                                       \
            List->last_m = List->first_m;                       \
    }

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// meta.cpp
#include "templates.h"
struct node_type
{
   int data;
   node_type *Next;
};
struct list_type
{
   int other_list_info;
   node_type *First;
   node_type *Last;
};
DEPLOY_SINGLY_LINKEDLIST_PUSH_FIRST(PushNodeOntoList, list_type, node_type, Next, First, Last);


from the command line:
1
2
CL /nologo /EP meta.cpp > autogen.cpp
clang-format.exe -i -style=file autogen.cpp

Your .clang-format file should be in the current direction or parent directories.
Now in your code you should include autogen.cpp

And @ratchetfreak, you are right! for the debuggablity.
Ben Smith
6 posts
clang-format for formatting C/C++ code
Clearly I need to work on my reading comprehension :)
Yes, using clang-format on the command line makes a lot of sense for generated code. I just find that I am not often generating code, but I am always writing code by hand. For that case, editor integration is great. But maybe I just am more OCD about unformatted code than most :)