This is an archive of the discontinued LLVM Phabricator instance.

tidy-llvm
Needs ReviewPublic

Authored by CSears on Dec 15 2021, 10:35 PM.

Details

Summary

Using clang-tidy on LLVM itself (the proverbial eating your own dogfood) is a little different from using a clang-tidy installation on another source tree. clang-tidy needs to know where to find include files within the LLVM source tree itself.

This is a Python script, tidy-llvm, which finds the root of the llvm tree, verifies LLVM build requirements (LLVM_ENABLE_PROJECTS:STRING=clang;clang-tools-extra and CMAKE_EXPORT_COMPILE_COMMANDS:UNINITIALIZED=On), constructs llvm and current directory include paths, concatenates up an argument list and then runs clang-tidy on the .h and .cpp files on the command line.

tidy-llvm XXAsmPrinter.cpp generates a clang-tidy command line that looks like:

clang-tidy -checks="llvm-*" -p .../build XXAsmPrinter.cpp -- -I.
  -IAsmParser -IGISel -IDisassembler -ITargetInfo -IXX -IMCTargetDesc
  -I.../libcxx/include -I.../build/include -I.../llvm/include

I'm pretty sure that the llvm include directories (-I.../libcxx/include -I.../build/include -I.../llvm/include others?) have to be enumerated rather than simply inheriting where clang-tidy thinks they are in the installation. Unless I manually specified them, clang-tidy wasn't finding them.

I think someone more expert in the build machinery could improve it greatly. For example, it currently gets a strange error about __config_site (which doesn't prevent the rest of the clang-tidy checking):

Error while processing path to XXAsmPrinter.cpp.
path/libcxx/include/__config:13:10: error: '__config_site' file not found [clang-diagnostic-error]
#include <__config_site>

config_site is indeed not in my build tree. There is a file libcxx/include/config_site.in but that's it. Again, someone more knowledgable in the build machinery might understand why this is the case.

clang-tidy still interprets its .clang_tidy file wherever it finds it. It's better to leave most of the -checks there. The tidy-llvm Python script itself is currently set to -checks=\"llvm-*\". I set modernize in the .clang_tidy file for my backend. I can imagine having a .clang-tidy file at the root of the llvm tree.

Adding support for run-clang-tidy would be nice but for tidying individual files it really won't matter.

Diff Detail

Event Timeline

CSears created this revision.Dec 15 2021, 10:35 PM
CSears requested review of this revision.Dec 15 2021, 10:35 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 15 2021, 10:35 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
This revision now requires changes to proceed.Dec 15 2021, 11:46 PM
beanz added a subscriber: beanz.Jan 19 2022, 8:33 PM

Sorry I’m a bit late here. If you want to use clang-tidy on a CMake built project (like LLVM) you can use CMake’s built-in support for clang-tidy:

https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_CLANG_TIDY.html

This will run clang-tidy during the project build with each compile invocation.

lebedev.ri resigned from this revision.Jan 12 2023, 4:56 PM

This review may be stuck/dead, consider abandoning if no longer relevant.
Removing myself as reviewer in attempt to clean dashboard.

This revision now requires review to proceed.Jan 12 2023, 4:56 PM
Herald added a project: Restricted Project. · View Herald Transcript
Herald added a subscriber: StephenFan. · View Herald Transcript