瀏覽代碼

ast-grep: add a rule to turn strings into characters

Ben Boeckel 6 月之前
父節點
當前提交
483d13daf4

+ 342 - 0
Utilities/ast-grep/rule-tests/__snapshots__/cmstrcat-to-char-literal-snapshot.yml

@@ -0,0 +1,342 @@
+id: cmstrcat-to-char-literal
+snapshots:
+  cmStrCat("'"):
+    fixed: cmStrCat('\'')
+    labels:
+    - source: '"''"'
+      style: primary
+      start: 9
+      end: 12
+    - source: cmStrCat("'")
+      style: secondary
+      start: 0
+      end: 13
+  cmStrCat("'", other):
+    fixed: cmStrCat('\'', other)
+    labels:
+    - source: '"''"'
+      style: primary
+      start: 9
+      end: 12
+    - source: cmStrCat("'", other)
+      style: secondary
+      start: 0
+      end: 20
+    - source: ','
+      style: secondary
+      start: 12
+      end: 13
+    - source: (
+      style: secondary
+      start: 8
+      end: 9
+  cmStrCat("\""):
+    fixed: cmStrCat('"')
+    labels:
+    - source: '"\""'
+      style: primary
+      start: 9
+      end: 13
+    - source: cmStrCat("\"")
+      style: secondary
+      start: 0
+      end: 14
+  cmStrCat("\"", other):
+    fixed: cmStrCat('"', other)
+    labels:
+    - source: '"\""'
+      style: primary
+      start: 9
+      end: 13
+    - source: cmStrCat("\"", other)
+      style: secondary
+      start: 0
+      end: 21
+    - source: ','
+      style: secondary
+      start: 13
+      end: 14
+    - source: (
+      style: secondary
+      start: 8
+      end: 9
+  cmStrCat("\'"):
+    fixed: cmStrCat('\'')
+    labels:
+    - source: '"\''"'
+      style: primary
+      start: 9
+      end: 13
+    - source: cmStrCat("\'")
+      style: secondary
+      start: 0
+      end: 14
+  cmStrCat("\'", other):
+    fixed: cmStrCat('\'', other)
+    labels:
+    - source: '"\''"'
+      style: primary
+      start: 9
+      end: 13
+    - source: cmStrCat("\'", other)
+      style: secondary
+      start: 0
+      end: 21
+    - source: ','
+      style: secondary
+      start: 13
+      end: 14
+    - source: (
+      style: secondary
+      start: 8
+      end: 9
+  cmStrCat("\n"):
+    fixed: cmStrCat('\n')
+    labels:
+    - source: '"\n"'
+      style: primary
+      start: 9
+      end: 13
+    - source: cmStrCat("\n")
+      style: secondary
+      start: 0
+      end: 14
+  cmStrCat("\n", other):
+    fixed: cmStrCat('\n', other)
+    labels:
+    - source: '"\n"'
+      style: primary
+      start: 9
+      end: 13
+    - source: cmStrCat("\n", other)
+      style: secondary
+      start: 0
+      end: 21
+    - source: ','
+      style: secondary
+      start: 13
+      end: 14
+    - source: (
+      style: secondary
+      start: 8
+      end: 9
+  cmStrCat("n"):
+    fixed: cmStrCat('n')
+    labels:
+    - source: '"n"'
+      style: primary
+      start: 9
+      end: 12
+    - source: cmStrCat("n")
+      style: secondary
+      start: 0
+      end: 13
+  cmStrCat("n", other):
+    fixed: cmStrCat('n', other)
+    labels:
+    - source: '"n"'
+      style: primary
+      start: 9
+      end: 12
+    - source: cmStrCat("n", other)
+      style: secondary
+      start: 0
+      end: 20
+    - source: ','
+      style: secondary
+      start: 12
+      end: 13
+    - source: (
+      style: secondary
+      start: 8
+      end: 9
+  cmStrCat(other, "'"):
+    fixed: cmStrCat(other, '\'')
+    labels:
+    - source: '"''"'
+      style: primary
+      start: 16
+      end: 19
+    - source: cmStrCat(other, "'")
+      style: secondary
+      start: 0
+      end: 20
+    - source: )
+      style: secondary
+      start: 19
+      end: 20
+    - source: ','
+      style: secondary
+      start: 14
+      end: 15
+  cmStrCat(other, "'", other):
+    fixed: cmStrCat(other, '\'', other)
+    labels:
+    - source: '"''"'
+      style: primary
+      start: 16
+      end: 19
+    - source: cmStrCat(other, "'", other)
+      style: secondary
+      start: 0
+      end: 27
+    - source: ','
+      style: secondary
+      start: 19
+      end: 20
+    - source: ','
+      style: secondary
+      start: 14
+      end: 15
+  cmStrCat(other, "\""):
+    fixed: cmStrCat(other, '"')
+    labels:
+    - source: '"\""'
+      style: primary
+      start: 16
+      end: 20
+    - source: cmStrCat(other, "\"")
+      style: secondary
+      start: 0
+      end: 21
+    - source: )
+      style: secondary
+      start: 20
+      end: 21
+    - source: ','
+      style: secondary
+      start: 14
+      end: 15
+  cmStrCat(other, "\"", other):
+    fixed: cmStrCat(other, '"', other)
+    labels:
+    - source: '"\""'
+      style: primary
+      start: 16
+      end: 20
+    - source: cmStrCat(other, "\"", other)
+      style: secondary
+      start: 0
+      end: 28
+    - source: ','
+      style: secondary
+      start: 20
+      end: 21
+    - source: ','
+      style: secondary
+      start: 14
+      end: 15
+  cmStrCat(other, "\'"):
+    fixed: cmStrCat(other, '\'')
+    labels:
+    - source: '"\''"'
+      style: primary
+      start: 16
+      end: 20
+    - source: cmStrCat(other, "\'")
+      style: secondary
+      start: 0
+      end: 21
+    - source: )
+      style: secondary
+      start: 20
+      end: 21
+    - source: ','
+      style: secondary
+      start: 14
+      end: 15
+  cmStrCat(other, "\'", other):
+    fixed: cmStrCat(other, '\'', other)
+    labels:
+    - source: '"\''"'
+      style: primary
+      start: 16
+      end: 20
+    - source: cmStrCat(other, "\'", other)
+      style: secondary
+      start: 0
+      end: 28
+    - source: ','
+      style: secondary
+      start: 20
+      end: 21
+    - source: ','
+      style: secondary
+      start: 14
+      end: 15
+  cmStrCat(other, "\n"):
+    fixed: cmStrCat(other, '\n')
+    labels:
+    - source: '"\n"'
+      style: primary
+      start: 16
+      end: 20
+    - source: cmStrCat(other, "\n")
+      style: secondary
+      start: 0
+      end: 21
+    - source: )
+      style: secondary
+      start: 20
+      end: 21
+    - source: ','
+      style: secondary
+      start: 14
+      end: 15
+  cmStrCat(other, "\n", other):
+    fixed: cmStrCat(other, '\n', other)
+    labels:
+    - source: '"\n"'
+      style: primary
+      start: 16
+      end: 20
+    - source: cmStrCat(other, "\n", other)
+      style: secondary
+      start: 0
+      end: 28
+    - source: ','
+      style: secondary
+      start: 20
+      end: 21
+    - source: ','
+      style: secondary
+      start: 14
+      end: 15
+  cmStrCat(other, "n"):
+    fixed: cmStrCat(other, 'n')
+    labels:
+    - source: '"n"'
+      style: primary
+      start: 16
+      end: 19
+    - source: cmStrCat(other, "n")
+      style: secondary
+      start: 0
+      end: 20
+    - source: )
+      style: secondary
+      start: 19
+      end: 20
+    - source: ','
+      style: secondary
+      start: 14
+      end: 15
+  cmStrCat(other, "n", other):
+    fixed: cmStrCat(other, 'n', other)
+    labels:
+    - source: '"n"'
+      style: primary
+      start: 16
+      end: 19
+    - source: cmStrCat(other, "n", other)
+      style: secondary
+      start: 0
+      end: 27
+    - source: ','
+      style: secondary
+      start: 19
+      end: 20
+    - source: ','
+      style: secondary
+      start: 14
+      end: 15

