[all-commits] [llvm/llvm-project] 39b80c: [clang-tidy] Add modernize-macro-to-enum check

Richard Thomson via All-commits all-commits at lists.llvm.org
Fri Mar 25 08:46:49 PDT 2022


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 39b80c8380c86539de391600efaa17184b5a52b4
      https://github.com/llvm/llvm-project/commit/39b80c8380c86539de391600efaa17184b5a52b4
  Author: Richard <legalize at xmission.com>
  Date:   2022-03-25 (Fri, 25 Mar 2022)

  Changed paths:
    M clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
    A clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
    A clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.h
    M clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
    M clang-tools-extra/docs/ReleaseNotes.rst
    A clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-macro-to-enum.rst
    M clang-tools-extra/docs/clang-tidy/checks/list.rst
    A clang-tools-extra/docs/clang-tidy/checks/modernize-macro-to-enum.rst
    A clang-tools-extra/test/clang-tidy/checkers/Inputs/modernize-macro-to-enum/modernize-macro-to-enum.h
    A clang-tools-extra/test/clang-tidy/checkers/Inputs/modernize-macro-to-enum/modernize-macro-to-enum2.h
    A clang-tools-extra/test/clang-tidy/checkers/Inputs/modernize-macro-to-enum/modernize-macro-to-enum3.h
    A clang-tools-extra/test/clang-tidy/checkers/modernize-macro-to-enum.cpp

  Log Message:
  -----------
  [clang-tidy] Add modernize-macro-to-enum check

This check performs basic analysis of macros and replaces them
with an anonymous unscoped enum.  Using an unscoped anonymous enum
ensures that everywhere the macro token was used previously, the
enumerator name may be safely used.

Potential macros for replacement must meet the following constraints:
- Macros must expand only to integral literal tokens.  The unary
  operators plus, minus and tilde are recognized to allow for positive,
  negative and bitwise negated integers.
- Macros must be defined on sequential source file lines, or with
  only comment lines in between macro definitions.
- Macros must all be defined in the same source file.
- Macros must not be defined within a conditional compilation block.
- Macros must not be defined adjacent to other preprocessor directives.
- Macros must not be used in preprocessor conditions

Each cluster of macros meeting the above constraints is presumed to
be a set of values suitable for replacement by an anonymous enum.
>From there, a developer can give the anonymous enum a name and
continue refactoring to a scoped enum if desired.  Comments on the
same line as a macro definition or between subsequent macro definitions
are preserved in the output.  No formatting is assumed in the provided
replacements.

The check cppcoreguidelines-macro-to-enum is an alias for this check.

Fixes #27408

Differential Revision: https://reviews.llvm.org/D117522




More information about the All-commits mailing list