diff --git a/Utilities/ast-grep/rule-tests/__snapshots__/cmhasprefix-char-snapshot.yml b/Utilities/ast-grep/rule-tests/__snapshots__/cmhasprefix-char-snapshot.yml new file mode 100644 index 0000000000..0b75c5e501 --- /dev/null +++ b/Utilities/ast-grep/rule-tests/__snapshots__/cmhasprefix-char-snapshot.yml @@ -0,0 +1,47 @@ +id: cmhasprefix-char +snapshots: + cmHasPrefix(haystack, "'"): + fixed: cmHasPrefix(haystack, '\'') + labels: + - source: '"''"' + style: primary + start: 22 + end: 25 + - source: cmHasPrefix(haystack, "'") + style: secondary + start: 0 + end: 26 + - source: ',' + style: secondary + start: 20 + end: 21 + cmHasPrefix(haystack, "\n"): + fixed: cmHasPrefix(haystack, '\n') + labels: + - source: '"\n"' + style: primary + start: 22 + end: 26 + - source: cmHasPrefix(haystack, "\n") + style: secondary + start: 0 + end: 27 + - source: ',' + style: secondary + start: 20 + end: 21 + cmHasPrefix(haystack, "a"): + fixed: cmHasPrefix(haystack, 'a') + labels: + - source: '"a"' + style: primary + start: 22 + end: 25 + - source: cmHasPrefix(haystack, "a") + style: secondary + start: 0 + end: 26 + - source: ',' + style: secondary + start: 20 + end: 21 diff --git a/Utilities/ast-grep/rule-tests/__snapshots__/cmhassuffix-char-snapshot.yml b/Utilities/ast-grep/rule-tests/__snapshots__/cmhassuffix-char-snapshot.yml new file mode 100644 index 0000000000..d376a53426 --- /dev/null +++ b/Utilities/ast-grep/rule-tests/__snapshots__/cmhassuffix-char-snapshot.yml @@ -0,0 +1,47 @@ +id: cmhassuffix-char +snapshots: + cmHasSuffix(haystack, "'"): + fixed: cmHasSuffix(haystack, '\'') + labels: + - source: '"''"' + style: primary + start: 22 + end: 25 + - source: cmHasSuffix(haystack, "'") + style: secondary + start: 0 + end: 26 + - source: ',' + style: secondary + start: 20 + end: 21 + cmHasSuffix(haystack, "\n"): + fixed: cmHasSuffix(haystack, '\n') + labels: + - source: '"\n"' + style: primary + start: 22 + end: 26 + - source: cmHasSuffix(haystack, "\n") + style: secondary + start: 0 + end: 27 + - source: ',' + style: secondary + start: 20 + end: 21 + cmHasSuffix(haystack, "a"): + fixed: cmHasSuffix(haystack, 'a') + labels: + - source: '"a"' + style: primary + start: 22 + end: 25 + - source: cmHasSuffix(haystack, "a") + style: secondary + start: 0 + end: 26 + - source: ',' + style: secondary + start: 20 + end: 21 diff --git a/Utilities/ast-grep/rule-tests/cmhasprefix-char.yml b/Utilities/ast-grep/rule-tests/cmhasprefix-char.yml new file mode 100644 index 0000000000..37e51ca898 --- /dev/null +++ b/Utilities/ast-grep/rule-tests/cmhasprefix-char.yml @@ -0,0 +1,9 @@ +--- +id: cmhasprefix-char +valid: + - "cmHasPrefix(haystack, 'a')" + - 'cmHasPrefix(haystack, "foo")' +invalid: + - 'cmHasPrefix(haystack, "a")' + - 'cmHasPrefix(haystack, "\n")' + - "cmHasPrefix(haystack, \"'\")" diff --git a/Utilities/ast-grep/rule-tests/cmhassuffix-char.yml b/Utilities/ast-grep/rule-tests/cmhassuffix-char.yml new file mode 100644 index 0000000000..17a146b61f --- /dev/null +++ b/Utilities/ast-grep/rule-tests/cmhassuffix-char.yml @@ -0,0 +1,9 @@ +--- +id: cmhassuffix-char +valid: + - "cmHasSuffix(haystack, 'a')" + - 'cmHasSuffix(haystack, "foo")' +invalid: + - 'cmHasSuffix(haystack, "a")' + - 'cmHasSuffix(haystack, "\n")' + - "cmHasSuffix(haystack, \"'\")" diff --git a/Utilities/ast-grep/rules/cmhasprefix-char.yml b/Utilities/ast-grep/rules/cmhasprefix-char.yml new file mode 100644 index 0000000000..06027a7fb2 --- /dev/null +++ b/Utilities/ast-grep/rules/cmhasprefix-char.yml @@ -0,0 +1,34 @@ +--- +id: cmhasprefix-char +language: Cpp +severity: warning +message: "`cmHasPrefix` with a one-char prefix search should use `cmHasPrefix`" +rule: + kind: string_literal + pattern: $ARG + follows: + regex: '(,|[(])' + inside: + matches: cmhasprefix-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 diff --git a/Utilities/ast-grep/rules/cmhassuffix-char.yml b/Utilities/ast-grep/rules/cmhassuffix-char.yml new file mode 100644 index 0000000000..1d45b83f98 --- /dev/null +++ b/Utilities/ast-grep/rules/cmhassuffix-char.yml @@ -0,0 +1,34 @@ +--- +id: cmhassuffix-char +language: Cpp +severity: warning +message: "`cmHasSuffix` with a one-char suffix search should use `cmHasSuffix`" +rule: + kind: string_literal + pattern: $ARG + follows: + regex: '(,|[(])' + inside: + matches: cmhassuffix-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 diff --git a/Utilities/ast-grep/utils/cmhasprefix-call.yml b/Utilities/ast-grep/utils/cmhasprefix-call.yml new file mode 100644 index 0000000000..eb57a79ac3 --- /dev/null +++ b/Utilities/ast-grep/utils/cmhasprefix-call.yml @@ -0,0 +1,5 @@ +--- +id: cmhasprefix-call +language: Cpp +rule: + pattern: cmHasPrefix($$$) diff --git a/Utilities/ast-grep/utils/cmhassuffix-call.yml b/Utilities/ast-grep/utils/cmhassuffix-call.yml new file mode 100644 index 0000000000..d0fca6e72e --- /dev/null +++ b/Utilities/ast-grep/utils/cmhassuffix-call.yml @@ -0,0 +1,5 @@ +--- +id: cmhassuffix-call +language: Cpp +rule: + pattern: cmHasSuffix($$$)