https://github.com/zeyi2 created 
https://github.com/llvm/llvm-project/pull/202054

None

>From 1c93bf1dd19508890252f4d9ecd86b999397a87f Mon Sep 17 00:00:00 2001
From: Zeyi Xu <[email protected]>
Date: Sat, 6 Jun 2026 23:47:15 +0800
Subject: [PATCH] [clang-tidy] Preserve line endings in macro-to-enum fixes

---
 .../clang-tidy/modernize/MacroToEnumCheck.cpp         | 11 +++++++++--
 clang-tools-extra/docs/ReleaseNotes.rst               |  4 ++++
 clang-tools-extra/test/.gitattributes                 |  2 ++
 .../checkers/modernize/Inputs/macro-to-enum/crlf.cpp  |  2 ++
 .../modernize/Inputs/macro-to-enum/crlf.cpp.expected  |  4 ++++
 .../checkers/modernize/macro-to-enum-crlf.cpp         |  4 ++++
 6 files changed, 25 insertions(+), 2 deletions(-)
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/crlf.cpp
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/crlf.cpp.expected
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-crlf.cpp

diff --git a/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
index 1c49c32f9fecb..1036d08883195 100644
--- a/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
@@ -470,11 +470,18 @@ void MacroToEnumCallbacks::warnMacroEnum(const EnumMacro 
&Macro) const {
 void MacroToEnumCallbacks::fixEnumMacro(const MacroList &MacroList) const {
   SourceLocation Begin =
       MacroList.front().Directive->getMacroInfo()->getDefinitionLoc();
+  const StringRef LineEnding =
+      SM.getBufferData(SM.getFileID(Begin)).detectEOL();
+  std::string EnumBegin = "enum {";
+  EnumBegin += LineEnding;
+  std::string EnumEnd = "};";
+  EnumEnd += LineEnding;
+
   Begin = SM.translateLineCol(SM.getFileID(Begin),
                               SM.getSpellingLineNumber(Begin), 1);
   const DiagnosticBuilder Diagnostic =
       Check->diag(Begin, "replace macro with enum")
-      << FixItHint::CreateInsertion(Begin, "enum {\n");
+      << FixItHint::CreateInsertion(Begin, EnumBegin);
 
   for (size_t I = 0U; I < MacroList.size(); ++I) {
     const EnumMacro &Macro = MacroList[I];
@@ -503,7 +510,7 @@ void MacroToEnumCallbacks::fixEnumMacro(const MacroList 
&MacroList) const {
       LangOpts);
   End = SM.translateLineCol(SM.getFileID(End),
                             SM.getSpellingLineNumber(End) + 1, 1);
-  Diagnostic << FixItHint::CreateInsertion(End, "};\n");
+  Diagnostic << FixItHint::CreateInsertion(End, EnumEnd);
 }
 
 void MacroToEnumCheck::registerPPCallbacks(const SourceManager &SM,
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 3bc902529a1dc..602c0b8ba3663 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -568,6 +568,10 @@ Changes in existing checks
   positives on project headers that use the same name as a standard library
   header.
 
+- Improved :doc:`modernize-macro-to-enum
+  <clang-tidy/checks/modernize/macro-to-enum>` check by preserving source file
+  line endings in fix-it replacements.
+
 - Improved :doc:`modernize-pass-by-value
   <clang-tidy/checks/modernize/pass-by-value>` check by adding `IgnoreMacros`
   option to suppress warnings in macros.
diff --git a/clang-tools-extra/test/.gitattributes 
b/clang-tools-extra/test/.gitattributes
index 3d4df1e9976bc..3c39030a95eab 100644
--- a/clang-tools-extra/test/.gitattributes
+++ b/clang-tools-extra/test/.gitattributes
@@ -13,3 +13,5 @@ clang-tidy/infrastructure/export-diagnostics.cpp -text
 # These test input files rely on two-byte Windows (CRLF) line endings.
 clang-apply-replacements/Inputs/crlf/crlf.cpp -text
 clang-apply-replacements/Inputs/crlf/crlf.cpp.expected -text
+clang-tidy/checkers/modernize/Inputs/macro-to-enum/crlf.cpp -text
+clang-tidy/checkers/modernize/Inputs/macro-to-enum/crlf.cpp.expected -text
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/crlf.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/crlf.cpp
new file mode 100644
index 0000000000000..c05d3cd5823ba
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/crlf.cpp
@@ -0,0 +1,2 @@
+#define RED 1
+#define GREEN 2
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/crlf.cpp.expected
 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/crlf.cpp.expected
new file mode 100644
index 0000000000000..31100df321bb3
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/crlf.cpp.expected
@@ -0,0 +1,4 @@
+enum {
+RED = 1,
+GREEN = 2
+};
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-crlf.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-crlf.cpp
new file mode 100644
index 0000000000000..52bad7b05f6a5
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-crlf.cpp
@@ -0,0 +1,4 @@
+// RUN: cp %S/Inputs/macro-to-enum/crlf.cpp %t.cpp
+// RUN: chmod u+w %t.cpp
+// RUN: clang-tidy %t.cpp -fix --checks='-*,modernize-macro-to-enum' 
--config={} -- --std=c++14 > %t.out 2>&1
+// RUN: diff %t.cpp %S/Inputs/macro-to-enum/crlf.cpp.expected

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

Reply via email to