如何配置clangd检测C++类未实现的成员函数
问题
我在WSL环境下的VSCode中使用config.yaml配置clangd-12,当前配置如下:
Diagnostics: ClangTidy: Add: [ performance-*, bugprone-*, portability-*, modernize-*, ] Remove: modernize-use-trailing-return-type CheckOptions: WarnOnFloatingPointNarrowingConversion: false
定义了如下C++类:
#ifndef __MODEL_H__ #define __MODEL_H__ #include <vector> #include "geometry.h" class Model { private: std::vector<Vec3f> verts_; //顶点 std::vector<std::vector<int> > faces_; //面片 public: explicit Model(const char *filename); ~Model(); int GetVertsSize(); int GetFacesSize(); int test();// 这是未实现的函数 Vec3f GetVertByIndex(int i); std::vector<int> GetFaceByIndex(int idx); }; #endif //__MODEL_H__
其中test()是未实现的成员函数,但clangd未给出任何警告或错误,在main.cpp中调用该函数时出现编译错误。请问如何修改config.yaml配置,让clangd能够检测出这类未实现的成员函数?
解决方案
要让clangd检测到未实现的成员函数,需要在配置中添加对应的编译警告参数——clangd依赖clang的编译警告体系,而非clang-tidy的检查项。
修改你的config.yaml,新增CompileFlags配置段来启用目标警告:
Diagnostics: ClangTidy: Add: [ performance-*, bugprone-*, portability-*, modernize-*, ] Remove: modernize-use-trailing-return-type CheckOptions: WarnOnFloatingPointNarrowingConversion: false # 新增编译标志,启用未实现成员函数警告 CompileFlags: Add: - "-Wunimplemented-member-function"
如果clangd-12对-Wunimplemented-member-function支持不佳,可以改用更通用的-Wall(启用大部分基础警告,包含未实现成员函数检测),还可选择添加-Werror将警告转为错误:
CompileFlags: Add: - "-Wall" # 可选:将警告转为错误,强制处理这类问题 # - "-Werror"
说明:
-Wunimplemented-member-function:专门针对C++类中声明但未实现的非纯虚成员函数发出警告-Wall:覆盖一系列常用警告,其中包含对未实现成员函数的检测- 若项目有CMakeLists.txt等构建配置,clangd通常会自动读取编译上下文,但手动添加编译标志可强制启用该警告
内容的提问来源于stack exchange,提问作者cooronx
相关产品推荐
相关产品推荐

