工具找到包括头未使用? [英] Tools to find included headers which are unused?
问题描述
我知道 PC-Lint 可以告诉您包含但未使用的标头。有没有其他工具可以做到这一点,最好在linux?
我们有一个大的代码库,在过去的15年已经看到大量的功能移动,但很少当函数从一个实现文件移动到另一个实现文件时,剩下的#include指令被删除,这使我们有一个很好的混乱。我显然可以做删除所有的#include指令,让编译器告诉我哪些包括,但我宁愿解决问题反向 - 找到未使用的 - 而不是重建一个使用的列表的艰难的事情。
免责声明:我的工作是为一家开发静态分析工具的公司工作。 p>
如果大多数(如果不是全部)静态分析工具没有某种形式的头使用检查,我会感到惊讶。您可以使用维基百科页面获取可用工具列表,
您在评估工具时可能会考虑以下几点:
对于函数重载,您希望所有包含重载的头文件都可见,而不只是包含通过重载解析选择的函数的头文件:
// f1.h
void foo(char);
// f2.h
void foo(int);
// bar.cc
#includef1.h
#includef2.h
int main )
{
foo(0); //调用'foo(int)'但所有函数都在重载集合中
}
如果你采取强力的方法,首先删除所有头,然后重新添加,直到它编译,如果'f1.h'首先添加,然后代码将编译,但程序的语义已更改。
当您有部分和特殊化时,类似的规则适用。无论是否选择专业化,您都需要确保所有专业化都是可见的:
// f1.h
template< typename T>
void foo(T);
// f2.h
模板<>
void foo(int);
// bar.cc
#includef1.h
#includef2.h
int main )
{
foo(0); // Calls specialization'foo< int>(int)'
}
重载示例,强力方法可能导致程序仍然编译但具有不同的行为。
您可以注意的另一种相关类型的分析是检查类型可以向前声明。考虑以下内容:
// A.h
class A {};
// foo.h
#includeA.h
void foo(A const&);
// bar.cc
#includefoo.h
void bar(A const& a)
{
foo(a);
}
在上面的示例中,'A'的定义不是必需的,因此可以更改头文件'foo.h',以便它只对'A'有前向声明:
/ foo.h
class A;
void foo(A const&);
这种检查也会减少标题依赖性。
I know PC-Lint can tell you about headers which are included but not used. Are there any other tools that can do this, preferably on linux?
We have a large codebase that through the last 15 years has seen plenty of functionality move around, but rarely do the leftover #include directives get removed when functionality moves from one implementation file to another, leaving us with a pretty good mess by this point. I can obviously do the painstaking thing of removing all the #include directives and letting the compiler tell me which ones to reinclude, but I'd rather solve the problem in reverse - find the unused ones - rather than rebuilding a list of used ones.
DISCLAIMER: My day job is working for a company that develops static analysis tools.
I would be surprised if most (if not all) static analysis tools did not have some form of header usage check. You could use this wikipedia page to get a list of available tools and then email the companies to ask them.
Some points you might consider when you're evaluating a tool:
For function overloads, you want all headers containing overloads to be visible, not just the header that contains the function that was selected by overload resolution:
// f1.h
void foo (char);
// f2.h
void foo (int);
// bar.cc
#include "f1.h"
#include "f2.h"
int main ()
{
foo (0); // Calls 'foo(int)' but all functions were in overload set
}
If you take the brute force approach, first remove all headers and then re-add them until it compiles, if 'f1.h' is added first then the code will compile but the semantics of the program have been changed.
A similar rule applies when you have partial and specializations. It doesn't matter if the specialization is selected or not, you need to make sure that all specializations are visible:
// f1.h
template <typename T>
void foo (T);
// f2.h
template <>
void foo (int);
// bar.cc
#include "f1.h"
#include "f2.h"
int main ()
{
foo (0); // Calls specialization 'foo<int>(int)'
}
As for the overload example, the brute force approach may result in a program which still compiles but has different behaviour.
Another related type of analysis that you can look out for is checking if types can be forward declared. Consider the following:
// A.h
class A { };
// foo.h
#include "A.h"
void foo (A const &);
// bar.cc
#include "foo.h"
void bar (A const & a)
{
foo (a);
}
In the above example, the definition of 'A' is not required, and so the header file 'foo.h' can be changed so that it has a forward declaration only for 'A':
// foo.h
class A;
void foo (A const &);
This kind of check also reduces header dependencies.
这篇关于工具找到包括头未使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!