[PATCH] D97121: [clang-tidy] Add a single fix mode to clang-tidy

Nathan James via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Feb 21 05:39:08 PST 2021


njames93 created this revision.
njames93 added reviewers: aaron.ballman, steveire.
Herald added subscribers: usaxena95, kadircet, arphaman, kbarton, xazax.hun, nemanjai.
njames93 updated this revision to Diff 325234.
njames93 added a comment.
njames93 updated this revision to Diff 325238.
njames93 updated this revision to Diff 325239.
njames93 published this revision for review.
Herald added subscribers: cfe-commits, ilya-biryukov.
Herald added a project: clang.

Fix test


njames93 added a comment.

Disable formatting causing test to fail and document it.


njames93 added a comment.

Remove unintended change.


Adds a flag to ClangTidyContext that is used to indicate to checks that fixes will only be applied one at a time.
This is to indicate to checks that each fix emitted should not depend on any other fixes emitted across the translation unit.
I've currently implemented the IncludeInserter and PreferMemberInitializerCheck to use these support these modes.

Reasoning behind this is use cases like clangd its only possible to apply one fix at a time.
For include inserter checks, the include is only added once for the first diagnostic that requires it, this will result in subsequent fixes not having the included needed.

A similar issue is seen in the PreferMemberInitializerCheck where the `:` will only be added for the first member that needs fixing.

Fixes emitted in SingleFixMode will likely result in malformed code if they are applied all together, conversely fixes currently emitted may result in malformed code if they are applied one at a time.
For this reason invoking clang-tidy from the binary will always with SingleFixMode disabled, However using it as a library its possible to select the mode you wish to use, clangd always selects SingleFixMode.

This is an example of the current behaviour failing

  struct Foo {
    int A, B;
    Foo(int D, int E) {
      A = D;
      B = E; // Fix Here
    }
  };

Incorrectly transformed to:

  struct Foo {
    int A, B;
    Foo(int D, int E), B(E) {
      A = D;
       // Fix Here
    }
  };

In SingleFixMode, it gets transformed to:

  struct Foo {
    int A, B;
    Foo(int D, int E) : B(E) {
      A = D;
       // Fix Here
    }
  };


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D97121

Files:
  clang-tools-extra/clang-tidy/ClangTidyCheck.h
  clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
  clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h
  clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp
  clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
  clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
  clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp
  clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp
  clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
  clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp
  clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.cpp
  clang-tools-extra/clang-tidy/modernize/ReplaceRandomShuffleCheck.cpp
  clang-tools-extra/clang-tidy/performance/MoveConstructorInitCheck.cpp
  clang-tools-extra/clang-tidy/performance/TypePromotionInMathFnCheck.cpp
  clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp
  clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp
  clang-tools-extra/clang-tidy/utils/IncludeInserter.h
  clang-tools-extra/clang-tidy/utils/TransformerClangTidyCheck.cpp
  clang-tools-extra/clangd/ParsedAST.cpp
  clang-tools-extra/unittests/clang-tidy/IncludeInserterTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97121.325239.patch
Type: text/x-patch
Size: 18221 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210221/09db4ee8/attachment-0001.bin>


More information about the cfe-commits mailing list