有没有一个工具可以测试c程序的简洁性?

发布于 2024-12-02 11:02:18 字数 1539 浏览 0 评论 0原文

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

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

发布评论

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

评论(3

旧城烟雨 2024-12-09 11:02:18

您可能会在这里要求几件事。对您的问题的一种可能的解释是,您正在寻找一个切片器,一种接受程序并生成由原始程序中的指令选择组成的程序的工具,并计算部分或全部的原始结果。换句话说,切片器会删除至少一个您感兴趣的结果所不需要的指令。

有一个用于 C 程序的开源切片器 此处

为了使程序更加简洁,仅保留原始程序中的指令的约束可能太强了。您可能还希望允许除“保留”或“删除”之外的一些转换。上面链接的切片器所属的框架也提供这种转换。例如:

int main(int c, char **v)
{
  int x = 2;
  int y = x + c;
  return y; 
}

在上面的程序中,指示保留退出代码的切片器无法删除任何指令:它们都对结果有贡献。但是,如果您首先应用常量传播,将每个常量表达式转换为其值:

int main(int c, char **v)
{
  int x = 2;
  int y = 2 + c;
  return y; 
}

那么切片器可以在第二遍中删除无用的变量x

int main(int c, char **v)
{
  int y = 2 + c;
  return y; 
}

You could be asking for several things here. One possible interpretation of your question is that you are looking for a slicer, a tool that takes a program and produces a program made of a selection of the instructions from the original program, and that computes some or all of the results of the original. In other words, a slicer removes that instructions that are not necessary for at least one of the results you are interested in.

There is an Open-Source slicer for C programs here.

For making programs more concise, perhaps the constraint of keeping only instructions from the original program as they are is too strong. You may also want to allow some transformations other than "keeping" or "removing". The framework that the slicer linked above is part of provides this kind of transformation too. For instance:

int main(int c, char **v)
{
  int x = 2;
  int y = x + c;
  return y; 
}

In the above program, a slicer instructed to preserve the exit code cannot remove any instruction: they all contribute to the result. But if you first apply a constant propagation, transforming every constant expression in its value:

int main(int c, char **v)
{
  int x = 2;
  int y = 2 + c;
  return y; 
}

Then a slicer can in a second pass remove the useless variable x:

int main(int c, char **v)
{
  int y = 2 + c;
  return y; 
}
我不咬妳我踢妳 2024-12-09 11:02:18

答案是合格的“不”,

  • 不,因为不可能编写一个始终回答代码是否可以变得更简洁的程序。如果这样的程序可能存在,您可以用它来创建一个逻辑悖论(这就是为什么我们知道它不可能存在)。

  • 这种性质的程序通常不存在,因为以机械方式缩小源代码会降低代码的可读性。大多数程序员并没有看到使 C 代码尽可能小有任何好处。

  • 是的,因为您可以使用静态分析来查找死代码。

人们投入了大量的研究来使用静态分析自动使程序变得更小、更快,但几乎所有这些研究都应用于为编译器制作更好的优化器。优化器可以产生几乎不可读的输出,这就是为什么我们不使用它们来生成源代码,而仅使用它们来生成目标代码。

保持源代码干净且可读。 99% 的情况下,编译器会处理剩下的事情。

The answer is a qualified "no",

  • No, because it is impossible to write a program that will always answer whether code can be made more concise or not. If such a program could exist, you could use it to create a logical paradox (which is why we know it can't exist).

  • Programs of this nature don't generally exist, because making source code smaller in a mechanical way makes the code less readable. Most programmers just don't see any benefit in making the C code as small as possible.

  • Yes, because you can use static analysis to find dead code.

People put enormous amounts of research into making programs smaller and faster automatically, using static analysis, but nearly all of that research gets applied to making better optimizers for compilers. Optimizers can produce almost unreadable output, which is why we don't use them to produce source code but object code only.

Keep your source code clean and readable. The compiler will take care of the rest, 99% of the time.

℉絮湮 2024-12-09 11:02:18

您可以尝试通过优化来编译它并查看优化的汇编代码。但是,由于编译器无论如何都优化掉了所有额外的东西,因此您实际上在速度方面没有获得任何好处。您所获得的只是可读性的提高。

You could try compiling it with optimizations and looking at the optimized assembly code. But then you really aren't gaining anything in terms of speed since the compiler optimized away all the extra stuff anyway. All you gain is an increase in readability.

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