comparison docs/CodingStandards.rst @ 95:afa8332a0e37

LLVM 3.8
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Tue, 13 Oct 2015 17:48:58 +0900
parents 60c9769439b8
children 1172e4bd9c6f
comparison
equal deleted inserted replaced
84:f3e34b893a5f 95:afa8332a0e37
26 26
27 Note that some code bases (e.g. ``libc++``) have really good reasons to deviate 27 Note that some code bases (e.g. ``libc++``) have really good reasons to deviate
28 from the coding standards. In the case of ``libc++``, this is because the 28 from the coding standards. In the case of ``libc++``, this is because the
29 naming and other conventions are dictated by the C++ standard. If you think 29 naming and other conventions are dictated by the C++ standard. If you think
30 there is a specific good reason to deviate from the standards here, please bring 30 there is a specific good reason to deviate from the standards here, please bring
31 it up on the LLVMdev mailing list. 31 it up on the LLVM-dev mailing list.
32 32
33 There are some conventions that are not uniformly followed in the code base 33 There are some conventions that are not uniformly followed in the code base
34 (e.g. the naming convention). This is because they are relatively new, and a 34 (e.g. the naming convention). This is because they are relatively new, and a
35 lot of code was written before they were put in place. Our long term goal is 35 lot of code was written before they were put in place. Our long term goal is
36 for the entire codebase to follow the convention, but we explicitly *do not* 36 for the entire codebase to follow the convention, but we explicitly *do not*
37 want patches that do large-scale reformating of existing code. On the other 37 want patches that do large-scale reformating of existing code. On the other
38 hand, it is reasonable to rename the methods of a class if you're about to 38 hand, it is reasonable to rename the methods of a class if you're about to
39 change it in some other way. Just do the reformating as a separate commit from 39 change it in some other way. Just do the reformating as a separate commit from
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 to increase the 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 45
46 Languages, Libraries, and Standards 46 Languages, Libraries, and Standards
47 =================================== 47 ===================================
127 * Explicit conversion operators: N2437_ 127 * Explicit conversion operators: N2437_
128 * Defaulted and deleted functions: N2346_ 128 * Defaulted and deleted functions: N2346_
129 129
130 * But not defaulted move constructors or move assignment operators, MSVC 2013 130 * But not defaulted move constructors or move assignment operators, MSVC 2013
131 cannot synthesize them. 131 cannot synthesize them.
132 * Initializer lists: N2627_
133 * Delegating constructors: N1986_
134 * Default member initializers (non-static data member initializers): N2756_
135
136 * Only use these for scalar members that would otherwise be left
137 uninitialized. Non-scalar members generally have appropriate default
138 constructors, and MSVC 2013 has problems when braced initializer lists are
139 involved.
132 140
133 .. _N2118: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2118.html 141 .. _N2118: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2118.html
134 .. _N2439: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2439.htm 142 .. _N2439: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2439.htm
135 .. _N1720: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1720.html 143 .. _N1720: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1720.html
136 .. _N1984: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1984.pdf 144 .. _N1984: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1984.pdf
150 .. _N3272: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3272.htm 158 .. _N3272: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3272.htm
151 .. _N2429: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2429.htm 159 .. _N2429: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2429.htm
152 .. _N2242: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2242.pdf 160 .. _N2242: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2242.pdf
153 .. _N2437: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2437.pdf 161 .. _N2437: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2437.pdf
154 .. _N2346: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm 162 .. _N2346: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm
155 .. _MSVC-compatible RTTI: http://llvm.org/PR18951 163 .. _N2627: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2672.htm
164 .. _N1986: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1986.pdf
165 .. _N2756: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2756.htm
156 166
157 The supported features in the C++11 standard libraries are less well tracked, 167 The supported features in the C++11 standard libraries are less well tracked,
158 but also much greater. Most of the standard libraries implement most of C++11's 168 but also much greater. Most of the standard libraries implement most of C++11's
159 library. The most likely lowest common denominator is Linux support. For 169 library. The most likely lowest common denominator is Linux support. For
160 libc++, the support is just poorly tested and undocumented but expected to be 170 libc++, the support is just poorly tested and undocumented but expected to be
166 * Not all of the type traits are implemented 176 * Not all of the type traits are implemented
167 * No regular expression library. 177 * No regular expression library.
168 * While most of the atomics library is well implemented, the fences are 178 * While most of the atomics library is well implemented, the fences are
169 missing. Fortunately, they are rarely needed. 179 missing. Fortunately, they are rarely needed.
170 * The locale support is incomplete. 180 * The locale support is incomplete.
171 * ``std::initializer_list`` (and the constructors and functions that take it as
172 an argument) are not always available, so you cannot (for example) initialize
173 a ``std::vector`` with a braced initializer list.
174 * ``std::equal()`` (and other algorithms) incorrectly assert in MSVC when given
175 ``nullptr`` as an iterator.
176 181
177 Other than these areas you should assume the standard library is available and 182 Other than these areas you should assume the standard library is available and
178 working as expected until some build bot tells you otherwise. If you're in an 183 working as expected until some build bot tells you otherwise. If you're in an
179 uncertain area of one of the above points, but you cannot test on a Linux 184 uncertain area of one of the above points, but you cannot test on a Linux
180 system, your best approach is to minimize your use of these features, and watch 185 system, your best approach is to minimize your use of these features, and watch
238 // License. See LICENSE.TXT for details. 243 // License. See LICENSE.TXT for details.
239 // 244 //
240 //===----------------------------------------------------------------------===// 245 //===----------------------------------------------------------------------===//
241 /// 246 ///
242 /// \file 247 /// \file
243 /// \brief This file contains the declaration of the Instruction class, which is 248 /// This file contains the declaration of the Instruction class, which is the
244 /// the base class for all of the VM instructions. 249 /// base class for all of the VM instructions.
245 /// 250 ///
246 //===----------------------------------------------------------------------===// 251 //===----------------------------------------------------------------------===//
247 252
248 A few things to note about this particular format: The "``-*- C++ -*-``" string 253 A few things to note about this particular format: The "``-*- C++ -*-``" string
249 on the first line is there to tell Emacs that the source file is a C++ file, not 254 on the first line is there to tell Emacs that the source file is a C++ file, not
259 The next section in the file is a concise note that defines the license that the 264 The next section in the file is a concise note that defines the license that the
260 file is released under. This makes it perfectly clear what terms the source 265 file is released under. This makes it perfectly clear what terms the source
261 code can be distributed under and should not be modified in any way. 266 code can be distributed under and should not be modified in any way.
262 267
263 The main body is a ``doxygen`` comment (identified by the ``///`` comment 268 The main body is a ``doxygen`` comment (identified by the ``///`` comment
264 marker instead of the usual ``//``) describing the purpose of the file. It 269 marker instead of the usual ``//``) describing the purpose of the file. The
265 should have a ``\brief`` command that describes the file in one or two 270 first sentence or a passage beginning with ``\brief`` is used as an abstract.
266 sentences. Any additional information should be separated by a blank line. If 271 Any additional information should be separated by a blank line. If an
267 an algorithm is being implemented or something tricky is going on, a reference 272 algorithm is being implemented or something tricky is going on, a reference
268 to the paper where it is published should be included, as well as any notes or 273 to the paper where it is published should be included, as well as any notes or
269 *gotchas* in the code to watch out for. 274 *gotchas* in the code to watch out for.
270 275
271 Class overviews 276 Class overviews
272 """"""""""""""" 277 """""""""""""""
311 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 316 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
312 317
313 Use the ``\file`` command to turn the standard file header into a file-level 318 Use the ``\file`` command to turn the standard file header into a file-level
314 comment. 319 comment.
315 320
316 Include descriptive ``\brief`` paragraphs for all public interfaces (public 321 Include descriptive paragraphs for all public interfaces (public classes,
317 classes, member and non-member functions). Explain API use and purpose in 322 member and non-member functions). Don't just restate the information that can
318 ``\brief`` paragraphs, don't just restate the information that can be inferred 323 be inferred from the API name. The first sentence or a paragraph beginning
319 from the API name. Put detailed discussion into separate paragraphs. 324 with ``\brief`` is used as an abstract. Put detailed discussion into separate
325 paragraphs.
320 326
321 To refer to parameter names inside a paragraph, use the ``\p name`` command. 327 To refer to parameter names inside a paragraph, use the ``\p name`` command.
322 Don't use the ``\arg name`` command since it starts a new paragraph that 328 Don't use the ``\arg name`` command since it starts a new paragraph that
323 contains documentation for the parameter. 329 contains documentation for the parameter.
324 330
334 340
335 A minimal documentation comment: 341 A minimal documentation comment:
336 342
337 .. code-block:: c++ 343 .. code-block:: c++
338 344
339 /// \brief Does foo and bar. 345 /// Sets the xyzzy property to \p Baz.
340 void fooBar(bool Baz); 346 void setXyzzy(bool Baz);
341 347
342 A documentation comment that uses all Doxygen features in a preferred way: 348 A documentation comment that uses all Doxygen features in a preferred way:
343 349
344 .. code-block:: c++ 350 .. code-block:: c++
345 351
392 398
393 .. code-block:: c++ 399 .. code-block:: c++
394 400
395 // In Something.h: 401 // In Something.h:
396 402
397 /// \brief An abstraction for some complicated thing. 403 /// An abstraction for some complicated thing.
398 class Something { 404 class Something {
399 public: 405 public:
400 /// \brief Does foo and bar. 406 /// Does foo and bar.
401 void fooBar(); 407 void fooBar();
402 }; 408 };
403 409
404 // In Something.cpp: 410 // In Something.cpp:
405 411
719 }; 725 };
720 726
721 // Bar isn't POD, but it does look like a struct. 727 // Bar isn't POD, but it does look like a struct.
722 struct Bar { 728 struct Bar {
723 int Data; 729 int Data;
724 Foo() : Data(0) { } 730 Bar() : Data(0) { }
725 }; 731 };
726 732
727 Do not use Braced Initializer Lists to Call a Constructor 733 Do not use Braced Initializer Lists to Call a Constructor
728 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 734 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
729 735
1299 if you return from each case of a covered switch-over-enum because GCC assumes 1305 if you return from each case of a covered switch-over-enum because GCC assumes
1300 that the enum expression may take any representable value, not just those of 1306 that the enum expression may take any representable value, not just those of
1301 individual enumerators. To suppress this warning, use ``llvm_unreachable`` after 1307 individual enumerators. To suppress this warning, use ``llvm_unreachable`` after
1302 the switch. 1308 the switch.
1303 1309
1304 Use ``LLVM_DELETED_FUNCTION`` to mark uncallable methods
1305 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1306
1307 Prior to C++11, a common pattern to make a class uncopyable was to declare an
1308 unimplemented copy constructor and copy assignment operator and make them
1309 private. This would give a compiler error for accessing a private method or a
1310 linker error because it wasn't implemented.
1311
1312 With C++11, we can mark methods that won't be implemented with ``= delete``.
1313 This will trigger a much better error message and tell the compiler that the
1314 method will never be implemented. This enables other checks like
1315 ``-Wunused-private-field`` to run correctly on classes that contain these
1316 methods.
1317
1318 For compatibility with MSVC, ``LLVM_DELETED_FUNCTION`` should be used which
1319 will expand to ``= delete`` on compilers that support it. These methods should
1320 still be declared private. Example of the uncopyable pattern:
1321
1322 .. code-block:: c++
1323
1324 class DontCopy {
1325 private:
1326 DontCopy(const DontCopy&) LLVM_DELETED_FUNCTION;
1327 DontCopy &operator =(const DontCopy&) LLVM_DELETED_FUNCTION;
1328 public:
1329 ...
1330 };
1331
1332 Don't evaluate ``end()`` every time through a loop 1310 Don't evaluate ``end()`` every time through a loop
1333 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1311 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1334 1312
1335 Because C++ doesn't have a standard "``foreach``" loop (though it can be 1313 Because C++ doesn't have a standard "``foreach``" loop (though it can be
1336 emulated with macros and may be coming in C++'0x) we end up writing a lot of 1314 emulated with macros and may be coming in C++'0x) we end up writing a lot of