+ 24 - 0
Utilities/ast-grep/rule-tests/cmstrcat-to-char-literal-test.yml

@@ -0,0 +1,24 @@
+---
+id: cmstrcat-to-char-literal
+valid:
+  - 'cmStrCat(other, "li")'
+  - 'cmStrCat(other, variable)'
+  - "cmStrCat(other, 'c')"
+  - "cmStrCat(other, '\\n')"
+  - "cmStrCat(other, \"a\" \"b\")"
+invalid:
+  - 'cmStrCat(other, "\n")'
+  - 'cmStrCat(other, "n")'
+  - 'cmStrCat(other, "\"")'
+  - "cmStrCat(other, \"\\'\")"
+  - "cmStrCat(other, \"'\")"
+  - 'cmStrCat("\n", other)'
+  - 'cmStrCat("n", other)'
+  - 'cmStrCat("\"", other)'
+  - "cmStrCat(\"\\'\", other)"
+  - "cmStrCat(\"'\", other)"
+  - 'cmStrCat(other, "\n", other)'
+  - 'cmStrCat(other, "n", other)'
+  - 'cmStrCat(other, "\"", other)'
+  - "cmStrCat(other, \"\\'\", other)"
+  - "cmStrCat(other, \"'\", other)"

+ 36 - 0
Utilities/ast-grep/rules/cmstrcat-to-char-literal.yaml

@@ -0,0 +1,36 @@
+---
+id: cmstrcat-to-char-literal
+language: Cpp
+severity: warning
+message: "`cmStrCat` string literal arguments which can be char literals should be"
+rule:
+  kind: string_literal
+  pattern: $ARG
+  follows:
+    regex: '(,|[(])'
+  precedes:
+    regex: '(,|[)])'
+  inside:
+    matches: cmstrcat-call
+    stopBy:
+      kind: call_expression
+constraints:
+  ARG:
+    regex: '^"(.|\\.)"$'
+transform:
+  ARG_CHANGE_QUOTE:
+    replace:
+      source: $ARG
+      replace: '(^"|"$)'
+      by: "'"
+  ARG_ESCAPE_SINGLE_QUOTE:
+    replace:
+      source: $ARG_CHANGE_QUOTE
+      replace: "'''"
+      by: "'\\''"
+  ARG_OUT:
+    replace:
+      source: $ARG_ESCAPE_SINGLE_QUOTE
+      replace: '\\"'
+      by: '"'
+fix: $ARG_OUT