https://github.com/LegalizeAdulthood created 
https://github.com/llvm/llvm-project/pull/244989

Require every observed macro expansion to form a complete expression before 
replacing its cluster with an enum. Reject call argument lists, partial 
expressions, redefined names, and definitions inside declarations.

Track expansions by definition identity and source location, validate nested 
boundaries, and avoid repeated candidate scans. Use translation- unit source 
order to exclude definitions inside declarations.

Add C and C++ regressions for aliases, wrappers, headers, preprocessing 
exclusions, and repeated template instantiations. Check diagnostics, exact 
fixes, and resulting syntax, and document the eligibility rules.

>From fc668ee43464a1a454494edd5d7f79d84a1c01f9 Mon Sep 17 00:00:00 2001
From: Richard <[email protected]>
Date: Sat, 21 Nov 2026 20:22:44 -0700
Subject: [PATCH] [clang-tidy] Validate macro expansions before enum conversion

Require every observed macro expansion to form a complete expression
before replacing its cluster with an enum. Reject call argument lists,
partial expressions, redefined names, and definitions inside declarations.

Track expansions by definition identity and source location, validate
nested boundaries, and avoid repeated candidate scans. Use translation-
unit source order to exclude definitions inside declarations.

Add C and C++ regressions for aliases, wrappers, headers, preprocessing
exclusions, and repeated template instantiations. Check diagnostics,
exact fixes, and resulting syntax, and document the eligibility rules.
---
 .../clang-tidy/modernize/MacroToEnumCheck.cpp | 92 +++++++++++++++++-
 .../checks/modernize/macro-to-enum.md         | 22 ++++-
 .../Inputs/macro-to-enum/expansion-local.h    |  8 ++
 .../macro-to-enum/expansion-redefined.h       |  8 ++
 .../Inputs/macro-to-enum/expansion-uses.h     | 22 +++++
 .../macro-to-enum-expand-headers.cpp          | 42 +++++++++
 .../modernize/macro-to-enum-expand-nested.cpp | 94 +++++++++++++++++++
 .../macro-to-enum-expand-redefined.c          | 67 +++++++++++++
 .../checkers/modernize/macro-to-enum-expand.c | 86 +++++++++++++++++
 .../modernize/macro-to-enum-expand.cpp        | 27 ++++++
 .../checkers/modernize/macro-to-enum.cpp      |  7 +-
 11 files changed, 469 insertions(+), 6 deletions(-)
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/expansion-local.h
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/expansion-redefined.h
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/expansion-uses.h
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-expand-headers.cpp
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-expand-nested.cpp
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-expand-redefined.c
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-expand.c
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-expand.cpp

diff --git a/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
index 2b5b8fe0e07e7b..67fec3c00cb277 100644
--- a/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
@@ -13,6 +13,7 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/Lex/Preprocessor.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/STLExtras.h"
 #include <cassert>
 #include <cctype>
@@ -85,6 +86,11 @@ struct EnumMacro {
   const MacroDirective *Directive;
 };
 
+struct MacroExpansion {
+  const MacroInfo *Info;
+  SourceLocation End;
+};
+
 using MacroList = SmallVector<EnumMacro>;
 
 enum class IncludeGuard { None, FileChanged, IfGuard, DefineGuard };
@@ -174,11 +180,16 @@ class MacroToEnumCallbacks : public PPCallbacks {
   void PragmaDirective(SourceLocation Loc,
                        PragmaIntroducerKind Introducer) override;
 
+  void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
+                    SourceRange Range, const MacroArgs *Args) override;
+
   // After we've seen everything, issue warnings and fix-its.
   void EndOfMainFile() override;
 
   void invalidateRange(SourceRange Range);
 
