简体   繁体   English

是否可以使用clang自动包含缺少的c ++头文件?

[英]Is possible to include missing c++ header file automatic using clang?

My plan is using clang to parse code and find the type undefined , and then find the corresponding header file, finally insert #include"xxx" in the file. 我的计划是使用clang解析代码并找到未定义的类型,然后找到相应的头文件,最后在文件中插入#include“xxx”。 Does clang can accomplish this? clang可以做到这一点吗?

I think I should describe my problem in more detail, which can make it easier to solve. 我想我应该更详细地描述我的问题,这可以使它更容易解决。 I have a big project, it cost a long time to complie. 我有一个大项目,它需要花费很长时间才能完成。 I find almost every cpp file include "common.h" which is consists of many header files. 我发现几乎每个cpp文件都包含“common.h”,它由许多头文件组成。 So I want to clear the header files in "common.h", and insert the necessary header file in each cpp. 所以我想清除“common.h”中的头文件,并在每个cpp中插入必要的头文件。 It's a very boring and time costing work. 这是一项非常无聊且耗时的工作。 I wonder if any automatic method can help. 我想知道任何自动方法是否有帮助。

IDE(或自动化工具)可以分析编译结果并提供建议(例如xcode执行此操作),但并非所有人都希望在没有程序员意识的情况下自动修复代码。

I don't think clang in itself can figure out which header file contains what type declarations and come up with which include should be used. 我不认为clang本身可以找出哪个头文件包含什么类型的声明,并提出应该使用哪些包含。

You could certainly use clang to determine what types are needed in a particular translation unit (source code) and which types are provided by a headerfile, and be a little smarter than grep -r typename somedir /dev/null , but it wouldn't be MUCH better. 您当然可以使用clang来确定特定翻译单元中需要哪些类型(源代码)以及哪些类型grep -r typename somedir /dev/null文件提供,并且比grep -r typename somedir /dev/null更聪明,但它不会好多了 As comments say, you could end up suggesting the wrong header file - just because a file contains a class or struct definition with the right name (and content if you are being more clever about it), doesn't mean that it is the one the original author intended to use. 正如评论所说,你最终可能会建议错误的头文件 - 只是因为一个文件包含一个具有正确名称的类或结构定义(如果你对它更聪明的内容),并不意味着它是一个原作者打算使用。

The main problem would be that you have to index all the existing header files, or it would take quite a long time to parse everything for every source file available. 主要问题是您必须索引所有现有的头文件,否则需要花费很长时间来解析每个可用源文件的所有内容。

As I see it, the main difficulties are: 我认为,主要的困难是:

  • not suggesting "the wrong solution". 不建议“错误的解决方案”。 Particularly when there are multiple solutions (eg portable code that has a header for windows, another for mac, another for linux - picking the right one would not be possible from just reading the sources!) 特别是当有多个解决方案时(例如,便携式代码有一个用于windows的标题,另一个用于mac,另一个用于linux - 只需阅读源代码就无法选择正确的解决方案!)
  • to sort out multiple definitions (and it's far from unusual for various header files to have #if x; typedef ab; #endif sequences, so you can often end up with multiple places that COULD define a type). 排序多个定义(对于各种头文件来说, #if x; typedef ab; #endif序列很不常见,所以你最终可能会找到多个可以定义类型的地方)。
  • dealing with code that needs #define to work correctly. 处理需要#define才能正常工作的代码。
  • to determine when the name is a simple typo, rather than a new headerfile needed. 确定名称何时是简单的拼写错误,而不是需要新的头文件。 Eg std::sting blah = "blah"; 例如std::sting blah = "blah"; does not mean you should try to find a std::sting type, but that the code needs to be fixed to std::string ... . 并不意味着你应该尝试找到一个std::sting类型,但代码需要修复为std::string ...
  • Chains that are required. 需要的链条。 You need to add #include "foo.h" but since foo.h contains std::string (but no include for it), you also need to add #include <string> before foo.h is included. 你需要添加#include "foo.h"但由于foo.h包含std::string (但没有包含它),你还需要在包含foo.h之前添加#include <string>
  • Types declared in macros... Could make for an interesting challenge... ;) 在宏中声明的类型......可以带来有趣的挑战......;)

There is nothing technically stopping a solution that suggests header files that solve a particular compiler error [and, if desired, update the code to solve the problem - that's simply a matter of text file manipulation, which for this purpose isn't particularly difficult], but I think that most of the time, a user that is familiar with the project will do a much better job than this sort of solution. 从技术上讲,没有任何解决方案可以建议解决特定编译器错误的头文件[并且,如果需要,更新代码以解决问题 - 这只是文本文件操作的问题,为此目的并不是特别困难] ,但我认为大多数时候,熟悉该项目的用户将比这种解决方案做得更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM