1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-17 15:32:10 +08:00

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

This commit is contained in:
Ben Boeckel
2025-05-15 15:21:53 +02:00
parent 61743471d9
commit 483d13daf4
3 changed files with 402 additions and 0 deletions

View File

@@ -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

View File

@@ -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)"

View File

@@ -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