+  void validateExpansionRange(SourceRange Range);
+
 private:
   void newEnum() {
     if (Enums.empty() || !Enums.back().empty())
@@ -216,6 +227,9 @@ class MacroToEnumCallbacks : public PPCallbacks {
   const LangOptions &LangOpts;
   const SourceManager &SM;
   SmallVector<MacroList> Enums;
+  // Count uses of each definition not yet matched to a complete expression.
+  llvm::DenseMap<const MacroInfo *, unsigned> UnmatchedExpansions;
+  llvm::DenseMap<SourceLocation, MacroExpansion> Expansions;
   SmallVector<FileState> Files;
   std::vector<std::string> ExpressionNames;
   FileState *CurrentFile = nullptr;
@@ -325,6 +339,10 @@ void MacroToEnumCallbacks::MacroDefined(const Token 
&MacroNameTok,
     return;
   }
 
+  // An earlier definition would still expand the generated enumerator name.
+  if (MD->getPrevious())
+    checkName(MacroNameTok);
+
   if (insideConditional())
     return;
 
@@ -346,6 +364,7 @@ void MacroToEnumCallbacks::MacroDefined(const Token 
&MacroNameTok,
   if (!isConsecutiveMacro(MD))
     newEnum();
   Enums.back().emplace_back(MacroNameTok, MD);
+  UnmatchedExpansions.try_emplace(Info, 0);
   rememberLastMacroLocation(MD);
 }
 
@@ -416,6 +435,25 @@ void MacroToEnumCallbacks::PragmaDirective(SourceLocation 
Loc,
     CurrentFile->GuardScanner = IncludeGuard::IfGuard;
 }
 
+void MacroToEnumCallbacks::MacroExpands(const Token &MacroNameTok,
+                                        const MacroDefinition &MD,
+                                        SourceRange Range,
+                                        const MacroArgs *Args) {
+  // Ignore function-like macro expansions.
+  if (Args != nullptr)
+    return;
+
+  const MacroInfo *Info = MD.getMacroInfo();
+  auto Candidate = UnmatchedExpansions.find(Info);
+  if (Candidate == UnmatchedExpansions.end())
+    return;
+
+  if (Expansions
+          .try_emplace(Range.getBegin(), MacroExpansion{Info, Range.getEnd()})
+          .second)
+    ++Candidate->second;
+}
+
 void MacroToEnumCallbacks::invalidateExpressionNames() {
   for (const std::string &Id : ExpressionNames) {
     llvm::erase_if(Enums, [Id](const MacroList &MacroList) {
@@ -428,18 +466,60 @@ void MacroToEnumCallbacks::invalidateExpressionNames() {
 
 void MacroToEnumCallbacks::EndOfMainFile() {
   invalidateExpressionNames();
+  llvm::erase_if(Enums, [this](const MacroList &Macros) {
+    return llvm::any_of(Macros, [this](const EnumMacro &Macro) {
+      return UnmatchedExpansions.lookup(Macro.Directive->getMacroInfo()) != 0;
+    });
+  });
   issueDiagnostics();
 }
 
 void MacroToEnumCallbacks::invalidateRange(SourceRange Range) {
-  llvm::erase_if(Enums, [Range](const MacroList &MacroList) {
-    return llvm::any_of(MacroList, [Range](const EnumMacro &Macro) {
-      return Macro.Directive->getLocation() >= Range.getBegin() &&
-             Macro.Directive->getLocation() <= Range.getEnd();
+  // A declaration can end in a macro expansion. Compare file locations so it
+  // does not also invalidate unrelated macros defined after the declaration.
+  Range = SM.getExpansionRange(Range).getAsRange();
+  llvm::erase_if(Enums, [this, Range](const MacroList &MacroList) {
+    return llvm::any_of(MacroList, [this, Range](const EnumMacro &Macro) {
+      return SM.isPointWithin(Macro.Directive->getLocation(), Range.getBegin(),
+                              Range.getEnd());
     });
   });
 }
 
+void MacroToEnumCallbacks::validateExpansionRange(SourceRange Range) {
+  SourceLocation Begin = Range.getBegin();
+  SourceLocation End = Range.getEnd();
+  while (Begin.isMacroID() && End.isMacroID() &&
+         SM.getFileID(Begin) == SM.getFileID(End)) {
+    // Validate each complete expansion separately: an inner expression can be
+    // complete without consuming its enclosing object-like macro. Arguments to
+    // function-like macros remain excluded.
+    SourceLocation ExpansionBegin, ExpansionEnd;
+    if (SM.isMacroArgExpansion(Begin) ||
+        !SM.isAtStartOfImmediateMacroExpansion(Begin, &ExpansionBegin))
+      return;
+
+    const unsigned Length =
+        Lexer::MeasureTokenLength(SM.getSpellingLoc(End), SM, LangOpts);
+    const SourceLocation TokenEnd = End.getLocWithOffset(Length);
+    if (Length == 0 || !SM.isInFileID(TokenEnd, SM.getFileID(End)) ||
+        !SM.isAtEndOfImmediateMacroExpansion(TokenEnd, &ExpansionEnd))
+      return;
+
+    auto Expansion = Expansions.find(ExpansionBegin);
+    if (Expansion != Expansions.end() &&
+        Expansion->second.End == ExpansionEnd) {
+      auto Candidate = UnmatchedExpansions.find(Expansion->second.Info);
+      assert(Candidate != UnmatchedExpansions.end() && Candidate->second != 0);
+      --Candidate->second;
+      // A template instantiation can revisit this expression's source range.
+      Expansions.erase(Expansion);
+    }
+    Begin = ExpansionBegin;
+    End = ExpansionEnd;
+  }
+}
+
 void MacroToEnumCallbacks::issueDiagnostics() {
   for (const MacroList &MacroList : Enums) {
     if (MacroList.empty())
@@ -515,6 +595,7 @@ void 
MacroToEnumCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
   using namespace ast_matchers;
   const auto TopLevelDecl = hasParent(translationUnitDecl());
   Finder->addMatcher(decl(TopLevelDecl).bind("top"), this);
+  Finder->addMatcher(expr().bind("expr"), this);
 }
 
 static bool isValid(SourceRange Range) {
@@ -527,6 +608,9 @@ static bool empty(SourceRange Range) {
 
 void MacroToEnumCheck::check(
     const ast_matchers::MatchFinder::MatchResult &Result) {
+  if (const auto *E = Result.Nodes.getNodeAs<Expr>("expr"))
+    PPCallback->validateExpansionRange(E->getSourceRange());
+
   auto *TLDecl = Result.Nodes.getNodeAs<Decl>("top");
   if (TLDecl == nullptr)
     return;
diff --git 
a/clang-tools-extra/docs/clang-tidy/checks/modernize/macro-to-enum.md 
b/clang-tools-extra/docs/clang-tidy/checks/modernize/macro-to-enum.md
index 35a3a5e9f558d9..16c90c5fd393b0 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/modernize/macro-to-enum.md
+++ b/clang-tools-extra/docs/clang-tidy/checks/modernize/macro-to-enum.md
@@ -23,6 +23,9 @@ Potential macros for replacement must meet the following 
constraints:
   Parenthesized expressions are also recognized. This recognizes
   most valid expressions. In particular, expressions with the
   `sizeof` operator are not recognized.
+- Every observed expansion in the translation unit must form a complete
+  expression. Macros supplying call parentheses or argument lists are
+  excluded, even if their replacement tokens are otherwise eligible.
 - 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.
@@ -31,10 +34,27 @@ Potential macros for replacement must meet the following 
constraints:
 - Macros must not be defined adjacent to other preprocessor directives.
 - Macros must not be used in any conditional preprocessing directive.
 - Macros must not be used as arguments to other macros.
-- Macros must not be undefined.
+- Macros must not be referenced in the body of a function-like macro.
+- Macros must not be undefined or redefined.
 - Macros must be defined at the top-level, not inside any declaration or
   definition.
 
+For example, this macro remains unchanged:
+
+```c++
+#define CALL_ARGS (1, 2)
+
+void consume(int, int);
+void example() {
+  consume CALL_ARGS;
+}
+```
+
+Here, `CALL_ARGS` supplies the function call's argument list. Each argument
+is an expression, but the complete expansion is not. Replacing the macro
+with an enumerator would remove the call parentheses. In C++, the same
+replacement text used as a complete comma expression can still be eligible.
+
 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
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/expansion-local.h
 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/expansion-local.h
new file mode 100644
index 00000000000000..12cf12baf06263
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/expansion-local.h
@@ -0,0 +1,8 @@
+#ifndef EXPANSION_LOCAL_H
+#define EXPANSION_LOCAL_H
+
+#define HEADER_LOCAL 41
+// CHECK-MESSAGES-NOT: warning:
+// CHECK-FIXES: #define HEADER_LOCAL 41
+
+#endif
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/expansion-redefined.h
 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/expansion-redefined.h
new file mode 100644
index 00000000000000..cf6f0434e43406
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/expansion-redefined.h
@@ -0,0 +1,8 @@
+#ifndef EXPANSION_REDEFINED_H
+#define EXPANSION_REDEFINED_H
+
+#define REDEFINED_HEADER (22)
+// CHECK-MESSAGES-NOT: warning:
+// CHECK-FIXES: #define REDEFINED_HEADER (22)
+
+#endif
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/expansion-uses.h
 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/expansion-uses.h
new file mode 100644
index 00000000000000..60704b4e0e53e2
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/expansion-uses.h
@@ -0,0 +1,22 @@
+#ifndef EXPANSION_USES_H
+#define EXPANSION_USES_H
+
+#define HEADER_SAFE (13)
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: replace macro with enum
+// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: macro 'HEADER_SAFE' defines an 
integral constant; prefer an enum instead
+// CHECK-FIXES: enum {
+// CHECK-FIXES-NEXT: HEADER_SAFE = (13)
+// CHECK-FIXES-NEXT: };
+
+#define HEADER_BAD (14)
+// CHECK-FIXES: #define HEADER_BAD (14)
+
+#define HEADER_LATE_BAD (15)
+// CHECK-FIXES: #define HEADER_LATE_BAD (15)
+
+void header_uses() {
+  use_one(HEADER_SAFE);
+  use_one HEADER_BAD;
+}
+
+#endif
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-expand-headers.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-expand-headers.cpp
new file mode 100644
index 00000000000000..2881a97b747987
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-expand-headers.cpp
@@ -0,0 +1,42 @@
+// RUN: %check_clang_tidy -std=c++14-or-later \
+// RUN:   -check-header %S/Inputs/macro-to-enum/expansion-uses.h \
+// RUN:   -check-header %S/Inputs/macro-to-enum/expansion-local.h \
+// RUN:   -check-header %S/Inputs/macro-to-enum/expansion-redefined.h \
+// RUN:   %s modernize-macro-to-enum %t -- \
+// RUN:   -- -I%S/Inputs/macro-to-enum -Wno-macro-redefined 
-fno-delayed-template-parsing
+// RUN: %clang -std=c++14 -Wno-macro-redefined -fsyntax-only -I%t.cpp.headers 
%t.cpp
+
+void use_one(int);
+
+#include "expansion-uses.h"
+#include "expansion-uses.h"
+
+// The definition is in a header, but its scope is the enclosing function.
+void local_header_definition() {
+#include "expansion-local.h"
+}
+
+int LocalUse = HEADER_LOCAL;
+
+void old_header_definition() {
+#define REDEFINED_HEADER (21)
+  // CHECK-FIXES: #define REDEFINED_HEADER (21)
+}
+
+#include "expansion-redefined.h"
+
+// Keep a positive control so source diagnostics are checked as well.
+#define SOURCE_SAFE (23)
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: replace macro with enum
+// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: macro 'SOURCE_SAFE' defines an 
integral constant; prefer an enum instead
+// CHECK-FIXES: enum {
+// CHECK-FIXES-NEXT: SOURCE_SAFE = (23)
+// CHECK-FIXES-NEXT: };
+
+void including_file_uses() {
+  use_one(SOURCE_SAFE);
+  use_one(HEADER_SAFE);
+  use_one(HEADER_SAFE);
+  use_one HEADER_LATE_BAD;
+  use_one REDEFINED_HEADER;
+}
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-expand-nested.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-expand-nested.cpp
new file mode 100644
index 00000000000000..d9b5cd97d25059
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-expand-nested.cpp
@@ -0,0 +1,94 @@
+// RUN: %check_clang_tidy -std=c++14-or-later %s modernize-macro-to-enum %t -- 
-- -Wno-unused-value -fno-delayed-template-parsing
+// RUN: %clang -std=c++14 -Wno-unused-value -fno-delayed-template-parsing 
-fsyntax-only %t.cpp
+
+void use_one(int);
+void use_two(int, int);
+
+// Validate the defining macro through several object-like expansions.
+#define NESTED_VALUE 12345
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: replace macro with enum
+// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: macro 'NESTED_VALUE' defines an 
integral constant; prefer an enum instead
+// CHECK-FIXES: enum {
+// CHECK-FIXES-NEXT: NESTED_VALUE = 12345
+// CHECK-FIXES-NEXT: };
+#define VALUE_ALIAS NESTED_VALUE
+#define SECOND_ALIAS VALUE_ALIAS
+
+int Nested = SECOND_ALIAS;
+
+// The inner expression need not consume its enclosing macro expansion.
+#define NESTED_TERM (12 + 34)
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: replace macro with enum
+// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: macro 'NESTED_TERM' defines an 
integral constant; prefer an enum instead
+// CHECK-FIXES: enum {
+// CHECK-FIXES-NEXT: NESTED_TERM = (12 + 34)
+// CHECK-FIXES-NEXT: };
+#define TERM_ALIAS 3 * NESTED_TERM
+
+int Term = TERM_ALIAS;
+
+// A containing expression must not validate a partial inner expansion.
+#define PARTIAL_VALUE 1 + 2
+// CHECK-FIXES: #define PARTIAL_VALUE 1 + 2
+#define PARTIAL_ALIAS 3 * PARTIAL_VALUE
+
+int Partial = PARTIAL_ALIAS;
+
+// One safe nested use cannot validate another invocation used as syntax.
+#define MIXED_ARGS (1, 2)
+// CHECK-FIXES: #define MIXED_ARGS (1, 2)
+#define ARGS_ALIAS MIXED_ARGS
+
+int Mixed = ARGS_ALIAS;
+void nested_call() {
+  use_two ARGS_ALIAS;
+}
+
+// Preserve exclusions for function-like macro bodies and macro arguments.
+#define BODY_VALUE 20
+// CHECK-FIXES: #define BODY_VALUE 20
+#define BODY_WRAPPER() BODY_VALUE
+
+int Body = BODY_WRAPPER();
+
+#define ARG_VALUE 21
+// CHECK-FIXES: #define ARG_VALUE 21
+#define IDENTITY(x) x
+
+int Argument = IDENTITY(ARG_VALUE);
+
+#define PASTE_VALUE 22
+// CHECK-FIXES: #define PASTE_VALUE 22
+#define PASTE_IMPL(a, b) a##b
+#define PASTE(a, b) PASTE_IMPL(a, b)
+
+int pasted22;
+int *Pasted = &PASTE(pasted, PASTE_VALUE);
+
+// Instantiations revisit source locations already seen in the template body.
+#define TEMPLATE_VALUE (31)
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: replace macro with enum
+// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: macro 'TEMPLATE_VALUE' defines an 
integral constant; prefer an enum instead
+// CHECK-FIXES: enum {
+// CHECK-FIXES-NEXT: TEMPLATE_VALUE = (31)
+// CHECK-FIXES-NEXT: };
+
+template <typename T> int safe_template(T) {
+  use_one(TEMPLATE_VALUE);
+  return TEMPLATE_VALUE;
+}
+
+int First = safe_template(0);
+int Second = safe_template(0L);
+
+#define TEMPLATE_ARGS (32)
+// CHECK-FIXES: #define TEMPLATE_ARGS (32)
+
+template <typename T> void unsafe_template(T) {
+  use_one TEMPLATE_ARGS;
+}
+
+void instantiate() {
+  unsafe_template(0);
+  unsafe_template(0L);
+}
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-expand-redefined.c
 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-expand-redefined.c
new file mode 100644
index 00000000000000..93e877c3b8a10a
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-expand-redefined.c
@@ -0,0 +1,67 @@
+// RUN: %check_clang_tidy -std=c11-or-later %s modernize-macro-to-enum %t-c -- 
-- -Wno-macro-redefined
+// RUN: %clang -std=c11 -Wno-macro-redefined -fsyntax-only %t-c.c
+// RUN: %check_clang_tidy -std=c++14-or-later -assume-filename=redefined.cpp 
%s modernize-macro-to-enum %t-cxx -- -- -Wno-macro-redefined
+// RUN: %clang -std=c++14 -Wno-macro-redefined -fsyntax-only %t-cxx.cpp
+
+void use_one(int);
+
+// A new definition's unsafe use must not attach to an older, excluded one.
+void old_definition(void) {
+#define REDEFINED (1)
+  // CHECK-FIXES: #define REDEFINED (1)
+}
+
+#define REDEFINED (2)
+// CHECK-FIXES: #define REDEFINED (2)
+
+void redefined_use(void) {
+  use_one REDEFINED;
+}
+
+// Replacing the second definition would leave the first macro active at the
+// enumerator declaration, even though all uses are complete expressions.
+#define REDEFINED_LITERAL unknown_identifier
+// CHECK-FIXES: #define REDEFINED_LITERAL unknown_identifier
+#define REDEFINED_LITERAL 3
+// CHECK-FIXES: #define REDEFINED_LITERAL 3
+
+int Literal = REDEFINED_LITERAL;
+
+#define CONDITIONAL_REDEF (4)
+// CHECK-FIXES: #define CONDITIONAL_REDEF (4)
+
+int BeforeConditional = CONDITIONAL_REDEF;
+#if 1
+#define CONDITIONAL_REDEF (5)
+// CHECK-FIXES: #define CONDITIONAL_REDEF (5)
+#endif
+int AfterConditional = CONDITIONAL_REDEF;
+
+// Preserve the existing exclusion of all definitions of an undefined name.
+#define UNDEFINED (6)
+// CHECK-FIXES: #define UNDEFINED (6)
+
+int BeforeUndef = UNDEFINED;
+#undef UNDEFINED
+#define UNDEFINED (7)
+// CHECK-FIXES: #define UNDEFINED (7)
+
+int AfterUndef = UNDEFINED;
+
+// Redefined function-like macros still exclude names in their new bodies.
+#define WRAPPER() 0
+#define WRAPPED_VALUE 8
+// CHECK-FIXES: #define WRAPPED_VALUE 8
+#define WRAPPER() WRAPPED_VALUE
+
+int Wrapped = WRAPPER();
+
+// Unrelated definitions still convert.
+#define INDEPENDENT 70
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: replace macro with enum
+// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: macro 'INDEPENDENT' defines an 
integral constant; prefer an enum instead
+// CHECK-FIXES: enum {
+// CHECK-FIXES-NEXT: INDEPENDENT = 70
+// CHECK-FIXES-NEXT: };
+
+int Independent = INDEPENDENT;
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-expand.c 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-expand.c
new file mode 100644
index 00000000000000..49ffe7f6f99586
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-expand.c
@@ -0,0 +1,86 @@
+// RUN: %check_clang_tidy -std=c11-or-later %s modernize-macro-to-enum %t-c
+// RUN: %clang -std=c11 -fsyntax-only %t-c.c
+// RUN: %check_clang_tidy -std=c++14-or-later -assume-filename=expand.cpp %s 
modernize-macro-to-enum %t-cxx
+// RUN: %clang -std=c++14 -fsyntax-only %t-cxx.cpp
+
+void use_one(int);
+
+// A macro supplying call parentheses is not an expression. Reject its entire
+// cluster, including a safely used macro and an unused macro.
+#define CLUSTER_SAFE 9
+#define CALL_ARGS (1)
+#define CLUSTER_UNUSED 2
+// CHECK-FIXES: #define CLUSTER_SAFE 9
+// CHECK-FIXES-NEXT: #define CALL_ARGS (1)
+// CHECK-FIXES-NEXT: #define CLUSTER_UNUSED 2
+
+int ClusterValue = CLUSTER_SAFE;
+void call_args(void) {
+  use_one CALL_ARGS;
+}
+
+// A safe use cannot validate a different, unsafe use of the same macro.
+#define SAFE_THEN_UNSAFE (3)
+// CHECK-FIXES: #define SAFE_THEN_UNSAFE (3)
+
+int SafeFirst = SAFE_THEN_UNSAFE;
+void safe_then_unsafe(void) {
+  use_one SAFE_THEN_UNSAFE;
+}
+
+#define UNSAFE_THEN_SAFE (4)
+// CHECK-FIXES: #define UNSAFE_THEN_SAFE (4)
+
+void unsafe_then_safe(void) {
+  use_one UNSAFE_THEN_SAFE;
+}
+int SafeLast = UNSAFE_THEN_SAFE;
+
+// Replacing either fragment with an enumerator would change parsing.
+#define UNGROUPED 1 + 2
+// CHECK-FIXES: #define UNGROUPED 1 + 2
+
+int Product = 3 * UNGROUPED;
+
+#define SUBTRACTION -1
+// CHECK-FIXES: #define SUBTRACTION -1
+
+int Difference = 3 SUBTRACTION;
+
+// Independent safe clusters still convert, including repeated expansions.
+#define SAFE_LITERAL 1
+#define SAFE_UNARY -2
+#define SAFE_BINARY 1 + 2
+#define SAFE_PARENS ((4))
+// CHECK-MESSAGES: :[[@LINE-4]]:1: warning: replace macro with enum
+// CHECK-MESSAGES: :[[@LINE-5]]:9: warning: macro 'SAFE_LITERAL' defines an 
integral constant; prefer an enum instead
+// CHECK-MESSAGES: :[[@LINE-5]]:9: warning: macro 'SAFE_UNARY' defines an 
integral constant; prefer an enum instead
+// CHECK-MESSAGES: :[[@LINE-5]]:9: warning: macro 'SAFE_BINARY' defines an 
integral constant; prefer an enum instead
+// CHECK-MESSAGES: :[[@LINE-5]]:9: warning: macro 'SAFE_PARENS' defines an 
integral constant; prefer an enum instead
+// CHECK-FIXES: enum {
+// CHECK-FIXES-NEXT: SAFE_LITERAL = 1,
+// CHECK-FIXES-NEXT: SAFE_UNARY = -2,
+// CHECK-FIXES-NEXT: SAFE_BINARY = 1 + 2,
+// CHECK-FIXES-NEXT: SAFE_PARENS = ((4))
+// CHECK-FIXES-NEXT: };
+
+int Literal = SAFE_LITERAL;
+int Unary = SAFE_UNARY;
+int Binary = SAFE_BINARY;
+int Parens = SAFE_PARENS;
+
+void safe_uses(void) {
+  use_one(SAFE_LITERAL);
+  use_one(SAFE_LITERAL);
+  use_one(SAFE_UNARY);
+  use_one(SAFE_BINARY);
+  use_one(SAFE_PARENS);
+}
+
+// No observed expansions is not evidence of an unsafe use.
+#define UNUSED 7
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: replace macro with enum
+// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: macro 'UNUSED' defines an integral 
constant; prefer an enum instead
+// CHECK-FIXES: enum {
+// CHECK-FIXES-NEXT: UNUSED = 7
+// CHECK-FIXES-NEXT: };
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-expand.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-expand.cpp
new file mode 100644
index 00000000000000..fdd4efb04e226e
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-expand.cpp
@@ -0,0 +1,27 @@
+// RUN: %check_clang_tidy -std=c++14-or-later %s modernize-macro-to-enum %t -- 
-- -Wno-unused-value
+// RUN: %clang -std=c++14 -Wno-unused-value -fsyntax-only %t.cpp
+
+void use_two(int, int);
+
+// The arguments are expressions, but the complete expansion is an argument
+// list. Neither a partial argument nor the enclosing call validates it.
+#define CALL_ARGS (1, 2)
+// CHECK-FIXES: #define CALL_ARGS (1, 2)
+
+void call_args() {
+  use_two CALL_ARGS;
+}
+
+// Identical replacement tokens can instead supply a complete comma expression.
+#define COMMA_EXPR (1, 2)
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: replace macro with enum
+// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: macro 'COMMA_EXPR' defines an 
integral constant; prefer an enum instead
+// CHECK-FIXES: enum {
+// CHECK-FIXES-NEXT: COMMA_EXPR = (1, 2)
+// CHECK-FIXES-NEXT: };
+
+int Comma = COMMA_EXPR;
+
+void comma_uses() {
+  use_two(COMMA_EXPR, COMMA_EXPR);
+}
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum.cpp
index e2df29d2d163e6..ced22e532b09e5 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum.cpp
@@ -1,4 +1,9 @@
-// RUN: %check_clang_tidy -std=c++14-or-later %s modernize-macro-to-enum %t -- 
-- -I%S/Inputs/macro-to-enum -fno-delayed-template-parsing
+// RUN: %check_clang_tidy -std=c++14-or-later \
+// RUN:   -check-header %S/Inputs/macro-to-enum/modernize-macro-to-enum.h \
+// RUN:   -check-header %S/Inputs/macro-to-enum/modernize-macro-to-enum2.h \
+// RUN:   -check-header %S/Inputs/macro-to-enum/modernize-macro-to-enum3.h \
+// RUN:   %s modernize-macro-to-enum %t -- \
+// RUN:   -- -I%S/Inputs/macro-to-enum -fno-delayed-template-parsing
 // C++14 or later required for binary literals.
 
 #if 1

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to