protonscr

clang-tidy linter usage

dxvkclosed enhancement
doitsujin/dxvk#259 · opened 2018-04-09 by pchome · updated 2018-06-02 · 5 comments · github
Ppchome 2018-04-09 github

I'm currently playing with clang-tidy, just for fun. So here is some useful (?) notes how to use it with DXVK:

#!/bin/sh

./run-clang-tidy.py \
-extra-arg="-std=c++17" \
-extra-arg="-stdlib=libc++" \
-extra-arg="-isystem/usr/include/wine/windows" \
-extra-arg="-isystem/usr/include/c++/v1" \
-extra-arg="-DNOMINMAX" \
-extra-arg="-DWINE_UNICODE_NATIVE" \
-extra-arg="-D__WINE__" \
-extra-arg="-fshort-wchar" \
-header-filter='.*,-vulkan.h,-vulkan_platform.h' \
-checks='-*,modernize-*' 

Output example:

/tmp/build.w64/../dxvk/src/util/util_env.cpp:60:49: warning: use nullptr [modernize-use-nullptr]
    if (::CreateDirectoryW(dxvkTempDir.c_str(), 0) == 0) {
                                                ^~
                                                nullptr                                                                                                
Suppressed 6433 warnings (6433 in non-user code).
/tmp/build.w64/../dxvk/src/dxvk/dxvk_graphics.cpp:218:5: warning: use range-based for loop instead [modernize-loop-convert]
    for (uint32_t i = 0; i < 4; i++)
    ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        (float & blendConstant : cbInfo.blendConstants)                                                                                                
Suppressed 14428 warnings (14427 in non-user code, 1 with check filters).
/tmp/build.w64/../dxvk/src/dxvk/hud/dxvk_hud_text.cpp:75:5: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto]
    HudTextVertex* vertexData = reinterpret_cast<HudTextVertex*>(
    ^~~~~~~~~~~~~~
    auto                                                                                                                                               
Suppressed 14082 warnings (14082 in non-user code).

To list all available modernize checks use:

$ clang-tidy --list-checks -checks='*' | grep "modernize"

To let clang-tidy fix warnings use:

$ ./run-clang-tidy.py ... -checks='-*,modernize-use-nullptr' -fix

Ppchome 2018-04-09 github

Fix: -header-filter='.*,-vulkan.h,-vulkan_platform.h' used to silence typedef warnings in vulkan headers, but with -header-filter='.*' there is other warnings like modernize-use-override

/tmp/build.w64/../dxvk/src/dxvk/dxvk_graphics.h:111:5: warning: annotate this function with 'override' or (rarely) 'final' [modernize-use-override]
    ~DxvkGraphicsPipeline();
    ^                      ~
                            override

Ppchome 2018-04-09 github

modernize-* checks it's good, but there is some warnings with general checks to be reviewed :
-checks='-*,modernize-*' removed from commandline
(nice step-by-step explanation produced after warning)

../dxvk/src/d3d11/d3d11_context_imm.cpp:192:33: warning: Access to field 'pData' results in a dereference of a null pointer (loaded from variable 'pMappedResource') [clang-analyzer-core.NullDereference]
    pMappedResource->pData      = physicalSlice.mapPtr(0);
                                ^
../dxvk/src/d3d11/d3d11_context_imm.cpp:118:9: note: Assuming the condition is false
    if (pResource == nullptr) {
        ^
../dxvk/src/d3d11/d3d11_context_imm.cpp:118:5: note: Taking false branch
    if (pResource == nullptr) {
    ^
../dxvk/src/d3d11/d3d11_context_imm.cpp:123:9: note: Assuming pointer value is null
    if (pMappedResource != nullptr) {
        ^
../dxvk/src/d3d11/d3d11_context_imm.cpp:123:5: note: Taking false branch
    if (pMappedResource != nullptr) {
    ^
../dxvk/src/d3d11/d3d11_context_imm.cpp:132:9: note: Assuming 'resourceDim' is equal to D3D11_RESOURCE_DIMENSION_BUFFER
    if (resourceDim == D3D11_RESOURCE_DIMENSION_BUFFER) {
        ^
../dxvk/src/d3d11/d3d11_context_imm.cpp:132:5: note: Taking true branch
    if (resourceDim == D3D11_RESOURCE_DIMENSION_BUFFER) {
    ^
../dxvk/src/d3d11/d3d11_context_imm.cpp:135:28: note: Passing null pointer value via 4th parameter 'pMappedResource'
        MapType, MapFlags, pMappedResource);
                           ^
../dxvk/src/d3d11/d3d11_context_imm.cpp:133:14: note: Calling 'D3D11ImmediateContext::MapBuffer'
      return MapBuffer(
             ^
../dxvk/src/d3d11/d3d11_context_imm.cpp:163:9: note: Assuming the condition is false
    if (!(buffer->memFlags() & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)) {
        ^
../dxvk/src/d3d11/d3d11_context_imm.cpp:163:5: note: Taking false branch
    if (!(buffer->memFlags() & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)) {
    ^
../dxvk/src/d3d11/d3d11_context_imm.cpp:168:9: note: Assuming 'MapType' is not equal to D3D11_MAP_WRITE_DISCARD
    if (MapType == D3D11_MAP_WRITE_DISCARD) {
        ^
../dxvk/src/d3d11/d3d11_context_imm.cpp:168:5: note: Taking false branch
    if (MapType == D3D11_MAP_WRITE_DISCARD) {
    ^
../dxvk/src/d3d11/d3d11_context_imm.cpp:181:16: note: Assuming 'MapType' is equal to D3D11_MAP_WRITE_NO_OVERWRITE
    } else if (MapType != D3D11_MAP_WRITE_NO_OVERWRITE) {
               ^
../dxvk/src/d3d11/d3d11_context_imm.cpp:181:12: note: Taking false branch
    } else if (MapType != D3D11_MAP_WRITE_NO_OVERWRITE) {
           ^
../dxvk/src/d3d11/d3d11_context_imm.cpp:192:33: note: Access to field 'pData' results in a dereference of a null pointer (loaded from variable 'pMappedResource')
    pMappedResource->pData      = physicalSlice.mapPtr(0);
                                ^

?ghost 2018-04-10 github

It can be added as meson option. I think something like that:
https://github.com/rwengine/openrw/pull/403/files
(that's for cmake)

Ppchome 2018-04-30 github

compile_flags.txt example, can be used by clangd (for IDE language server plugins like vscode-clangd). Can be placed to project root directory and/or any src subdir for source-specific flags (AFAIK).

-std=c++17

-DNOMINMAX

-D__WINESRC__

-isystem/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/include/g++-v7
-isystem/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/include/g++-v7/x86_64-pc-linux-gnu

-isystem/usr/include/wine/windows
-isystem./include

-Isrc
-Isrc/dxbc
-Isrc/dxvk

-m64
-fshort-wchar
-DWINE_UNICODE_NATIVE
-D_REENTRANT
-DWIN64
-D_WIN64
-D__WIN64
-D__WIN64__
-DWIN32
-D_WIN32
-D__WIN32
-D__WIN32__
-D__WINNT
-D__WINNT__
-D__stdcall=__attribute__((ms_abi))
-D__cdecl=__attribute__((ms_abi))
-D_stdcall=__attribute__((ms_abi))
-D_cdecl=__attribute__((ms_abi))
-D__fastcall=__attribute__((ms_abi))
-D_fastcall=__attribute__((ms_abi))
-D__declspec(x)=__declspec_##x
-D__declspec_align(x)=__attribute__((aligned(x)))
-D__declspec_allocate(x)=__attribute__((section(x)))
-D__declspec_deprecated=__attribute__((deprecated))
-D__declspec_dllimport=__attribute__((dllimport))
-D__declspec_dllexport=__attribute__((dllexport))
-D__declspec_naked=__attribute__((naked))
-D__declspec_noinline=__attribute__((noinline))
-D__declspec_noreturn=__attribute__((noreturn))
-D__declspec_nothrow=__attribute__((nothrow))
-D__declspec_novtable=__attribute__(())
-D__declspec_selectany=__attribute__((weak))
-D__declspec_thread=__thread
-D__int8=char
-D__int16=short
-D__int32=int
-D__int64=long
-D__WINE__

Final part is a dump from winegcc -m64 -v ....

Modified clang-tidy.sh script to also use this parameters:

#!/bin/sh

RUN_CLANG_TIDY_PATH=/usr/lib/llvm/7/share/clang
PROJECT_PATH=${HOME}/Projects/wine-playground/dxvk

${RUN_CLANG_TIDY_PATH}/run-clang-tidy.py \
  $(for s in `cat ${PROJECT_PATH}/compile_flags.txt`; do echo -n "-extra-arg=$s "; done) \
  -extra-arg="-isystem${PROJECT_PATH}/include" \
  -header-filter='.*' \
  -quiet
Ppchome 2018-06-02 github

Not an issue, but info. Closing.

Nothing extracted yet.