1

我有两个使用相同技巧和功能的程序,但只有一个可以编译。

A)这个编译,也可以按预期工作:

#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>


/*
 * int  mallocs(T **restrict p, ptrdiff_t nmemb);
 */
#define mallocs(ptr, nmemb) (                                       \
{                                                                   \
        ptrdiff_t   nmemb_  = (nmemb);                              \
        __auto_type ptr_    = (ptr);                                \
        int         err_;                                           \
                                                                    \
        err_    = 0;                                                \
        if (ptr_ == NULL) {                                         \
                errno   = EINVAL;                                   \
                err_    = EINVAL;                                   \
                goto ret_;                                          \
        }                                                           \
        if (nmemb_ < 0) {                                           \
                *ptr_   = NULL;                                     \
                errno   = EOVERFLOW;                                \
                err_    = -EOVERFLOW;                               \
                goto ret_;                                          \
        }                                                           \
        if (nmemb_ > (PTRDIFF_MAX / (ptrdiff_t)sizeof(**ptr_))) {   \
                *ptr_   = NULL;                                     \
                errno   = EOVERFLOW;                                \
                err_    = EOVERFLOW;                                \
                goto ret_;                                          \
        }                                                           \
                                                                    \
        *ptr_   = malloc(sizeof(**ptr_) * nmemb_);                  \
        if (!(*ptr_))                                               \
                err_    = ENOMEM;                                   \
ret_:                                                               \
        err_;                                                       \
}                                                                   \
)


int main(void)
{
        int *b1;
        int **p;

        int c = getchar();

        p = &b1;
        if (c == 'a')
                p = 0;
        printf("%c\n", c);

        if (mallocs(p, 47))
                goto err;

        b1[4] = 52;
        printf("Hi: %i\n", b1[4]);

        free(b1);

        return  0;
err:
        perror(NULL);
        exit(EXIT_FAILURE);
}

B)这个甚至没有编译(错误如下所示):

#include <assert.h>
#include <errno.h>
#include <stdio.h>


#define alx_same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))

#define alx_static_assert_array(a)      do                              \
{                                                                       \
        static_assert(!alx_same_type((a), &(a)[0]), "Not an array!");   \
} while (0)

/*
 * int  alx_sbprintf(char buff[restrict], int *restrict written,
 *              const char *restrict format, ...);
 */
#define alx_sbprintf(buff, written, format, ...)        (               \
{                                                                       \
        __auto_type w_  = (written);                                    \
        int         len_;                                               \
        int         err_;                                               \
                                                                        \
        alx_static_assert_array(buff);                                  \
        err_    = 0;                                                    \
                                                                        \
        len_    = snprintf(buff, sizeof(buff), format, ##__VA_ARGS__);  \
        if (w_ != NULL)                                                 \
                *w_ = len_;                                             \
                                                                        \
        if (len_ < 0) {                                                 \
                err_    = -errno;                                       \
                goto ret_;                                              \
        }                                                               \
        if ((unsigned)len_ >= sizeof(buff)) {                           \
                if (w_ != NULL)                                         \
                        *w_ = sizeof(buff) - 1;                         \
                errno   = ENOMEM;                                       \
                err_    = ENOMEM;                                       \
                goto ret_;                                              \
        }                                                               \
ret_:                                                                   \
        err_;                                                           \
}                                                                       \
)


int main(void)
{
        char    b1[10];
        char    b2[BUFSIZ];

        int     w1;
        int     *w2 = NULL;

        if (alx_sbprintf(b1, &w1, "testttt%i", 12))
                printf("Error 1.1\n");
        printf("b1: %s; w1 = %i\n", b1, w1);

        if (alx_sbprintf(b2, w2, "test%s", "testtt"))
                printf("Error 2.1\n");
        printf("b2: %s; w2 = %p\n", b2, w2);

        return  0;
}

错误:

$ gcc -std=gnu17 -Wall -Wextra -Werror  main.c
main.c: In function ‘main’:
main.c:39:3: error: jump into statement expression
   goto ret_;      \
   ^~~~
main.c:70:6: note: in expansion of macro ‘alx_sbprintf’
  if (alx_sbprintf(b2, w2, "test%s", "testtt"))
      ^~~~~~~~~~~~
