This is an archive of the discontinued LLVM Phabricator instance.

[analyzer][ctu] Allow loading invocation list from a compilation database automatically detected from the ctu-dir
AbandonedPublic

Authored by OikawaKirie on May 9 2021, 10:58 PM.

Details

Summary

It seems to be simpler and more convenient to reuse the compilation database generated for ClangTool when using clang-check to run the analyzer.
In this patch, when the invocation list file is failed to be loaded, the ASTLoader will try to find a compilation database from the ctu-dir and convert the detected database to an invocation list.

For each compile command objects in the database:

  • the "directory" entry is converted to option -working-directory appended to the invocation commands;
  • the "file" entry is updated to an absolute path based on the "directory" entry as the index;
  • and the "command" entry is converted to the invocation commands.

Diff Detail

Event Timeline

OikawaKirie created this revision.May 9 2021, 10:58 PM
OikawaKirie requested review of this revision.May 9 2021, 10:58 PM

Thank you @OikawaKirie for working on this many CTU related patches! I am going to find time for a thorough review and going to pursue @gamesh411 as well to do the same! On the other hand, it would be really useful if you could build a "Stack" from these patches, I mean could you please set which patch depends from which other (see "Edit related revisions")?

Thank you @OikawaKirie for working on this many CTU related patches! I am going to find time for a thorough review and going to pursue @gamesh411 as well to do the same! On the other hand, it would be really useful if you could build a "Stack" from these patches, I mean could you please set which patch depends from which other (see "Edit related revisions")?

To make it easier to review, all these recently submitted patches are based on the main branch. Their code does not depend on each other.

Logically, they can be separated into two branches: invocation list and ctu index.
The invocation list related patches target on constructing the invocation list in ASTLoader from a compilation database. And the problems of using invocation lists are also fixed.
Whereas the ctu index related patches mainly focus on the delimiter character in the ctu index, which is generated by the clang-extdef-mapping tool.

If the "stack" means the logical relations, I will update that later. Otherwise, it is OK to start with any of the patches.

Thank you for the patch!

Though, the idea is nice, there is a serious technical obstacle here: we cannot use the clangTooling lib as a dependency of the CTU lib because that would introduce a circular dependency. Actually, this was the reason for introducing the invocation list yaml file; we could not use the compilation database implementation from tooling (https://reviews.llvm.org/D75665?id=260238#inline-722328).

This comment was removed by steakhal.

Thank you for the patch!

Though, the idea is nice, there is a serious technical obstacle here: we cannot use the clangTooling lib as a dependency of the CTU lib because that would introduce a circular dependency. Actually, this was the reason for introducing the invocation list yaml file; we could not use the compilation database implementation from tooling (https://reviews.llvm.org/D75665?id=260238#inline-722328).

According to my recent experiences on using clang-check, converting the compilation database to an invocation list is also an acceptable solution.
Currently, a better solution may be adding a tool to handle the conversion, just as what has been mentioned in the revision you presented. Although, it would be a very simple python script such as:

a = dict()
for i in json.load(open("/path/to/compile_commands.json")):
    a[os.path.abspath(os.path.join(i['directory'], i['file']))] = \
        (shlex.split(i['command']) if 'command' in i else i['arguments']) + \
        ['-Xclang', '-working-directory=' + i['directory']]
print(yaml.dump(a));

If you agree with this idea, I will follow the way to create a tool for the conversion and drop this revision.

Actually, I am writing a makefile to schedule the ctu-index generation as well as analyzing the code with the clang-check tool. And the python script snippet presented above is just a part of my makefile.
Maybe I can add my makefile to as a part of the analyzer.

clang/lib/CrossTU/CrossTranslationUnit.cpp
711–712

The approach of converting a compilation database to an invocation list is just simply adding the -working-directory argument for the "directory" entry for each compile command object. Therefore, a script doing the conversion can make it simpler and easier to use.

The main reason why this patch is submitted is I previously do not know the -working-directory argument. Without this argument, it would be very difficult and complex to convert a compilation database to an invocation list.

If you agree that my approach of converting the database (presented in the for loop) is OK, I will continue with making a tool for the conversion.

OikawaKirie abandoned this revision.Nov 30 2021, 3:45 AM

It seems that this issue has better solutions outside the CTU module, I will drop this revision as mentioned above.
Sorry for the delay.