Set up a bot applying clang-tidy checks on push
What changes will be made and why they would make a better Orfeo ToolBox?
High level description
Automatically modify and check periodically the existing OTB code to use C++14 features.
Risks and benefits
Risk 1: This could be a lot of work (but some automation can be achieved https://www.kdab.com/clang-tidy-part-1-modernize-source-code-using-c11c14/) with limited usefulness.
Risk 2: Although C++14 is the official C++ standard for OTB, some C++14 code has been removed so that OTB builds with old compilers. This change could surprise some users.
Benefit 1: Cleaner and safer code.
Benefit 2: This would invite developers to use a modern style when making evolutions to existing code if they find a modern style already.
Alternatives for implementations
clang-tidy proposes many modernize checkers:
-
modernize-avoid-bind
-
modernize-deprecated-headers
-
modernize-loop-convert
-
modernize-make-shared
-
modernize-make-unique
-
modernize-pass-by-value
-
modernize-raw-string-literal
-
modernize-redundant-void-arg
-
modernize-replace-auto-ptr
-
modernize-shrink-to-fit
-
modernize-use-auto
-
modernize-use-bool-literals
-
modernize-use-default
-
modernize-use-emplace
-
modernize-use-nullptr
-
modernize-use-override
-
modernize-use-using
An experimental branch applying some of them (deprecated-headers, loop-convert, use-auto, use-bool-literals, use-nullptr, use-override) to OTB core, tests and examples was available in the old git repos. It built successfully with clang-3.9 and gcc-6.3.
To run clang-tidy on a complete CMake project, on has to run CMake with -DCMAKE_EXPORT_COMPILE_COMMANDS=ON in order to generate a JSON file which can be used by the run-clang-tidy script. Then, one can do:
run-clang-tidy-3.9.py -header-filter='.*' -checks='-*,modernize-use-auto,modernize-loop-convert,modernize-deprecated-headers,modernize-use-override,modernize-use-nullptr,modernize-use-bool-literals' -fix
Before running clang-tidy, it is useful to replace some ITK macros like ITK_NULLPTR and ITK_OVERRIDE by the standard keywords nullptr and override:
find ./ -type f -print0 | xargs -0 sed -i 's/ITK_OVERRIDE/override/g'
find ./ -type f -print0 | xargs -0 sed -i 's/ITK_NULLPTR/nullptr/g'
After running clang-tidy, 2 small corrections have to be made:
find ./ -type f -print0 | xargs -0 sed -i 's/itkFactorylessNewMacro(auto)/itkFactorylessNewMacro(Self)/g'
find ./ -type f -print0 | xargs -0 sed -i 's/auto char/auto/g'
More info is available here https://wiki.orfeo-toolbox.org/index.php/Request_for_Comments-36:_Move_OTB_to_C%2B%2B14#Transform_OTB_code_with_clang-tidy.