Ericsson / codechecker

CodeChecker is an analyzer tooling, defect database and viewer extension for the Clang Static Analyzer and Clang Tidy
https://codechecker.readthedocs.io
Apache License 2.0
2.24k stars 376 forks source link

Error when trying to use PCH file #3827

Closed hayalbaz closed 1 year ago

hayalbaz commented 1 year ago

Note: Issue includes a short bit on why I did something with clang-tidy, as i think it might be related.

I am using GCC/Qt/CMake in my project, want to integrate static analysis into my build process. I tried to use clang-tidy individually but I got an error because apparently precompiled header formats are different.

I used the following CMake piece to generate a fake precompiled header for clang-tidy and it worked fine:

set(PREFIX_HEADER ${CMAKE_CURRENT_LIST_DIR}"/src/PrecompiledDefs.h")
set(CLANG_PCH_OUTPUT "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${PROJECT_NAME}.dir/cmake_pch.hxx.pch")
get_property(CLANG_PCH_COMPILE_OPTIONS TARGET ${PROJECT_NAME} PROPERTY COMPILE_OPTIONS)
get_property(CLANG_PCH_INCLUDE_DIRECTORIES TARGET ${PROJECT_NAME} PROPERTY INTERFACE_SYSTEM_INCLUDE_DIRECTORIES)
set(CLANG_PCH_INCLUDE_STRING "")
foreach(dir ${CLANG_PCH_INCLUDE_DIRECTORIES})
    # Generate system includes to suppress warnings for third-party headers
    set(CLANG_PCH_INCLUDE_STRING ${CLANG_PCH_INCLUDE_STRING} -isystem ${dir})
endforeach()
add_custom_command(
    OUTPUT "${CLANG_PCH_OUTPUT}"
    COMMAND clang++ ${CLANG_PCH_COMPILE_OPTIONS} -fPIC -std=gnu++17 ${CLANG_PCH_INCLUDE_STRING} -x c++-header ${PREFIX_HEADER} -Xclang -emit-pch -o ${CLANG_PCH_OUTPUT}
    COMMENT "Creating Clang precompiled header for clang-tidy."
)
add_custom_target(clang-pch ALL
    DEPENDS ${CLANG_PCH_OUTPUT}
)
add_dependencies(${PROJECT_NAME} clang-pch)  # Create "fake" precompiled header for clang-tidy
target_precompile_headers(MyProject PRIVATE src/PrecompiledDefs.h)

Now I want to use codechecker instead, I used the same CMake build process to generate compilation database and my precompiled header files.

However I am getting the following error: error: PCH file was compiled for the target 'x86_64-pc-linux-gnu' but the current translation unit is being compiled for target 'x86_64-unknown-linux-gnu' [clang-diagnostic-error]

CodeChecker version

[INFO 2023-01-26 15:10] - CodeChecker analyzer version:
---------------------------------------
Kind                 | Version         
---------------------------------------
Base package version | 6.21.0-rc       
Package build date   | 2022-11-11T15:38
Git commit ID (hash) |                 
Git tag information  |                 
---------------------------------------
[WARNING 2023-01-26 15:10] - This version is only a release candidate! If you encounter any problems, please submit a bug report!

[INFO 2023-01-26 15:10] - CodeChecker web version:
------------------------------------------------------
Kind                                | Version         
------------------------------------------------------
Base package version                | 6.21.0-rc       
Package build date                  | 2022-11-11T15:38
Git commit ID (hash)                |                 
Git tag information                 |                 
Server supported Thrift API version | 6.51            
Client Thrift API version           | 6.51            
------------------------------------------------------
[WARNING 2023-01-26 15:10] - This version is only a release candidate! If you encounter any problems, please submit a bug report!

To Reproduce 1)Create a CMake project using target_precompile_headers using GCC as compiler 2)Generate compilation database 3)Invoke codechecker analyze compile_commands.json -o ./reports in build directory

Expected behaviour Expected it to work as clang-tidy did when called seperately.

Desktop (please complete the following information)

hayalbaz commented 1 year ago

I have decided to ditch precompiled headers as it seems to break just about any clang tool, so I am closing this.