基于c头文件的c文件中的重新排序函数
有没有工具可以根据.h
自动重新排序.c
文件?
例如, foo.h
void function1();
void function2();
void function3();
和 foo.c
void function2(){}
void function1(){}
void function3(){}
我可以将其重新排序为
void function1(){}
void function2(){}
void function3(){}
顺便说一句,我在 Ubuntu 中使用 Vim。
Is there any tool to automatically reorder the .c
file based on .h
?
For example, foo.h
void function1();
void function2();
void function3();
And foo.c
void function2(){}
void function1(){}
void function3(){}
Can I reorder it as
void function1(){}
void function2(){}
void function3(){}
By the way, I'm using Vim in Ubuntu.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不相信有这样的工具。在 C 中,声明和定义的顺序通常并不重要。当然,存在依赖循环的例外情况(
a()
调用b()
,而b()
又调用a()
),但是当函数在头文件中声明,即使这也不是问题,因为所有声明实际上都是处理依赖循环所需的“前向”声明。因此,翻译单元中定义的顺序本质上是品味和风格的问题。因此,这不是编辑器设计者倾向于解决的功能,因为创建足够通用和强大的功能以值得大多数用户使用的努力可能被认为是非常大的。想想(不可编程)编辑器花了多长时间才普遍拥有灵活而强大的自动缩进和重新格式化功能。
自动重组时还存在破坏代码逻辑、语法或可读性的风险。例如,如果注释位于函数定义之间,那么该工具如何知道注释是否与特定函数、一组函数或上述函数一起使用?正如 @Yuri 上面提到的,#if/#else/#endif 块内的函数怎么样?那么像扩展为函数的宏或夹在函数定义之间的 #include 指令之类的微妙情况又如何呢?我认为重新排序功能可以将其范围限制在简单的情况下,但如果情况变得太简单,那么它的吸引力相应就会受到限制,并且不会公开或广泛发布。
话虽这么说,我认为有了丰富的工具集可供使用,这样的功能实现起来不会太难,尽管如果我这样做,我预计在第一个意想不到的困难时,我会发现自己想知道它是否会手动编辑源文件并不容易。毕竟,多行剪切和粘贴非常容易。
I don't believe there is such a tool. In C, order of declaration and definition generally does not matter. There is of course the exception of a dependency loop (
a()
callsb()
which callsa()
), but when functions are declared in a header file, even this is not a problem, since all the declarations are effectively the "forward" declarations needed to handle dependency loops.Thus the order of definitions in a translation unit is essentially a matter of taste and style. As such, it is not a feature that editor designers are apt to address, since the effort to create features general and powerful enough to be worthwhile to a preponderance of users may be deemed to be prohibitively large. Think how long it took for (non-programmable) editors to commonly have flexible and powerful automatic indentation and reformatting features.
There is also risk of breaking the logic, grammar, or readability of code when it is reorganized automatically. For example, if comments lie between function definitions, how will the tool know if a comment goes with a particular function, or with a group of functions, or with the function above? And as @Yuri mentions above, what about functions inside #if/#else/#endif blocks? And what about subtle cases like macros that expand to functions or #include directives sandwiched between function definitions? I suppose a reordering feature could restrict its domain to a simple case, but if the case gets too simple, its appeal is correspondingly limited, and it doesn't get released publicly or widely.
All that being said, I think with a rich toolset at your disposal, such a feature would not be too hard to implement, though if I were doing it, I expect that at the first unanticipated difficulty, I would find myself wondering if it wouldn't be easier to just edit the source files by hand. Multiline cut and paste is pretty easy, after all.