Clang-format can be used to format (typesetting) a number of different languages of the code, its own layout format mainly include: LLVM, Google, Chromium, Mozilla, WebKit.
If you are writing code using visual Studio code ( Zoffong is currently used to write C + +), Vscode's C + + plugin comes with a clang-format formatting tool that not only has the 5 typographic formats described above, but also defines its own visual Studio layout format, and this format is the default typesetting format (Ubuntu under Formatting shortcut key: Ctrl+shift+i,windows format shortcut key: shift+alt+f), VS code format specific content can refer to the official website: Click to open the link
However, these typesetting formats may not meet all the requirements of your personal coding habits, and Clang-format also provides the ability to use custom typesetting formats.
If Clang-format is installed in Ubuntu, you can use the following command to export the 5 self-contained typesetting formats, and you can modify one of the exported files to implement a custom format:
clang-format-style= format name-dump-config > file name
The value of the format name can be LLVM, Google, Chromium, Mozilla, WebKit any one of the file name can take any name, generally take. Clang-format or _clang-format, Because the custom typesetting format file only takes one of these two names, can be recognized by Clang-format. Files named after the point in Ubuntu are hidden files, so after exporting to. Clang-format, you cannot see the file in the folder, and you can use the shortcut key ctrl+h to display the hidden file.
Place the modified file directly in the same folder as the code file, and set the formatting options-style=file, which allows you to use a custom typesetting format. VS code Simply put the file in the same folder as the code file and no additional settings are required. Zoffong The format file in the upper-level folder of the code file, or you can use it. Note that the file name must be. Clang-format or _clang-format.
Here is a brief introduction to the meaning of each of the options in Clang-format, where the Zoffong modified. clang-format file is directly posted, and each option has a corresponding Chinese description above it. If you feel that the description is not detailed enough, you can view the documentation for Clang-format's official website, and there is an example code for each option in the document, so it's easy to understand: Click to open the link
Note: It is possible that the content posted below is not the same as the official documents on the individual options, because the Clang-format version is different.
---# languages: None, Cpp, Java, JavaScript, OBJC, Proto, Tablegen, Textproto language:cpp # BASEDONSTYLE:LLVM # access specifier (public,
private, etc.) offset Accessmodifieroffset:-4 # Opening brackets (open parenthesis, open angle brackets, square brackets) after alignment: Align, Dontalign, Alwaysbreak (always wrapping after opening parentheses)
Alignafteropenbracket:align # Continuous assignment, aligns all the declared variable names when all equals Alignconsecutiveassignments:true # consecutive declarations are aligned
Alignconsecutivedeclarations:true # Left Justify escape line (use backslash wrapping) Backslash Alignescapednewlinesleft:true # Horizontal alignment of operands of two and ternary expressions
Alignoperands:true # Align consecutive trailing notes Aligntrailingcomments:true # allow all arguments for function declarations to be placed on the next line
Allowallparametersofdeclarationonnextline:true # allows short blocks to be placed on the same line Allowshortblocksonasingleline:false # allows short case labels to be placed on the same row
Allowshortcaselabelsonasingleline:false # allows short functions to be placed on the same line: None, inlineonly (defined in the Class), empty (empty function), Inline (defined in class, empty function), all
Allowshortfunctionsonasingleline:empty # Allow short if statements to remain on the same line Allowshortifstatementsonasingleline:false # allow short loops to remain on the same line Allowshortloopsonasingleline:false # always wrap (deprecated) Alwaysbreakafterdefinitionreturntype:none after defining return type # Always wrap after return type: None, all, toplevel(Top-level functions, which are not included in the Class), # alldefinitions (all definitions, not including declarations), topleveldefinitions (definition of all top-level functions) Alwaysbreakafterreturntype:none #
Always wrap a line before a multiline string literal Alwaysbreakbeforemultilinestrings:false # always wraps after the template declaration Alwaysbreaktemplatedeclarations:false # False indicates that the function arguments are either on the same line or on their own line Binpackarguments:true # False indicates that the physical parameters are either on the same line, or each row binpackparameters:true the # curly brace Wrap, Only valid if Breakbeforebraces is set to Custom bracewrapping: # class definition after Afterclass:false # control statement aftercontrolstatement:
False # Enum definition after afterenum:false # function definition after Afterfunction:false # namespace definition after Afternamespace:false # OBJC definition behind Afterobjcdeclaration:false # struct definition after afterstruct:false # Union definition after Afterunion:false # catch Beforeca Tch:true # Else before Beforeelse:true # indent curly Brace Indentbraces:false # Wrap before the two-tuple operator: None (wrapping after the operator), Nonassignment (in a non-assignment operator Previous newline), all (wrapping before the operator) Breakbeforebinaryoperators:nonassignment # wraps before curly braces: Attach (always attaches curly braces to surrounding contexts), Linux (except functions, namespaces, and class definitions, Similar to attach), # Mozilla (except enumerations, functions, record definitions, similar to attach), Stroustrup (except for function definitions, catch, else, similar to attach), # Allman (always wrapping before curly braces), GNU (always wrapping before curly braces, and adding extra indentation to the curly braces of the control statement), WebKit (wrapping before the function), Custom # Note: This is considered a statement block also belongs to the function Breakbeforebraces:custom # before the ternary operator Breakbeforeternaryoperators:true # wraps the line before the comma in the initialization list of the constructor Brea
Kconstructorinitializersbeforecomma:false # Limit of characters per line, 0 means no Limit columnlimit:200 # a regular expression that describes a comment that has a special meaning, it should not be split into multiple lines or otherwise changed Commentpragmas: ' ^ iwyu pragma: ' # The initialization list for constructors is either on the same line, or it's all on its own line Constructorinitializerallononelineoroneperline:false # The indentation width of the initialization list of the constructor Constructorinitializerindentwidth:4 # Continuation of the indentation width of the line Continuationindentwidth:4 # removes the c++11 of the list initialization curly braces {after and} Before the space Cpp11bracedliststyle:false # inherits the most commonly used pointers and the alignment of references Derivepointeralignment:false # turn off formatting disableformat:false # Auto-detect function calls and whether the definition is formatted as one parameter per line (experimental) Experimentalautodetectbinpacking:false # requires a macro foreachmacros that is interpreted as a foreach loop instead of a function call: [ foreach, Q_foreach, Boost_foreach] # to sort # include, matching a regular expression of # include has a corresponding priority, the default priority is Int_max (the smaller the priority, the higher the precedence), # You can define a negative priority so that some # include is always at the front includecategories:-Regex: ' ^ ' (llvm|llvm-c|clang|clang-c)/' Priority:2-Regex: ' ^ (<| " (Gtest|isl|json)/) ' Priority:3-Regex: '. * ' Priority:1 # indent case label Indentcaselabels:false # indent width indentwidth: 4 # function return type newline, indent function declaration or function definition function name Indentwrappedfunctionnames:false # leave blank line at the beginning of the Block Keepemptylinesatthestartofblocks:true # start Regular expression for a block of macros Macroblockbegin: "# The regular expression of a macro that ends a block macroblockend: ' # The maximum number of consecutive empty lines Maxemptylinestokeep:1 # namespace indentation: None, Inne
R (indent the contents of nested namespaces), all Namespaceindentation:inner # indent width when using OBJC block Objcblockindentwidth:4 # Add a space after OBJC @property
Objcspaceafterproperty:false # Add a space in front of OBJC's protocol list Objcspacebeforeprotocollist:true # in Call (post-newline penalty for function calls)
PENALTYBREAKBEFOREFIRSTCALLPARAMETER:19 # Introducing NewLine Penalty penaltybreakcomment:300 # in a note penalty for first line break in <<
PENALTYBREAKFIRSTLESSLESS:120 # Introducing NewLine Penalty penaltybreakstring:1000 # in a string literal penalty for characters other than the limit of the number of characters in the line penaltyexcesscharacter:1000000 # puts the return type of the function into its own line of penalty penaltyreturntypeonitsownline:60 # pointer and reference alignment: left, right, Middle Pointeralignment:left # allows re-typesetting of comments Reflowcomments:true # allow sort # include Sortincludes:true # add spaces after C-style type conversion Spaceaft
Ercstylecast:false # Add a space before the assignment operator Spacebeforeassignmentoperators:true # Add a space before opening parentheses: Never, controlstatements, always
Spacebeforeparens:controlstatements # Add spaces in empty parentheses Spaceinemptyparentheses:false # Number of spaces added before trailing comments (only for//)
Spacesbeforetrailingcomments:2 # Add spaces after < and > before the angle brackets Spacesinangles:true # Add spaces to the container (OBJC and JavaScript arrays and dictionaries, etc.) literal
Spacesincontainerliterals:true # Add spaces in parentheses in C-style type conversion Spacesincstylecastparentheses:true # add spaces before and after parentheses Spacesinparentheses:true # Add spaces before and after square brackets, lamda expressions and undeclared array declarations are unaffected Spacesinsquarebrackets:true # Standard: CPP03, CPP11, Auto standard:cpp11 # tab Width tabwidth:4 # use tab character: Never, forindentation, forcontinuationandindentation, always usetab:n Ever ...