Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang-format style options for enums

Does anyone know how to configure clang-format to keep enum's on individual lines?

i.e.

enum {
    ONE,
    TOW,
    THREE
};

vs.

enum {ONE, TWO, THREE};

EDIT:

Here are the style options i use to match Apple's Objective-C style guide.

http://pastebin.com/0cTEhvBv

like image 935
Rene Limberger Avatar asked Apr 14 '14 23:04

Rene Limberger


People also ask

How do I change my clang format?

clang-format supports two ways to provide custom style options: directly specify style configuration in the -style= command line option or use -style=file and put style configuration in the . clang-format or _clang-format file in the project directory.

What is clang format style?

Clang-Format Style Options are flags that are supported by the ClangFormat tool, which became the de-facto standard to format C++ code. Clang offers the option to use one of the predefined styles (LLVM, Google, Chromium, Mozilla, WebKit, Microsoft) or to create a custom configuration by using the given flags.

What is clang13?

Clang is an LLVM native C/C++/Objective-C compiler, which aims to deliver amazingly fast compiles (e.g. about 3x faster than GCC when compiling Objective-C code in a debug configuration), extremely useful error and warning messages and to provide a platform for building great source level tools.

Can clang format break code?

Short answer: YES. The clang-format tool has a -sort-includes option. Changing the order of #include directives can definitely change the behavior of existing code, and may break existing code.


2 Answers

This was intentionally introduced at some stage (so if you are unable to reproduce the behavior, you are likely on an older version).

clang-format contracts enums to a single line if all elements fit on one line. This conserves spaces and usually does not decrease readability. There is no style option, but you can override this by either adding a line comment somewhere or by adding a trailing comma after the last enumerator, e.g.:

enum {
    ONE,
    TOW,
    THREE,
};

or

enum {
    ONE,  // This means ...
    TOW,
    THREE
};
like image 121
djasper Avatar answered Oct 21 '22 20:10

djasper


AllowShortEnumsOnASingleLine: false

You might need a newer version of clang-format to support this. From https://clang.llvm.org/docs/ClangFormatStyleOptions.html

like image 22
Zhaolin Feng Avatar answered Oct 21 '22 21:10

Zhaolin Feng