|
| 1 | +AROS C/C Style Guide (KR-4) |
| 2 | +========================== |
| 3 | + |
| 4 | +Scope |
| 5 | +----- |
| 6 | + |
| 7 | +This guide applies to all ``.c`` and ``.h`` files. If there is a conflict |
| 8 | +between local style and this guide, this guide wins. |
| 9 | + |
| 10 | +1) Indentation and whitespace |
| 11 | +----------------------------- |
| 12 | + |
| 13 | +Indentation |
| 14 | +~~~~~~~~~~~ |
| 15 | + |
| 16 | +* Indent with **4 spaces** per level. |
| 17 | +* Do **not** use tabs for indentation. |
| 18 | + |
| 19 | +Example:: |
| 20 | + |
| 21 | + if (cond) { |
| 22 | + do_work(); |
| 23 | + } |
| 24 | + |
| 25 | +Operator spacing |
| 26 | +~~~~~~~~~~~~~~~~ |
| 27 | + |
| 28 | +* Put spaces around binary operators. |
| 29 | + |
| 30 | +Examples:: |
| 31 | + |
| 32 | + a = b + c; |
| 33 | + mask = (x << 2) | y; |
| 34 | + ptr = base + offset; |
| 35 | + |
| 36 | +* Unary operators remain tight where idiomatic. |
| 37 | + |
| 38 | +Examples:: |
| 39 | + |
| 40 | + i++; |
| 41 | + --j; |
| 42 | + p = &obj; |
| 43 | + q = *p; |
| 44 | + |
| 45 | +Parentheses spacing |
| 46 | +~~~~~~~~~~~~~~~~~~~ |
| 47 | + |
| 48 | +* Do not add spaces immediately inside parentheses. |
| 49 | + |
| 50 | +Examples:: |
| 51 | + |
| 52 | + if (a == b) /* not: if ( a == b ) */ |
| 53 | + func(x, y); /* not: func( x, y ) */ |
| 54 | + |
| 55 | +2) Braces and block layout |
| 56 | +-------------------------- |
| 57 | + |
| 58 | +K&R brace style |
| 59 | +~~~~~~~~~~~~~~~ |
| 60 | + |
| 61 | +* Opening brace on the **same line** as the control statement or signature. |
| 62 | +* Closing brace on its own line. |
| 63 | + |
| 64 | +Example:: |
| 65 | + |
| 66 | + static int foo(int x) |
| 67 | + { |
| 68 | + if (x > 0) { |
| 69 | + return x; |
| 70 | + } |
| 71 | + |
| 72 | + return 0; |
| 73 | + } |
| 74 | + |
| 75 | +``else`` placement |
| 76 | +~~~~~~~~~~~~~~~~~~ |
| 77 | + |
| 78 | +* ``else`` follows the closing brace on the same line. |
| 79 | + |
| 80 | +Example:: |
| 81 | + |
| 82 | + if (ok) { |
| 83 | + do_a(); |
| 84 | + } else { |
| 85 | + do_b(); |
| 86 | + } |
| 87 | + |
| 88 | +3) ``do { } while ();`` formatting |
| 89 | +---------------------------------- |
| 90 | + |
| 91 | +* Attach the ``while (...)`` to the closing brace line. |
| 92 | + |
| 93 | +Example:: |
| 94 | + |
| 95 | + do { |
| 96 | + work(); |
| 97 | + } while (cond); |
| 98 | + |
| 99 | +4) Pointer alignment |
| 100 | +-------------------- |
| 101 | + |
| 102 | +* The ``*`` binds to the **name**, not the type. |
| 103 | + |
| 104 | +Examples:: |
| 105 | + |
| 106 | + char *p; |
| 107 | + const struct Foo *foo; |
| 108 | + struct Node *node; |
| 109 | + void (*fn)(int x); |
| 110 | + |
| 111 | +* For multiple declarators, keep the ``*`` with each name. |
| 112 | + |
| 113 | +Example:: |
| 114 | + |
| 115 | + char *a, *b; |
| 116 | + |
| 117 | +5) Line length |
| 118 | +-------------- |
| 119 | + |
| 120 | +* Maximum line length is **120 columns**. |
| 121 | +* Wrap long expressions, conditions, and function calls. |
| 122 | + |
| 123 | +Recommended wrapping patterns |
| 124 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 125 | + |
| 126 | +Conditions:: |
| 127 | + |
| 128 | + if (really_long_condition && |
| 129 | + another_condition && |
| 130 | + final_condition) { |
| 131 | + ... |
| 132 | + } |
| 133 | + |
| 134 | +Function calls:: |
| 135 | + |
| 136 | + rc = some_function(arg1, arg2, arg3, |
| 137 | + arg4, arg5); |
| 138 | + |
| 139 | +Logging and long format strings:: |
| 140 | + |
| 141 | + pciusbDebug("xHCI", "%s: dev=%u ep=%u state=%lu\n", |
| 142 | + __func__, dev, ep, state); |
| 143 | + |
| 144 | +Guidance: |
| 145 | + |
| 146 | +* Break after logical operators (``&&``, ``||``) when wrapping conditions. |
| 147 | +* For function calls, align continued arguments under the first argument or use |
| 148 | + a consistent continuation indent. |
| 149 | + |
| 150 | +6) Line endings |
| 151 | +--------------- |
| 152 | + |
| 153 | +* Files must use Unix **LF** line endings. |
| 154 | +* Avoid committing CRLF. |
| 155 | + |
| 156 | +7) Statements and blank lines |
| 157 | +----------------------------- |
| 158 | + |
| 159 | +* One statement per line. |
| 160 | +* Use blank lines to separate logical sections within a function. |
| 161 | +* Avoid trailing whitespace. |
| 162 | +* Prefer braces for control blocks to reduce diff-risk. |
| 163 | + |
| 164 | +Preferred:: |
| 165 | + |
| 166 | + if (error) { |
| 167 | + return FAIL; |
| 168 | + } |
| 169 | + |
| 170 | +8) Headers and includes |
| 171 | +----------------------- |
| 172 | + |
| 173 | +* Group includes: |
| 174 | + |
| 175 | + 1. Local header for the ``.c`` file (if applicable) |
| 176 | + 2. System/SDK headers |
| 177 | + 3. Project headers |
| 178 | + |
| 179 | +* Separate include groups with a blank line. |
| 180 | +* Prefer forward declarations in headers where possible to reduce include |
| 181 | + coupling. |
| 182 | + |
| 183 | +9) Formatting workflow |
| 184 | +---------------------- |
| 185 | + |
| 186 | +* Format files consistently before submitting changes. |
| 187 | +* Avoid mixing large reformat-only diffs with functional changes unless |
| 188 | + necessary. |
0 commit comments