1
0

fix-patch-lines 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/awk -f
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Use #line directives to preserve original __LINE__ numbers across patches to
  5. # avoid unwanted compilation changes.
  6. BEGIN {
  7. in_hunk = 0
  8. skip = 0
  9. }
  10. /^--- / {
  11. skip = $2 !~ /\.(c|h)$/
  12. print
  13. next
  14. }
  15. /^@@/ {
  16. if (skip) {
  17. print
  18. next
  19. }
  20. in_hunk = 1
  21. # @@ -1,3 +1,4 @@:
  22. # 1: line number in old file
  23. # 3: how many lines the hunk covers in old file
  24. # 1: line number in new file
  25. # 4: how many lines the hunk covers in new file
  26. match($0, /^@@ -([0-9]+)(,([0-9]+))? \+([0-9]+)(,([0-9]+))? @@/, m)
  27. # Set 'cur' to the old file's line number at the start of the hunk. It
  28. # gets incremented for every context line and every line removal, so
  29. # that it always represents the old file's current line number.
  30. cur = m[1]
  31. # last = last line number of current hunk
  32. last = cur + (m[3] ? m[3] : 1) - 1
  33. need_line_directive = 0
  34. print
  35. next
  36. }
  37. {
  38. if (skip || !in_hunk || $0 ~ /^\\ No newline at end of file/) {
  39. print
  40. next
  41. }
  42. # change line
  43. if ($0 ~ /^[+-]/) {
  44. # inject #line after this group of changes
  45. need_line_directive = 1
  46. if ($0 ~ /^-/)
  47. cur++
  48. print
  49. next
  50. }
  51. # If this is the first context line after a group of changes, inject
  52. # the #line directive to force the compiler to correct the line
  53. # numbering to match the original file.
  54. if (need_line_directive) {
  55. print "+#line " cur
  56. need_line_directive = 0
  57. }
  58. if (cur == last)
  59. in_hunk = 0
  60. cur++
  61. print
  62. }