5

Several static analysis tools designed for C/C++ exist, but they are not particularly useful for testing CUDA sources.

Since clang version 6 is able to compile CUDA, I wanted to check what are my options with using clang-tidy, which does not seem to have option for switching architectures.

Is there a way to make it work? For example compile time switch for turning on CUDA parser, extension in form of custom check, or is it maybe planned feature?

Johny
  • 1,947
  • 13
  • 23

1 Answers1

4

One of the problem with the clang-based tools is that they are not parsing the files in exactly the same way as clang does.

The first problem is that unlike C/C++ compilation, CUDA compilation compiles the source multiple times. By default clang creates multiple compilation jobs when you give it a CUDA file and that trips many tools that expect only one compilation. In order to work that around you need to pass --cuda-host-only option to clang-tidy.

You may also need to pass --cuda-path=/path/to/your/CUDA/install/root so clang can find CUDA headers.

Another problem you may run into would be related to include paths. Clang-derived tools do not have the same default include paths that clang itself uses and that occasionally causes weird problems. At the very least clang-tidy needs to find __clang_cuda_runtime_wrapper.h which is installed along with clang. If you run clang-tidy your-file.c -- -v it will print clang's arguments and include search paths it uses. Compare that to what clang -x c /dev/null -fsyntax-only -vprints. You may need to give clang-tidy extra include paths to match those used by clang itself. Note that you should not explicitly add the path to the CUDA includes here. It will be added in the right place automatically by --cuda-path=....

Once you have it all in place, clang-tidy should work on CUDA files.

Something like this: clang-tidy your-file.cu -- --cuda-host-only --cuda-path=... -isystem /clang/includes -isystem /extra/system/includes

ArtemB
  • 3,496
  • 17
  • 18
  • Ok, thanks - I'll try to experiment with that. Do you expect that I can make it work also for device code? – Johny Dec 21 '18 at 07:33
  • Probably, but my guess is that you don't need to. The host/device differences mostly kick in during code generation. clang-tidy does not get there (I think it passes `-fsyntax-only` under the hood) and should give you about the same results for host and device code (modulo things that depend on __CUDA_ARCH__). – ArtemB Dec 22 '18 at 05:31