gpt4 book ai didi

根据函数原型(prototype)编译时决定调用参数

转载 作者:行者123 更新时间:2023-12-02 19:18:08 27 4
gpt4 key购买 nike

在使用第三方库时,我经常发现自己需要编写粘合“代码”来处理跨版本更改的函数原型(prototype)。

以 Linux 内核为例:这是一个常见的场景 - 假设我们有函数 int my_function(int param) 并且在某些时候我们需要添加一个可选的 void *data 。将以这种方式添加新参数,而不是破坏 API:

int __my_function(int param, void *data);

static inline my_function(int param) {
return __my_function(param, NULL);
}

这很好:它向不需要的人隐藏了新参数和 API 损坏。现有代码可以继续将 my_function 与旧原型(prototype)一起使用。

然而,情况并非总是如此(无论是在 Linux 中还是在其他库中),我最终得到了这样的片段,来处理我遇到的所有可能的版本:

#if LIBRARY_VERSION > 5
my_function(1, 2, 3);
#elif LIBRARY_VERSION > 4
my_function(1, 2);
#else
my_function(1);
#endif

所以我在想,对于简单的原型(prototype)更改(参数的重新排序、添加/删除“默认”参数等),让编译器自动完成会很好。

我希望自动完成此操作(无需指定确切的版本),因为有时要确定引入更改的确切库/内核版本需要大量工作。因此,如果我不关心新参数并且我可以只使用 0,我希望编译器在需要时使用 0

我最远的距离是这样的:

#include <stdio.h>

#if 1
int f(int a, int b) {
return a + b;
}
#else
int f(int a) {
return a + 5;
}
#endif

int main(void) {
int ret = 0;
int (*p)() = (int(*)())f;
if (__builtin_types_compatible_p(typeof(f), int(int, int)))
ret = p(3, 4);
else if (__builtin_types_compatible_p(typeof(f), int(int)))
ret = p(1);
else
printf("no matching call\n");
printf("ret: %d\n", ret);
return 0;
}

这可行 - GCC 在编译类型中选择适当的调用,但它有两个问题:

  1. 通过“无类型”函数指针调用,我们失去了类型检查。因此 p("a", "b") 是合法的,但给出的结果是垃圾。
  2. 似乎没有发生标准类型提升(同样,可能是因为我们通过无类型指针进行调用)

(这里可能还有其他问题,但这些是我认为最关键的点)

我相信它可以通过一些宏魔法来实现:如果调用参数是分开的,宏可以生成找到适当原型(prototype)的代码,然后一一测试所有给定参数的类型兼容性。但我认为它使用起来会复杂得多,所以我正在寻找一个更简单的解决方案。

有什么想法如何通过适当的类型检查+促销来实现这一点吗?我认为如果不使用编译器扩展就无法完成此任务,因此我的问题集中在针对 Linux 的现代 GCC。

编辑:在对其进行宏观化+添加Acorn的 Actor 想法后,我留下了这个:

#define START_COMPAT_CALL(f, ret_type, params, args, ret_value) if (__builtin_types_compatible_p(typeof(f), ret_type params)) (ret_value) = ( ( ret_type(*) params ) (f) ) args
#define ELSE_COMPAT_CALL(f, ret_type, params, args, ret_value) else START_COMPAT_CALL(f, ret_type, params, args, ret_value)
#define END_COMPAT_CALL() else printf("no matching call!\n")

int main(void) {
int ret = 0;

START_COMPAT_CALL(f, int, (int, int), (999, 9999), ret);
ELSE_COMPAT_CALL(f, int, (int), (1), ret);
ELSE_COMPAT_CALL(f, int, (char, char), (5, 9999), ret);
ELSE_COMPAT_CALL(f, int, (float, int), (3, 5), ret);
ELSE_COMPAT_CALL(f, int, (float, char), (3, 5), ret);
END_COMPAT_CALL();
printf("ret: %d\n", ret);
return 0;
}

这对于我注意到的两点是有效的 - 它给出类型检查警告并正确执行升级。 但是它也会对所有“未选择”的调用站点发出警告:警告:通过不兼容类型调用的函数。我尝试用 __builtin_choose_expr 包装调用,但我没有运气:/

最佳答案

您可以使用 _Generic 在标准 C 中执行此操作:

//  Define sample functions.
int foo(int a, int b) { return a+b; }
int bar(int a, int b, int c) { return a+b+c; }

// Define names for the old and new types.
typedef int (*Type0)(int, int);
typedef int (*Type1)(int, int, int );

/* Given an identifier (f) for a function and the arguments (a and b) we
want to pass to it, the MyFunctio macro below uses _Generic to test which
type the function is and call it with appropriate arguments.

However, the unchosen items in the _Generic would contain function calls
with mismatched arguments. Even though these are never evaluated, they
violate translation-time contraints for function calls. One solution would
be to cast the function (automatically converted to a function pointer) to
the type being used in the call expression. However, GCC sees through this
and complains the function is called through a non-compatible type, even
though it never actually is. To solve this, the Sanitize macro is used.

The Sanitize macro is given a function type and a function. It uses a
_Generic to select either the function (when it matches the type) or a null
pointer of the type (when the function does not match the type). (And GCC,
although unhappy with a call to a function with wrong parameters, is happy
with a call to a null pointer.)
*/
#define Sanitize(Type, f) \
_Generic((f), Type: (f), default: (Type) 0)
#define MyFunction(f, a, b) \
_Generic(f, \
Type0: Sanitize(Type0, (f)) ((a), (b)), \
Type1: Sanitize(Type1, (f)) ((a), (b), 0) \
)


#include <stdio.h>


int main(void)
{
printf("%d\n", MyFunction(foo, 3, 4));
printf("%d\n", MyFunction(bar, 3, 4));
}

上面使用参数化函数名称 (f) 来让我们通过定义的两个函数(foobar)来演示这一点。对于问题中的情况,只有一个函数定义,只有一个名称,因此我们可以简化宏。我们还可以使用函数名称作为宏名称。 (在预处理过程中不会递归地替换,因为 C 不会递归地替换宏,而且名称不会出现在替换文本中,其后带有左括号。)这看起来像:

#define Sanitize(Type, f)   \
_Generic((f), Type: (f), default: (Type) 0)
#define foo(a, b) \
_Generic(foo, \
Type0: Sanitize(Type0, foo) ((a), (b)), \
Type1: Sanitize(Type1, foo) ((a), (b), 0) \
)

printf("%d\n", foo(3, 4));

(此处使用的 Sanitize 定义由 Artyerthis answer 中提供。)

关于根据函数原型(prototype)编译时决定调用参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63403418/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com