如何实现标准C函数提取?

发布于 2024-10-03 03:17:34 字数 250 浏览 4 评论 0原文

我有一个“a $$ 中的痛苦”任务来提取/解析 main() 函数中调用的所有标准 C 函数。例如: printf、fseek 等...

目前,我唯一的计划是读取 main() 中的每一行,并通过检查我也将定义的标准 C 函数列表来搜索标准 C 函数是否存在(#define CFUNCTIONS "printf...")

如您所知,有很多标准 C 函数,因此定义所有这些函数会很烦人。

关于如何检查字符串是否是标准 C 函数的任何想法?

I have a "a pain in the a$$" task to extract/parse all standard C functions that were called in the main() function. Ex: printf, fseek, etc...

Currently, my only plan is to read each line inside the main() and search if a standard C functions exists by checking the list of standard C functions that I will also be defining (#define CFUNCTIONS "printf...")

As you know there are so many standard C functions, so defining all of them will be so annoying.

Any idea on how can I check if a string is a standard C functions?

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

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

发布评论

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

评论(4

围归者 2024-10-10 03:17:34

如果您听说过 cscope,请尝试查看它生成的数据库。 cscope 前端有可用的指令来列出给定函数调用的所有函数。

如果您查看 main() 的调用列表,您应该能够大大缩小您的工作范围。

如果您必须手动解析,我建议从包含的标准标头开始。它们应该能让您清楚地了解您希望在 main() 中看到哪些函数。

不管怎样,这项工作听起来不平凡而且有趣。

If you have heard of cscope, try looking into the database it generates. There are instructions available at the cscope front end to list out all the functions that a given function has called.

If you look at the list of the calls from main(), you should be able to narrow down your work considerably.

If you have to parse by hand, I suggest starting with the included standard headers. They should give you a decent idea about which functions could you expect to see in main().

Either way, the work sounds non-trivial and interesting.

本王不退位尔等都是臣 2024-10-10 03:17:34

乍一看,解析 C 源代码似乎很简单,但正如其他人指出的那样,程序员有可能通过使用 #define#include 摆脱束缚是比较常见的。除非知道要解析的特定程序在文本替换方面是温和的,否则解析任意 C 源代码的复杂性是相当大的。

考虑较少使用但更有效的解析对象模块的策略。编译源模块,但不链接它。为了进一步简化,请重新处理包含 main 的文件以删除所有其他函数,但在其位置保留声明。

根据要求,有两种方法可以完成任务:

  1. 编写一个程序,打开目标模块并迭代外部引用符号表。如果该符号与有趣的函数名称之一匹配,请将其列出。许多平台都有用于解析目标模块的库函数。
  2. 编写一个命令文件或脚本,使用开发人员工具来检查对象模块。例如,在 Linux 上,命令 nm 列出带有 U 的外部引用。

Parsing C source code seems simple at first blush, but as others have pointed out, the possibility of a programmer getting far off the leash by using #defines and #includes is rather common. Unless it is known that the specific program to be parsed is mild-mannered with respect to text substitution, the complexity of parsing arbitrary C source code is considerable.

Consider the less used, but far more effective tactic of parsing the object module. Compile the source module, but do not link it. To further simplify, reprocess the file containing main to remove all other functions, but leave declarations in their places.

Depending on the requirements, there are two ways to complete the task:

  1. Write a program which opens the object module and iterates through the external reference symbol table. If the symbol matches one of the interesting function names, list it. Many platforms have library functions for parsing an object module.
  2. Write a command file or script which uses the developer tools to examine object modules. For example, on Linux, the command nm lists external references with a U.
情何以堪。 2024-10-10 03:17:34

该任务乍一看可能很简单,但为了真正 100% 确定,您需要解析 C 文件。仅仅查找名称是不够的,您还需要知道上下文,即何时检查 id,首先当您确定 id 是一个函数时,您可以检查它是否是标准的 c 运行时函数。

(而且我想这会让任务变得更有趣:-)

The task may look simple at first but in order to be really 100% sure you would need to parse the C-file. It is not sufficient to just look for the name, you need to know the context as well i.e. when to check the id, first when you have determined that the id is a function you can check if it is a standard c-runtime function.

(plus I guess it makes the task more interesting :-)

2024-10-10 03:17:34

我认为没有任何方法可以绕过必须定义标准 C 函数列表来完成您的任务。但比这更烦人的是——考虑一下宏,
例如:

#define OUTPUT(foo) printf("%s\n",foo)

main()
{
   OUTPUT("Ha ha!\n");
}

因此,您可能希望在检查之前通过预处理器运行代码
哪些函数是从 main() 调用的。那么你可能会遇到这样的情况:

some_func("This might look like a call to fclose(fp), but surprise!\n");

所以你可能需要一个成熟的解析器来严格执行此操作,因为字符串文字
可以跨越多条线。

我不会提出三字母组合……那只是毫无意义的虐待狂。 :-) 不管怎样,祝你好运,编码愉快!

I don't think there's any way around having to define a list of standard C functions to accomplish your task. But it's even more annoying than that -- consider macros,
for example:

#define OUTPUT(foo) printf("%s\n",foo)

main()
{
   OUTPUT("Ha ha!\n");
}

So you'll probably want to run your code through the preprocessor before checking
which functions are called from main(). Then you might have cases like this:

some_func("This might look like a call to fclose(fp), but surprise!\n");

So you'll probably need a full-blown parser to do this rigorously, since string literals
may span multiple lines.

I won't bring up trigraphs...that would just be pointless sadism. :-) Anyway, good luck, and happy coding!

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