编者按:本文作者是蚂蚁集团客户端工程师希德,教会你如何在 Android c++ 项目中应用 c++ core guideline checker,以统一最佳编码风格,提高代码的可读性和可维护性,提升 c++ 代码安全性、运行效率。
c++ core guideline(https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) 是 c++ 之父 BS 博士为了解决现代 c++ 中的安全性、效能问题,而做出来的一套指南。也是目前最权威、规范的一套现代 c++ 编程的最佳实践。如果说 Effective cpp/More Effective cpp 是在给你敲警钟你怎么写就不对,那么 c++ core guide 则是 c++ 现代版的正面实施指南。为什么说现代呢,一是 c++ core guideline 出来得更晚,从 c++ 各场景应用中接收了更多的反馈融入了更多好的经验;二是这些 guideline 面向更多现代化 c++ 语言版本(c++ 14 及以后),现代 c++ 和我们过去 vc 6.0 时代受过的 c++ 教育是截然不同的。
文档介绍:
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines(非常详细、全面,为实操快速指路)
Core guildeline 支持库(方便你改善、迁移老代码):
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#gsl-guidelines-support-library
工具(checker):
现在 c++ core guideline check 在微软 visual studio 里也得到了很好的支持。
https://learn.microsoft.com/en-us/cpp/code-quality/using-the-cpp-core-guidelines-checkers?view=msvc-170#the-c-core-guidelines-project
正逢 mars native 需要重构、对外开放,改善代码品质,发现潜在编码问题、语言使用问题。我就去挖掘下,如何在 android c++ 工程中自动化应用 c++ core guilde 的方式。毕竟 c++ 语言使用本身涉及的知识汗牛充栋,侯捷老师靠介绍 c++ 知识可以著作等身,成为一门可以持续一辈子的职业。我并无这么大的知识储备,也无这么持续的注意力带宽。
只有一个办法:找一个成熟的 c++ core guideline 检查工具。
clang-tidy 基于 clang/LLVM,是一个成熟、功能丰富的 c++ linter,非 visual studio 环境,目前调研了下,需要靠集成 clang-tidy 的方式来跑 c++ core guideline。且在 android 开发的 ndk-build/Android Studio cmake 工作流里都得到了很好的支持。
Enable application-wide with APP_CLANG_TIDY := true
android studio 从 3.3+ 开始支持了 clang-tidy。使用方式很简单,我简单截图如下:
可以看到 check 规则数量非常多。
quick-fixes:
点击小灯泡上的提示(quick-fixes),就能直接帮你重构/改良代码。而这正是 Intelli IDEA/Android Studio 的核心优势。
而这个 c++ 检查器的价值有多大呢,我给大家截图高亮下,发现了 1100 多个警告,0 Error。修复这些警告,可以避免较多临时对象的构造,还能用上更多运行时更有效率的“等效函数/等效写法”,编码风格统一,跟上最新最好实践。要人肉做到这一点,得需要多大精力和经验。
当你熟悉了各个 check 以及对应 quick-fix 是什么,认定它适合你的需要。你可以批量应用自动修改:(你可能会觉得我上边说的话比较滴水不漏,是不是暗示我有坑啊。我使用下来的经验是 quick-fix 质量比较高,值得信赖。大家睁大眼,放开胆。)
手工修改:
有些 check,比如向窄数据类型转型
构建函数里有未初始化的字段
不太费精力地批量应用 quick-fixes 之后,成功实现了批量应用自动修改。改动概要信息如下:
commit db6625732e09156501f32f7be0f2e5515e44ea11 (HEAD -> cpp_core_guideline_warning_fix, origin/cpp_core_guideline_warning_fix)
Author: xide.wf
Date: Thu Aug 31 20:50:36 2023 +0800
通过 Clang-Tidy 做了基础的 cpp core guideline 校验,增加了相应的修改.
android/README.md | 11 ++++++++++-
core/filters/bloom_filter_define.cpp | 18 +++++++++---------
core/filters/delay_filter_define.cpp | 2 +-
core/filters/distortion_filter_define.cpp | 14 +++++++-------
core/filters/filter_utils.cpp | 11 +++++++----
core/filters/filter_utils.hpp | 4 ++--
core/filters/gaussian_filter_define.cpp | 4 +++-
core/include/mars_native_player.h | 2 +-
core/load/loader.cpp | 8 +++-----
core/math/camera.cpp | 14 ++++++--------
core/math/camera.hpp | 4 ++--
core/math/mat3.cpp | 4 +---
core/math/mat4.cpp | 8 +++-----
core/math/mat4.hpp | 2 +-
core/math/math_util.cpp | 4 ++--
core/math/math_util.hpp | 2 +-
core/math/mersenne_twister.cpp | 4 ++--
core/math/transform.cpp | 8 ++++----
core/math/transform.h | 4 ++--
core/math/value/bezier_value_getter.cpp | 2 --
core/math/value/curve_value_getter.cpp | 8 ++++----
core/math/value/gradient_value_getter.cpp | 1 -
core/math/value/line_segments_value_getter.cpp | 4 ++--
core/math/value/path_value_getter.cpp | 8 ++++----
core/math/value/random_value_getter.cpp | 6 ++----
core/math/value/static_value_getter.cpp | 2 +-
core/math/value/static_value_getter.hpp | 2 +-
core/math/value/value_getter_utils.cpp | 6 +++---
core/math/value/value_getter_utils.hpp | 6 +++---
core/math/vec2.cpp | 9 ++-------
core/math/vec3.cpp | 4 +---
core/math/vec4.cpp | 4 +---
core/model/model_plugin.cpp | 11 ++++-------
core/model/model_tree_plugin.cpp | 9 +++------
core/model/model_vfx_item.cpp | 14 +++++++-------
core/model/renderer/animation/interpolation_sampler.cpp | 38 +++++++++++++++-----------------------
core/model/renderer/animation/m_animation.cpp | 65 ++++++++++++++++++++++++++---------------------------------------
core/model/renderer/animation/m_animation.hpp | 16 ++++++++--------
core/model/renderer/animation/m_skin.cpp | 16 +++++++---------
core/model/renderer/animation/m_skin.hpp | 4 ++--
core/model/renderer/m_camera.cpp | 29 +++++++++++------------------
core/model/renderer/m_camera.hpp | 10 +++++-----
core/model/renderer/m_entity.cpp | 2 +- core/model/renderer/m_geometry.cpp | 10 ++++++----
core/model/renderer/m_light.cpp | 31 ++++++++++++-------------------
core/model/renderer/m_light.hpp | 10 +++++-----
core/model/renderer/m_mesh.cpp | 58 +++++++++++++++++++++++++++++-----------------------------
core/model/renderer/m_mesh.hpp | 12 ++++++------
core/model/renderer/m_scene_manager.cpp | 34 ++++++++++++++++------------------
core/model/renderer/m_scene_manager.hpp | 8 ++++----
core/model/renderer/m_skybox.cpp | 22 ++++++++++------------
core/model/renderer/m_skybox.hpp | 12 ++++++------
core/model/renderer/m_transform.cpp | 6 ++----
core/model/renderer/m_transform.hpp | 2 +-
core/model/renderer/material/m_material_base.cpp | 12 +++++-------
core/model/renderer/material/m_material_base.hpp | 2 +-
core/model/renderer/material/m_material_pbr.cpp | 20 +++++++++-----------
core/model/renderer/material/m_material_pbr.hpp | 2 +-
core/model/renderer/material/m_material_skybox.cpp | 4 +---
core/model/sceneData/data/model_data.cpp | 6 +++---
core/model/sceneData/data/model_data.h | 2 +-
core/model/sceneData/decoder/model_content_decoder.cpp | 45 +++++++++++++++++++++++----------------------
core/model/shader/shader_libs.cpp | 18 +++++++++---------
core/model/shader/shader_libs.hpp | 2 +-
core/model/utility/skybox_utils.cpp | 2 +-
core/model/utility/skybox_utils.hpp | 2 +-
core/platform/android/android_impl.cpp | 9 +++++----
core/platform/android/egl_pbuffer_context.cpp | 6 +++---
core/platform/android/egl_pbuffer_context.h | 8 ++++----
core/platform/android/egl_window_context.cpp | 16 ++++++++--------
core/platform/android/egl_window_context.h | 6 +++---
core/platform/android/jni_bridge.cpp | 8 ++++----
core/platform/android/mars_offscreen_surface.cpp | 2 +-
core/platform/android/mars_player_jni.cpp | 38 +++++++++++++++++++-------------------
core/platform/ios/ios_impl.mm | 2 +-
core/player/comp_vfx_item.cpp | 37 ++++++++++++++++++++-----------------
core/player/comp_vfx_item.h | 6 +++---
core/player/composition.cpp | 40 +++++++++++++++++++---------------------
core/player/composition.h | 18 +++++++++---------
core/player/mars_native_data.cpp | 12 ++++++------
core/player/mars_native_player.cpp | 10 +++++-----
core/player/mars_player.cpp | 16 ++++++++--------
core/player/mars_player.h | 2 +-
core/player/mars_player_mgr.cpp | 53 ++++++++++++++++++++++-------------------------------
core/player/plugin_system.cpp | 26 +++++++++++++-------------
core/player/vfx_item.cpp | 4 +++-
core/player/vfx_item.h | 2 +-
core/plugin/arTrack/ar_track_loader.cpp | 5 ++---
core/plugin/arTrack/ar_track_vfx_item.cpp | 14 +++++++-------
core/plugin/cal/calculate_group.cpp | 36 ++++++++++++++++--------------------
core/plugin/cal/calculate_group.h | 2 +-
core/plugin/cal/calculate_item.cpp | 2 +-
core/plugin/cal/calculate_item.h | 1 +
core/plugin/cal/calculate_loader.cpp | 59 ++++++++++++++++++++++++++++-------------------------------
core/plugin/cal/calculate_loader.h | 2 +-
core/plugin/cal/calculate_vfx_item.cpp | 3 +--
core/plugin/interact/interact_vfx_item.cpp | 3 +--
core/plugin/particle/link_chain.cpp | 14 +++++++-------
core/plugin/particle/link_chain.hpp | 4 ++--
core/plugin/particle/particle_loader.cpp | 8 +++-----
core/plugin/particle/particle_mesh.cpp | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------
core/plugin/particle/particle_system.cpp | 10 +++++-----
core/plugin/particle/particle_system.hpp | 2 +- core/plugin/particle/particle_vfx_item.cpp | 10 ++++------
core/plugin/particle/shape/donut.cpp | 8 ++------
core/plugin/particle/shape/earcut.cpp | 14 +++++++-------
core/plugin/particle/shape/geometry_2d.cpp | 7 +++----
core/plugin/sprite/filter_sprite_vfx_item.cpp | 4 ++--
core/plugin/sprite/sprite_group.cpp | 84 ++++++++++++++++++++++++++++++++++++++----------------------------------------------
core/plugin/sprite/sprite_group.h | 10 +++++-----
core/plugin/sprite/sprite_item.cpp | 12 ++++++------
core/plugin/sprite/sprite_item.h | 4 ++--
core/plugin/sprite/sprite_loader.cpp | 19 ++++++++-----------
core/plugin/sprite/sprite_loader.h | 2 +-
core/plugin/sprite/sprite_mesh.cpp | 37 +++++++++++++++++--------------------
core/plugin/sprite/sprite_mesh.h | 8 ++++----
core/plugin/sprite/sprite_vfx_item.cpp | 5 ++---
core/plugin/tree/tree_item.cpp | 16 ++++++++--------
core/plugin/tree/tree_item.hpp | 4 ++--
core/plugin/tree/tree_vfx_item.cpp | 20 ++++++++------------
core/ri/backend/opengl/gl_frame_buffer.cpp | 32 +++++++++++++++++---------------
core/ri/backend/opengl/gl_frame_buffer.hpp | 4 ++--
core/ri/backend/opengl/gl_geometry.cpp | 4 +++-
core/ri/backend/opengl/gl_gpu_buffer.cpp | 4 ++--
core/ri/backend/opengl/gl_gpu_buffer.hpp | 2 +-
core/ri/backend/opengl/gl_gpu_capability.cpp | 6 +++---
core/ri/backend/opengl/gl_gpu_capability.hpp | 2 +-
core/ri/backend/opengl/gl_gpu_renderer.cpp | 7 ++++---
core/ri/backend/opengl/gl_gpu_renderer.hpp | 8 ++++----
core/ri/backend/opengl/gl_material.cpp | 62 +++++++++++++++++++++++++++++++-------------------------------
core/ri/backend/opengl/gl_material.hpp | 2 +-
core/ri/backend/opengl/gl_program.cpp | 6 +++---
core/ri/backend/opengl/gl_program.hpp | 8 ++++----
core/ri/backend/opengl/gl_render_buffer.cpp | 2 +-
core/ri/backend/opengl/gl_shader_library.cpp | 10 +++++-----
core/ri/backend/opengl/gl_shader_library.hpp | 8 ++++----
core/ri/backend/opengl/gl_state.cpp | 4 ++--
core/ri/backend/opengl/gl_state.hpp | 44 ++++++++++++++++++++++----------------------
core/ri/backend/opengl/gl_texture.cpp | 12 ++++++------
core/ri/backend/opengl/gl_material.cpp | 62 +++++++++++++++++++++++++++++++-------------------------------
core/ri/backend/opengl/gl_material.hpp | 2 +-
core/ri/backend/opengl/gl_program.cpp | 6 +++---
core/ri/backend/opengl/gl_program.hpp | 8 ++++----
core/ri/backend/opengl/gl_render_buffer.cpp | 2 +-
core/ri/backend/opengl/gl_shader_library.cpp | 10 +++++-----
core/ri/backend/opengl/gl_shader_library.hpp | 8 ++++----
core/ri/backend/opengl/gl_state.cpp | 4 ++--
core/ri/backend/opengl/gl_state.hpp | 44 ++++++++++++++++++++++----------------------
core/ri/backend/opengl/gl_texture.cpp | 12 ++++++------
core/ri/backend/opengl/gl_texture.hpp | 4 ++--
core/ri/render/mars_geometry.cpp | 12 ++++++------
core/ri/render/mars_geometry.hpp | 4 ++--
core/ri/render/mars_material.cpp | 22 ++++++++++++----------
core/ri/render/mars_material.hpp | 10 +++++-----
core/ri/render/mars_mesh.cpp | 20 ++++++++++++--------
core/ri/render/mars_mesh.hpp | 10 +++++-----
core/ri/render/mars_player_render_frame.cpp | 32 +++++++++++++++++---------------
core/ri/render/mars_player_render_frame.h | 16 ++++++++--------
core/ri/render/mars_renderer.cpp | 14 +++++++-------
core/ri/render/mars_renderer_ext.cpp | 4 ++--
core/ri/render/mars_renderer_ext.hpp | 2 +-
core/ri/render/mars_texture.cpp | 4 ++--
core/ri/render/render_frame.cpp | 14 ++++++++------
core/ri/render/render_frame.hpp | 8 ++++----
core/ri/render/render_frame_internal.cpp | 8 ++++----
core/ri/render/render_frame_internal.hpp | 8 ++++----
core/ri/render/render_pass.cpp | 42 +++++++++++++++++++++++-------------------
core/ri/render/render_pass.hpp | 18 +++++++++---------
core/ri/render/render_pass_color_attachment.cpp | 4 +++-
core/ri/render_base.cpp | 24 ++++++++++++++----------
core/ri/render_base.hpp | 4 ++--
core/sceneData/data/sprite_mesh_geometry.cpp | 24 +++++++++++-------------
core/sceneData/decoder/common_properties_decoder.cpp | 25 +++++++++++++------------
core/sceneData/decoder/composition_decoder.cpp | 6 +++---
core/sceneData/decoder/filter_content_decoder.cpp | 16 ++++++++--------
core/sceneData/decoder/interact_content_decoder.cpp | 8 ++++----
core/sceneData/decoder/item_decoder.cpp | 6 +++---
core/sceneData/decoder/particle_content_decoder.cpp | 9 +++++----
core/sceneData/decoder/plugin_content_decoder.cpp | 4 ++--
core/sceneData/decoder/sprite_content_decoder.cpp | 7 ++++---
core/sceneData/mars_data_base.cpp | 12 ++++++------
core/sceneData/mars_data_base.h | 2 +-
core/sceneData/scene_data_decoder.cpp | 58 ++++++++++++++++++++++++++++------------------------------
core/sceneData/scene_data_decoder_new.cpp | 30 +++++++++++++++---------------
core/util/color_utils.cpp | 15 ++++++++-------
core/util/mars_thread.cpp | 12 ++++++------
core/util/util.cpp | 12 ++++++------
core/util/vfx_value_getter.cpp | 28 ++++++++++++++--------------
core/util/vfx_value_getter.h | 2 +-
179 files changed, 1153 insertions(+), 1230 deletions(-)
改动这么大,仍然能通过编译,成功驱动 GPU 来雕花:
JSON compilation database 是跨工具间分享构建参数和环境的一个通用格式,也顺带介绍给大家,做 c++ 工具/工作流的必备知识点。最常见的使用场景:vs code 里看 c++ 项目代码,也需要 compile_commands.json,那么如何获得它呢
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON,android cmake 构建流中默认被打开。
https://github.com/android/ndk/wiki/Changelog-r18
Generate with either ndk-build compile_commands.json (does not build) or ndk-build GEN_COMPILE_COMMANDS_DB=true (builds and generates database).
个人自己学习需要,或为团队项目需要,看过一些 c++ 的书,我稍微罗列下:
可见 c++ 人才达到既要视野广博、又能实战,培养/学习周期之长。同时有些历史包袱(以及过去不太好的 c++ 教育)被现代 c++ 给恰当解决了。而当下又恰逢普及现代 c++ 的十字路口,系统编程继续走现代 cpp(渐进式改良)或迁移到 Rust(彻底革新)的选型十字路口。若选型继续现代 c++ 路径,我们大可以把 cpp core guideline 的材料和工具当作抛弃包袱、培养现代 c++ 人才的一个契机。
以上都是人肉总结。
最后的最后,我们问问大模型怎么说:
智谱清言:
C++ Core Guidelines 是一组旨在帮助 C++ 开发者编写更可靠、可维护和高效代码的建议。它们是由 C++ 社区的专家制定的,经过广泛讨论和审查。遵循这些指南,对开发者有以下几个价值:
提高代码质量:遵循 Core Guidelines 可以帮助你编写更符合 C++ 语言特性和标准库的代码,从而提高代码的质量。这些指南覆盖了许多常见的编码问题,如资源管理、异常处理和现代 C++ 功能。
提高代码可维护性:遵循 Core Guidelines 可以帮助你编写更易于阅读、理解和维护的代码。这意味着你可以更容易地找到和修复代码中的错误,同时也使得新成员更易于加入项目。
提高代码可扩展性:遵循 Core Guidelines 可以帮助你编写更具有可扩展性的代码。这意味着你可以更容易地为代码添加新功能,而无需对现有代码进行大量的修改。
提高代码性能:遵循 Core Guidelines 可以帮助你编写更高效的代码。这些指南提供了关于如何优化代码性能的建议,如避免不必要的复制、使用合适的算法等。
促进团队协作:遵循 Core Guidelines 可以使你的代码与其他开发者的代码更容易地协同工作。这意味着团队成员可以更容易地理解、修改和扩展彼此的代码。
降低缺陷风险:遵循 Core Guidelines 可以降低你的代码中出现缺陷的风险。这些指南涵盖了许多常见的错误和陷阱,遵循它们可以帮助你避免这些问题。
总之,遵循 C++ Core Guidelines 可以帮助你编写更可靠、可维护、高效和易于扩展的代码,从而提高你的开发质量和工作效率。同时,这也有助于促进团队协作,降低项目风险,并提高代码的整体质量。
Copyright© 2013-2019