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

Richard Thomson via All-commits all-commits at lists.llvm.org
Fri Apr 1 14:24:49 PDT 2022


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: f547fc89c0730e0fee220d790957791bdfd23fcd
      https://github.com/llvm/llvm-project/commit/f547fc89c0730e0fee220d790957791bdfd23fcd
  Author: Richard <legalize at xmission.com>
  Date:   2022-04-01 (Fri, 01 Apr 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

[buildbot issues fixed]

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