Visual Studio clang-tidy上的信息不清楚

qv7cva1a  于 2023-10-23  发布在  其他
关注(0)|答案(1)|浏览(190)

我在一个文件夹上运行clang-tidy,它显示

  1. Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
  2. Found compiler error(s).

但它没有显示关于错误细节的附加消息。顺便说一句,日志文件也没有产生。检查错误的方法是什么?

tpgth1q7

tpgth1q71#

留言内容:“使用-header-filter=...”

信息:

  1. Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.

意味着有些调查结果未显示,因为它们位于非主要源文件的文件中,并且与头过滤器设置不匹配。
该消息在ClangTidyMain.cpp:290生成:

  1. if (Stats.ErrorsIgnoredNonUserCode)
  2. llvm::errs() << "Use -header-filter=.* to display errors from all "
  3. "non-system headers. Use -system-headers to display "
  4. "errors from system headers as well.\n";

ErrorsIgnoredNonUserCode计数器在ClangTidyDiagnosticConsumer.cpp:309时递增:

  1. } else if (!LastErrorRelatesToUserCode) {
  2. ++Context.Stats.ErrorsIgnoredNonUserCode;
  3. Errors.pop_back(); // <------- discards the finding

LastErrorRelatesToUserCode标志最初是false,但可以在ClangTidyDiagnosticConsumer.cpp:547中设置为true

  1. LastErrorRelatesToUserCode = LastErrorRelatesToUserCode ||
  2. Sources.isInMainFile(Location) ||
  3. getHeaderFilter()->match(FileName);

因此,如果clang-tidy想要报告某些内容,但由于其位置而选择不报告,则会打印“Use -header-filter=...”消息。

选项:-header-filter

如果您想查看被隐藏的调查结果,请按照消息中的说明进行操作并添加:

  1. -header-filter=.*

clang-tidy命令行。
不过,我建议在.*部分周围加上引号,以确保shell不会扩展它:

  1. -header-filter='.*'

(It通常在没有引号的情况下工作,因为当前目录中通常没有任何文件与它匹配为glob表达式。)
documentation表示:

  1. --header-filter=<string> - Regular expression matching the names of the
  2. headers to output diagnostics from. Diagnostics
  3. from the main file of each translation unit are
  4. always displayed.
  5. Can be used together with -line-filter.
  6. This option overrides the 'HeaderFilterRegex'
  7. option in .clang-tidy file, if any.

请注意,消息中提到了-header-filter(一个前导连字符),文档中提到了--header-filter(两个前导连字符)。这两种选择具有相同的效果。
另外,请注意,clang-tidy默认启用的许多检查不需要-header-filter标志来报告头文件中定义的函数中的问题,因为它们也报告主源文件中的调用位置。(如果主源文件中没有调用站点,它们根本不会报告,即使使用-header-filter=.*也是如此。)

选项:-system-headers

即使使用-header-filter='.*',如果在“system”标头中有查找结果,例如通过-isystem搜索路径选项找到的结果,也会产生“Use -header-filter=...”消息。在这种情况下,你必须 * 也 * 传递-system-headers--system-headers

  1. --system-headers - Display the errors from system headers.
  2. This option overrides the 'SystemHeaders' option
  3. in .clang-tidy file, if any.

头过滤示例

我将使用这些文件演示这些选项:

  1. // psf.cc
  2. #include <sys-header.h>
  3. #include "header.h"
  4. struct PSFClass { PSFClass(int); };
  1. // header.h
  2. struct HeaderClass { HeaderClass(int); };
  1. // mysys/sys-header.h
  2. struct SysHeaderClass { SysHeaderClass(int); };

使用google-explicit-constructor检查运行clang-tidy(版本16.0.0),但仅使用默认头过滤器:

  1. $ clang-tidy -checks='-*,google-explicit-constructor' psf.cc -- -isystem mysys
  2. 3 warnings generated.
  3. <path>/psf.cc:4:19: warning: single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor]
  4. struct PSFClass { PSFClass(int); };
  5. ^
  6. explicit
  7. Suppressed 2 warnings (2 in non-user code).
  8. Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.

添加-header-filter=.*

  1. $ clang-tidy -header-filter='.*' -checks='-*,google-explicit-constructor' psf.cc -- -isystem mysys
  2. 3 warnings generated.
  3. <path>/header.h:2:22: warning: single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor]
  4. struct HeaderClass { HeaderClass(int); };
  5. ^
  6. explicit
  7. <path>/psf.cc:4:19: warning: single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor]
  8. struct PSFClass { PSFClass(int); };
  9. ^
  10. explicit
  11. Suppressed 1 warnings (1 in non-user code).
  12. Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.

添加-system-headers

  1. $ clang-tidy -header-filter='.*' -system-headers -checks='-*,google-explicit-constructor' psf.cc -- -isystem mysys
  2. 3 warnings generated.
  3. <path>/header.h:2:22: warning: single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor]
  4. struct HeaderClass { HeaderClass(int); };
  5. ^
  6. explicit
  7. <path>/psf.cc:4:19: warning: single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor]
  8. struct PSFClass { PSFClass(int); };
  9. ^
  10. explicit
  11. mysys/sys-header.h:2:25: warning: single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor]
  12. struct SysHeaderClass { SysHeaderClass(int); };
  13. ^

异常:在最后的结果中,clang-tidy没有在插入符号(^)下面打印单词explicit。我不知道为什么

留言内容:“发现编译器错误。”

独立于上述,clang-tidy将打印:

  1. Found compiler error(s).

如果解析器在到达运行clang-tidy检查的阶段之前遇到代码中的错误。clang本身也会报告这些错误。
如果您看到此消息,则clang-tidy无法完全理解代码,因此修复这些消息应该是第一要务。基本上,首先让clang干净地编译代码,然后考虑使用clang-tidy

展开查看全部

相关问题