https://github.com/zeyi2 updated https://github.com/llvm/llvm-project/pull/202054
>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 1/4] [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 >From 7f0291b4e1816b767e45c57448bb9c14b87a75f5 Mon Sep 17 00:00:00 2001 From: Zeyi Xu <[email protected]> Date: Sun, 7 Jun 2026 00:10:51 +0800 Subject: [PATCH 2/4] change the way of testing this --- clang-tools-extra/test/.gitattributes | 2 -- .../checkers/modernize/Inputs/macro-to-enum/crlf.cpp | 2 -- .../modernize/Inputs/macro-to-enum/crlf.cpp.expected | 4 ---- .../clang-tidy/checkers/modernize/macro-to-enum-crlf.cpp | 5 ++--- 4 files changed, 2 insertions(+), 11 deletions(-) delete mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/crlf.cpp delete mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/crlf.cpp.expected diff --git a/clang-tools-extra/test/.gitattributes b/clang-tools-extra/test/.gitattributes index 3c39030a95eab..3d4df1e9976bc 100644 --- a/clang-tools-extra/test/.gitattributes +++ b/clang-tools-extra/test/.gitattributes @@ -13,5 +13,3 @@ 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 deleted file mode 100644 index c05d3cd5823ba..0000000000000 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/crlf.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#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 deleted file mode 100644 index 31100df321bb3..0000000000000 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/crlf.cpp.expected +++ /dev/null @@ -1,4 +0,0 @@ -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 index 52bad7b05f6a5..49ced9b4a632c 100644 --- 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 @@ -1,4 +1,3 @@ -// RUN: cp %S/Inputs/macro-to-enum/crlf.cpp %t.cpp -// RUN: chmod u+w %t.cpp +// 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: diff %t.cpp %S/Inputs/macro-to-enum/crlf.cpp.expected +// 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" >From d286a26f62ef479ae6ddda776a924c329318b2c3 Mon Sep 17 00:00:00 2001 From: Zeyi Xu <[email protected]> Date: Sun, 7 Jun 2026 23:25:10 +0800 Subject: [PATCH 3/4] Revert "change the way of testing this" This reverts commit 7f0291b4e1816b767e45c57448bb9c14b87a75f5. --- clang-tools-extra/test/.gitattributes | 2 ++ .../checkers/modernize/Inputs/macro-to-enum/crlf.cpp | 2 ++ .../modernize/Inputs/macro-to-enum/crlf.cpp.expected | 4 ++++ .../clang-tidy/checkers/modernize/macro-to-enum-crlf.cpp | 5 +++-- 4 files changed, 11 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 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 index 49ced9b4a632c..52bad7b05f6a5 100644 --- 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 @@ -1,3 +1,4 @@ -// 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: 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: %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" +// RUN: diff %t.cpp %S/Inputs/macro-to-enum/crlf.cpp.expected >From d2c3788756daf8d763d12e9b12a2a45af012eb22 Mon Sep 17 00:00:00 2001 From: Zeyi Xu <[email protected]> Date: Mon, 8 Jun 2026 00:19:11 +0800 Subject: [PATCH 4/4] address feedback --- .../clang-tidy/modernize/MacroToEnumCheck.cpp | 10 ++++------ .../checkers/modernize/macro-to-enum-crlf.cpp | 4 +++- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp b/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp index 1036d08883195..aa4732ceba7e0 100644 --- a/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp @@ -472,16 +472,13 @@ void MacroToEnumCallbacks::fixEnumMacro(const MacroList &MacroList) const { 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, EnumBegin); + << FixItHint::CreateInsertion(Begin, + (llvm::Twine("enum {") + LineEnding).str()); for (size_t I = 0U; I < MacroList.size(); ++I) { const EnumMacro &Macro = MacroList[I]; @@ -510,7 +507,8 @@ void MacroToEnumCallbacks::fixEnumMacro(const MacroList &MacroList) const { LangOpts); End = SM.translateLineCol(SM.getFileID(End), SM.getSpellingLineNumber(End) + 1, 1); - Diagnostic << FixItHint::CreateInsertion(End, EnumEnd); + Diagnostic << FixItHint::CreateInsertion( + End, (llvm::Twine("};") + LineEnding).str()); } void MacroToEnumCheck::registerPPCallbacks(const SourceManager &SM, 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 index 52bad7b05f6a5..ae54e5b7fe430 100644 --- 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 @@ -1,4 +1,6 @@ +// Verify the fix-it preserves CRLF line endings. + // 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: clang-tidy %t.cpp -fix --checks='-*,modernize-macro-to-enum' > %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
