Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clazy #32

Open
vadi2 opened this issue Jul 2, 2019 · 11 comments
Open

clazy #32

vadi2 opened this issue Jul 2, 2019 · 11 comments

Comments

@vadi2
Copy link

vadi2 commented Jul 2, 2019

https://github.com/KDE/clazy

Best practices checker for Qt (C++) with fixits.

@pbrisbin
Copy link
Member

pbrisbin commented Jul 2, 2019

This looks really cool! But I might need some help implementing it; the docs seem to assume some general tool-chain familiarity that might be a blocker for me. Please let me know if you'd like help implementing this as a Restyler yourself.

@vadi2
Copy link
Author

vadi2 commented Jul 2, 2019

Yes sure, what would you like help with?

@pbrisbin
Copy link
Member

pbrisbin commented Jul 2, 2019

I was more thinking you (or any contributor who knew this tool well) could take the lead, and I would help them :)

https://github.com/restyled-io/restyled.io/wiki/Adding-a-Restyler

@pbrisbin
Copy link
Member

Reading up on this a big,

Enabling Fixits

[P]ass ... -export-fixes=somefile.yaml for clazy-standalone. Then run clang-apply-replacements <folder_with_yaml_files>, which will modify your code.

So, this will require an extra script to:

  1. Run clazy-standalone -export-fixes=x/..., then
  2. Run clang-apply-replacements x

@pbrisbin
Copy link
Member

I've started on this (will push a PR soon), but can't get it working.

It seems like clazy-standalone is actually trying to compile things (or expects it to somehow have been compiled). Our test case fails with:

       Error while trying to load a compilation database:
       Could not auto-detect compilation database for file "./clazy-test-1.cpp"
       No compilation database found in /code 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
       Running without flags.
       /code/clazy-test-1.cpp:1:10: fatal error: 'QString' file not found
       #include <QString>
                ^~~~~~~~~
       1 error generated.
       Error while processing /code/clazy-test-1.cpp.

I'm not sure what would be required to get it working.

@vadi2
Copy link
Author

vadi2 commented Jul 12, 2023

Hey thanks for taking up work on this. Clazy requires a file called compile_commands.json which are instructions for the compiler on how to compile a certain file. This file can be generated by a bunch of build systems, including cmake if you pass it the -DCMAKE_EXPORT_COMPILE_COMMANDS argument.

I'm not familiar with how restylers works (but looking forward to using it with clazy in) - you might need to get the user to provide some configuration to generate the compile commands file?

@pbrisbin
Copy link
Member

Under normal usage would that ./compile_commands.json be present in the repository when cloned? If so, then we will see/use it and that's good -- I just have to update my test harness to create that scenario correctly.

And do you think this warning is the reason it can't find QString? I've installed qt6 in the restyler image and confirmed that library exists on the filesystem, at least.

@vadi2
Copy link
Author

vadi2 commented Jul 12, 2023

It's an autogenerated file and it's typically not checked in because the paths to the libraries would be different on every developers machine.

Here's an example of how I use another clang-based tool to first generate the file and then run the tool when the file is present: https://github.com/Mudlet/Mudlet/blob/development/.github/workflows/clangtidy-diff-analysis.yml#L107-L136

And do you think this warning is the reason it can't find QString?

Yeah, that would be it.

@pbrisbin
Copy link
Member

OK, a little further:

       CMake Error: The source directory "/code" does not appear to contain CMakeLists.txt.

I added cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON . before running clazy-standalone.

Is this a file I can expect to be present in a checked-out clone? If so, how do I fake it for the test-harness.

For more context, the test harness is just running a single .cpp file with the contents:

        #include <QString>
        QString str = "hello";
        str.mid(5).toInt(ok);

@vadi2
Copy link
Author

vadi2 commented Jul 12, 2023

Try this cpp file:

#include <iostream>
#include <QString>

int main() {
    QString str = "hello";
    QString result = str.mid(5);

    std::cout << "Original string: " << str.toStdString() << std::endl;
    std::cout << "Substring from index 5 onwards: " << result.toStdString() << std::endl;

    return 0;
}

With this CMakeLists.txt file present in the same folder:

cmake_minimum_required(VERSION 3.16)

project(helloworld VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_AUTOMOC ON)

find_package(Qt6 REQUIRED COMPONENTS Core)

add_executable(helloworld
    main.cpp
)

target_link_libraries(helloworld PRIVATE Qt6::Core)

and then run cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON . as you did before.

@pbrisbin
Copy link
Member

OK, I think I'm in a good place. The following seems to function, without error:

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .
clazy-standalone --export-fixes="$tmp"/fixes.yaml --checks=level0 <input>
clang-apply-replacements "$tmp"

The problem now is that it seems to do nothing. fixes.yaml is not produced, apply-replacements is a no-op, and my test fails because .mid was not replaced by midRef.

🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: 🏇 In progress
Development

No branches or pull requests

2 participants