Mercurial > hg > CbC > CbC_llvm
comparison docs/CodingStandards.rst @ 77:54457678186b LLVM3.6
LLVM 3.6
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 08 Sep 2014 22:06:00 +0900 |
parents | e4204d083e25 |
children | 60c9769439b8 |
comparison
equal
deleted
inserted
replaced
34:e874dbf0ad9d | 77:54457678186b |
---|---|
12 the LLVM source tree. Although no coding standards should be regarded as | 12 the LLVM source tree. Although no coding standards should be regarded as |
13 absolute requirements to be followed in all instances, coding standards are | 13 absolute requirements to be followed in all instances, coding standards are |
14 particularly important for large-scale code bases that follow a library-based | 14 particularly important for large-scale code bases that follow a library-based |
15 design (like LLVM). | 15 design (like LLVM). |
16 | 16 |
17 This document intentionally does not prescribe fixed standards for religious | 17 While this document may provide guidance for some mechanical formatting issues, |
18 issues such as brace placement and space usage. For issues like this, follow | 18 whitespace, or other "microscopic details", these are not fixed standards. |
19 the golden rule: | 19 Always follow the golden rule: |
20 | 20 |
21 .. _Golden Rule: | 21 .. _Golden Rule: |
22 | 22 |
23 **If you are extending, enhancing, or bug fixing already implemented code, | 23 **If you are extending, enhancing, or bug fixing already implemented code, |
24 use the style that is already being used so that the source is uniform and | 24 use the style that is already being used so that the source is uniform and |
40 the functionality change. | 40 the functionality change. |
41 | 41 |
42 The ultimate goal of these guidelines is the increase readability and | 42 The ultimate goal of these guidelines is the increase readability and |
43 maintainability of our common source base. If you have suggestions for topics to | 43 maintainability of our common source base. If you have suggestions for topics to |
44 be included, please mail them to `Chris <mailto:sabre@nondot.org>`_. | 44 be included, please mail them to `Chris <mailto:sabre@nondot.org>`_. |
45 | |
46 Languages, Libraries, and Standards | |
47 =================================== | |
48 | |
49 Most source code in LLVM and other LLVM projects using these coding standards | |
50 is C++ code. There are some places where C code is used either due to | |
51 environment restrictions, historical restrictions, or due to third-party source | |
52 code imported into the tree. Generally, our preference is for standards | |
53 conforming, modern, and portable C++ code as the implementation language of | |
54 choice. | |
55 | |
56 C++ Standard Versions | |
57 --------------------- | |
58 | |
59 LLVM, Clang, and LLD are currently written using C++11 conforming code, | |
60 although we restrict ourselves to features which are available in the major | |
61 toolchains supported as host compilers. The LLDB project is even more | |
62 aggressive in the set of host compilers supported and thus uses still more | |
63 features. Regardless of the supported features, code is expected to (when | |
64 reasonable) be standard, portable, and modern C++11 code. We avoid unnecessary | |
65 vendor-specific extensions, etc. | |
66 | |
67 C++ Standard Library | |
68 -------------------- | |
69 | |
70 Use the C++ standard library facilities whenever they are available for | |
71 a particular task. LLVM and related projects emphasize and rely on the standard | |
72 library facilities for as much as possible. Common support libraries providing | |
73 functionality missing from the standard library for which there are standard | |
74 interfaces or active work on adding standard interfaces will often be | |
75 implemented in the LLVM namespace following the expected standard interface. | |
76 | |
77 There are some exceptions such as the standard I/O streams library which are | |
78 avoided. Also, there is much more detailed information on these subjects in the | |
79 :doc:`ProgrammersManual`. | |
80 | |
81 Supported C++11 Language and Library Features | |
82 --------------------------------------------- | |
83 | |
84 While LLVM, Clang, and LLD use C++11, not all features are available in all of | |
85 the toolchains which we support. The set of features supported for use in LLVM | |
86 is the intersection of those supported in MSVC 2012, GCC 4.7, and Clang 3.1. | |
87 The ultimate definition of this set is what build bots with those respective | |
88 toolchains accept. Don't argue with the build bots. However, we have some | |
89 guidance below to help you know what to expect. | |
90 | |
91 Each toolchain provides a good reference for what it accepts: | |
92 | |
93 * Clang: http://clang.llvm.org/cxx_status.html | |
94 * GCC: http://gcc.gnu.org/projects/cxx0x.html | |
95 * MSVC: http://msdn.microsoft.com/en-us/library/hh567368.aspx | |
96 | |
97 In most cases, the MSVC list will be the dominating factor. Here is a summary | |
98 of the features that are expected to work. Features not on this list are | |
99 unlikely to be supported by our host compilers. | |
100 | |
101 * Rvalue references: N2118_ | |
102 | |
103 * But *not* Rvalue references for ``*this`` or member qualifiers (N2439_) | |
104 | |
105 * Static assert: N1720_ | |
106 * ``auto`` type deduction: N1984_, N1737_ | |
107 * Trailing return types: N2541_ | |
108 * Lambdas: N2927_ | |
109 | |
110 * But *not* lambdas with default arguments. | |
111 | |
112 * ``decltype``: N2343_ | |
113 * Nested closing right angle brackets: N1757_ | |
114 * Extern templates: N1987_ | |
115 * ``nullptr``: N2431_ | |
116 * Strongly-typed and forward declarable enums: N2347_, N2764_ | |
117 * Local and unnamed types as template arguments: N2657_ | |
118 * Range-based for-loop: N2930_ | |
119 | |
120 * But ``{}`` are required around inner ``do {} while()`` loops. As a result, | |
121 ``{}`` are required around function-like macros inside range-based for | |
122 loops. | |
123 | |
124 * ``override`` and ``final``: N2928_, N3206_, N3272_ | |
125 * Atomic operations and the C++11 memory model: N2429_ | |
126 | |
127 .. _N2118: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2118.html | |
128 .. _N2439: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2439.htm | |
129 .. _N1720: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1720.html | |
130 .. _N1984: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1984.pdf | |
131 .. _N1737: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1737.pdf | |
132 .. _N2541: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2541.htm | |
133 .. _N2927: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2927.pdf | |
134 .. _N2343: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2343.pdf | |
135 .. _N1757: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1757.html | |
136 .. _N1987: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1987.htm | |
137 .. _N2431: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2431.pdf | |
138 .. _N2347: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2347.pdf | |
139 .. _N2764: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2764.pdf | |
140 .. _N2657: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm | |
141 .. _N2930: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2930.html | |
142 .. _N2928: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2928.htm | |
143 .. _N3206: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3206.htm | |
144 .. _N3272: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3272.htm | |
145 .. _N2429: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2429.htm | |
146 .. _MSVC-compatible RTTI: http://llvm.org/PR18951 | |
147 | |
148 The supported features in the C++11 standard libraries are less well tracked, | |
149 but also much greater. Most of the standard libraries implement most of C++11's | |
150 library. The most likely lowest common denominator is Linux support. For | |
151 libc++, the support is just poorly tested and undocumented but expected to be | |
152 largely complete. YMMV. For libstdc++, the support is documented in detail in | |
153 `the libstdc++ manual`_. There are some very minor missing facilities that are | |
154 unlikely to be common problems, and there are a few larger gaps that are worth | |
155 being aware of: | |
156 | |
157 * Not all of the type traits are implemented | |
158 * No regular expression library. | |
159 * While most of the atomics library is well implemented, the fences are | |
160 missing. Fortunately, they are rarely needed. | |
161 * The locale support is incomplete. | |
162 * ``std::initializer_list`` (and the constructors and functions that take it as | |
163 an argument) are not always available, so you cannot (for example) initialize | |
164 a ``std::vector`` with a braced initializer list. | |
165 * ``std::equal()`` (and other algorithms) incorrectly assert in MSVC when given | |
166 ``nullptr`` as an iterator. | |
167 | |
168 Other than these areas you should assume the standard library is available and | |
169 working as expected until some build bot tells you otherwise. If you're in an | |
170 uncertain area of one of the above points, but you cannot test on a Linux | |
171 system, your best approach is to minimize your use of these features, and watch | |
172 the Linux build bots to find out if your usage triggered a bug. For example, if | |
173 you hit a type trait which doesn't work we can then add support to LLVM's | |
174 traits header to emulate it. | |
175 | |
176 .. _the libstdc++ manual: | |
177 http://gcc.gnu.org/onlinedocs/gcc-4.7.3/libstdc++/manual/manual/status.html#status.iso.2011 | |
45 | 178 |
46 Mechanical Source Issues | 179 Mechanical Source Issues |
47 ======================== | 180 ======================== |
48 | 181 |
49 Source Code Formatting | 182 Source Code Formatting |
333 | 466 |
334 Indent Code Consistently | 467 Indent Code Consistently |
335 ^^^^^^^^^^^^^^^^^^^^^^^^ | 468 ^^^^^^^^^^^^^^^^^^^^^^^^ |
336 | 469 |
337 Okay, in your first year of programming you were told that indentation is | 470 Okay, in your first year of programming you were told that indentation is |
338 important. If you didn't believe and internalize this then, now is the time. | 471 important. If you didn't believe and internalize this then, now is the time. |
339 Just do it. | 472 Just do it. With the introduction of C++11, there are some new formatting |
340 | 473 challenges that merit some suggestions to help have consistent, maintainable, |
341 Compiler Issues | 474 and tool-friendly formatting and indentation. |
342 --------------- | 475 |
476 Format Lambdas Like Blocks Of Code | |
477 """""""""""""""""""""""""""""""""" | |
478 | |
479 When formatting a multi-line lambda, format it like a block of code, that's | |
480 what it is. If there is only one multi-line lambda in a statement, and there | |
481 are no expressions lexically after it in the statement, drop the indent to the | |
482 standard two space indent for a block of code, as if it were an if-block opened | |
483 by the preceding part of the statement: | |
484 | |
485 .. code-block:: c++ | |
486 | |
487 std::sort(foo.begin(), foo.end(), [&](Foo a, Foo b) -> bool { | |
488 if (a.blah < b.blah) | |
489 return true; | |
490 if (a.baz < b.baz) | |
491 return true; | |
492 return a.bam < b.bam; | |
493 }); | |
494 | |
495 To take best advantage of this formatting, if you are designing an API which | |
496 accepts a continuation or single callable argument (be it a functor, or | |
497 a ``std::function``), it should be the last argument if at all possible. | |
498 | |
499 If there are multiple multi-line lambdas in a statement, or there is anything | |
500 interesting after the lambda in the statement, indent the block two spaces from | |
501 the indent of the ``[]``: | |
502 | |
503 .. code-block:: c++ | |
504 | |
505 dyn_switch(V->stripPointerCasts(), | |
506 [] (PHINode *PN) { | |
507 // process phis... | |
508 }, | |
509 [] (SelectInst *SI) { | |
510 // process selects... | |
511 }, | |
512 [] (LoadInst *LI) { | |
513 // process loads... | |
514 }, | |
515 [] (AllocaInst *AI) { | |
516 // process allocas... | |
517 }); | |
518 | |
519 Braced Initializer Lists | |
520 """""""""""""""""""""""" | |
521 | |
522 With C++11, there are significantly more uses of braced lists to perform | |
523 initialization. These allow you to easily construct aggregate temporaries in | |
524 expressions among other niceness. They now have a natural way of ending up | |
525 nested within each other and within function calls in order to build up | |
526 aggregates (such as option structs) from local variables. To make matters | |
527 worse, we also have many more uses of braces in an expression context that are | |
528 *not* performing initialization. | |
529 | |
530 The historically common formatting of braced initialization of aggregate | |
531 variables does not mix cleanly with deep nesting, general expression contexts, | |
532 function arguments, and lambdas. We suggest new code use a simple rule for | |
533 formatting braced initialization lists: act as-if the braces were parentheses | |
534 in a function call. The formatting rules exactly match those already well | |
535 understood for formatting nested function calls. Examples: | |
536 | |
537 .. code-block:: c++ | |
538 | |
539 foo({a, b, c}, {1, 2, 3}); | |
540 | |
541 llvm::Constant *Mask[] = { | |
542 llvm::ConstantInt::get(llvm::Type::getInt32Ty(getLLVMContext()), 0), | |
543 llvm::ConstantInt::get(llvm::Type::getInt32Ty(getLLVMContext()), 1), | |
544 llvm::ConstantInt::get(llvm::Type::getInt32Ty(getLLVMContext()), 2)}; | |
545 | |
546 This formatting scheme also makes it particularly easy to get predictable, | |
547 consistent, and automatic formatting with tools like `Clang Format`_. | |
548 | |
549 .. _Clang Format: http://clang.llvm.org/docs/ClangFormat.html | |
550 | |
551 Language and Compiler Issues | |
552 ---------------------------- | |
343 | 553 |
344 Treat Compiler Warnings Like Errors | 554 Treat Compiler Warnings Like Errors |
345 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | 555 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
346 | 556 |
347 If your code has compiler warnings in it, something is wrong --- you aren't | 557 If your code has compiler warnings in it, something is wrong --- you aren't |
397 executable bloat even if exceptions are never used in the code base, or if RTTI | 607 executable bloat even if exceptions are never used in the code base, or if RTTI |
398 is never used for a class. Because of this, we turn them off globally in the | 608 is never used for a class. Because of this, we turn them off globally in the |
399 code. | 609 code. |
400 | 610 |
401 That said, LLVM does make extensive use of a hand-rolled form of RTTI that use | 611 That said, LLVM does make extensive use of a hand-rolled form of RTTI that use |
402 templates like `isa<>, cast<>, and dyn_cast<> <ProgrammersManual.html#isa>`_. | 612 templates like :ref:`isa\<>, cast\<>, and dyn_cast\<> <isa>`. |
403 This form of RTTI is opt-in and can be | 613 This form of RTTI is opt-in and can be |
404 :doc:`added to any class <HowToSetUpLLVMStyleRTTI>`. It is also | 614 :doc:`added to any class <HowToSetUpLLVMStyleRTTI>`. It is also |
405 substantially more efficient than ``dynamic_cast<>``. | 615 substantially more efficient than ``dynamic_cast<>``. |
406 | 616 |
407 .. _static constructor: | 617 .. _static constructor: |
449 ``class`` makes all members private by default while ``struct`` makes all | 659 ``class`` makes all members private by default while ``struct`` makes all |
450 members public by default. | 660 members public by default. |
451 | 661 |
452 Unfortunately, not all compilers follow the rules and some will generate | 662 Unfortunately, not all compilers follow the rules and some will generate |
453 different symbols based on whether ``class`` or ``struct`` was used to declare | 663 different symbols based on whether ``class`` or ``struct`` was used to declare |
454 the symbol. This can lead to problems at link time. | 664 the symbol (e.g., MSVC). This can lead to problems at link time. |
455 | 665 |
456 So, the rule for LLVM is to always use the ``class`` keyword, unless **all** | 666 * All declarations and definitions of a given ``class`` or ``struct`` must use |
457 members are public and the type is a C++ `POD | 667 the same keyword. For example: |
458 <http://en.wikipedia.org/wiki/Plain_old_data_structure>`_ type, in which case | 668 |
459 ``struct`` is allowed. | 669 .. code-block:: c++ |
670 | |
671 class Foo; | |
672 | |
673 // Breaks mangling in MSVC. | |
674 struct Foo { int Data; }; | |
675 | |
676 * As a rule of thumb, ``struct`` should be kept to structures where *all* | |
677 members are declared public. | |
678 | |
679 .. code-block:: c++ | |
680 | |
681 // Foo feels like a class... this is strange. | |
682 struct Foo { | |
683 private: | |
684 int Data; | |
685 public: | |
686 Foo() : Data(0) { } | |
687 int getData() const { return Data; } | |
688 void setData(int D) { Data = D; } | |
689 }; | |
690 | |
691 // Bar isn't POD, but it does look like a struct. | |
692 struct Bar { | |
693 int Data; | |
694 Foo() : Data(0) { } | |
695 }; | |
696 | |
697 Do not use Braced Initializer Lists to Call a Constructor | |
698 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
699 | |
700 In C++11 there is a "generalized initialization syntax" which allows calling | |
701 constructors using braced initializer lists. Do not use these to call | |
702 constructors with any interesting logic or if you care that you're calling some | |
703 *particular* constructor. Those should look like function calls using | |
704 parentheses rather than like aggregate initialization. Similarly, if you need | |
705 to explicitly name the type and call its constructor to create a temporary, | |
706 don't use a braced initializer list. Instead, use a braced initializer list | |
707 (without any type for temporaries) when doing aggregate initialization or | |
708 something notionally equivalent. Examples: | |
709 | |
710 .. code-block:: c++ | |
711 | |
712 class Foo { | |
713 public: | |
714 // Construct a Foo by reading data from the disk in the whizbang format, ... | |
715 Foo(std::string filename); | |
716 | |
717 // Construct a Foo by looking up the Nth element of some global data ... | |
718 Foo(int N); | |
719 | |
720 // ... | |
721 }; | |
722 | |
723 // The Foo constructor call is very deliberate, no braces. | |
724 std::fill(foo.begin(), foo.end(), Foo("name")); | |
725 | |
726 // The pair is just being constructed like an aggregate, use braces. | |
727 bar_map.insert({my_key, my_value}); | |
728 | |
729 If you use a braced initializer list when initializing a variable, use an equals before the open curly brace: | |
730 | |
731 .. code-block:: c++ | |
732 | |
733 int data[] = {0, 1, 2, 3}; | |
734 | |
735 Use ``auto`` Type Deduction to Make Code More Readable | |
736 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
737 | |
738 Some are advocating a policy of "almost always ``auto``" in C++11, however LLVM | |
739 uses a more moderate stance. Use ``auto`` if and only if it makes the code more | |
740 readable or easier to maintain. Don't "almost always" use ``auto``, but do use | |
741 ``auto`` with initializers like ``cast<Foo>(...)`` or other places where the | |
742 type is already obvious from the context. Another time when ``auto`` works well | |
743 for these purposes is when the type would have been abstracted away anyways, | |
744 often behind a container's typedef such as ``std::vector<T>::iterator``. | |
745 | |
746 Beware unnecessary copies with ``auto`` | |
747 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
748 | |
749 The convenience of ``auto`` makes it easy to forget that its default behavior | |
750 is a copy. Particularly in range-based ``for`` loops, careless copies are | |
751 expensive. | |
752 | |
753 As a rule of thumb, use ``auto &`` unless you need to copy the result, and use | |
754 ``auto *`` when copying pointers. | |
755 | |
756 .. code-block:: c++ | |
757 | |
758 // Typically there's no reason to copy. | |
759 for (const auto &Val : Container) { observe(Val); } | |
760 for (auto &Val : Container) { Val.change(); } | |
761 | |
762 // Remove the reference if you really want a new copy. | |
763 for (auto Val : Container) { Val.change(); saveSomewhere(Val); } | |
764 | |
765 // Copy pointers, but make it clear that they're pointers. | |
766 for (const auto *Ptr : Container) { observe(*Ptr); } | |
767 for (auto *Ptr : Container) { Ptr->change(); } | |
460 | 768 |
461 Style Issues | 769 Style Issues |
462 ============ | 770 ============ |
463 | 771 |
464 The High-Level Issues | 772 The High-Level Issues |
975 This will trigger a much better error message and tell the compiler that the | 1283 This will trigger a much better error message and tell the compiler that the |
976 method will never be implemented. This enables other checks like | 1284 method will never be implemented. This enables other checks like |
977 ``-Wunused-private-field`` to run correctly on classes that contain these | 1285 ``-Wunused-private-field`` to run correctly on classes that contain these |
978 methods. | 1286 methods. |
979 | 1287 |
980 To maintain compatibility with C++03, ``LLVM_DELETED_FUNCTION`` should be used | 1288 For compatibility with MSVC, ``LLVM_DELETED_FUNCTION`` should be used which |
981 which will expand to ``= delete`` if the compiler supports it. These methods | 1289 will expand to ``= delete`` on compilers that support it. These methods should |
982 should still be declared private. Example of the uncopyable pattern: | 1290 still be declared private. Example of the uncopyable pattern: |
983 | 1291 |
984 .. code-block:: c++ | 1292 .. code-block:: c++ |
985 | 1293 |
986 class DontCopy { | 1294 class DontCopy { |
987 private: | 1295 private: |
1188 Namespace Indentation | 1496 Namespace Indentation |
1189 ^^^^^^^^^^^^^^^^^^^^^ | 1497 ^^^^^^^^^^^^^^^^^^^^^ |
1190 | 1498 |
1191 In general, we strive to reduce indentation wherever possible. This is useful | 1499 In general, we strive to reduce indentation wherever possible. This is useful |
1192 because we want code to `fit into 80 columns`_ without wrapping horribly, but | 1500 because we want code to `fit into 80 columns`_ without wrapping horribly, but |
1193 also because it makes it easier to understand the code. Namespaces are a funny | 1501 also because it makes it easier to understand the code. To facilitate this and |
1194 thing: they are often large, and we often desire to put lots of stuff into them | 1502 avoid some insanely deep nesting on occasion, don't indent namespaces. If it |
1195 (so they can be large). Other times they are tiny, because they just hold an | 1503 helps readability, feel free to add a comment indicating what namespace is |
1196 enum or something similar. In order to balance this, we use different | 1504 being closed by a ``}``. For example: |
1197 approaches for small versus large namespaces. | |
1198 | |
1199 If a namespace definition is small and *easily* fits on a screen (say, less than | |
1200 35 lines of code), then you should indent its body. Here's an example: | |
1201 | |
1202 .. code-block:: c++ | |
1203 | |
1204 namespace llvm { | |
1205 namespace X86 { | |
1206 /// \brief An enum for the x86 relocation codes. Note that | |
1207 /// the terminology here doesn't follow x86 convention - word means | |
1208 /// 32-bit and dword means 64-bit. | |
1209 enum RelocationType { | |
1210 /// \brief PC relative relocation, add the relocated value to | |
1211 /// the value already in memory, after we adjust it for where the PC is. | |
1212 reloc_pcrel_word = 0, | |
1213 | |
1214 /// \brief PIC base relative relocation, add the relocated value to | |
1215 /// the value already in memory, after we adjust it for where the | |
1216 /// PIC base is. | |
1217 reloc_picrel_word = 1, | |
1218 | |
1219 /// \brief Absolute relocation, just add the relocated value to the | |
1220 /// value already in memory. | |
1221 reloc_absolute_word = 2, | |
1222 reloc_absolute_dword = 3 | |
1223 }; | |
1224 } | |
1225 } | |
1226 | |
1227 Since the body is small, indenting adds value because it makes it very clear | |
1228 where the namespace starts and ends, and it is easy to take the whole thing in | |
1229 in one "gulp" when reading the code. If the blob of code in the namespace is | |
1230 larger (as it typically is in a header in the ``llvm`` or ``clang`` namespaces), | |
1231 do not indent the code, and add a comment indicating what namespace is being | |
1232 closed. For example: | |
1233 | 1505 |
1234 .. code-block:: c++ | 1506 .. code-block:: c++ |
1235 | 1507 |
1236 namespace llvm { | 1508 namespace llvm { |
1237 namespace knowledge { | 1509 namespace knowledge { |
1249 }; | 1521 }; |
1250 | 1522 |
1251 } // end namespace knowledge | 1523 } // end namespace knowledge |
1252 } // end namespace llvm | 1524 } // end namespace llvm |
1253 | 1525 |
1254 Because the class is large, we don't expect that the reader can easily | 1526 |
1255 understand the entire concept in a glance, and the end of the file (where the | 1527 Feel free to skip the closing comment when the namespace being closed is |
1256 namespaces end) may be a long ways away from the place they open. As such, | 1528 obvious for any reason. For example, the outer-most namespace in a header file |
1257 indenting the contents of the namespace doesn't add any value, and detracts from | 1529 is rarely a source of confusion. But namespaces both anonymous and named in |
1258 the readability of the class. In these cases it is best to *not* indent the | 1530 source files that are being closed half way through the file probably could use |
1259 contents of the namespace. | 1531 clarification. |
1260 | 1532 |
1261 .. _static: | 1533 .. _static: |
1262 | 1534 |
1263 Anonymous Namespaces | 1535 Anonymous Namespaces |
1264 ^^^^^^^^^^^^^^^^^^^^ | 1536 ^^^^^^^^^^^^^^^^^^^^ |
1283 good: | 1555 good: |
1284 | 1556 |
1285 .. code-block:: c++ | 1557 .. code-block:: c++ |
1286 | 1558 |
1287 namespace { | 1559 namespace { |
1288 class StringSort { | |
1289 ... | |
1290 public: | |
1291 StringSort(...) | |
1292 bool operator<(const char *RHS) const; | |
1293 }; | |
1294 } // end anonymous namespace | |
1295 | |
1296 static void runHelper() { | |
1297 ... | |
1298 } | |
1299 | |
1300 bool StringSort::operator<(const char *RHS) const { | |
1301 ... | |
1302 } | |
1303 | |
1304 This is bad: | |
1305 | |
1306 .. code-block:: c++ | |
1307 | |
1308 namespace { | |
1309 class StringSort { | 1560 class StringSort { |
1310 ... | 1561 ... |
1311 public: | 1562 public: |
1312 StringSort(...) | 1563 StringSort(...) |
1313 bool operator<(const char *RHS) const; | 1564 bool operator<(const char *RHS) const; |
1314 }; | 1565 }; |
1566 } // end anonymous namespace | |
1567 | |
1568 static void runHelper() { | |
1569 ... | |
1570 } | |
1571 | |
1572 bool StringSort::operator<(const char *RHS) const { | |
1573 ... | |
1574 } | |
1575 | |
1576 This is bad: | |
1577 | |
1578 .. code-block:: c++ | |
1579 | |
1580 namespace { | |
1581 | |
1582 class StringSort { | |
1583 ... | |
1584 public: | |
1585 StringSort(...) | |
1586 bool operator<(const char *RHS) const; | |
1587 }; | |
1315 | 1588 |
1316 void runHelper() { | 1589 void runHelper() { |
1317 ... | 1590 ... |
1318 } | 1591 } |
1319 | 1592 |