Is there any tool to automatically reorder the .c
file based on .h
?
是否有任何工具可以根据.h自动重新排序.c文件?
For example, foo.h
例如,foo.h
void function1();
void function2();
void function3();
And foo.c
和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.
顺便说一句,我在Ubuntu中使用Vim。
1 个解决方案
#1
2
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()
calls b()
which calls a()
), 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.
我不相信有这样的工具。在C中,声明和定义的顺序通常无关紧要。当然有一个异常循环的例外(a()调用b()调用a()),但是当在头文件中声明函数时,即使这不是问题,因为所有的声明都是有效的“转发“处理依赖循环所需的声明。
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.
当代码自动重组时,还存在破坏代码的逻辑,语法或可读性的风险。例如,如果注释位于函数定义之间,那么工具如何知道注释是否与特定函数,一组函数或上述函数一起使用?正如@Yuri上面提到的那样,#if /#else /#endif块内的函数怎么样?那些微妙的情况如扩展到函数的宏或#include指令夹在函数定义之间呢?我认为重新排序功能可以将其域名限制为一个简单的案例,但如果案例过于简单,其吸引力相应有限,并且不会公开或广泛发布。
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.
所有这一切,我认为你可以使用丰富的工具集,这样的功能不会太难实现,但如果我这样做,我希望在第一个意想不到的难度,我会发现自己想知道它是否会手动编辑源文件更容易。毕竟,多线剪切和粘贴非常简单。
#1
2
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()
calls b()
which calls a()
), 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.
我不相信有这样的工具。在C中,声明和定义的顺序通常无关紧要。当然有一个异常循环的例外(a()调用b()调用a()),但是当在头文件中声明函数时,即使这不是问题,因为所有的声明都是有效的“转发“处理依赖循环所需的声明。
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.
当代码自动重组时,还存在破坏代码的逻辑,语法或可读性的风险。例如,如果注释位于函数定义之间,那么工具如何知道注释是否与特定函数,一组函数或上述函数一起使用?正如@Yuri上面提到的那样,#if /#else /#endif块内的函数怎么样?那些微妙的情况如扩展到函数的宏或#include指令夹在函数定义之间呢?我认为重新排序功能可以将其域名限制为一个简单的案例,但如果案例过于简单,其吸引力相应有限,并且不会公开或广泛发布。
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.
所有这一切,我认为你可以使用丰富的工具集,这样的功能不会太难实现,但如果我这样做,我希望在第一个意想不到的难度,我会发现自己想知道它是否会手动编辑源文件更容易。毕竟,多线剪切和粘贴非常简单。