Skip to content

style(clang-tidy): modularize clang-tidy config #1754

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

bavulapati
Copy link
Contributor

Read blogs and played around with CMAKE_CXX_CLANG_TIDY. Also read source code to understand the clang-tidy config of chromium, llvm and other projects.

We can provide complete context including headers to the clang-tidy tool for static analyzer by running it while building the code. This also makes static analysis first-class citizen of the project, reporting errors before making commits.

For example, misc-internal-linkage check was reporting mostly false positives prior to this. With this, it also considered the headers, where methods/functions were declared constant. Refer #1752

And we should also incrementally add the checks.

bavulapati added a commit to bavulapati/core that referenced this pull request Jun 24, 2025
These are reported with sourcemeta#1754
Refs: sourcemeta#1654

Signed-off-by: Balakrishna Avulapati <ba@bavulapati.com>
bavulapati added a commit to bavulapati/core that referenced this pull request Jun 24, 2025
Reported with sourcemeta#1754
Refs sourcemeta#1654

Signed-off-by: Balakrishna Avulapati <ba@bavulapati.com>
bavulapati added a commit to bavulapati/core that referenced this pull request Jun 24, 2025
Reported with sourcemeta#1754
Refs sourcemeta#1657

Signed-off-by: Balakrishna Avulapati <ba@bavulapati.com>
bavulapati added a commit to bavulapati/core that referenced this pull request Jun 24, 2025
Reported with sourcemeta#1754

Signed-off-by: Balakrishna Avulapati <ba@bavulapati.com>
jviotti pushed a commit that referenced this pull request Jun 24, 2025
These are reported with #1754
    Refs: #1654

Signed-off-by: Balakrishna Avulapati <ba@bavulapati.com>
@bavulapati
Copy link
Contributor Author

@jviotti Need your thoughts on this?
Will resolve the conflicts once we agree on the approach.

@jviotti
Copy link
Member

jviotti commented Jun 25, 2025

I'm not sure I understand the PR. You have deleted the targets that even call ClangTidy, so what is invoking it now? The CI builds also seem to be failing without being able to execute anything. Also a few comments:

  • Can we still be explicit about the configuration file path, even if the compiler is executing ClangTidy now?
  • Would always running ClangTidy slow down i.e. development builds? If so we probably want a way to turn it off to only run it on CI

CMakeLists.txt Outdated
endif()

# Add clang-tidy to the targets. Clang-tidy uses the .clang-tidy file for configuration.
set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_BIN}" "--allow-no-checks")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you instead set this variable inside the clang tidy target?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought of using global scope

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can still affect the global scope from within a function (there are options for that). That way, we keep everything ClangTidy related in a single file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered PARENT_SCOPE but that didn't sound global.
But I can try to encapsulate it.

