How to specify compilation database for clang-tidy
Asked Answered
A

1

11

I am struggling with running clang-tidy for my project. I am trying to run clang-tidy for my project for send data to Codacy. I am doing it like this:

clang-tidy $PWD -header-filter=.*,-checks=-*,clang-analyzer-*,-clang-analyzer-cplusplus* | ./codacy-clang-tidy-1.1.1 | \
        curl -XPOST -L -H "project-token: $CODACY_PROJECT_TOKEN" \
            -H "Content-type: application/json" -d @- \
            "https://api.codacy.com/2.0/commit/$TRAVIS_COMMIT/issuesRemoteResults"

        curl -XPOST -L -H "project-token: $CODACY_PROJECT_TOKEN" \
            -H "Content-type: application/json" \
            "https://api.codacy.com/2.0/commit/$TRAVIS_COMMIT/resultsFinal"

But it is complaining that compilation data cannot be found:

Error while trying to load a compilation database:
Could not auto-detect compilation database for file "/home/travis/build/mVento3/Duckvil/build"
No compilation database found in /home/travis/build/mVento3/Duckvil or any parent directory
fixed-compilation-database: Error while opening fixed database: No such file or directory
json-compilation-database: Error while opening JSON database: No such file or directory

I am sure that compile_commands.json is in build directory where I am trying to run clang-tidy.
Main CMakeLists.txt:

cmake_minimum_required(VERSION 3.17)

project(Duckvil)

find_program(CLANG_TIDY_COMMAND NAMES clang-tidy)

if(NOT CLANG_TIDY_COMMAND)
    message(WARNING "Could not find clang-tidy!")
    set(CMAKE_CXX_CLANG_TIDY "" CACHE STRING "" FORCE)
else()
    message(WARNING "Found clang-tidy")
    set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
    set(CMAKE_CXX_CLANG_TIDY
        clang-tidy;
        -header-filter=.*;
        -checks=*;
        --dump-config > .clang-tidy;
    )
endif()

if(WIN32)
    add_definitions(-DDUCKVIL_PLATFORM_WINDOWS)
else()
    if(UNIX)
        add_definitions(-DDUCKVIL_PLATFORM_LINUX)

        SET(GCC_COVERAGE_COMPILE_FLAGS "-g -O0 -coverage -fprofile-arcs -ftest-coverage")
        SET(GCC_COVERAGE_LINK_FLAGS "-coverage -lgcov")

        SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
        SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")
    endif()
endif()

add_definitions(-DDUCKVIL_OUTPUT="${CMAKE_SOURCE_DIR}/bin")

add_subdirectory(source)
list(APPEND CMAKE_CTEST_ARGUMENTS "--output-on-failure")
ENABLE_TESTING()
add_subdirectory(test)

Do i need to specify some additional option, or I misunderstood something?

EDIT

At second thought, maybe I should not doing it in separate "go", but while generating CMake project?

EDIT2

I came up with this:

./codacy-clang-tidy-1.1.1 < compile_commands.json | \
        curl -XPOST -L -H "project-token: ${CODACY_PROJECT_TOKEN}" \
            -H "Content-type: application/json" -d @- \
            "https://api.codacy.com/2.0/commit/${TRAVIS_COMMIT}/issuesRemoteResults"

        curl -XPOST -L -H "project-token: ${CODACY_PROJECT_TOKEN}" \
            -H "Content-type: application/json" \
            "https://api.codacy.com/2.0/commit/${TRAVIS_COMMIT}/resultsFinal"

Now it is not complaining about database, seems it is sending the data to codacy, but I cannot see anything on codacy... I read through codacy-clang-tidy and it seems it is getting the data from stdin.

Alcazar answered 17/9, 2020 at 18:46 Comment(5)
CMAKE_CXX_CLANG_TIDY is a cmake variable that sets each target to run clang-tidy automatically against each built source -- why are you setting this to pipe to .clang-tidy? --dump-configs should be something done once by yourself ahead of time, but not as an automated command in the CMakelistsErenow
I thought I should dump it to .clang-tidy file and then the codacy-clang-tidy will read itAlcazar
The .clang-tidy file is effectively the serialized form of the command-line arguments. Generally it's desirable to store this file in your repo and tune it to your own settings (naming convention, desired checks, etc). Even if you want to generate this on-the-fly, you will probably want to do this before you run CMake, so that the CMAKE_CXX_CLANG_TIDY can be used on each target (allowing CMake to build each target and run clang-tidy against it)Erenow
In general clang-tidy is working fine, it is generating warnings and additional info while building project, but I am not sure what should I pass into codacy-clang-tidy or what file does it needs, .clang-tidy or the compilation database. It is complaining that the compilation database cannot be foundAlcazar
Hey @Vento, Codacy dev here. Can you reach Codacy through support so they can check exactly what is wrong. You can do it through in app chat or send an email to "support at codacy dot com".Striation
A
12

Specify the location explicitly using -p like -p ${CMAKE_BUILD_DIR}:

-p <build-path> is used to read a compile command database.

        For example, it can be a CMake build directory in which a file named
        compile_commands.json exists (use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
        CMake option to get this output). When no build path is specified,
        a search for compile_commands.json will be attempted through all
        parent paths of the first input file . See:
        https://clang.llvm.org/docs/HowToSetupToolingForLLVM.html for an
        example of setting up Clang Tooling on a source tree.
Amedeo answered 11/4, 2022 at 10:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.