[clang-tools-extra] [clang-tidy] Preserve line endings in macro-to-enum fixes (PR #202054)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Jun 6 10:08:47 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tidy
Author: Zeyi Xu (zeyi2)
<details>
<summary>Changes</summary>
Use `StringRef::detectEOL()` when inserting enum braces so fix-its do not mix LF into CRLF source files.
Closes https://github.com/llvm/llvm-project/issues/61593
---
Full diff: https://github.com/llvm/llvm-project/pull/202054.diff
3 Files Affected:
- (modified) clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp (+9-2)
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
- (added) clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-crlf.cpp (+3)
``````````diff
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/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..49ced9b4a632c
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-crlf.cpp
@@ -0,0 +1,3 @@
+// RUN: %python -c "from pathlib import Path; Path(r'%/t.cpp').write_bytes(b'#define RED 1\r\n#define GREEN 2\r\n')"
+// RUN: clang-tidy %t.cpp -fix --checks='-*,modernize-macro-to-enum' --config={} -- --std=c++14 > %t.out 2>&1
+// RUN: %python -c "from pathlib import Path; data = Path(r'%/t.cpp').read_bytes(); expected = b'enum {\r\nRED = 1,\r\nGREEN = 2\r\n};\r\n'; assert data == expected, data"
``````````
</details>
https://github.com/llvm/llvm-project/pull/202054
More information about the cfe-commits
mailing list