ClangFormatStyleOptions.rst 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  1. ==========================
  2. Clang-Format Style Options
  3. ==========================
  4. :doc:`ClangFormatStyleOptions` describes configurable formatting style options
  5. supported by :doc:`LibFormat` and :doc:`ClangFormat`.
  6. When using :program:`clang-format` command line utility or
  7. ``clang::format::reformat(...)`` functions from code, one can either use one of
  8. the predefined styles (LLVM, Google, Chromium, Mozilla, WebKit) or create a
  9. custom style by configuring specific style options.
  10. Configuring Style with clang-format
  11. ===================================
  12. :program:`clang-format` supports two ways to provide custom style options:
  13. directly specify style configuration in the ``-style=`` command line option or
  14. use ``-style=file`` and put style configuration in the ``.clang-format`` or
  15. ``_clang-format`` file in the project directory.
  16. When using ``-style=file``, :program:`clang-format` for each input file will
  17. try to find the ``.clang-format`` file located in the closest parent directory
  18. of the input file. When the standard input is used, the search is started from
  19. the current directory.
  20. The ``.clang-format`` file uses YAML format:
  21. .. code-block:: yaml
  22. key1: value1
  23. key2: value2
  24. # A comment.
  25. ...
  26. The configuration file can consist of several sections each having different
  27. ``Language:`` parameter denoting the programming language this section of the
  28. configuration is targeted at. See the description of the **Language** option
  29. below for the list of supported languages. The first section may have no
  30. language set, it will set the default style options for all lanugages.
  31. Configuration sections for specific language will override options set in the
  32. default section.
  33. When :program:`clang-format` formats a file, it auto-detects the language using
  34. the file name. When formatting standard input or a file that doesn't have the
  35. extension corresponding to its language, ``-assume-filename=`` option can be
  36. used to override the file name :program:`clang-format` uses to detect the
  37. language.
  38. An example of a configuration file for multiple languages:
  39. .. code-block:: yaml
  40. ---
  41. # We'll use defaults from the LLVM style, but with 4 columns indentation.
  42. BasedOnStyle: LLVM
  43. IndentWidth: 4
  44. ---
  45. Language: Cpp
  46. # Force pointers to the type for C++.
  47. DerivePointerAlignment: false
  48. PointerAlignment: Left
  49. ---
  50. Language: JavaScript
  51. # Use 100 columns for JS.
  52. ColumnLimit: 100
  53. ---
  54. Language: Proto
  55. # Don't format .proto files.
  56. DisableFormat: true
  57. ...
  58. An easy way to get a valid ``.clang-format`` file containing all configuration
  59. options of a certain predefined style is:
  60. .. code-block:: console
  61. clang-format -style=llvm -dump-config > .clang-format
  62. When specifying configuration in the ``-style=`` option, the same configuration
  63. is applied for all input files. The format of the configuration is:
  64. .. code-block:: console
  65. -style='{key1: value1, key2: value2, ...}'
  66. Disabling Formatting on a Piece of Code
  67. =======================================
  68. Clang-format understands also special comments that switch formatting in a
  69. delimited range. The code between a comment ``// clang-format off`` or
  70. ``/* clang-format off */`` up to a comment ``// clang-format on`` or
  71. ``/* clang-format on */`` will not be formatted. The comments themselves
  72. will be formatted (aligned) normally.
  73. .. code-block:: c++
  74. int formatted_code;
  75. // clang-format off
  76. void unformatted_code ;
  77. // clang-format on
  78. void formatted_code_again;
  79. Configuring Style in Code
  80. =========================
  81. When using ``clang::format::reformat(...)`` functions, the format is specified
  82. by supplying the `clang::format::FormatStyle
  83. <http://clang.llvm.org/doxygen/structclang_1_1format_1_1FormatStyle.html>`_
  84. structure.
  85. Configurable Format Style Options
  86. =================================
  87. This section lists the supported style options. Value type is specified for
  88. each option. For enumeration types possible values are specified both as a C++
  89. enumeration member (with a prefix, e.g. ``LS_Auto``), and as a value usable in
  90. the configuration (without a prefix: ``Auto``).
  91. **BasedOnStyle** (``string``)
  92. The style used for all options not specifically set in the configuration.
  93. This option is supported only in the :program:`clang-format` configuration
  94. (both within ``-style='{...}'`` and the ``.clang-format`` file).
  95. Possible values:
  96. * ``LLVM``
  97. A style complying with the `LLVM coding standards
  98. <http://llvm.org/docs/CodingStandards.html>`_
  99. * ``Google``
  100. A style complying with `Google's C++ style guide
  101. <http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml>`_
  102. * ``Chromium``
  103. A style complying with `Chromium's style guide
  104. <http://www.chromium.org/developers/coding-style>`_
  105. * ``Mozilla``
  106. A style complying with `Mozilla's style guide
  107. <https://developer.mozilla.org/en-US/docs/Developer_Guide/Coding_Style>`_
  108. * ``WebKit``
  109. A style complying with `WebKit's style guide
  110. <http://www.webkit.org/coding/coding-style.html>`_
  111. .. START_FORMAT_STYLE_OPTIONS
  112. **AccessModifierOffset** (``int``)
  113. The extra indent or outdent of access modifiers, e.g. ``public:``.
  114. **AlignAfterOpenBracket** (``BracketAlignmentStyle``)
  115. If ``true``, horizontally aligns arguments after an open bracket.
  116. This applies to round brackets (parentheses), angle brackets and square
  117. brackets.
  118. Possible values:
  119. * ``BAS_Align`` (in configuration: ``Align``)
  120. Align parameters on the open bracket, e.g.:
  121. .. code-block:: c++
  122. someLongFunction(argument1,
  123. argument2);
  124. * ``BAS_DontAlign`` (in configuration: ``DontAlign``)
  125. Don't align, instead use ``ContinuationIndentWidth``, e.g.:
  126. .. code-block:: c++
  127. someLongFunction(argument1,
  128. argument2);
  129. * ``BAS_AlwaysBreak`` (in configuration: ``AlwaysBreak``)
  130. Always break after an open bracket, if the parameters don't fit
  131. on a single line, e.g.:
  132. .. code-block:: c++
  133. someLongFunction(
  134. argument1, argument2);
  135. **AlignConsecutiveAssignments** (``bool``)
  136. If ``true``, aligns consecutive assignments.
  137. This will align the assignment operators of consecutive lines. This
  138. will result in formattings like
  139. .. code-block:: c++
  140. int aaaa = 12;
  141. int b = 23;
  142. int ccc = 23;
  143. **AlignConsecutiveDeclarations** (``bool``)
  144. If ``true``, aligns consecutive declarations.
  145. This will align the declaration names of consecutive lines. This
  146. will result in formattings like
  147. .. code-block:: c++
  148. int aaaa = 12;
  149. float b = 23;
  150. std::string ccc = 23;
  151. **AlignEscapedNewlines** (``EscapedNewlineAlignmentStyle``)
  152. Options for aligning backslashes in escaped newlines.
  153. Possible values:
  154. * ``ENAS_DontAlign`` (in configuration: ``DontAlign``)
  155. Don't align escaped newlines.
  156. .. code-block:: c++
  157. #define A \
  158. int aaaa; \
  159. int b; \
  160. int dddddddddd;
  161. * ``ENAS_Left`` (in configuration: ``Left``)
  162. Align escaped newlines as far left as possible.
  163. .. code-block:: c++
  164. true:
  165. #define A \
  166. int aaaa; \
  167. int b; \
  168. int dddddddddd;
  169. false:
  170. * ``ENAS_Right`` (in configuration: ``Right``)
  171. Align escaped newlines in the right-most column.
  172. .. code-block:: c++
  173. #define A \
  174. int aaaa; \
  175. int b; \
  176. int dddddddddd;
  177. **AlignOperands** (``bool``)
  178. If ``true``, horizontally align operands of binary and ternary
  179. expressions.
  180. Specifically, this aligns operands of a single expression that needs to be
  181. split over multiple lines, e.g.:
  182. .. code-block:: c++
  183. int aaa = bbbbbbbbbbbbbbb +
  184. ccccccccccccccc;
  185. **AlignTrailingComments** (``bool``)
  186. If ``true``, aligns trailing comments.
  187. .. code-block:: c++
  188. true:
  189. int a; // My comment a
  190. int b = 2; // comment b
  191. false:
  192. int a; // My comment a
  193. int b = 2; // comment about b
  194. **AllowAllParametersOfDeclarationOnNextLine** (``bool``)
  195. If the function declaration doesn't fit on a line,
  196. allow putting all parameters of a function declaration onto
  197. the next line even if ``BinPackParameters`` is ``false``.
  198. .. code-block:: c++
  199. true:
  200. void myFunction(
  201. int a, int b, int c, int d, int e);
  202. false:
  203. void myFunction(int a,
  204. int b,
  205. int c,
  206. int d,
  207. int e);
  208. **AllowShortBlocksOnASingleLine** (``bool``)
  209. Allows contracting simple braced statements to a single line.
  210. E.g., this allows ``if (a) { return; }`` to be put on a single line.
  211. **AllowShortCaseLabelsOnASingleLine** (``bool``)
  212. If ``true``, short case labels will be contracted to a single line.
  213. .. code-block:: c++
  214. true: false:
  215. switch (a) { vs. switch (a) {
  216. case 1: x = 1; break; case 1:
  217. case 2: return; x = 1;
  218. } break;
  219. case 2:
  220. return;
  221. }
  222. **AllowShortFunctionsOnASingleLine** (``ShortFunctionStyle``)
  223. Dependent on the value, ``int f() { return 0; }`` can be put on a
  224. single line.
  225. Possible values:
  226. * ``SFS_None`` (in configuration: ``None``)
  227. Never merge functions into a single line.
  228. * ``SFS_InlineOnly`` (in configuration: ``InlineOnly``)
  229. Only merge functions defined inside a class. Same as "inline",
  230. except it does not implies "empty": i.e. top level empty functions
  231. are not merged either.
  232. .. code-block:: c++
  233. class Foo {
  234. void f() { foo(); }
  235. };
  236. void f() {
  237. foo();
  238. }
  239. void f() {
  240. }
  241. * ``SFS_Empty`` (in configuration: ``Empty``)
  242. Only merge empty functions.
  243. .. code-block:: c++
  244. void f() {}
  245. void f2() {
  246. bar2();
  247. }
  248. * ``SFS_Inline`` (in configuration: ``Inline``)
  249. Only merge functions defined inside a class. Implies "empty".
  250. .. code-block:: c++
  251. class Foo {
  252. void f() { foo(); }
  253. };
  254. void f() {
  255. foo();
  256. }
  257. void f() {}
  258. * ``SFS_All`` (in configuration: ``All``)
  259. Merge all functions fitting on a single line.
  260. .. code-block:: c++
  261. class Foo {
  262. void f() { foo(); }
  263. };
  264. void f() { bar(); }
  265. **AllowShortIfStatementsOnASingleLine** (``bool``)
  266. If ``true``, ``if (a) return;`` can be put on a single line.
  267. **AllowShortLoopsOnASingleLine** (``bool``)
  268. If ``true``, ``while (true) continue;`` can be put on a single
  269. line.
  270. **AlwaysBreakAfterDefinitionReturnType** (``DefinitionReturnTypeBreakingStyle``)
  271. The function definition return type breaking style to use. This
  272. option is **deprecated** and is retained for backwards compatibility.
  273. Possible values:
  274. * ``DRTBS_None`` (in configuration: ``None``)
  275. Break after return type automatically.
  276. ``PenaltyReturnTypeOnItsOwnLine`` is taken into account.
  277. * ``DRTBS_All`` (in configuration: ``All``)
  278. Always break after the return type.
  279. * ``DRTBS_TopLevel`` (in configuration: ``TopLevel``)
  280. Always break after the return types of top-level functions.
  281. **AlwaysBreakAfterReturnType** (``ReturnTypeBreakingStyle``)
  282. The function declaration return type breaking style to use.
  283. Possible values:
  284. * ``RTBS_None`` (in configuration: ``None``)
  285. Break after return type automatically.
  286. ``PenaltyReturnTypeOnItsOwnLine`` is taken into account.
  287. .. code-block:: c++
  288. class A {
  289. int f() { return 0; };
  290. };
  291. int f();
  292. int f() { return 1; }
  293. * ``RTBS_All`` (in configuration: ``All``)
  294. Always break after the return type.
  295. .. code-block:: c++
  296. class A {
  297. int
  298. f() {
  299. return 0;
  300. };
  301. };
  302. int
  303. f();
  304. int
  305. f() {
  306. return 1;
  307. }
  308. * ``RTBS_TopLevel`` (in configuration: ``TopLevel``)
  309. Always break after the return types of top-level functions.
  310. .. code-block:: c++
  311. class A {
  312. int f() { return 0; };
  313. };
  314. int
  315. f();
  316. int
  317. f() {
  318. return 1;
  319. }
  320. * ``RTBS_AllDefinitions`` (in configuration: ``AllDefinitions``)
  321. Always break after the return type of function definitions.
  322. .. code-block:: c++
  323. class A {
  324. int
  325. f() {
  326. return 0;
  327. };
  328. };
  329. int f();
  330. int
  331. f() {
  332. return 1;
  333. }
  334. * ``RTBS_TopLevelDefinitions`` (in configuration: ``TopLevelDefinitions``)
  335. Always break after the return type of top-level definitions.
  336. .. code-block:: c++
  337. class A {
  338. int f() { return 0; };
  339. };
  340. int f();
  341. int
  342. f() {
  343. return 1;
  344. }
  345. **AlwaysBreakBeforeMultilineStrings** (``bool``)
  346. If ``true``, always break before multiline string literals.
  347. This flag is mean to make cases where there are multiple multiline strings
  348. in a file look more consistent. Thus, it will only take effect if wrapping
  349. the string at that point leads to it being indented
  350. ``ContinuationIndentWidth`` spaces from the start of the line.
  351. .. code-block:: c++
  352. true: false:
  353. aaaa = vs. aaaa = "bbbb"
  354. "bbbb" "cccc";
  355. "cccc";
  356. **AlwaysBreakTemplateDeclarations** (``bool``)
  357. If ``true``, always break after the ``template<...>`` of a template
  358. declaration.
  359. .. code-block:: c++
  360. true: false:
  361. template <typename T> vs. template <typename T> class C {};
  362. class C {};
  363. **BinPackArguments** (``bool``)
  364. If ``false``, a function call's arguments will either be all on the
  365. same line or will have one line each.
  366. .. code-block:: c++
  367. true:
  368. void f() {
  369. f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,
  370. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);
  371. }
  372. false:
  373. void f() {
  374. f(aaaaaaaaaaaaaaaaaaaa,
  375. aaaaaaaaaaaaaaaaaaaa,
  376. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);
  377. }
  378. **BinPackParameters** (``bool``)
  379. If ``false``, a function declaration's or function definition's
  380. parameters will either all be on the same line or will have one line each.
  381. .. code-block:: c++
  382. true:
  383. void f(int aaaaaaaaaaaaaaaaaaaa, int aaaaaaaaaaaaaaaaaaaa,
  384. int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}
  385. false:
  386. void f(int aaaaaaaaaaaaaaaaaaaa,
  387. int aaaaaaaaaaaaaaaaaaaa,
  388. int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}
  389. **BraceWrapping** (``BraceWrappingFlags``)
  390. Control of individual brace wrapping cases.
  391. If ``BreakBeforeBraces`` is set to ``BS_Custom``, use this to specify how
  392. each individual brace case should be handled. Otherwise, this is ignored.
  393. .. code-block:: yaml
  394. # Example of usage:
  395. BreakBeforeBraces: Custom
  396. BraceWrapping:
  397. AfterEnum: true
  398. AfterStruct: false
  399. SplitEmptyFunction: false
  400. Nested configuration flags:
  401. * ``bool AfterClass`` Wrap class definitions.
  402. .. code-block:: c++
  403. true:
  404. class foo {};
  405. false:
  406. class foo
  407. {};
  408. * ``bool AfterControlStatement`` Wrap control statements (``if``/``for``/``while``/``switch``/..).
  409. .. code-block:: c++
  410. true:
  411. if (foo())
  412. {
  413. } else
  414. {}
  415. for (int i = 0; i < 10; ++i)
  416. {}
  417. false:
  418. if (foo()) {
  419. } else {
  420. }
  421. for (int i = 0; i < 10; ++i) {
  422. }
  423. * ``bool AfterEnum`` Wrap enum definitions.
  424. .. code-block:: c++
  425. true:
  426. enum X : int
  427. {
  428. B
  429. };
  430. false:
  431. enum X : int { B };
  432. * ``bool AfterFunction`` Wrap function definitions.
  433. .. code-block:: c++
  434. true:
  435. void foo()
  436. {
  437. bar();
  438. bar2();
  439. }
  440. false:
  441. void foo() {
  442. bar();
  443. bar2();
  444. }
  445. * ``bool AfterNamespace`` Wrap namespace definitions.
  446. .. code-block:: c++
  447. true:
  448. namespace
  449. {
  450. int foo();
  451. int bar();
  452. }
  453. false:
  454. namespace {
  455. int foo();
  456. int bar();
  457. }
  458. * ``bool AfterObjCDeclaration`` Wrap ObjC definitions (``@autoreleasepool``, interfaces, ..).
  459. * ``bool AfterStruct`` Wrap struct definitions.
  460. .. code-block:: c++
  461. true:
  462. struct foo
  463. {
  464. int x;
  465. };
  466. false:
  467. struct foo {
  468. int x;
  469. };
  470. * ``bool AfterUnion`` Wrap union definitions.
  471. .. code-block:: c++
  472. true:
  473. union foo
  474. {
  475. int x;
  476. }
  477. false:
  478. union foo {
  479. int x;
  480. }
  481. * ``bool AfterExternBlock`` Wrap extern blocks.
  482. .. code-block:: c++
  483. true:
  484. extern "C"
  485. {
  486. int foo();
  487. }
  488. false:
  489. extern "C" {
  490. int foo();
  491. }
  492. * ``bool BeforeCatch`` Wrap before ``catch``.
  493. .. code-block:: c++
  494. true:
  495. try {
  496. foo();
  497. }
  498. catch () {
  499. }
  500. false:
  501. try {
  502. foo();
  503. } catch () {
  504. }
  505. * ``bool BeforeElse`` Wrap before ``else``.
  506. .. code-block:: c++
  507. true:
  508. if (foo()) {
  509. }
  510. else {
  511. }
  512. false:
  513. if (foo()) {
  514. } else {
  515. }
  516. * ``bool IndentBraces`` Indent the wrapped braces themselves.
  517. * ``bool SplitEmptyFunction`` If ``false``, empty function body can be put on a single line.
  518. This option is used only if the opening brace of the function has
  519. already been wrapped, i.e. the `AfterFunction` brace wrapping mode is
  520. set, and the function could/should not be put on a single line (as per
  521. `AllowShortFunctionsOnASingleLine` and constructor formatting options).
  522. .. code-block:: c++
  523. int f() vs. inf f()
  524. {} {
  525. }
  526. * ``bool SplitEmptyRecord`` If ``false``, empty record (e.g. class, struct or union) body
  527. can be put on a single line. This option is used only if the opening
  528. brace of the record has already been wrapped, i.e. the `AfterClass`
  529. (for classes) brace wrapping mode is set.
  530. .. code-block:: c++
  531. class Foo vs. class Foo
  532. {} {
  533. }
  534. * ``bool SplitEmptyNamespace`` If ``false``, empty namespace body can be put on a single line.
  535. This option is used only if the opening brace of the namespace has
  536. already been wrapped, i.e. the `AfterNamespace` brace wrapping mode is
  537. set.
  538. .. code-block:: c++
  539. namespace Foo vs. namespace Foo
  540. {} {
  541. }
  542. **BreakAfterJavaFieldAnnotations** (``bool``)
  543. Break after each annotation on a field in Java files.
  544. .. code-block:: java
  545. true: false:
  546. @Partial vs. @Partial @Mock DataLoad loader;
  547. @Mock
  548. DataLoad loader;
  549. **BreakBeforeBinaryOperators** (``BinaryOperatorStyle``)
  550. The way to wrap binary operators.
  551. Possible values:
  552. * ``BOS_None`` (in configuration: ``None``)
  553. Break after operators.
  554. .. code-block:: c++
  555. LooooooooooongType loooooooooooooooooooooongVariable =
  556. someLooooooooooooooooongFunction();
  557. bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
  558. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==
  559. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
  560. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >
  561. ccccccccccccccccccccccccccccccccccccccccc;
  562. * ``BOS_NonAssignment`` (in configuration: ``NonAssignment``)
  563. Break before operators that aren't assignments.
  564. .. code-block:: c++
  565. LooooooooooongType loooooooooooooooooooooongVariable =
  566. someLooooooooooooooooongFunction();
  567. bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  568. + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  569. == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  570. && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  571. > ccccccccccccccccccccccccccccccccccccccccc;
  572. * ``BOS_All`` (in configuration: ``All``)
  573. Break before operators.
  574. .. code-block:: c++
  575. LooooooooooongType loooooooooooooooooooooongVariable
  576. = someLooooooooooooooooongFunction();
  577. bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  578. + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  579. == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  580. && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  581. > ccccccccccccccccccccccccccccccccccccccccc;
  582. **BreakBeforeBraces** (``BraceBreakingStyle``)
  583. The brace breaking style to use.
  584. Possible values:
  585. * ``BS_Attach`` (in configuration: ``Attach``)
  586. Always attach braces to surrounding context.
  587. .. code-block:: c++
  588. try {
  589. foo();
  590. } catch () {
  591. }
  592. void foo() { bar(); }
  593. class foo {};
  594. if (foo()) {
  595. } else {
  596. }
  597. enum X : int { A, B };
  598. * ``BS_Linux`` (in configuration: ``Linux``)
  599. Like ``Attach``, but break before braces on function, namespace and
  600. class definitions.
  601. .. code-block:: c++
  602. try {
  603. foo();
  604. } catch () {
  605. }
  606. void foo() { bar(); }
  607. class foo
  608. {
  609. };
  610. if (foo()) {
  611. } else {
  612. }
  613. enum X : int { A, B };
  614. * ``BS_Mozilla`` (in configuration: ``Mozilla``)
  615. Like ``Attach``, but break before braces on enum, function, and record
  616. definitions.
  617. .. code-block:: c++
  618. try {
  619. foo();
  620. } catch () {
  621. }
  622. void foo() { bar(); }
  623. class foo
  624. {
  625. };
  626. if (foo()) {
  627. } else {
  628. }
  629. enum X : int { A, B };
  630. * ``BS_Stroustrup`` (in configuration: ``Stroustrup``)
  631. Like ``Attach``, but break before function definitions, ``catch``, and
  632. ``else``.
  633. .. code-block:: c++
  634. try {
  635. foo();
  636. } catch () {
  637. }
  638. void foo() { bar(); }
  639. class foo
  640. {
  641. };
  642. if (foo()) {
  643. } else {
  644. }
  645. enum X : int
  646. {
  647. A,
  648. B
  649. };
  650. * ``BS_Allman`` (in configuration: ``Allman``)
  651. Always break before braces.
  652. .. code-block:: c++
  653. try {
  654. foo();
  655. }
  656. catch () {
  657. }
  658. void foo() { bar(); }
  659. class foo {
  660. };
  661. if (foo()) {
  662. }
  663. else {
  664. }
  665. enum X : int { A, B };
  666. * ``BS_GNU`` (in configuration: ``GNU``)
  667. Always break before braces and add an extra level of indentation to
  668. braces of control statements, not to those of class, function
  669. or other definitions.
  670. .. code-block:: c++
  671. try
  672. {
  673. foo();
  674. }
  675. catch ()
  676. {
  677. }
  678. void foo() { bar(); }
  679. class foo
  680. {
  681. };
  682. if (foo())
  683. {
  684. }
  685. else
  686. {
  687. }
  688. enum X : int
  689. {
  690. A,
  691. B
  692. };
  693. * ``BS_WebKit`` (in configuration: ``WebKit``)
  694. Like ``Attach``, but break before functions.
  695. .. code-block:: c++
  696. try {
  697. foo();
  698. } catch () {
  699. }
  700. void foo() { bar(); }
  701. class foo {
  702. };
  703. if (foo()) {
  704. } else {
  705. }
  706. enum X : int { A, B };
  707. * ``BS_Custom`` (in configuration: ``Custom``)
  708. Configure each individual brace in `BraceWrapping`.
  709. **BreakBeforeInheritanceComma** (``bool``)
  710. If ``true``, in the class inheritance expression clang-format will
  711. break before ``:`` and ``,`` if there is multiple inheritance.
  712. .. code-block:: c++
  713. true: false:
  714. class MyClass vs. class MyClass : public X, public Y {
  715. : public X };
  716. , public Y {
  717. };
  718. **BreakBeforeTernaryOperators** (``bool``)
  719. If ``true``, ternary operators will be placed after line breaks.
  720. .. code-block:: c++
  721. true:
  722. veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongDescription
  723. ? firstValue
  724. : SecondValueVeryVeryVeryVeryLong;
  725. false:
  726. veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongDescription ?
  727. firstValue :
  728. SecondValueVeryVeryVeryVeryLong;
  729. **BreakConstructorInitializers** (``BreakConstructorInitializersStyle``)
  730. The constructor initializers style to use.
  731. Possible values:
  732. * ``BCIS_BeforeColon`` (in configuration: ``BeforeColon``)
  733. Break constructor initializers before the colon and after the commas.
  734. .. code-block:: c++
  735. Constructor()
  736. : initializer1(),
  737. initializer2()
  738. * ``BCIS_BeforeComma`` (in configuration: ``BeforeComma``)
  739. Break constructor initializers before the colon and commas, and align
  740. the commas with the colon.
  741. .. code-block:: c++
  742. Constructor()
  743. : initializer1()
  744. , initializer2()
  745. * ``BCIS_AfterColon`` (in configuration: ``AfterColon``)
  746. Break constructor initializers after the colon and commas.
  747. .. code-block:: c++
  748. Constructor() :
  749. initializer1(),
  750. initializer2()
  751. **BreakStringLiterals** (``bool``)
  752. Allow breaking string literals when formatting.
  753. **ColumnLimit** (``unsigned``)
  754. The column limit.
  755. A column limit of ``0`` means that there is no column limit. In this case,
  756. clang-format will respect the input's line breaking decisions within
  757. statements unless they contradict other rules.
  758. **CommentPragmas** (``std::string``)
  759. A regular expression that describes comments with special meaning,
  760. which should not be split into lines or otherwise changed.
  761. .. code-block:: c++
  762. // CommentPragmas: '^ FOOBAR pragma:'
  763. // Will leave the following line unaffected
  764. #include <vector> // FOOBAR pragma: keep
  765. **CompactNamespaces** (``bool``)
  766. If ``true``, consecutive namespace declarations will be on the same
  767. line. If ``false``, each namespace is declared on a new line.
  768. .. code-block:: c++
  769. true:
  770. namespace Foo { namespace Bar {
  771. }}
  772. false:
  773. namespace Foo {
  774. namespace Bar {
  775. }
  776. }
  777. If it does not fit on a single line, the overflowing namespaces get
  778. wrapped:
  779. .. code-block:: c++
  780. namespace Foo { namespace Bar {
  781. namespace Extra {
  782. }}}
  783. **ConstructorInitializerAllOnOneLineOrOnePerLine** (``bool``)
  784. If the constructor initializers don't fit on a line, put each
  785. initializer on its own line.
  786. .. code-block:: c++
  787. true:
  788. SomeClass::Constructor()
  789. : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa) {
  790. return 0;
  791. }
  792. false:
  793. SomeClass::Constructor()
  794. : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa),
  795. aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa) {
  796. return 0;
  797. }
  798. **ConstructorInitializerIndentWidth** (``unsigned``)
  799. The number of characters to use for indentation of constructor
  800. initializer lists.
  801. **ContinuationIndentWidth** (``unsigned``)
  802. Indent width for line continuations.
  803. .. code-block:: c++
  804. ContinuationIndentWidth: 2
  805. int i = // VeryVeryVeryVeryVeryLongComment
  806. longFunction( // Again a long comment
  807. arg);
  808. **Cpp11BracedListStyle** (``bool``)
  809. If ``true``, format braced lists as best suited for C++11 braced
  810. lists.
  811. Important differences:
  812. - No spaces inside the braced list.
  813. - No line break before the closing brace.
  814. - Indentation with the continuation indent, not with the block indent.
  815. Fundamentally, C++11 braced lists are formatted exactly like function
  816. calls would be formatted in their place. If the braced list follows a name
  817. (e.g. a type or variable name), clang-format formats as if the ``{}`` were
  818. the parentheses of a function call with that name. If there is no name,
  819. a zero-length name is assumed.
  820. .. code-block:: c++
  821. true: false:
  822. vector<int> x{1, 2, 3, 4}; vs. vector<int> x{ 1, 2, 3, 4 };
  823. vector<T> x{{}, {}, {}, {}}; vector<T> x{ {}, {}, {}, {} };
  824. f(MyMap[{composite, key}]); f(MyMap[{ composite, key }]);
  825. new int[3]{1, 2, 3}; new int[3]{ 1, 2, 3 };
  826. **DerivePointerAlignment** (``bool``)
  827. If ``true``, analyze the formatted file for the most common
  828. alignment of ``&`` and ``*``.
  829. Pointer and reference alignment styles are going to be updated according
  830. to the preferences found in the file.
  831. ``PointerAlignment`` is then used only as fallback.
  832. **DisableFormat** (``bool``)
  833. Disables formatting completely.
  834. **ExperimentalAutoDetectBinPacking** (``bool``)
  835. If ``true``, clang-format detects whether function calls and
  836. definitions are formatted with one parameter per line.
  837. Each call can be bin-packed, one-per-line or inconclusive. If it is
  838. inconclusive, e.g. completely on one line, but a decision needs to be
  839. made, clang-format analyzes whether there are other bin-packed cases in
  840. the input file and act accordingly.
  841. NOTE: This is an experimental flag, that might go away or be renamed. Do
  842. not use this in config files, etc. Use at your own risk.
  843. **FixNamespaceComments** (``bool``)
  844. If ``true``, clang-format adds missing namespace end comments and
  845. fixes invalid existing ones.
  846. .. code-block:: c++
  847. true: false:
  848. namespace a { vs. namespace a {
  849. foo(); foo();
  850. } // namespace a; }
  851. **ForEachMacros** (``std::vector<std::string>``)
  852. A vector of macros that should be interpreted as foreach loops
  853. instead of as function calls.
  854. These are expected to be macros of the form:
  855. .. code-block:: c++
  856. FOREACH(<variable-declaration>, ...)
  857. <loop-body>
  858. In the .clang-format configuration file, this can be configured like:
  859. .. code-block:: yaml
  860. ForEachMacros: ['RANGES_FOR', 'FOREACH']
  861. For example: BOOST_FOREACH.
  862. **IncludeCategories** (``std::vector<IncludeCategory>``)
  863. Regular expressions denoting the different ``#include`` categories
  864. used for ordering ``#includes``.
  865. These regular expressions are matched against the filename of an include
  866. (including the <> or "") in order. The value belonging to the first
  867. matching regular expression is assigned and ``#includes`` are sorted first
  868. according to increasing category number and then alphabetically within
  869. each category.
  870. If none of the regular expressions match, INT_MAX is assigned as
  871. category. The main header for a source file automatically gets category 0.
  872. so that it is generally kept at the beginning of the ``#includes``
  873. (http://llvm.org/docs/CodingStandards.html#include-style). However, you
  874. can also assign negative priorities if you have certain headers that
  875. always need to be first.
  876. To configure this in the .clang-format file, use:
  877. .. code-block:: yaml
  878. IncludeCategories:
  879. - Regex: '^"(llvm|llvm-c|clang|clang-c)/'
  880. Priority: 2
  881. - Regex: '^(<|"(gtest|gmock|isl|json)/)'
  882. Priority: 3
  883. - Regex: '.*'
  884. Priority: 1
  885. **IncludeIsMainRegex** (``std::string``)
  886. Specify a regular expression of suffixes that are allowed in the
  887. file-to-main-include mapping.
  888. When guessing whether a #include is the "main" include (to assign
  889. category 0, see above), use this regex of allowed suffixes to the header
  890. stem. A partial match is done, so that:
  891. - "" means "arbitrary suffix"
  892. - "$" means "no suffix"
  893. For example, if configured to "(_test)?$", then a header a.h would be seen
  894. as the "main" include in both a.cc and a_test.cc.
  895. **IndentCaseLabels** (``bool``)
  896. Indent case labels one level from the switch statement.
  897. When ``false``, use the same indentation level as for the switch statement.
  898. Switch statement body is always indented one level more than case labels.
  899. .. code-block:: c++
  900. false: true:
  901. switch (fool) { vs. switch (fool) {
  902. case 1: case 1:
  903. bar(); bar();
  904. break; break;
  905. default: default:
  906. plop(); plop();
  907. } }
  908. **IndentPPDirectives** (``PPDirectiveIndentStyle``)
  909. The preprocessor directive indenting style to use.
  910. Possible values:
  911. * ``PPDIS_None`` (in configuration: ``None``)
  912. Does not indent any directives.
  913. .. code-block:: c++
  914. #if FOO
  915. #if BAR
  916. #include <foo>
  917. #endif
  918. #endif
  919. * ``PPDIS_AfterHash`` (in configuration: ``AfterHash``)
  920. Indents directives after the hash.
  921. .. code-block:: c++
  922. #if FOO
  923. # if BAR
  924. # include <foo>
  925. # endif
  926. #endif
  927. **IndentWidth** (``unsigned``)
  928. The number of columns to use for indentation.
  929. .. code-block:: c++
  930. IndentWidth: 3
  931. void f() {
  932. someFunction();
  933. if (true, false) {
  934. f();
  935. }
  936. }
  937. **IndentWrappedFunctionNames** (``bool``)
  938. Indent if a function definition or declaration is wrapped after the
  939. type.
  940. .. code-block:: c++
  941. true:
  942. LoooooooooooooooooooooooooooooooooooooooongReturnType
  943. LoooooooooooooooooooooooooooooooongFunctionDeclaration();
  944. false:
  945. LoooooooooooooooooooooooooooooooooooooooongReturnType
  946. LoooooooooooooooooooooooooooooooongFunctionDeclaration();
  947. **JavaScriptQuotes** (``JavaScriptQuoteStyle``)
  948. The JavaScriptQuoteStyle to use for JavaScript strings.
  949. Possible values:
  950. * ``JSQS_Leave`` (in configuration: ``Leave``)
  951. Leave string quotes as they are.
  952. .. code-block:: js
  953. string1 = "foo";
  954. string2 = 'bar';
  955. * ``JSQS_Single`` (in configuration: ``Single``)
  956. Always use single quotes.
  957. .. code-block:: js
  958. string1 = 'foo';
  959. string2 = 'bar';
  960. * ``JSQS_Double`` (in configuration: ``Double``)
  961. Always use double quotes.
  962. .. code-block:: js
  963. string1 = "foo";
  964. string2 = "bar";
  965. **JavaScriptWrapImports** (``bool``)
  966. Whether to wrap JavaScript import/export statements.
  967. .. code-block:: js
  968. true:
  969. import {
  970. VeryLongImportsAreAnnoying,
  971. VeryLongImportsAreAnnoying,
  972. VeryLongImportsAreAnnoying,
  973. } from 'some/module.js'
  974. false:
  975. import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying,} from "some/module.js"
  976. **KeepEmptyLinesAtTheStartOfBlocks** (``bool``)
  977. If true, the empty line at the start of blocks is kept.
  978. .. code-block:: c++
  979. true: false:
  980. if (foo) { vs. if (foo) {
  981. bar();
  982. bar(); }
  983. }
  984. **Language** (``LanguageKind``)
  985. Language, this format style is targeted at.
  986. Possible values:
  987. * ``LK_None`` (in configuration: ``None``)
  988. Do not use.
  989. * ``LK_Cpp`` (in configuration: ``Cpp``)
  990. Should be used for C, C++.
  991. * ``LK_Java`` (in configuration: ``Java``)
  992. Should be used for Java.
  993. * ``LK_JavaScript`` (in configuration: ``JavaScript``)
  994. Should be used for JavaScript.
  995. * ``LK_ObjC`` (in configuration: ``ObjC``)
  996. Should be used for Objective-C, Objective-C++.
  997. * ``LK_Proto`` (in configuration: ``Proto``)
  998. Should be used for Protocol Buffers
  999. (https://developers.google.com/protocol-buffers/).
  1000. * ``LK_TableGen`` (in configuration: ``TableGen``)
  1001. Should be used for TableGen code.
  1002. * ``LK_TextProto`` (in configuration: ``TextProto``)
  1003. Should be used for Protocol Buffer messages in text format
  1004. (https://developers.google.com/protocol-buffers/).
  1005. **MacroBlockBegin** (``std::string``)
  1006. A regular expression matching macros that start a block.
  1007. .. code-block:: c++
  1008. # With:
  1009. MacroBlockBegin: "^NS_MAP_BEGIN|\
  1010. NS_TABLE_HEAD$"
  1011. MacroBlockEnd: "^\
  1012. NS_MAP_END|\
  1013. NS_TABLE_.*_END$"
  1014. NS_MAP_BEGIN
  1015. foo();
  1016. NS_MAP_END
  1017. NS_TABLE_HEAD
  1018. bar();
  1019. NS_TABLE_FOO_END
  1020. # Without:
  1021. NS_MAP_BEGIN
  1022. foo();
  1023. NS_MAP_END
  1024. NS_TABLE_HEAD
  1025. bar();
  1026. NS_TABLE_FOO_END
  1027. **MacroBlockEnd** (``std::string``)
  1028. A regular expression matching macros that end a block.
  1029. **MaxEmptyLinesToKeep** (``unsigned``)
  1030. The maximum number of consecutive empty lines to keep.
  1031. .. code-block:: c++
  1032. MaxEmptyLinesToKeep: 1 vs. MaxEmptyLinesToKeep: 0
  1033. int f() { int f() {
  1034. int = 1; int i = 1;
  1035. i = foo();
  1036. i = foo(); return i;
  1037. }
  1038. return i;
  1039. }
  1040. **NamespaceIndentation** (``NamespaceIndentationKind``)
  1041. The indentation used for namespaces.
  1042. Possible values:
  1043. * ``NI_None`` (in configuration: ``None``)
  1044. Don't indent in namespaces.
  1045. .. code-block:: c++
  1046. namespace out {
  1047. int i;
  1048. namespace in {
  1049. int i;
  1050. }
  1051. }
  1052. * ``NI_Inner`` (in configuration: ``Inner``)
  1053. Indent only in inner namespaces (nested in other namespaces).
  1054. .. code-block:: c++
  1055. namespace out {
  1056. int i;
  1057. namespace in {
  1058. int i;
  1059. }
  1060. }
  1061. * ``NI_All`` (in configuration: ``All``)
  1062. Indent in all namespaces.
  1063. .. code-block:: c++
  1064. namespace out {
  1065. int i;
  1066. namespace in {
  1067. int i;
  1068. }
  1069. }
  1070. **ObjCBlockIndentWidth** (``unsigned``)
  1071. The number of characters to use for indentation of ObjC blocks.
  1072. .. code-block:: objc
  1073. ObjCBlockIndentWidth: 4
  1074. [operation setCompletionBlock:^{
  1075. [self onOperationDone];
  1076. }];
  1077. **ObjCSpaceAfterProperty** (``bool``)
  1078. Add a space after ``@property`` in Objective-C, i.e. use
  1079. ``@property (readonly)`` instead of ``@property(readonly)``.
  1080. **ObjCSpaceBeforeProtocolList** (``bool``)
  1081. Add a space in front of an Objective-C protocol list, i.e. use
  1082. ``Foo <Protocol>`` instead of ``Foo<Protocol>``.
  1083. **PenaltyBreakAssignment** (``unsigned``)
  1084. The penalty for breaking around an assignment operator.
  1085. **PenaltyBreakBeforeFirstCallParameter** (``unsigned``)
  1086. The penalty for breaking a function call after ``call(``.
  1087. **PenaltyBreakComment** (``unsigned``)
  1088. The penalty for each line break introduced inside a comment.
  1089. **PenaltyBreakFirstLessLess** (``unsigned``)
  1090. The penalty for breaking before the first ``<<``.
  1091. **PenaltyBreakString** (``unsigned``)
  1092. The penalty for each line break introduced inside a string literal.
  1093. **PenaltyExcessCharacter** (``unsigned``)
  1094. The penalty for each character outside of the column limit.
  1095. **PenaltyReturnTypeOnItsOwnLine** (``unsigned``)
  1096. Penalty for putting the return type of a function onto its own
  1097. line.
  1098. **PointerAlignment** (``PointerAlignmentStyle``)
  1099. Pointer and reference alignment style.
  1100. Possible values:
  1101. * ``PAS_Left`` (in configuration: ``Left``)
  1102. Align pointer to the left.
  1103. .. code-block:: c++
  1104. int* a;
  1105. * ``PAS_Right`` (in configuration: ``Right``)
  1106. Align pointer to the right.
  1107. .. code-block:: c++
  1108. int *a;
  1109. * ``PAS_Middle`` (in configuration: ``Middle``)
  1110. Align pointer in the middle.
  1111. .. code-block:: c++
  1112. int * a;
  1113. **ReflowComments** (``bool``)
  1114. If ``true``, clang-format will attempt to re-flow comments.
  1115. .. code-block:: c++
  1116. false:
  1117. // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information
  1118. /* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information */
  1119. true:
  1120. // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of
  1121. // information
  1122. /* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of
  1123. * information */
  1124. **SortIncludes** (``bool``)
  1125. If ``true``, clang-format will sort ``#includes``.
  1126. .. code-block:: c++
  1127. false: true:
  1128. #include "b.h" vs. #include "a.h"
  1129. #include "a.h" #include "b.h"
  1130. **SortUsingDeclarations** (``bool``)
  1131. If ``true``, clang-format will sort using declarations.
  1132. .. code-block:: c++
  1133. false: true:
  1134. using std::cout; vs. using std::cin;
  1135. using std::cin; using std::cout;
  1136. **SpaceAfterCStyleCast** (``bool``)
  1137. If ``true``, a space is inserted after C style casts.
  1138. .. code-block:: c++
  1139. true: false:
  1140. (int)i; vs. (int) i;
  1141. **SpaceAfterTemplateKeyword** (``bool``)
  1142. If ``true``, a space will be inserted after the 'template' keyword.
  1143. .. code-block:: c++
  1144. true: false:
  1145. template <int> void foo(); vs. template<int> void foo();
  1146. **SpaceBeforeAssignmentOperators** (``bool``)
  1147. If ``false``, spaces will be removed before assignment operators.
  1148. .. code-block:: c++
  1149. true: false:
  1150. int a = 5; vs. int a=5;
  1151. a += 42 a+=42;
  1152. **SpaceBeforeParens** (``SpaceBeforeParensOptions``)
  1153. Defines in which cases to put a space before opening parentheses.
  1154. Possible values:
  1155. * ``SBPO_Never`` (in configuration: ``Never``)
  1156. Never put a space before opening parentheses.
  1157. .. code-block:: c++
  1158. void f() {
  1159. if(true) {
  1160. f();
  1161. }
  1162. }
  1163. * ``SBPO_ControlStatements`` (in configuration: ``ControlStatements``)
  1164. Put a space before opening parentheses only after control statement
  1165. keywords (``for/if/while...``).
  1166. .. code-block:: c++
  1167. void f() {
  1168. if (true) {
  1169. f();
  1170. }
  1171. }
  1172. * ``SBPO_Always`` (in configuration: ``Always``)
  1173. Always put a space before opening parentheses, except when it's
  1174. prohibited by the syntax rules (in function-like macro definitions) or
  1175. when determined by other style rules (after unary operators, opening
  1176. parentheses, etc.)
  1177. .. code-block:: c++
  1178. void f () {
  1179. if (true) {
  1180. f ();
  1181. }
  1182. }
  1183. **SpaceInEmptyParentheses** (``bool``)
  1184. If ``true``, spaces may be inserted into ``()``.
  1185. .. code-block:: c++
  1186. true: false:
  1187. void f( ) { vs. void f() {
  1188. int x[] = {foo( ), bar( )}; int x[] = {foo(), bar()};
  1189. if (true) { if (true) {
  1190. f( ); f();
  1191. } }
  1192. } }
  1193. **SpacesBeforeTrailingComments** (``unsigned``)
  1194. The number of spaces before trailing line comments
  1195. (``//`` - comments).
  1196. This does not affect trailing block comments (``/*`` - comments) as
  1197. those commonly have different usage patterns and a number of special
  1198. cases.
  1199. .. code-block:: c++
  1200. SpacesBeforeTrailingComments: 3
  1201. void f() {
  1202. if (true) { // foo1
  1203. f(); // bar
  1204. } // foo
  1205. }
  1206. **SpacesInAngles** (``bool``)
  1207. If ``true``, spaces will be inserted after ``<`` and before ``>``
  1208. in template argument lists.
  1209. .. code-block:: c++
  1210. true: false:
  1211. static_cast< int >(arg); vs. static_cast<int>(arg);
  1212. std::function< void(int) > fct; std::function<void(int)> fct;
  1213. **SpacesInCStyleCastParentheses** (``bool``)
  1214. If ``true``, spaces may be inserted into C style casts.
  1215. .. code-block:: c++
  1216. true: false:
  1217. x = ( int32 )y vs. x = (int32)y
  1218. **SpacesInContainerLiterals** (``bool``)
  1219. If ``true``, spaces are inserted inside container literals (e.g.
  1220. ObjC and Javascript array and dict literals).
  1221. .. code-block:: js
  1222. true: false:
  1223. var arr = [ 1, 2, 3 ]; vs. var arr = [1, 2, 3];
  1224. f({a : 1, b : 2, c : 3}); f({a: 1, b: 2, c: 3});
  1225. **SpacesInParentheses** (``bool``)
  1226. If ``true``, spaces will be inserted after ``(`` and before ``)``.
  1227. .. code-block:: c++
  1228. true: false:
  1229. t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete;
  1230. **SpacesInSquareBrackets** (``bool``)
  1231. If ``true``, spaces will be inserted after ``[`` and before ``]``.
  1232. Lambdas or unspecified size array declarations will not be affected.
  1233. .. code-block:: c++
  1234. true: false:
  1235. int a[ 5 ]; vs. int a[5];
  1236. std::unique_ptr<int[]> foo() {} // Won't be affected
  1237. **Standard** (``LanguageStandard``)
  1238. Format compatible with this standard, e.g. use ``A<A<int> >``
  1239. instead of ``A<A<int>>`` for ``LS_Cpp03``.
  1240. Possible values:
  1241. * ``LS_Cpp03`` (in configuration: ``Cpp03``)
  1242. Use C++03-compatible syntax.
  1243. * ``LS_Cpp11`` (in configuration: ``Cpp11``)
  1244. Use features of C++11, C++14 and C++1z (e.g. ``A<A<int>>`` instead of
  1245. ``A<A<int> >``).
  1246. * ``LS_Auto`` (in configuration: ``Auto``)
  1247. Automatic detection based on the input.
  1248. **TabWidth** (``unsigned``)
  1249. The number of columns used for tab stops.
  1250. **UseTab** (``UseTabStyle``)
  1251. The way to use tab characters in the resulting file.
  1252. Possible values:
  1253. * ``UT_Never`` (in configuration: ``Never``)
  1254. Never use tab.
  1255. * ``UT_ForIndentation`` (in configuration: ``ForIndentation``)
  1256. Use tabs only for indentation.
  1257. * ``UT_ForContinuationAndIndentation`` (in configuration: ``ForContinuationAndIndentation``)
  1258. Use tabs only for line continuation and indentation.
  1259. * ``UT_Always`` (in configuration: ``Always``)
  1260. Use tabs whenever we need to fill whitespace that spans at least from
  1261. one tab stop to the next one.
  1262. .. END_FORMAT_STYLE_OPTIONS
  1263. Adding additional style options
  1264. ===============================
  1265. Each additional style option adds costs to the clang-format project. Some of
  1266. these costs affect the clang-format development itself, as we need to make
  1267. sure that any given combination of options work and that new features don't
  1268. break any of the existing options in any way. There are also costs for end users
  1269. as options become less discoverable and people have to think about and make a
  1270. decision on options they don't really care about.
  1271. The goal of the clang-format project is more on the side of supporting a
  1272. limited set of styles really well as opposed to supporting every single style
  1273. used by a codebase somewhere in the wild. Of course, we do want to support all
  1274. major projects and thus have established the following bar for adding style
  1275. options. Each new style option must ..
  1276. * be used in a project of significant size (have dozens of contributors)
  1277. * have a publicly accessible style guide
  1278. * have a person willing to contribute and maintain patches
  1279. Examples
  1280. ========
  1281. A style similar to the `Linux Kernel style
  1282. <https://www.kernel.org/doc/Documentation/CodingStyle>`_:
  1283. .. code-block:: yaml
  1284. BasedOnStyle: LLVM
  1285. IndentWidth: 8
  1286. UseTab: Always
  1287. BreakBeforeBraces: Linux
  1288. AllowShortIfStatementsOnASingleLine: false
  1289. IndentCaseLabels: false
  1290. The result is (imagine that tabs are used for indentation here):
  1291. .. code-block:: c++
  1292. void test()
  1293. {
  1294. switch (x) {
  1295. case 0:
  1296. case 1:
  1297. do_something();
  1298. break;
  1299. case 2:
  1300. do_something_else();
  1301. break;
  1302. default:
  1303. break;
  1304. }
  1305. if (condition)
  1306. do_something_completely_different();
  1307. if (x == y) {
  1308. q();
  1309. } else if (x > y) {
  1310. w();
  1311. } else {
  1312. r();
  1313. }
  1314. }
  1315. A style similar to the default Visual Studio formatting style:
  1316. .. code-block:: yaml
  1317. UseTab: Never
  1318. IndentWidth: 4
  1319. BreakBeforeBraces: Allman
  1320. AllowShortIfStatementsOnASingleLine: false
  1321. IndentCaseLabels: false
  1322. ColumnLimit: 0
  1323. The result is:
  1324. .. code-block:: c++
  1325. void test()
  1326. {
  1327. switch (suffix)
  1328. {
  1329. case 0:
  1330. case 1:
  1331. do_something();
  1332. break;
  1333. case 2:
  1334. do_something_else();
  1335. break;
  1336. default:
  1337. break;
  1338. }
  1339. if (condition)
  1340. do_somthing_completely_different();
  1341. if (x == y)
  1342. {
  1343. q();
  1344. }
  1345. else if (x > y)
  1346. {
  1347. w();
  1348. }
  1349. else
  1350. {
  1351. r();
  1352. }
  1353. }