CMakeLists.txt Outdated
src/*.h src/*.cc
benchmark/*.h benchmark/*.cc
test/*.h test/*.cc)
sourcemeta_find_clang_tidy() # Locates CLANG_TIDY program
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does it know what files to run it against? i.e. can we ignore dependencies like vendor/uriparser?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'll use the source files of the target

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are using .clang-tidy file at the root level to disable the checks outside the src/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how would that work with nested projects. For example, other projects like Hydra will consume Core, and still have their own external libraries that they probably should not lint (like BearSSL). If we override the ClangTidy config in the child, we still have to retain the blacklist from this repo? I'm just concerned about how re-usability and extensibility of the config file will play when other projects consume this. That's why I had a single config file for everything

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering hydra folder structure, core goes vendored along with hydra's external library dependencies.
With this PR's approach, we disable all the clang-tidy checks at root directory and enable the required checks at the src/folder level.

hydra
|_.clang-tidy(-)
|_src/.clang-tidy(performance-
)
|_test
|_vendor--|core/.clang-tidy
|
... |bearssl
|
..
So, having this hierarchy doesn't mess with each other's config.
All the configuration we do for core doesn't have any role in the configuration of hydra.
If we want to have a single configuration, we can consider symlinks.(Re-usability)
This approach gives extensible too by allowing us to control the configuration at the folder level.
Let me know if you see any issues with this approach.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't tell right now. But let's make it work, and then I can attempt i.e. an upgrade on Hydra pointing at the PR and see how it goes

function(sourcemeta_target_clang_tidy)
cmake_parse_arguments(SOURCEMETA_TARGET_CLANG_TIDY "REQUIRED" "" "SOURCES" ${ARGN})

function(sourcemeta_find_clang_tidy)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this not only finds ClangTidy but also enables it? Also if we are not making it a target anymore, we should move this file outside of cmake/common/targets

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not making it a target anymore. We can move it out of targets

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense! Probably under cmake/common

@bavulapati
Copy link
Contributor Author

I'm not sure I understand the PR. You have deleted the targets that even call ClangTidy, so what is invoking it now? The CI builds also seem to be failing without being able to execute anything. Also a few comments:

  • Can we still be explicit about the configuration file path, even if the compiler is executing ClangTidy now?
  • Would always running ClangTidy slow down i.e. development builds? If so we probably want a way to turn it off to only run it on CI

This works at the target level. Clang-tidy checks can be enabled/disabled at the target level.

We can be explicit about config path. But it picks up the .clang-tidy from the current directory and combines the config from the parent directory. So, specifying just the current dir config file might be misleading.

Clang-tidy takes time. So, we can have a variable to disable the clang-tidy check.

@bavulapati
Copy link
Contributor Author

execute anything. Also a few comments:

CI builds are failing because of older version of clang-tidy available to them.
We need to install clang-tidy similar to clang-format.

bavulapati added a commit to bavulapati/core that referenced this pull request Jun 26, 2025
Refs sourcemeta#1754
This makes it use consistent version of clang-tidy on all the platforms.
And also uses fairy latest version of clang-tidy, instead of using old
versions shipped along with apple llvm and similar distros.

Signed-off-by: Balakrishna Avulapati <ba@bavulapati.com>
bavulapati added a commit to bavulapati/core that referenced this pull request Jun 26, 2025
Refs sourcemeta#1754
Inspired by sourcemeta#1739
This makes it use consistent version of clang-tidy on all the platforms.
And also uses fairy latest version of clang-tidy, instead of using old
versions shipped along with apple llvm and similar distros.

Signed-off-by: Balakrishna Avulapati <ba@bavulapati.com>
@bavulapati
Copy link
Contributor Author

I'm not sure I understand the PR. You have deleted the targets that even call ClangTidy, so what is invoking it now? The CI builds also seem to be failing without being able to execute anything. Also a few comments:

  • Can we still be explicit about the configuration file path, even if the compiler is executing ClangTidy now?
  • Would always running ClangTidy slow down i.e. development builds? If so we probably want a way to turn it off to only run it on CI

I'm not sure I understand the PR. You have deleted the targets that even call ClangTidy, so what is invoking it now? The CI builds also seem to be failing without being able to execute anything. Also a few comments:

  • Can we still be explicit about the configuration file path, even if the compiler is executing ClangTidy now?
  • Would always running ClangTidy slow down i.e. development builds? If so we probably want a way to turn it off to only run it on CI

Regarding the slow down, I would propose us to try enabling it by default.
Let's experience the difference and then send a pr to make it optional from our experience.
The builds/targets won't run once they are done and thus it shouldn't make a much of a difference. WDYT?

jviotti pushed a commit that referenced this pull request Jun 26, 2025
Refs #1754

This makes it use consistent version of clang-tidy on all the platforms.
And also uses fairy latest version of clang-tidy, instead of using old
versions shipped along with apple llvm and similar distros.

Signed-off-by: Balakrishna Avulapati <ba@bavulapati.com>
@jviotti
Copy link
Member

jviotti commented Jun 26, 2025

Sounds good. Let's first make it work, and then we evaluate and polish?

jviotti pushed a commit that referenced this pull request Jun 26, 2025
)

Reported with #1754
Refs #1657

Signed-off-by: Balakrishna Avulapati <ba@bavulapati.com>
jviotti pushed a commit that referenced this pull request Jun 26, 2025
Reported with #1754

Signed-off-by: Balakrishna Avulapati <ba@bavulapati.com>
@bavulapati bavulapati force-pushed the modularise-clang-tidy-config branch from 86515ba to a974e5e Compare June 27, 2025 04:50
Read blogs and played around with [CMAKE_CXX_CLANG_TIDY](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_CLANG_TIDY.html).
Also read source code to understand the clang-tidy config of chromium, llvm and other projects.

We can provide complete context including headers to the clang-tidy tool for static analyzer by running it while building the code.
This also makes static analysis first-class citizen of the project, reporting errors before making commits.

For example, `misc-internal-linkage` check was reporting mostly false positives prior to this. With this, it also considered the headers,
where methods/functions were declared constant. Refer sourcemeta#1752

And we should also incrementally add the checks.

Signed-off-by: Balakrishna Avulapati <ba@bavulapati.com>
Signed-off-by: Balakrishna Avulapati <ba@bavulapati.com>
Signed-off-by: Balakrishna Avulapati <ba@bavulapati.com>
Signed-off-by: Balakrishna Avulapati <ba@bavulapati.com>
Signed-off-by: Balakrishna Avulapati <ba@bavulapati.com>
@bavulapati bavulapati force-pushed the modularise-clang-tidy-config branch from 294aaad to 95ef54c Compare June 27, 2025 05:46
Signed-off-by: Balakrishna Avulapati <ba@bavulapati.com>
Signed-off-by: Balakrishna Avulapati <ba@bavulapati.com>
Signed-off-by: Balakrishna Avulapati <ba@bavulapati.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants