The error error: no member named 'RoTransformError' in the global namespace from within wrl\event.h indicates that Clang-Tidy's compilation environment is not fully configured for the Windows SDK, specifically for Windows Runtime (WinRT) APIs. RoTransformError is a WinRT function that requires certain preprocessor definitions to be available.
Even though you "don't care about external headers" for analysis, Clang-Tidy still needs to successfully compile them without errors before it can perform any static analysis on your code. A compilation error in a header means Clang-Tidy cannot parse the translation unit.
Here's how to resolve this, from most recommended to more specific workarounds:
The Recommended Solution: Configure Clang-Tidy's Compilation Environment
Clang-Tidy needs to compile your code with the same flags, include paths, and preprocessor definitions that your actual compiler (likely MSVC if you're using WRL) uses.
1. Generate compile_commands.json (Most Robust)
This is the gold standard for telling Clang-Tidy (and Clangd, if you're using it for IntelliSense) exactly how to compile your project.
- For CMake projects:
Add
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) to your CMakeLists.txt. Then, when you build your project with CMake (e.g., cmake -B build -DCMAKE_BUILD_TYPE=Debug followed by cmake --build build), a compile_commands.json file will be generated in your build directory.
- For MSBuild (Visual Studio) projects:
You can use tools like
MSBuildToCMake (a Python script) or CompilationDatabaseGenerator (a Clang-based tool) to generate compile_commands.json from your .sln or .vcxproj files.
- Using
MSBuildToCMake (preferred by many):
- Install it:
pip install msbuild-to-cmake
- Run it from your solution directory:
msbuild-to-cmake .
This will often create a compile_commands.json in the root.
- For other build systems:
Look for specific tools that generate
compile_commands.json for your build system.
Once you have compile_commands.json, place it in your project's root directory. Most Clang-Tidy integrations (like in VS Code) will automatically pick it up.
2. Manual Configuration (If compile_commands.json isn't feasible)
If you're running Clang-Tidy manually or your IDE integration allows custom arguments, you need to provide the necessary preprocessor definitions and include paths.
The RoTransformError function is part of the Windows Runtime Error Handling API (roerrorapi.h), which is typically only exposed when certain WINAPI_FAMILY macros are defined.
You'll likely need these flags:
-D_WIN32_WINNT=0x0A00: Defines the target Windows version (0x0A00 is Windows 10). Modern WRL features often implicitly rely on a more recent SDK version.
-DWINAPI_FAMILY=WINAPI_FAMILY_DESKTOP_APP: Specifies that you are building for a desktop application. This macro is crucial for enabling the correct set of WinRT APIs and definitions in the Windows SDK headers.
Example clang-tidy command line:
clang-tidy your_file.cpp \
-header-filter="^your_project_root/.*" \
-D_WIN32_WINNT=0x0A00 \
-DWINAPI_FAMILY=WINAPI_FAMILY_DESKTOP_APP \
--extra-arg="-IC:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/ucrt" \
--extra-arg="-IC:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/um" \
--extra-arg="-IC:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/shared" \
--extra-arg="-IC:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/winrt" \
--extra-arg="-IC:/Program Files (x86)/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.36.32532/include" \
--
Important Notes for Manual Flags:
- SDK Version: Replace
10.0.22621.0 with your exact Windows SDK version.
- VS Version: Replace
14.36.32532 with your exact MSVC toolset version and 2022/Community with your VS edition.
- The
-- at the end separates Clang-Tidy options from compiler options.
- You may need additional include paths if your project uses other libraries.
3. VS Code clangd / Clang-Tidy Extension Configuration
If you're using VS Code, ensure your settings.json (or workspace settings) correctly configures clangd or the Clang-Tidy extension to use your build system or specify flags.
- If using
compile_commands.json: Most extensions pick this up automatically. Ensure it's in your workspace root.
- If configuring manually:
"clang-tidy.compilerArgs": [
"-D_WIN32_WINNT=0x0A00",
"-DWINAPI_FAMILY=WINAPI_FAMILY_DESKTOP_APP",
// Add your include paths here, e.g.:
"-IC:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/ucrt",
"-IC:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/um",
"-IC:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/shared",
"-IC:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/winrt",
"-IC:/Program Files (x86)/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.36.32532/include"
],
"clang-tidy.buildDirectory": "${workspaceFolder}/build", // If you use a build directory
"clang-tidy.headerFilter": "^${workspaceFolder}/.*", // To filter checks to your code
Addressing "I don't care about external headers"
Once the compilation errors are gone (by correctly configuring the environment), you can use Clang-Tidy's header-filter option to limit the checks to your own code.
header-filter: This option tells Clang-Tidy to only apply checks to files whose paths match the given regular expression.
clang-tidy your_file.cpp -header-filter="C:/Source/repos/Project/.*" --
- Or, in your VS Code settings:
"clang-tidy.headerFilter": "^${workspaceFolder}/.*"
This will ensure that errors from your code are reported, but errors/warnings from wrl.h or other system headers (if they were not compilation errors) would be ignored by the checks.
By correctly setting up the compilation environment, Clang-Tidy will successfully parse wrl.h and proceed to analyze your File.cpp.