0

Clang-tidy is now part of the C/C++ plugin for VS Code. Earlier I were using the clang-tidy extension, and it was working fine. The issue is that clang-tidy now treats size_t, uintptr_t and all pointers as 64-bits, and issues warnings for 8/16/32-bit code (I'm working with microcontrollers). There are a lot of warnings that were not there earlier. Instead of disabling these checks, it would be better to configure clang-tidy appropriately.

Is there a way to tell clang-tidy the size of these datatypes?

vjalle
  • 549
  • 4
  • 13
  • 1
    Do you have `compiler_commands.json` generated? – HolyBlackCat Aug 04 '22 at 08:09
  • No, I don't have that file anywhere. Google says it's produced by CMake and/or LLVM. I'm using various embedded compilers (Tasking, Hightec, Diab etc...) and the build system is based on GNU make / MSYS2, so this file is not produced. But my issue is independent of the actual compilation: Code can run clang-tidy on the sources when saving and display issues online with ErrorLens and in the Problems view. This feature is not working properly due to the mismatching data sizes. – vjalle Aug 04 '22 at 10:35
  • 1
    Then you need to create this file manually. I see two options: 1. Put a `--target=??` flag into this file, passing your target triplet to it. 2. Or, specify your embedded compiler in this file (as opposed to a placeholder `clang++`), also pass it to `--query-driver=??` (Clangd flag, not in `compiler_commands.json`) and hope that Clangd figures out the target architecture from it. – HolyBlackCat Aug 04 '22 at 10:40
  • That file was a dead end, but googling about it led me to the --target option. Configuring that in Code seems to have solved the issue. Thanks for looking into my issue and give the right directions. – vjalle Aug 04 '22 at 16:37
  • 1
    You're welcome. There are multiple ways to pass flags to Clang-tidy. `compile_commands.json` should be one of them, and the most flexible one, allowing you to set flags per file. Apparently there's a single global config too, that you've found. – HolyBlackCat Aug 04 '22 at 18:08
  • 1
    Actually that file was not even fetched by clang-tidy, tested by invoking it from the command line. I guess it could be made working somehow. I'm using clang-tidy (and clang-format) through the Code plugin, without the rest of the LLVM infrastructure (literally only these 2 executables are present). Worked out of the box, but configuration is more tricky than it is in the native environment. I don't want per file configuration so this is perfect for me. – vjalle Aug 04 '22 at 18:33

1 Answers1

0

Adding this to settings.json seems to have solved the issue:

"C_Cpp.codeAnalysis.clangTidy.args": [
    "--extra-arg=--target=arm"
],

All warnings for initializing size_t with unsigned int values and casting pointers to/from unsigned int disappeared.

vjalle
  • 549
  • 4
  • 13