Saturday, October 18, 2025

Using clang-cl in Visual Studio

clang-cl

 clang-cl is the command line tool in Visual Studio capable of invoking the clang compiler with the arguments of msvc. In Visual Studio projects one can just flip the toolset and the clang compiler will be chosen. clang has the following aspects:

  • + better C++ conformance. msvc is leniant towards missing 'typename' for dependent types and 'template' for nesting templates; clang picks them up
  • + offers some code improvements like correct member order in constructors and virtuals which override base class
  • +  offers some performance improvements like using shared_ptr by reference in loops
  • - some noisy warnings 
  • - does not understand all msvc code. For example the msvc's #import extension is not understood

 We use msvc's code analysis for code issues. Still the clang compiler was able to pick up other issues. Some clang warnings are far fetched and one can disable them. This is especially needed for external libraries which one cannot easily patch. To disable warnings one can use the following:

#ifdef __clang__
#pragma clang diagnostic ignored "-Wimplicit-exception-spec-mismatch"
#pragma clang diagnostic ignored "-Wmissing-field-initializers"
#pragma clang diagnostic ignored "-W#pragma-messages"
#pragma clang diagnostic ignored "-Wunused-but-set-variable"
#pragma clang diagnostic ignored "-Wunused-local-typedef"
#endif

 The first one for example is necessary to suppress warnings in MFC. 'delete' should be specified with 'noexcept' but the MFC delete lacks this.


Using clang-cl in Visual Studio

clang-cl  clang-cl is the command line tool in Visual Studio capable of invoking the clang compiler with the arguments of msvc. In Visual St...