【问题标题】:/* ARGSUSED */ and other special comments/* ARGSUSED */ 和其他特殊注释
【发布时间】:2018-01-23 06:38:48
【问题描述】:

我在 SO 上进行了搜索并用谷歌搜索,但我不明白它们的含义。它们是什么以及它们的目的是什么?它们什么时候使用?我认为在现代编程和我这一代人中看到它们可能为时已晚。

其中一些是 AFAIS,

带有 /* ARGSUSED */

的示例代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#define BUFSIZE 1024
#define TEN_MILLION 10000000L


/* ARGSUSED */
void *threadout(void *args) {
    char buffer[BUFSIZE];
    char *c;
    struct timespec sleeptime;

    sleeptime.tv_sec = 0;
    sleeptime.tv_nsec = TEN_MILLION;
    snprintf(buffer, BUFSIZE, "This is a thread from process %ld\n",
             (long)getpid());
    c = buffer;
    /*****************start of critical section ********************/
    while (*c != '\0') {
        fputc(*c, stderr);
        c++;
        nanosleep(&sleeptime, NULL);
    }
    /*******************end of critical section ********************/
    return NULL;
}


int main(int argc, char *argv[]) {
    int error;
    int i;
    int n;
    pthread_t *tids;

    if (argc != 2){   /* check for valid number of command-line arguments */
        fprintf (stderr, "Usage: %s numthreads\n", argv[0]);
        return 1;
    }
    n = atoi(argv[1]);
    tids = (pthread_t *)calloc(n, sizeof(pthread_t));
    if (tids == NULL) {
        perror("Failed to allocate memory for thread IDs");
        return 1;
    }
    for (i = 0; i < n; i++)
        if (error = pthread_create(tids+i, NULL, threadout, NULL)) {
            fprintf(stderr, "Failed to create thread:%s\n", strerror(error));
            return 1;
        }
    for (i = 0; i < n; i++)
        if (error = pthread_join(tids[i], NULL)) {
            fprintf(stderr, "Failed to join thread:%s\n", strerror(error));
            return 1;
        }
    return 0;
}

【问题讨论】:

  • 它们是您的特定库之类的“#define”标志吗?
  • /* ARGSUSED */ 似乎抑制了函数参数args not used 警告。我想知道为什么代码没有使用void *threadout(void *args) { (void) args; 来做同样的事情?

标签: c comments


【解决方案1】:

lint 专门针对特定问题压制 cmets

什么是 lint - 来自维基百科

在计算机编程中,lint 是一个 Unix 实用程序,可以标记一些 C 中的可疑和不可移植的构造(可能是错误) 语言源代码;一般来说,lint 或 linter 是任何工具 标记以任何计算机语言编写的软件中的可疑使用。 术语 lint-like 行为有时应用于 标记可疑的语言使用。类似 Lint 的工具通常执行 源代码静态分析。

Lint 作为一个术语也可以更广泛地指代语法差异 一般来说,尤其是在解释型语言中,如 JavaScript 和 Python。例如,现代 lint 检查器通常用于查找代码 这不符合某些样式指南。因为这些 语言缺少一个编译阶段,该阶段显示之前的错误列表 执行,它们也可以用作常见错误的简单调试器 (将语法差异显示为错误)或难以发现错误 例如 heisenbugs(提请注意可疑代码为“可能 错误”)。

物品

说明

/*NOTREACHED*/  Suppresses comments about unreachable code.
/*VARARGSNumber*/   Suppresses checking the following old style function declaration for varying numbers of arguments, but does check the data type of the first Number arguments. If you do not include a value for Number, the lint command checks no arguments (Number=0). The ANSI function prototypes should use the ellipsis to indicate unspecified parameters rather than this comment mechanism.
/*ARGSUSED*/    Suppresses warnings about function parameters not used within the function definition.
/*LINTLIBRARY*/     If you place this comment at the beginning of a file, the lint command does not identify unused functions and function parameters in the file. This is used when running the lint command on libraries.
/*NOTUSED*/     Suppresses warnings about unused external symbols, functions and function parameters in the file beginning at its point of occurrence. This is a superset of the /*LINTLIBRARY*/ comment directive, but applies also to external symbols. It is useful for suppressing warnings about unused function prototypes and other external object declarations.
/*NOTDEFINED*/  Suppresses warnings about used, but undefined external symbols and functions in the file beginning at its point of occurrence.
/*LINTSTDLIB*/  Permits a standard prototype-checking library to be formed from header files by making function prototype declarations appear as function definitions. This directive implicitly activates both the /*NOTUSED*/ and /*LINTLIBRARY*/ comment directives to reduce warning noise levels.

也许其他工具也使用它们。

您还可以找到其他特殊的 cmets。例如,许多 IDE 在 cmets 中放置了自己的令牌 - 例如将某些内容添加到 TO DO List

【讨论】:

  • 好答案。我想知道为什么这个问题被否决了,因为我没有找到重复的......
猜你喜欢
  • 2011-09-16
  • 2014-06-08
  • 1970-01-01
  • 2011-02-10
  • 2010-10-15
  • 2014-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多