diff options
| -rw-r--r-- | Documentation/CodingStyle | 98 |
1 files changed, 49 insertions, 49 deletions
diff --git a/Documentation/CodingStyle b/Documentation/CodingStyle index f103de7e2028..c25528d76af1 100644 --- a/Documentation/CodingStyle +++ b/Documentation/CodingStyle | |||
| @@ -37,8 +37,8 @@ benefit of warning you when you're nesting your functions too deep. | |||
| 37 | Heed that warning. | 37 | Heed that warning. |
| 38 | 38 | ||
| 39 | The preferred way to ease multiple indentation levels in a switch statement is | 39 | The preferred way to ease multiple indentation levels in a switch statement is |
| 40 | to align the "switch" and its subordinate "case" labels in the same column | 40 | to align the ``switch`` and its subordinate ``case`` labels in the same column |
| 41 | instead of "double-indenting" the "case" labels. E.g.: | 41 | instead of ``double-indenting`` the ``case`` labels. E.g.: |
| 42 | 42 | ||
| 43 | .. code-block:: c | 43 | .. code-block:: c |
| 44 | 44 | ||
| @@ -141,7 +141,7 @@ special anyway (you can't nest them in C). | |||
| 141 | 141 | ||
| 142 | Note that the closing brace is empty on a line of its own, _except_ in | 142 | Note that the closing brace is empty on a line of its own, _except_ in |
| 143 | the cases where it is followed by a continuation of the same statement, | 143 | the cases where it is followed by a continuation of the same statement, |
| 144 | ie a "while" in a do-statement or an "else" in an if-statement, like | 144 | ie a ``while`` in a do-statement or an ``else`` in an if-statement, like |
| 145 | this: | 145 | this: |
| 146 | 146 | ||
| 147 | .. code-block:: c | 147 | .. code-block:: c |
| @@ -228,7 +228,7 @@ Do not add spaces around (inside) parenthesized expressions. This example is | |||
| 228 | s = sizeof( struct file ); | 228 | s = sizeof( struct file ); |
| 229 | 229 | ||
| 230 | When declaring pointer data or a function that returns a pointer type, the | 230 | When declaring pointer data or a function that returns a pointer type, the |
| 231 | preferred use of '\*' is adjacent to the data name or function name and not | 231 | preferred use of ``*`` is adjacent to the data name or function name and not |
| 232 | adjacent to the type name. Examples: | 232 | adjacent to the type name. Examples: |
| 233 | 233 | ||
| 234 | .. code-block:: c | 234 | .. code-block:: c |
| @@ -255,10 +255,10 @@ no space after the prefix increment & decrement unary operators:: | |||
| 255 | 255 | ||
| 256 | ++ -- | 256 | ++ -- |
| 257 | 257 | ||
| 258 | and no space around the '.' and "->" structure member operators. | 258 | and no space around the ``.`` and ``->`` structure member operators. |
| 259 | 259 | ||
| 260 | Do not leave trailing whitespace at the ends of lines. Some editors with | 260 | Do not leave trailing whitespace at the ends of lines. Some editors with |
| 261 | "smart" indentation will insert whitespace at the beginning of new lines as | 261 | ``smart`` indentation will insert whitespace at the beginning of new lines as |
| 262 | appropriate, so you can start typing the next line of code right away. | 262 | appropriate, so you can start typing the next line of code right away. |
| 263 | However, some such editors do not remove the whitespace if you end up not | 263 | However, some such editors do not remove the whitespace if you end up not |
| 264 | putting a line of code there, such as if you leave a blank line. As a result, | 264 | putting a line of code there, such as if you leave a blank line. As a result, |
| @@ -276,17 +276,17 @@ context lines. | |||
| 276 | C is a Spartan language, and so should your naming be. Unlike Modula-2 | 276 | C is a Spartan language, and so should your naming be. Unlike Modula-2 |
| 277 | and Pascal programmers, C programmers do not use cute names like | 277 | and Pascal programmers, C programmers do not use cute names like |
| 278 | ThisVariableIsATemporaryCounter. A C programmer would call that | 278 | ThisVariableIsATemporaryCounter. A C programmer would call that |
| 279 | variable "tmp", which is much easier to write, and not the least more | 279 | variable ``tmp``, which is much easier to write, and not the least more |
| 280 | difficult to understand. | 280 | difficult to understand. |
| 281 | 281 | ||
| 282 | HOWEVER, while mixed-case names are frowned upon, descriptive names for | 282 | HOWEVER, while mixed-case names are frowned upon, descriptive names for |
| 283 | global variables are a must. To call a global function "foo" is a | 283 | global variables are a must. To call a global function ``foo`` is a |
| 284 | shooting offense. | 284 | shooting offense. |
| 285 | 285 | ||
| 286 | GLOBAL variables (to be used only if you _really_ need them) need to | 286 | GLOBAL variables (to be used only if you _really_ need them) need to |
| 287 | have descriptive names, as do global functions. If you have a function | 287 | have descriptive names, as do global functions. If you have a function |
| 288 | that counts the number of active users, you should call that | 288 | that counts the number of active users, you should call that |
| 289 | "count_active_users()" or similar, you should _not_ call it "cntusr()". | 289 | ``count_active_users()`` or similar, you should _not_ call it ``cntusr()``. |
| 290 | 290 | ||
| 291 | Encoding the type of a function into the name (so-called Hungarian | 291 | Encoding the type of a function into the name (so-called Hungarian |
| 292 | notation) is brain damaged - the compiler knows the types anyway and can | 292 | notation) is brain damaged - the compiler knows the types anyway and can |
| @@ -294,9 +294,9 @@ check those, and it only confuses the programmer. No wonder MicroSoft | |||
| 294 | makes buggy programs. | 294 | makes buggy programs. |
| 295 | 295 | ||
| 296 | LOCAL variable names should be short, and to the point. If you have | 296 | LOCAL variable names should be short, and to the point. If you have |
| 297 | some random integer loop counter, it should probably be called "i". | 297 | some random integer loop counter, it should probably be called ``i``. |
| 298 | Calling it "loop_counter" is non-productive, if there is no chance of it | 298 | Calling it ``loop_counter`` is non-productive, if there is no chance of it |
| 299 | being mis-understood. Similarly, "tmp" can be just about any type of | 299 | being mis-understood. Similarly, ``tmp`` can be just about any type of |
| 300 | variable that is used to hold a temporary value. | 300 | variable that is used to hold a temporary value. |
| 301 | 301 | ||
| 302 | If you are afraid to mix up your local variable names, you have another | 302 | If you are afraid to mix up your local variable names, you have another |
| @@ -307,7 +307,7 @@ See chapter 6 (Functions). | |||
| 307 | 5) Typedefs | 307 | 5) Typedefs |
| 308 | ----------- | 308 | ----------- |
| 309 | 309 | ||
| 310 | Please don't use things like "vps_t". | 310 | Please don't use things like ``vps_t``. |
| 311 | It's a _mistake_ to use typedef for structures and pointers. When you see a | 311 | It's a _mistake_ to use typedef for structures and pointers. When you see a |
| 312 | 312 | ||
| 313 | .. code-block:: c | 313 | .. code-block:: c |
| @@ -322,35 +322,35 @@ In contrast, if it says | |||
| 322 | 322 | ||
| 323 | struct virtual_container *a; | 323 | struct virtual_container *a; |
| 324 | 324 | ||
| 325 | you can actually tell what "a" is. | 325 | you can actually tell what ``a`` is. |
| 326 | 326 | ||
| 327 | Lots of people think that typedefs "help readability". Not so. They are | 327 | Lots of people think that typedefs ``help readability``. Not so. They are |
| 328 | useful only for: | 328 | useful only for: |
| 329 | 329 | ||
| 330 | (a) totally opaque objects (where the typedef is actively used to _hide_ | 330 | (a) totally opaque objects (where the typedef is actively used to _hide_ |
| 331 | what the object is). | 331 | what the object is). |
| 332 | 332 | ||
| 333 | Example: "pte_t" etc. opaque objects that you can only access using | 333 | Example: ``pte_t`` etc. opaque objects that you can only access using |
| 334 | the proper accessor functions. | 334 | the proper accessor functions. |
| 335 | 335 | ||
| 336 | NOTE! Opaqueness and "accessor functions" are not good in themselves. | 336 | NOTE! Opaqueness and ``accessor functions`` are not good in themselves. |
| 337 | The reason we have them for things like pte_t etc. is that there | 337 | The reason we have them for things like pte_t etc. is that there |
| 338 | really is absolutely _zero_ portably accessible information there. | 338 | really is absolutely _zero_ portably accessible information there. |
| 339 | 339 | ||
| 340 | (b) Clear integer types, where the abstraction _helps_ avoid confusion | 340 | (b) Clear integer types, where the abstraction _helps_ avoid confusion |
| 341 | whether it is "int" or "long". | 341 | whether it is ``int`` or ``long``. |
| 342 | 342 | ||
| 343 | u8/u16/u32 are perfectly fine typedefs, although they fit into | 343 | u8/u16/u32 are perfectly fine typedefs, although they fit into |
| 344 | category (d) better than here. | 344 | category (d) better than here. |
| 345 | 345 | ||
| 346 | NOTE! Again - there needs to be a _reason_ for this. If something is | 346 | NOTE! Again - there needs to be a _reason_ for this. If something is |
| 347 | "unsigned long", then there's no reason to do | 347 | ``unsigned long``, then there's no reason to do |
| 348 | 348 | ||
| 349 | typedef unsigned long myflags_t; | 349 | typedef unsigned long myflags_t; |
| 350 | 350 | ||
| 351 | but if there is a clear reason for why it under certain circumstances | 351 | but if there is a clear reason for why it under certain circumstances |
| 352 | might be an "unsigned int" and under other configurations might be | 352 | might be an ``unsigned int`` and under other configurations might be |
| 353 | "unsigned long", then by all means go ahead and use a typedef. | 353 | ``unsigned long``, then by all means go ahead and use a typedef. |
| 354 | 354 | ||
| 355 | (c) when you use sparse to literally create a _new_ type for | 355 | (c) when you use sparse to literally create a _new_ type for |
| 356 | type-checking. | 356 | type-checking. |
| @@ -359,10 +359,10 @@ useful only for: | |||
| 359 | exceptional circumstances. | 359 | exceptional circumstances. |
| 360 | 360 | ||
| 361 | Although it would only take a short amount of time for the eyes and | 361 | Although it would only take a short amount of time for the eyes and |
| 362 | brain to become accustomed to the standard types like 'uint32_t', | 362 | brain to become accustomed to the standard types like ``uint32_t``, |
| 363 | some people object to their use anyway. | 363 | some people object to their use anyway. |
| 364 | 364 | ||
| 365 | Therefore, the Linux-specific 'u8/u16/u32/u64' types and their | 365 | Therefore, the Linux-specific ``u8/u16/u32/u64`` types and their |
| 366 | signed equivalents which are identical to standard types are | 366 | signed equivalents which are identical to standard types are |
| 367 | permitted -- although they are not mandatory in new code of your | 367 | permitted -- although they are not mandatory in new code of your |
| 368 | own. | 368 | own. |
| @@ -373,7 +373,7 @@ useful only for: | |||
| 373 | (e) Types safe for use in userspace. | 373 | (e) Types safe for use in userspace. |
| 374 | 374 | ||
| 375 | In certain structures which are visible to userspace, we cannot | 375 | In certain structures which are visible to userspace, we cannot |
| 376 | require C99 types and cannot use the 'u32' form above. Thus, we | 376 | require C99 types and cannot use the ``u32`` form above. Thus, we |
| 377 | use __u32 and similar types in all structures which are shared | 377 | use __u32 and similar types in all structures which are shared |
| 378 | with userspace. | 378 | with userspace. |
| 379 | 379 | ||
| @@ -440,13 +440,13 @@ locations and some common work such as cleanup has to be done. If there is no | |||
| 440 | cleanup needed then just return directly. | 440 | cleanup needed then just return directly. |
| 441 | 441 | ||
| 442 | Choose label names which say what the goto does or why the goto exists. An | 442 | Choose label names which say what the goto does or why the goto exists. An |
| 443 | example of a good name could be "out_free_buffer:" if the goto frees "buffer". | 443 | example of a good name could be ``out_free_buffer:`` if the goto frees ``buffer``. |
| 444 | Avoid using GW-BASIC names like "err1:" and "err2:", as you would have to | 444 | Avoid using GW-BASIC names like ``err1:`` and ``err2:``, as you would have to |
| 445 | renumber them if you ever add or remove exit paths, and they make correctness | 445 | renumber them if you ever add or remove exit paths, and they make correctness |
| 446 | difficult to verify anyway. | 446 | difficult to verify anyway. |
| 447 | 447 | ||
| 448 | It is advised to indent labels with a single space (not tab), so that | 448 | It is advised to indent labels with a single space (not tab), so that |
| 449 | "diff -p" does not confuse labels with functions. | 449 | ``diff -p`` does not confuse labels with functions. |
| 450 | 450 | ||
| 451 | The rationale for using gotos is: | 451 | The rationale for using gotos is: |
| 452 | 452 | ||
| @@ -480,7 +480,7 @@ The rationale for using gotos is: | |||
| 480 | return result; | 480 | return result; |
| 481 | } | 481 | } |
| 482 | 482 | ||
| 483 | A common type of bug to be aware of is "one err bugs" which look like this: | 483 | A common type of bug to be aware of is ``one err bugs`` which look like this: |
| 484 | 484 | ||
| 485 | .. code-block:: c | 485 | .. code-block:: c |
| 486 | 486 | ||
| @@ -489,9 +489,9 @@ A common type of bug to be aware of is "one err bugs" which look like this: | |||
| 489 | kfree(foo); | 489 | kfree(foo); |
| 490 | return ret; | 490 | return ret; |
| 491 | 491 | ||
| 492 | The bug in this code is that on some exit paths "foo" is NULL. Normally the | 492 | The bug in this code is that on some exit paths ``foo`` is NULL. Normally the |
| 493 | fix for this is to split it up into two error labels "err_free_bar:" and | 493 | fix for this is to split it up into two error labels ``err_free_bar:`` and |
| 494 | "err_free_foo:": | 494 | ``err_free_foo:``: |
| 495 | 495 | ||
| 496 | .. code-block:: c | 496 | .. code-block:: c |
| 497 | 497 | ||
| @@ -560,7 +560,7 @@ item, explaining its use. | |||
| 560 | --------------------------- | 560 | --------------------------- |
| 561 | 561 | ||
| 562 | That's OK, we all do. You've probably been told by your long-time Unix | 562 | That's OK, we all do. You've probably been told by your long-time Unix |
| 563 | user helper that "GNU emacs" automatically formats the C sources for | 563 | user helper that ``GNU emacs`` automatically formats the C sources for |
| 564 | you, and you've noticed that yes, it does do that, but the defaults it | 564 | you, and you've noticed that yes, it does do that, but the defaults it |
| 565 | uses are less than desirable (in fact, they are worse than random | 565 | uses are less than desirable (in fact, they are worse than random |
| 566 | typing - an infinite number of monkeys typing into GNU emacs would never | 566 | typing - an infinite number of monkeys typing into GNU emacs would never |
| @@ -605,26 +605,26 @@ This will make emacs go better with the kernel coding style for C | |||
| 605 | files below ``~/src/linux-trees``. | 605 | files below ``~/src/linux-trees``. |
| 606 | 606 | ||
| 607 | But even if you fail in getting emacs to do sane formatting, not | 607 | But even if you fail in getting emacs to do sane formatting, not |
| 608 | everything is lost: use "indent". | 608 | everything is lost: use ``indent``. |
| 609 | 609 | ||
| 610 | Now, again, GNU indent has the same brain-dead settings that GNU emacs | 610 | Now, again, GNU indent has the same brain-dead settings that GNU emacs |
| 611 | has, which is why you need to give it a few command line options. | 611 | has, which is why you need to give it a few command line options. |
| 612 | However, that's not too bad, because even the makers of GNU indent | 612 | However, that's not too bad, because even the makers of GNU indent |
| 613 | recognize the authority of K&R (the GNU people aren't evil, they are | 613 | recognize the authority of K&R (the GNU people aren't evil, they are |
| 614 | just severely misguided in this matter), so you just give indent the | 614 | just severely misguided in this matter), so you just give indent the |
| 615 | options "-kr -i8" (stands for "K&R, 8 character indents"), or use | 615 | options ``-kr -i8`` (stands for ``K&R, 8 character indents``), or use |
| 616 | "scripts/Lindent", which indents in the latest style. | 616 | ``scripts/Lindent``, which indents in the latest style. |
| 617 | 617 | ||
| 618 | "indent" has a lot of options, and especially when it comes to comment | 618 | ``indent`` has a lot of options, and especially when it comes to comment |
| 619 | re-formatting you may want to take a look at the man page. But | 619 | re-formatting you may want to take a look at the man page. But |
| 620 | remember: "indent" is not a fix for bad programming. | 620 | remember: ``indent`` is not a fix for bad programming. |
| 621 | 621 | ||
| 622 | 622 | ||
| 623 | 10) Kconfig configuration files | 623 | 10) Kconfig configuration files |
| 624 | ------------------------------- | 624 | ------------------------------- |
| 625 | 625 | ||
| 626 | For all of the Kconfig* configuration files throughout the source tree, | 626 | For all of the Kconfig* configuration files throughout the source tree, |
| 627 | the indentation is somewhat different. Lines under a "config" definition | 627 | the indentation is somewhat different. Lines under a ``config`` definition |
| 628 | are indented with one tab, while help text is indented an additional two | 628 | are indented with one tab, while help text is indented an additional two |
| 629 | spaces. Example:: | 629 | spaces. Example:: |
| 630 | 630 | ||
| @@ -669,13 +669,13 @@ counting is a memory management technique. Usually both are needed, and | |||
| 669 | they are not to be confused with each other. | 669 | they are not to be confused with each other. |
| 670 | 670 | ||
| 671 | Many data structures can indeed have two levels of reference counting, | 671 | Many data structures can indeed have two levels of reference counting, |
| 672 | when there are users of different "classes". The subclass count counts | 672 | when there are users of different ``classes``. The subclass count counts |
| 673 | the number of subclass users, and decrements the global count just once | 673 | the number of subclass users, and decrements the global count just once |
| 674 | when the subclass count goes to zero. | 674 | when the subclass count goes to zero. |
| 675 | 675 | ||
| 676 | Examples of this kind of "multi-level-reference-counting" can be found in | 676 | Examples of this kind of ``multi-level-reference-counting`` can be found in |
| 677 | memory management ("struct mm_struct": mm_users and mm_count), and in | 677 | memory management (``struct mm_struct``: mm_users and mm_count), and in |
| 678 | filesystem code ("struct super_block": s_count and s_active). | 678 | filesystem code (``struct super_block``: s_count and s_active). |
| 679 | 679 | ||
| 680 | Remember: if another thread can find your data structure, and you don't | 680 | Remember: if another thread can find your data structure, and you don't |
| 681 | have a reference count on it, you almost certainly have a bug. | 681 | have a reference count on it, you almost certainly have a bug. |
| @@ -719,7 +719,7 @@ Things to avoid when using macros: | |||
| 719 | return -EBUGGERED; \ | 719 | return -EBUGGERED; \ |
| 720 | } while (0) | 720 | } while (0) |
| 721 | 721 | ||
| 722 | is a _very_ bad idea. It looks like a function call but exits the "calling" | 722 | is a _very_ bad idea. It looks like a function call but exits the ``calling`` |
| 723 | function; don't break the internal parsers of those who will read the code. | 723 | function; don't break the internal parsers of those who will read the code. |
| 724 | 724 | ||
| 725 | 2) macros that depend on having a local variable with a magic name: | 725 | 2) macros that depend on having a local variable with a magic name: |
| @@ -767,7 +767,7 @@ covers RTL which is used frequently with assembly language in the kernel. | |||
| 767 | 767 | ||
| 768 | Kernel developers like to be seen as literate. Do mind the spelling | 768 | Kernel developers like to be seen as literate. Do mind the spelling |
| 769 | of kernel messages to make a good impression. Do not use crippled | 769 | of kernel messages to make a good impression. Do not use crippled |
| 770 | words like "dont"; use "do not" or "don't" instead. Make the messages | 770 | words like ``dont``; use ``do not`` or ``don't`` instead. Make the messages |
| 771 | concise, clear, and unambiguous. | 771 | concise, clear, and unambiguous. |
| 772 | 772 | ||
| 773 | Kernel messages do not have to be terminated with a period. | 773 | Kernel messages do not have to be terminated with a period. |
| @@ -839,7 +839,7 @@ and return NULL if that occurred. | |||
| 839 | ---------------------- | 839 | ---------------------- |
| 840 | 840 | ||
| 841 | There appears to be a common misperception that gcc has a magic "make me | 841 | There appears to be a common misperception that gcc has a magic "make me |
| 842 | faster" speedup option called "inline". While the use of inlines can be | 842 | faster" speedup option called ``inline``. While the use of inlines can be |
| 843 | appropriate (for example as a means of replacing macros, see Chapter 12), it | 843 | appropriate (for example as a means of replacing macros, see Chapter 12), it |
| 844 | very often is not. Abundant use of the inline keyword leads to a much bigger | 844 | very often is not. Abundant use of the inline keyword leads to a much bigger |
| 845 | kernel, which in turn slows the system as a whole down, due to a bigger | 845 | kernel, which in turn slows the system as a whole down, due to a bigger |
| @@ -869,7 +869,7 @@ something it would have done anyway. | |||
| 869 | Functions can return values of many different kinds, and one of the | 869 | Functions can return values of many different kinds, and one of the |
| 870 | most common is a value indicating whether the function succeeded or | 870 | most common is a value indicating whether the function succeeded or |
| 871 | failed. Such a value can be represented as an error-code integer | 871 | failed. Such a value can be represented as an error-code integer |
| 872 | (-Exxx = failure, 0 = success) or a "succeeded" boolean (0 = failure, | 872 | (-Exxx = failure, 0 = success) or a ``succeeded`` boolean (0 = failure, |
| 873 | non-zero = success). | 873 | non-zero = success). |
| 874 | 874 | ||
| 875 | Mixing up these two sorts of representations is a fertile source of | 875 | Mixing up these two sorts of representations is a fertile source of |
| @@ -882,8 +882,8 @@ convention:: | |||
| 882 | the function should return an error-code integer. If the name | 882 | the function should return an error-code integer. If the name |
| 883 | is a predicate, the function should return a "succeeded" boolean. | 883 | is a predicate, the function should return a "succeeded" boolean. |
| 884 | 884 | ||
| 885 | For example, "add work" is a command, and the add_work() function returns 0 | 885 | For example, ``add work`` is a command, and the add_work() function returns 0 |
| 886 | for success or -EBUSY for failure. In the same way, "PCI device present" is | 886 | for success or -EBUSY for failure. In the same way, ``PCI device present`` is |
| 887 | a predicate, and the pci_dev_present() function returns 1 if it succeeds in | 887 | a predicate, and the pci_dev_present() function returns 1 if it succeeds in |
| 888 | finding a matching device or 0 if it doesn't. | 888 | finding a matching device or 0 if it doesn't. |
| 889 | 889 | ||
| @@ -969,7 +969,7 @@ that inline assembly can use C parameters. | |||
| 969 | 969 | ||
| 970 | Large, non-trivial assembly functions should go in .S files, with corresponding | 970 | Large, non-trivial assembly functions should go in .S files, with corresponding |
| 971 | C prototypes defined in C header files. The C prototypes for assembly | 971 | C prototypes defined in C header files. The C prototypes for assembly |
| 972 | functions should use "asmlinkage". | 972 | functions should use ``asmlinkage``. |
| 973 | 973 | ||
| 974 | You may need to mark your asm statement as volatile, to prevent GCC from | 974 | You may need to mark your asm statement as volatile, to prevent GCC from |
| 975 | removing it if GCC doesn't notice any side effects. You don't always need to | 975 | removing it if GCC doesn't notice any side effects. You don't always need to |
