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.

This reland fixes the previous buildbot failure by adding `--` in test file.

---
Full diff: https://github.com/llvm/llvm-project/pull/202271.diff


6 Files Affected:

- (modified) clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp (+7-2) 
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4) 
- (modified) clang-tools-extra/test/.gitattributes (+2) 
- (added) 
clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/crlf.cpp
 (+2) 
- (added) 
clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/macro-to-enum/crlf.cpp.expected
 (+4) 
- (added) 
clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-crlf.cpp 
(+6) 


``````````diff
diff --git a/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
index 1c49c32f9fecb..aa4732ceba7e0 100644
--- a/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
@@ -470,11 +470,15 @@ 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();
+
   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,
+                                    (llvm::Twine("enum {") + 
LineEnding).str());
 
   for (size_t I = 0U; I < MacroList.size(); ++I) {
     const EnumMacro &Macro = MacroList[I];
@@ -503,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, "};\n");
+  Diagnostic << FixItHint::CreateInsertion(
+      End, (llvm::Twine("};") + LineEnding).str());
 }
 
 void MacroToEnumCheck::registerPPCallbacks(const SourceManager &SM,
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 9703bb8f17208..c369b1fd8b373 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -581,6 +581,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..3420079902f63
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/macro-to-enum-crlf.cpp
@@ -0,0 +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' -- > 
%t.out 2>&1
+// RUN: diff %t.cpp %S/Inputs/macro-to-enum/crlf.cpp.expected

``````````

</details>


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

Reply via email to