如何在调用clang-tidy时让Visual Studio使用.clang-tidy配置文件?

huangapple go评论187阅读模式
英文:

How to get Visual Studio use a .clang-tidy configuration file when invoking clang-tidy?

问题

Visual Studio (Desktop)已经集成了clang-tidy。你可以让VS调用clang-tidy来运行一系列检查,但是我找不到一种方法来使用现有的.clang-tidy配置文件。

文档 暗示了这是可能的:

Clang-Tidy配置 默认情况下,启用Clang-Tidy时不会设置任何检查。要查看命令行版本中的检查列表,请在开发人员命令提示符中运行clang-tidy -list-checks。您可以在Visual Studio内配置Clang-Tidy运行的检查。在项目属性页对话框中,打开“配置属性” > “代码分析” > “Clang-Tidy”页面。在“Clang-Tidy检查”属性中输入要运行的检查。一个不错的默认集合是clang-analyzer-*。此属性值将提供给工具的--checks参数。任何进一步的配置都可以包含在自定义的.clang-tidy文件中。有关更多信息,请参阅LLVM.org上的Clang-Tidy文档。

这是我尝试在VS属性页上手动完成的操作:

如何在调用clang-tidy时让Visual Studio使用.clang-tidy配置文件?

但是在对文件进行分析时,它不起作用。

那么,如何让Visual Studio在调用clang-tidy时使用.clang-tidy配置文件呢?

英文:

Visual Studio (Desktop) has a clang-tidy integration. you can make VS invoke clang-tidy with a list of checks.
however, I could not find a way to make it use an existing .clang-tidy configuration file.

The documentation hints that it is possible:

> Clang-Tidy configuration By default, Clang-Tidy does not set any
> checks when enabled. To see the list of checks in the command-line
> version, run clang-tidy -list-checks in a developer command prompt.
> You can configure the checks that Clang-Tidy runs inside Visual
> Studio. In the project Property Pages dialog, open the Configuration
> Properties > Code Analysis > Clang-Tidy page. Enter checks to run in
> the Clang-Tidy Checks property. A good default set is
> clang-analyzer-*. This property value is provided to the --checks
> argument of the tool. Any further configuration can be included in
> custom .clang-tidy files. For more information, see the Clang-Tidy
> documentation on LLVM.org.

This is what I tried to do manually via the VS property pages:

如何在调用clang-tidy时让Visual Studio使用.clang-tidy配置文件?

But when running analyses on a file, it doesn't work.

So How to get Visual Studio use a .clang-tidy configuration file when invoking clang-tidy?

答案1

得分: 1

属性页用于直接设置clang-tidy检查项(而不是指向.clang-tidy文件的路径)。只要.clang-tidy文件位于您的工作区中的相同文件夹或父文件夹中,Visual Studio应该会自动检测到它,前提是您的源文件也在这些文件夹中。

英文:

The property pages are for setting clang-tidy checks directly (not a path to the .clang-tidy file). Visual Studio should automatically detect the .clang-tidy file in your workspace, as long as it's in the same or a parent folder of your source files.

答案2

得分: 1

代码部分不需要翻译,以下是已翻译的内容:

It seems Visual Studio may invoke clang-tidy from a directory outside the source tree in cases where the build is generated out-of-source (as commonly happens when using CMake).

我看到 Visual Studio 在生成项目时(通常在使用 CMake 时)可能会从源代码树之外的目录调用 clang-tidy。

I found a small hack around it. Basically I trick Visual Studio into thinking I am giving it a list of checks, where in fact I give it the path to the config file along with whatever arguments I want. (note the extra " characters).

我找到了一个小技巧来绕过这个问题。基本上,我欺骗 Visual Studio,让它以为我正在提供一个检查列表,实际上我提供了配置文件的路径以及我想要的任何参数(请注意额外的 " 字符)。

This is of course a hack. But still might serve somebody.

这当然是一种技巧。但仍然可能对某些人有用。

Here is a way to do that with CMake:

以下是使用 CMake 实现这一点的方法:

set_target_properties(MyTarget PROPERTIES
VS_GLOBAL_RunCodeAnalysis false

# Use visual studio core guidelines
VS_GLOBAL_EnableMicrosoftCodeAnalysis false
#VS_GLOBAL_CodeAnalysisRuleSet ${CMAKE_CURRENT_SOURCE_DIR}/foo.ruleset
#VS_GLOBAL_CodeAnalysisRuleSet ${CMAKE_CURRENT_SOURCE_DIR}/foo.ruleset

# Use clangtidy
VS_GLOBAL_EnableClangTidyCodeAnalysis true
VS_GLOBAL_ClangTidyChecks "-* \"\"--config-file=${MY_CLANG_TIDY_CONFIG_PATH} --header-filter=.*" 

)

英文:

It seems Visual Studio may invoke clang-tidy from a directory outside the source tree in cases where the build is generated out-of-source (as commonly happens when using CMake).

I found a small hack around it. Basically I trick Visual Studio into thinking I am giving it a list of checks, where in fact I give it the path to the config file along with whatever arguments I want. (note the extra " characters).

如何在调用clang-tidy时让Visual Studio使用.clang-tidy配置文件?

This is of course a hack. But still might serve somebody.

Here is a way to do that with CMake:

set_target_properties(MyTarget PROPERTIES
    VS_GLOBAL_RunCodeAnalysis false

    # Use visual studio core guidelines
    VS_GLOBAL_EnableMicrosoftCodeAnalysis false
    #VS_GLOBAL_CodeAnalysisRuleSet ${CMAKE_CURRENT_SOURCE_DIR}/foo.ruleset
    #VS_GLOBAL_CodeAnalysisRuleSet ${CMAKE_CURRENT_SOURCE_DIR}/foo.ruleset

    # Use clangtidy
    VS_GLOBAL_EnableClangTidyCodeAnalysis true
    VS_GLOBAL_ClangTidyChecks "-* \"\"--config-file=${MY_CLANG_TIDY_CONFIG_PATH} --header-filter=.*" 
)

huangapple
  • 本文由 发表于 2023年2月19日 05:17:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定