如何抑制“未使用的参数” C 中的警告?

发布于 2024-09-16 11:34:49 字数 228 浏览 10 评论 0原文

例如:

Bool NullFunc(const struct timespec *when, const char *who)
{
   return TRUE;
}

在 C++ 中,我可以在参数周围添加 /*...*/ 注释。但当然不是在 C 中,它给了我错误:

错误:参数名称省略

For instance:

Bool NullFunc(const struct timespec *when, const char *who)
{
   return TRUE;
}

In C++ I was able to put a /*...*/ comment around the parameters. But not in C of course, where it gives me the error:

error: parameter name omitted

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

つ低調成傷 2024-09-23 11:34:49

我通常会编写这样的宏:

#define UNUSED(x) (void)(x)

您可以将这个宏用于所有未使用的参数。 (请注意,这适用于任何编译器。)

例如:

void f(int x) {
    UNUSED(x);
    ...
}

I usually write a macro like this:

#define UNUSED(x) (void)(x)

You can use this macro for all your unused parameters. (Note that this works on any compiler.)

For example:

void f(int x) {
    UNUSED(x);
    ...
}
孤凫 2024-09-23 11:34:49

在 GCC 中,您可以使用 未使用属性

该属性附加到变量上,意味着该变量是
意味着可能未使用。 GCC 不会为此发出警告
变量。

实际上,这是通过在参数之前放置 __attribute__ ((unused)) 来实现的。例如:

void foo(workerid_t workerId) { }

变成

void foo(__attribute__((unused)) workerid_t workerId) { }

In GCC, you can label the parameter with the unused attribute.

This attribute, attached to a variable, means that the variable is
meant to be possibly unused. GCC will not produce a warning for this
variable.

In practice this is accomplished by putting __attribute__ ((unused)) just before the parameter. For example:

void foo(workerid_t workerId) { }

becomes

void foo(__attribute__((unused)) workerid_t workerId) { }
誰認得朕 2024-09-23 11:34:49

您可以使用 GCC 或 Clang未使用属性。然而,我在标头中使用这些宏是为了避免在整个源代码中使用 GCC 特定属性,而且到处都有 __attribute__ 有点冗长/丑陋。

#ifdef __GNUC__
#  define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
#else
#  define UNUSED(x) UNUSED_ ## x
#endif

#ifdef __GNUC__
#  define UNUSED_FUNCTION(x) __attribute__((__unused__)) UNUSED_ ## x
#else
#  define UNUSED_FUNCTION(x) UNUSED_ ## x
#endif

然后你可以做...

void foo(int UNUSED(bar)) { ... }

我更喜欢这个,因为如果你尝试在代码中的任何地方使用 bar ,你会得到一个错误,所以你不能错误地留下该属性。 >

对于函数...

static void UNUSED_FUNCTION(foo)(int bar) { ... }

注1):

据我所知,MSVC 没有与 __attribute__((__unused__)) 等效的项。

注 2):

UNUSED 宏不适用于包含括号的参数,
因此,如果您有像 float (*coords)[3] 这样的参数,您不能这样做,
float UNUSED((*coords)[3])float (*UNUSED(coords))[3]。这是迄今为止我发现的 UNUSED 宏的唯一缺点,在这些情况下,我会退回到 (void)coords;

You can use GCC or Clang's unused attribute. However, I use these macros in a header to avoid having GCC specific attributes all over the source, also having __attribute__ everywhere is a bit verbose/ugly.

#ifdef __GNUC__
#  define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
#else
#  define UNUSED(x) UNUSED_ ## x
#endif

#ifdef __GNUC__
#  define UNUSED_FUNCTION(x) __attribute__((__unused__)) UNUSED_ ## x
#else
#  define UNUSED_FUNCTION(x) UNUSED_ ## x
#endif

Then you can do...

void foo(int UNUSED(bar)) { ... }

I prefer this because you get an error if you try use bar in the code anywhere, so you can't leave the attribute in by mistake.

And for functions...

static void UNUSED_FUNCTION(foo)(int bar) { ... }

Note 1):

As far as I know, MSVC doesn't have an equivalent to __attribute__((__unused__)).

Note 2):

The UNUSED macro won't work for arguments which contain parenthesis,
so if you have an argument like float (*coords)[3] you can't do,
float UNUSED((*coords)[3]) or float (*UNUSED(coords))[3]. This is the only downside to the UNUSED macro I found so far, and in these cases I fall back to (void)coords;.

宫墨修音 2024-09-23 11:34:49

看到它被标记为 gcc,您可以使用命令行开关 Wno-unused-parameter

例如:

gcc -Wno-unused-parameter test.c

当然,这会影响整个文件(可能还会影响项目,具体取决于您设置开关的位置),但您不必更改任何代码。

Seeing that this is marked as gcc you can use the command line switch Wno-unused-parameter.

For example:

gcc -Wno-unused-parameter test.c

Of course this effects the whole file (and maybe project depending where you set the switch) but you don't have to change any code.

梦一生花开无言 2024-09-23 11:34:49

使用带有 未使用属性:

int foo (__attribute__((unused)) int bar) {
    return 0;
}

With GCC with the unused attribute:

int foo (__attribute__((unused)) int bar) {
    return 0;
}
み格子的夏天 2024-09-23 11:34:49

抑制源代码块的未使用参数警告的 g​​cc/g++ 特定方法是用以下 pragma 语句将其括起来:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
<code with unused parameters here>
#pragma GCC diagnostic pop

A gcc/g++ specific way to suppress the unused parameter warning for a block of source code is to enclose it with the following pragma statements:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
<code with unused parameters here>
#pragma GCC diagnostic pop
戈亓 2024-09-23 11:34:49

自 C++ 17 起,[[maybe_unused]] 属性可用于抑制有关未使用参数的警告。

基于OP的示例代码:

Bool NullFunc([[maybe_unused]] const struct timespec *when, [[maybe_unused]] const char *who)
{
   return TRUE;
}

Since C++ 17, the [[maybe_unused]] attribute can be used to suppress warnings about unused parameters.

Based on the OP's example code:

Bool NullFunc([[maybe_unused]] const struct timespec *when, [[maybe_unused]] const char *who)
{
   return TRUE;
}
败给现实 2024-09-23 11:34:49

我遇到了同样的问题。我使用了第三方库。当我编译这个库时,编译器(gcc/clang)会抱怨未使用的变量。

像这样

