Tsduck Coding Guidelines
Total Page:16
File Type:pdf, Size:1020Kb
TSDuck Coding Guidelines Version 3.2 April 2021 TSDuck coding guidelines License TSDuck is released under the terms of the license which is commonly referred to as "BSD 2-Clause License" or "Simplified BSD License" or "FreeBSD License". See http://opensource.org/licenses/BSD-2- Clause. Copyright (c) 2005-2021, Thierry Lelégard All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2 / 93 Version 3.2 TSDuck coding guidelines Contents 1 RATIONALE FOR CODING GUIDELINES ........................................................................................ 6 1.1 FOREWORD ............................................................................................................................................................................ 6 1.2 INDUSTRIAL SOFTWARE REQUIREMENTS ............................................................................................................................. 6 1.3 WHAT ARE CODING GUIDELINES? ....................................................................................................................................... 7 1.4 THE CHALLENGE OF TALENTED PROGRAMMERS................................................................................................................. 7 1.5 CLASSIFICATION OF CODING GUIDELINES ........................................................................................................................... 7 1.6 PURPOSE OF THIS DOCUMENT ............................................................................................................................................. 8 1.7 STRUCTURE OF THIS DOCUMENT ......................................................................................................................................... 8 1.8 ENFORCEMENT OF CODING GUIDELINES ............................................................................................................................. 9 1.9 WHEN TO APPLY CODING GUIDELINES? ............................................................................................................................. 9 2 GENERIC CODING GUIDELINES ................................................................................................. 10 2.1 GENERIC CODING RULES AND RECOMMENDATIONS ....................................................................................................... 10 2.1.1 Software architecture .................................................................................................................................. 10 2.1.2 Source code structure ................................................................................................................................. 10 2.1.2.1 File size ............................................................................................................................................. 11 2.1.2.2 Legal headers ................................................................................................................................. 11 2.1.2.3 Comments ....................................................................................................................................... 12 2.1.2.4 Self-documentation ..................................................................................................................... 12 2.1.3 Revision control system.............................................................................................................................. 13 2.1.4 Internationalization ...................................................................................................................................... 14 2.1.5 Modularity and compatibility ................................................................................................................... 14 2.1.6 Naming conventions ................................................................................................................................... 16 2.1.7 Coding principles .......................................................................................................................................... 16 2.1.8 Secure coding ................................................................................................................................................. 18 2.1.9 Software evolution ....................................................................................................................................... 23 2.1.10 Compilation errors and warnings ........................................................................................................... 24 2.1.11 Makefiles .......................................................................................................................................................... 25 2.1.12 Unit testing ...................................................................................................................................................... 26 2.1.13 Integration of open source software .................................................................................................... 27 2.2 GENERIC CODING CONVENTIONS ...................................................................................................................................... 27 2.2.1 Character encoding ...................................................................................................................................... 28 3 C++ CODING GUIDELINES ....................................................................................................... 30 3.1 C++ CODING RULES AND RECOMMENDATIONS ............................................................................................................. 30 3.1.1 Language selection ...................................................................................................................................... 30 3.1.2 Modularity ....................................................................................................................................................... 31 3.1.3 Naming and syntax formatting ............................................................................................................... 33 3.1.4 Coding style .................................................................................................................................................... 35 3.1.5 Strict typing ..................................................................................................................................................... 42 3.1.6 Assertions ......................................................................................................................................................... 46 3.1.7 Secure coding ................................................................................................................................................. 47 3.1.8 C++ classes ..................................................................................................................................................... 57 3.1.9 C++ constructors and destructors ......................................................................................................... 64 3.1.10 C++ operators ............................................................................................................................................... 68 3.1.11 C++ object management .......................................................................................................................... 71 3.2 C++ TOOLSET ..................................................................................................................................................................... 80 Version 3.2 3 / 93 TSDuck coding guidelines 3.2.1 C++ compilers ............................................................................................................................................... 80 3.2.2 Makefiles .......................................................................................................................................................... 81 3.2.3 Unit testing ...................................................................................................................................................... 82 3.2.4 Doxygen self-documentation .................................................................................................................