main.c:48:1: note: label ‘ret_’ defined here
 ret_:         \
 ^~~~
main.c:66:6: note: in expansion of macro ‘alx_sbprintf’
  if (alx_sbprintf(b1, &w1, "testttt%i", 12))
      ^~~~~~~~~~~~
main.c:46:3: error: jump into statement expression
   goto ret_;      \
   ^~~~
main.c:70:6: note: in expansion of macro ‘alx_sbprintf’
  if (alx_sbprintf(b2, w2, "test%s", "testtt"))
      ^~~~~~~~~~~~
main.c:48:1: note: label ‘ret_’ defined here
 ret_:         \
 ^~~~
main.c:66:6: note: in expansion of macro ‘alx_sbprintf’
  if (alx_sbprintf(b1, &w1, "testttt%i", 12))
      ^~~~~~~~~~~~
main.c:48:1: error: duplicate label ‘ret_’
 ret_:         \
 ^~~~
main.c:70:6: note: in expansion of macro ‘alx_sbprintf’
  if (alx_sbprintf(b2, w2, "test%s", "testtt"))
      ^~~~~~~~~~~~
main.c:48:1: note: previous definition of ‘ret_’ was here
 ret_:         \
 ^~~~
main.c:66:6: note: in expansion of macro ‘alx_sbprintf’
  if (alx_sbprintf(b1, &w1, "testttt%i", 12))
      ^~~~~~~~~~~~

为什么只有其中一个会抛出该错误?

4

2 回答 2

5

GNU C 确实禁止跳转到语句表达式,但您的主要问题是扩展宏会导致ret_标签被复制。

您需要将此语句表达式与声明范围本地标签的__label__扩展结合起来:

#include <assert.h>
#include <errno.h>
#include <stdio.h>


#define alx_same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))

#define alx_static_assert_array(a)      do                              \
{                                                                       \
        static_assert(!alx_same_type((a), &(a)[0]), "Not an array!");   \
} while (0)

/*
 * int  alx_sbprintf(char buff[restrict], int *restrict written,
 *              const char *restrict format, ...);
 */
#define alx_sbprintf(buff, written, format, ...)        (               \
{                                                                       \
        __label__ ret_; \
        __auto_type w_  = (written);                                    \
        int         len_;                                               \
        int         err_;                                               \
                                                                        \
        alx_static_assert_array(buff);                                  \
        err_    = 0;                                                    \
                                                                        \
        len_    = snprintf(buff, sizeof(buff), format, ##__VA_ARGS__);  \
        if (w_ != NULL)                                                 \
                *w_ = len_;                                             \
                                                                        \
        if (len_ < 0) {                                                 \
                err_    = -errno;                                       \
                goto ret_;                                              \
        }                                                               \
        if ((unsigned)len_ >= sizeof(buff)) {                           \
                if (w_ != NULL)                                         \
                        *w_ = sizeof(buff) - 1;                         \
                errno   = ENOMEM;                                       \
                err_    = ENOMEM;                                       \
                goto ret_;                                              \
        }                                                               \
ret_:                                                                   \
        err_;                                                           \
}                                                                       \
)


int main(void)
{
        char    b1[10];
        char    b2[BUFSIZ];

        int     w1;
        int     *w2 = NULL;

        if (alx_sbprintf(b1, &w1, "testttt%i", 12))
                printf("Error 1.1\n");
        printf("b1: %s; w1 = %i\n", b1, w1);

        if (alx_sbprintf(b2, w2, "test%s", "testtt"))
                printf("Error 2.1\n");
        printf("b2: %s; w2 = %p\n", b2, w2);

        return  0;
}

(我只复制了其余的代码,但添加__label__ ret_;使代码编译。)

于 2019-06-30T13:47:49.887 回答
4

在示例 B 中,您调用了alx_sbprintf两次宏。这会导致ret_标签被定义两次,从而导致“重复标签”错误。

标签的范围不限于语句表达式,它们的范围是函数。

我不确定为什么在您的第一个示例中没有引发相同的“跳转到语句表达式”错误。

当函数可以正常工作时,没有理由使用这样的 GCC 表达式语句。(static inline如果你想把它放在头文件中。)

于 2019-06-30T13:44:22.033 回答