test.cpp:29:11:警告:变量“magic”已设置但未使用 [-Wunused-but-set-variable]
短魔法[] = {

test.cpp:84:17:警告:未使用的变量“before_write”[-Wunused-variable]
int64_t before_write = Thread::currentTimeMillis();

所以解决方案非常明确。添加 -Wno-unused 作为 gcc/clang CFLAG 将抑制所有“未使用”警告,即使您设置了 -Wall 也是如此。

这样,您不需要更改任何代码。

I got the same problem. I used a third-part library. When I compile this library, the compiler (gcc/clang) will complain about unused variables.

Like this

test.cpp:29:11: warning: variable 'magic' set but not used [-Wunused-but-set-variable]
short magic[] = {

test.cpp:84:17: warning: unused variable 'before_write' [-Wunused-variable]
int64_t before_write = Thread::currentTimeMillis();

So the solution is pretty clear. Adding -Wno-unused as gcc/clang CFLAG will suppress all "unused" warnings, even thought you have -Wall set.

In this way, you DO NOT NEED to change any code.

命硬 2024-09-23 11:34:49

告诉您的编译器使用编译器特定的非标准机制

请参阅 __attribute__((unused))、各种 #pragma 等的单独答案。 (可选)将预处理器宏包裹在其周围以实现可移植性。

关闭警告

IDE 可以通过视觉方式指示未使用的变量(不同颜色或下划线)。有了这个,编译器警告可能就毫无用处了。

在 GCC 和 Clang 中,在命令行末尾添加 -Wno-unused-parameter 选项(在所有打开未使用参数警告的选项之后,例如 -Wall、<代码>-Wextra)。

将强制转换添加到 void

void foo(int bar) {
    (void)bar;
}

按照 jamesdlin 的回答 Mailbag:关闭编译器警告

不要给变量命名(仅限 C23 和 C++)

在 C23 标准之前的 C 中不允许,但使用最新的编译器(2023 年)和 C++(从今以后)可以这样做

void foo(int /*bar*/) {
    ...
}

请参阅 N2480 允许在函数定义中使用未命名参数 (pdf) 提案,以及检查实施状态 https://en.cppreference.com/w/c/compiler_support。

GCC 11、Clang 11 和 ICX 2022.2 (oneAPI 2022.3) 支持此功能。

使用标准属性(C23、C++17)

在 C++17 中,有 [[maybe_unused]] 属性已成为标准的一部分。在此之前,还有[[gnu::unused]]。请参阅clang 文档了解更多概述。 (gnu:: 属性在 clang 中也能正常工作。)

作为 C 标准化工作的一部分,将 C 和 C++ 功能更紧密地结合在一起,在 C23 中,我们获得了新的 C 语言中的属性。其中之一是 [[maybe_unused]] 与 C++ 版本的工作方式相同。 [[gnu::unused]] 编译器特定属性在 C23 之前的版本中不可用,因为早期的 C 语言版本根本没有这些属性。

Tell your compiler using a compiler specific nonstandard mechanism

See individual answers for __attribute__((unused)), various #pragmas and so on. Optionally, wrap a preprocesor macro around it for portability.

Switch the warning off

IDEs can signal unused variables visually (different color, or underline). Having that, compiler warning may be rather useless.

In GCC and Clang, add -Wno-unused-parameter option at the end of the command line (after all options that switch unused parameter warning on, like -Wall, -Wextra).

Add a cast to void

void foo(int bar) {
    (void)bar;
}

As per jamesdlin's answer and Mailbag: Shutting up compiler warnings.

Do not give the variable a name (C23 and C++ only)

Not allowed in C before the C23 standard, but with a latest compiler (in 2023) and in C++ (since like forever) one can do

void foo(int /*bar*/) {
    ...
}

See the N2480 Allowing unnamed parameters in a function definition (pdf) proposal, and check the implementation status at https://en.cppreference.com/w/c/compiler_support.

GCC 11, Clang 11, and ICX 2022.2 (oneAPI 2022.3) support this.

Use a standard attribute (C23, C++17)

In C++17, there is the [[maybe_unused]] attribute which has become part of the standard. Before that, there was [[gnu::unused]]. See clang docs for additional overview. (The gnu:: attributes in general work in clang as well.)

As part of the C standardization effort bringing closer together C and C++ features, in C23 we get new attributes in the C language. One of them is [[maybe_unused]] which works the same as the C++ version. The [[gnu::unused]] compiler-specific attribute is not available in versions prior to C23, because earlier C language versions did not have these attributes at all.

下雨或天晴 2024-09-23 11:34:49

标记属性是理想的方式。宏观有时会导致混乱。
通过使用 void(x),我们增加了处理开销。

如果不使用输入参数,请使用

void foo(int __attribute__((unused))key)
{
}

如果不使用函数内定义的变量

void foo(int key)
{
   int hash = 0;
   int bkt __attribute__((unused)) = 0;

   api_call(x, hash, bkt);
}

现在稍后将哈希变量用于您的逻辑,但不需要 bkt。将 bkt 定义为未使用,否则编译器会显示“bkt set bt notused”。

注意:这只是为了抑制警告而不是为了优化。

Labelling the attribute is ideal way. MACRO leads to sometime confusion.
and by using void(x),we are adding an overhead in processing.

If not using input argument, use

void foo(int __attribute__((unused))key)
{
}

If not using the variable defined inside the function

void foo(int key)
{
   int hash = 0;
   int bkt __attribute__((unused)) = 0;

   api_call(x, hash, bkt);
}

Now later using the hash variable for your logic but doesn’t need bkt. define bkt as unused, otherwise compiler says'bkt set bt not used".

NOTE: This is just to suppress the warning not for optimization.

迟月 2024-09-23 11:34:49

在 MSVC 中,为了抑制特定警告,只需将编译器的编号指定为 /wd# 即可。我的 CMakeLists.txt 包含这样的块:

If (MSVC)
    Set (CMAKE_EXE_LINKER_FLAGS "$ {CMAKE_EXE_LINKER_FLAGS} / NODEFAULTLIB: LIBCMT")
    Add_definitions (/W4 /wd4512 /wd4702 /wd4100 /wd4510 /wd4355 /wd4127)
    Add_definitions (/D_CRT_SECURE_NO_WARNINGS)
Elseif (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_GNUC)
    Add_definitions (-Wall -W -pedantic)
Else ()
    Message ("Unknown compiler")
Endif ()

现在我不能说出 /wd4512 /wd4702 /wd4100 /wd4510 /wd4355 /wd4127 到底是什么意思,因为我三年来没有关注 MSVC,但它们抑制了超级迂腐的警告不影响结果。

In MSVC to suppress a particular warning it is enough to specify the it's number to compiler as /wd#. My CMakeLists.txt contains such the block:

If (MSVC)
    Set (CMAKE_EXE_LINKER_FLAGS "$ {CMAKE_EXE_LINKER_FLAGS} / NODEFAULTLIB: LIBCMT")
    Add_definitions (/W4 /wd4512 /wd4702 /wd4100 /wd4510 /wd4355 /wd4127)
    Add_definitions (/D_CRT_SECURE_NO_WARNINGS)
Elseif (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_GNUC)
    Add_definitions (-Wall -W -pedantic)
Else ()
    Message ("Unknown compiler")
Endif ()

Now I can not say what exactly /wd4512 /wd4702 /wd4100 /wd4510 /wd4355 /wd4127 mean, because I do not pay any attention to MSVC for three years, but they suppress superpedantic warnings that does not influence the result.

旧人 2024-09-23 11:34:49

我见过这种风格的使用:

if (when || who || format || data || len);

I've seen this style being used:

if (when || who || format || data || len);
分分钟 2024-09-23 11:34:49

根据记录,我喜欢乔布的回答,但我很好奇在“什么都不做”语句中仅使用变量名本身的解决方案:

void foo(int x) {
    x; /* unused */
    ...
}

当然,这有缺点;例如,如果没有“未使用”注释,它看起来像是一个错误,而不是故意的代码行。

好处是不需要 DEFINE 并且可以消除警告。

For the record, I like Job's answer, but I'm curious about a solution just using the variable name by itself in a "do-nothing" statement:

void foo(int x) {
    x; /* unused */
    ...
}

Sure, this has drawbacks; for instance, without the "unused" note it looks like a mistake rather than an intentional line of code.

The benefit is that no DEFINE is needed and it gets rid of the warning.

何以心动 2024-09-23 11:34:49

对于抱怨已接受答案的编译器,以下替代方案应该有效。

#define UNUSED(x) if (&(x) == 0) {}

另一种选择可能是:

#define UNUSED(x) switch((long long)&(x)) { default: break; }

For compilers that complain about the accepted answer, the following alternative should work.

#define UNUSED(x) if (&(x) == 0) {}

Another alternative could be:

#define UNUSED(x) switch((long long)&(x)) { default: break; }
じее 2024-09-23 11:34:49

当在 C 程序中使用 main 函数而不使用主参数时,您可以如下无效参数;

#include <stdio.h>
int main(int argc, char **argv)
{
    (void)**argv;

    printf("The program has %d arguments\n`enter code here`",argc);

    return (0);
}

when using the main function in c program and not using the main arguments you can void the arguments as below;

#include <stdio.h>
int main(int argc, char **argv)
{
    (void)**argv;

    printf("The program has %d arguments\n`enter code here`",argc);

    return (0);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文