如何使用 clangd 抑制 C/C++ 函数在 vscode 中没有参数的警告?

how to suppress warnings that C/C++ functions hava no parameters in vscode with clangd?

提问人:menget 提问时间:2/12/2023 最后编辑:Vlad from Moscowmenget 更新时间:2/12/2023 访问量:386

问:

描述

我尝试使用 vscode 和 clangd 来编写 C/C++,但是对于那些参数为空的函数,vscode 会给出“没有原型的函数声明在所有版本的 C 中都已弃用(修复可用)”警告,看起来像这样:vscode-warning 并建议显式添加参数。 我想禁止显示此警告,我应该怎么做?void

环境

VSCode

版本: 1.76.0-insider 提交:c7930ca55d072608625ba76c13b5f9baaf9a2136 日期:2023-02-10T16:24:38.605Z 电子:19.1.9 铬:102.0.5005.194 节点.js:16.14.2 V8:10.2.154.23-电子.0 操作系统: Darwin arm64 22.2.0 沙盒:是

叮叮当当

0.1.23 版

相关信息

在 VSCode settings.json 中配置关于 clangd 的配置:

"clangd.path": "/opt/homebrew/opt/llvm/bin/clangd",
    "clangd.arguments": [
        "--log=verbose",
        "--pretty",
        "--all-scopes-completion",
        "--completion-style=bundled",
        "--cross-file-rename",
        "--header-insertion=iwyu",
        "--header-insertion-decorators",
        "--background-index",
        "--clang-tidy",
        "--clang-tidy-checks=cppcoreguidelines-*,performance-*,bugprone-*,portability-*,modernize-*,google-*",
        "--fallback-style=Google",
        "-j=2",
        "--pch-storage=memory",
        "--function-arg-placeholders=true",
        "--compile-commands-dir=${workspaceFolder}/build",
        "--completion-parse=auto",
        "--enable-config",
        "--ranking-model=decision_forest",
        "--query-driver=/opt/homebrew/opt/llvm/bin"
    ],
    "clangd.fallbackFlags": [
        "-pedantic",
        "-Wall",
        "-Wextra",
        "-Wcast-align",
        "-Wdouble-promotion",
        "-Wformat=2",
        "-Wimplicit-fallthrough",
        "-Wmisleading-indentation",
        "-Wnon-virtual-dtor",
        "-Wnull-dereference",
        "-Wold-style-cast",
        "-Woverloaded-virtual",
        "-Wpedantic",
        "-Wshadow",
        "-Wunused",
        "-pthread",
        "-fuse-ld=lld",
        "-fsanitize=address",
        "-fsanitize=undefined",
        "-stdlib=libc++",
        "-std=c++17",
    ],
    "clangd.checkUpdates": true,
    "clangd.onConfigChanged": "prompt",
    "clangd.serverCompletionRanking": true,
    "clangd.detectExtensionConflicts": true,
    "editor.suggest.snippetsPreventQuickSuggestions": false

我想这与但不知道如何设置有关。 请帮我提供线索或解决方案,非常感谢。--clang-tidy

C++ C 警告 声明 函数 原型

评论

1赞 Harith 2/12/2023
为什么不指定而不是寻找漏洞?正如警告所说,它已被弃用。但是你是在写 C 还是 C++,因为这是两种不同的语言,有不同的规则。void
0赞 menget 2/12/2023
对不起,我更喜欢没有虚空的样式,但不知道这与哪个设置有关。
1赞 Andrew Henle 2/12/2023
void function()指定不带参数的函数。这是一种已弃用的指定函数方式,该函数采用未指定数量的未指定类型的参数。如果不想收到该警告,请正确写入以指定不带参数的函数。void function(void)
4赞 Eric Postpischil 2/12/2023
使用或不使用不是一种风格。这是一个不同的语义含义。将代码更新为现代 C。void
0赞 menget 2/12/2023
谢谢,但是如何指定用于语法检查的 C 标准?

答:

1赞 Vlad from Moscow 2/12/2023 #1

从警告中可以看出

“没有原型的函数声明在所有中都被弃用了 C 版本(修复可用)”

你有一个 C 程序,其中函数是用空的参数列表声明的,例如

void f();

在 C 中,与 C++ 相反,这样的声明意味着对函数参数的数量和类型一无所知。

因此,您需要在 C 中声明函数,例如

void f( void );

或者,如果函数确实具有参数,则需要在函数声明中指定它们的类型。这样的声明被命名为函数原型。使用函数原型,编译器能够确定函数是否被正确调用,并提供相应类型的参数表达式。