简体   繁体   English

clang-tidy:什么可能导致 NOLINT 评论不被尊重?

[英]clang-tidy: what might cause NOLINT comments to not be respected?

I've created a PR for the opensource Traffic Server project.我为开源交通服务器项目创建了一个 PR。 They run clang-tidy as a part of their CI.作为 CI 的一部分,他们运行得很整洁。 My change exposes a new file to clang-tidy which is now being flagged with a use-after-move warning.我的更改向 clang-tidy 公开了一个新文件,该文件现在被标记为移动后使用警告。 Here's the test code, pinned at the commit that my change is based upon:这是测试代码,固定在我的更改所基于的提交处:

https://github.com/apache/trafficserver/blob/415cd8f/tests/tools/plugins/test_cppapi.cc#L115 https://github.com/apache/trafficserver/blob/415cd8f/tests/tools/plugins/test_cppapi.cc#L115

Here's a copy of that code, with a comment added showing where it complains:这是该代码的副本,并添加了一条注释,显示了它抱怨的地方:

void
f()
{
  TestCont::Mutex m(TSMutexCreate());

  TestCont c(m);

  ALWAYS_ASSERT(!!c)
  ALWAYS_ASSERT(c.asTSCont() != nullptr)
  ALWAYS_ASSERT(c.mutex() == m)

  TestCont c2(std::move(c));

  ALWAYS_ASSERT(!!c2)
  ALWAYS_ASSERT(c2.asTSCont() != nullptr)   // <--- Complains here
  ALWAYS_ASSERT(c2.mutex() == m)

  ALWAYS_ASSERT(!c)
  ALWAYS_ASSERT(c.asTSCont() == nullptr)
  ALWAYS_ASSERT(c.mutex() == nullptr)

So the complaint makes sense, c2 is used after a move.所以抱怨是有道理的,c2 是在移动之后使用的。 But in this case, TestCont explicitly supports use after a move by design and the test is intentionally exercising this to make sure its state is as expected.但在这种情况下, TestCont明确支持在设计移动后使用,并且测试有意执行此操作以确保其 state 符合预期。

Thus, this is a situation for which NOLINT and NOLINTNEXTLINE are created.因此,这是创建NOLINTNOLINTNEXTLINE的情况。 So I applied such comments like this (clearly I've added more comments than I should need to out of desperation):所以我应用了这样的评论(显然我添加的评论比我绝望的需要更多):

  // NOLINTNEXTLINE
   ALWAYS_ASSERT(!c) // NOLINT
   // We turn off the clang-tidy warning about this being a use after move
   // because that is the intention of the test. Continuations support use after
   // move.
   // NOLINTNEXTLINE
   const auto cont_after_move = c.asTSCont(); // NOLINT
   ALWAYS_ASSERT(cont_after_move == nullptr)
   // We turn off the clang-tidy warning about this being a use after move
   // because that is the intention of the test. Continuations support use after
   // move.
   // NOLINTNEXTLINE
   const auto mutex_after_move = c.mutex(); // NOLINT
   ALWAYS_ASSERT(mutex_after_move == nullptr)

Notice that I separated c.asTSCont() as a separate call in case things were being confused by the ALWAYS_ASSERT macro.请注意,我将c.asTSCont()分隔为单独的调用,以防被ALWAYS_ASSERT宏混淆。 Yet clang-tidy still complains.然而,clang-tidy 仍然抱怨。 Here's the latest jenkins run output:这是最新的 jenkins 运行 output:

https://ci.trafficserver.apache.org/job/clang-analyzer-github/12245/console https://ci.trafficserver.apache.org/job/clang-analyzer-github/12245/console

tools/plugins/test_cppapi.cc:124:32: warning: Method called on moved-from object 'c'
  const auto cont_after_move = c.asTSCont(); // NOLINT
                               ^~~~~~~~~~~~
tools/plugins/test_cppapi.cc:151:33: warning: Method called on moved-from object 'c2'
  const auto cont2_after_move = c2.asTSCont(); // NOLINT
                                ^~~~~~~~~~~~~
2 warnings generated.

Right there in the warning output is the NOLINT comment.就在警告 output 中是NOLINT注释。 What am I doing wrong?我究竟做错了什么? Why isn't clang-tidy respsecting NOLINT ?为什么 clang-tidy 不尊重NOLINT

The clang-tidy version is 10.0.0. clang-tidy 版本是 10.0.0。 Here's the clang-analyzer report in case it's helpful:以下是 clang-analyzer 报告,以防万一:

https://ci.trafficserver.apache.org/clang-analyzer/github/6945/2020-06-25-081635-12865-1/report-33708c.html#EndPath https://ci.trafficserver.apache.org/clang-analyzer/github/6945/2020-06-25-081635-12865-1/report-33708c.html#EndPath

Here's a copy and paste of the clang-tidy invocation:这是 clang-tidy 调用的复制和粘贴:

clang -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name test_cppapi.cc -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -relaxed-aliasing -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +cx16 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /opt/llvm/lib64/clang/10.0.0 -D HAVE_CONFIG_H -I . -I ../include -D linux -D _LARGEFILE64_SOURCE=1 -D _COMPILE64BIT_SOURCE=1 -D _REENTRANT -D __STDC_LIMIT_MACROS=1 -D __STDC_FORMAT_MACROS=1 -I /var/jenkins/workspace/clang-analyzer-github/src/proxy/api -I /var/jenkins/workspace/clang-analyzer-github/src/proxy/api -I /var/jenkins/workspace/clang-analyzer-github/src/include/cppapi/include -I /var/jenkins/workspace/clang-analyzer-github/src/lib/cppapi/include -I /var/jenkins/workspace/clang-analyzer-github/src/include -I /var/jenkins/workspace/clang-analyzer-github/src/lib -D _GNU_SOURCE -D OPENSSL_NO_SSL_INTERN -I /opt/llvm/include/c++/v1 -D PIC -internal-isystem /opt/llvm/bin/../include/c++/v1 -internal-isystem /usr/local/include -internal-isystem /opt/llvm/lib64/clang/10.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Wno-deprecated-declarations -Wno-ignored-qualifiers -Wno-unused-parameter -Wno-invalid-offsetof -std=c++17 -fdeprecated-macro -fdebug-compilation-dir /var/jenkins/workspace/clang-analyzer-github/src/tests -ferror-limit 19 -fmessage-length 0 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker alpha.unix.cstring.BufferOverlap -analyzer-checker alpha.core.BoolAssignment -analyzer-checker alpha.core.CastSize -analyzer-checker alpha.core.SizeofPtr -analyzer-output=html -faddrsig -o /CA/clang-analyzer/github/6945/2020-06-25-081635-12865-1 -x c++ tools/plugins/test_cppapi.cc

In the end I was confused between clang-tidy and clang-analyzer.最后,我在 clang-tidy 和 clang-analyzer 之间感到困惑。 NOLINT addresses clang-tidy issues, but I had to suppress clang-analyzer. NOLINT 解决了 clang-tidy 问题,但我不得不抑制 clang-analyzer。 I did so using the suggestion here:我这样做是使用这里的建议:

https://clang-analyzer.llvm.org/faq.html#exclude_code https://clang-analyzer.llvm.org/faq.html#exclude_code

The following directives quieted the warnings for me:以下指令为我消除了警告:

#ifndef __clang_analyzer__
// Code not to be analyzed
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM