summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean Orner <sorner@albany.edu>2022-11-28 13:37:52 -0500
committerKyle Edwards <kyle.edwards@kitware.com>2022-11-28 13:37:52 -0500
commitc6c8616468d63c5a39d7018f07e8586a89ae7025 (patch)
tree60800f0571d124d5519fb88d5ae3c541110277b7
parente1ec052d53c98294548b88f65d7e09dad537c163 (diff)
downloadcmake-c6c8616468d63c5a39d7018f07e8586a89ae7025.tar.gz
clang-tidy module: add tests for string concatenation check
-rw-r--r--Utilities/ClangTidyModule/Tests/CMakeLists.txt1
-rw-r--r--Utilities/ClangTidyModule/Tests/cmake-string-concatenation-use-cmstrcat-fixit.cxx36
-rw-r--r--Utilities/ClangTidyModule/Tests/cmake-string-concatenation-use-cmstrcat-stdout.txt113
-rw-r--r--Utilities/ClangTidyModule/Tests/cmake-string-concatenation-use-cmstrcat.cxx36
4 files changed, 186 insertions, 0 deletions
diff --git a/Utilities/ClangTidyModule/Tests/CMakeLists.txt b/Utilities/ClangTidyModule/Tests/CMakeLists.txt
index b53d5d2ea0..8220f39e1d 100644
--- a/Utilities/ClangTidyModule/Tests/CMakeLists.txt
+++ b/Utilities/ClangTidyModule/Tests/CMakeLists.txt
@@ -15,3 +15,4 @@ add_run_clang_tidy_test(cmake-use-cmsys-fstream)
add_run_clang_tidy_test(cmake-use-bespoke-enum-class)
add_run_clang_tidy_test(cmake-ostringstream-use-cmstrcat)
add_run_clang_tidy_test(cmake-use-pragma-once)
+add_run_clang_tidy_test(cmake-string-concatenation-use-cmstrcat)
diff --git a/Utilities/ClangTidyModule/Tests/cmake-string-concatenation-use-cmstrcat-fixit.cxx b/Utilities/ClangTidyModule/Tests/cmake-string-concatenation-use-cmstrcat-fixit.cxx
new file mode 100644
index 0000000000..79aecd4ccb
--- /dev/null
+++ b/Utilities/ClangTidyModule/Tests/cmake-string-concatenation-use-cmstrcat-fixit.cxx
@@ -0,0 +1,36 @@
+#include <string>
+
+template <typename... Args>
+std::string cmStrCat(Args&&... args)
+{
+ return "";
+}
+
+std::string a = "This is a string variable";
+std::string b = " and this is a string variable";
+std::string concat;
+
+// Correction needed
+void test1()
+{
+ concat = cmStrCat(a, b);
+ concat = cmStrCat(a, " and this is a string literal");
+ concat = cmStrCat(a, 'O');
+ concat = cmStrCat("This is a string literal", b);
+ concat = cmStrCat('O', a);
+ concat = cmStrCat(a, " and this is a string literal", 'O', b);
+
+ concat = cmStrCat(concat, b);
+ concat = cmStrCat(concat, " and this is a string literal");
+ concat = cmStrCat(concat, 'o');
+ concat = cmStrCat(concat, b, " and this is a string literal ", 'o', b);
+}
+
+// No correction needed
+void test2()
+{
+ a = b;
+ a = "This is a string literal";
+ a = 'X';
+ cmStrCat(a, b);
+}
diff --git a/Utilities/ClangTidyModule/Tests/cmake-string-concatenation-use-cmstrcat-stdout.txt b/Utilities/ClangTidyModule/Tests/cmake-string-concatenation-use-cmstrcat-stdout.txt
new file mode 100644
index 0000000000..3cfdef82b3
--- /dev/null
+++ b/Utilities/ClangTidyModule/Tests/cmake-string-concatenation-use-cmstrcat-stdout.txt
@@ -0,0 +1,113 @@
+cmake-string-concatenation-use-cmstrcat.cxx:16:12: warning: use cmStrCat() instead of string concatenation [cmake-string-concatenation-use-cmstrcat]
+ concat = a + b;
+ ^ ~
+ cmStrCat( , )
+cmake-string-concatenation-use-cmstrcat.cxx:16:12: note: FIX-IT applied suggested code changes
+cmake-string-concatenation-use-cmstrcat.cxx:16:14: note: FIX-IT applied suggested code changes
+ concat = a + b;
+ ^
+cmake-string-concatenation-use-cmstrcat.cxx:16:17: note: FIX-IT applied suggested code changes
+ concat = a + b;
+ ^
+cmake-string-concatenation-use-cmstrcat.cxx:17:12: warning: use cmStrCat() instead of string concatenation [cmake-string-concatenation-use-cmstrcat]
+ concat = a + " and this is a string literal";
+ ^ ~
+ cmStrCat( , )
+cmake-string-concatenation-use-cmstrcat.cxx:17:12: note: FIX-IT applied suggested code changes
+cmake-string-concatenation-use-cmstrcat.cxx:17:14: note: FIX-IT applied suggested code changes
+ concat = a + " and this is a string literal";
+ ^
+cmake-string-concatenation-use-cmstrcat.cxx:17:47: note: FIX-IT applied suggested code changes
+ concat = a + " and this is a string literal";
+ ^
+cmake-string-concatenation-use-cmstrcat.cxx:18:12: warning: use cmStrCat() instead of string concatenation [cmake-string-concatenation-use-cmstrcat]
+ concat = a + 'O';
+ ^ ~
+ cmStrCat( , )
+cmake-string-concatenation-use-cmstrcat.cxx:18:12: note: FIX-IT applied suggested code changes
+cmake-string-concatenation-use-cmstrcat.cxx:18:14: note: FIX-IT applied suggested code changes
+ concat = a + 'O';
+ ^
+cmake-string-concatenation-use-cmstrcat.cxx:18:19: note: FIX-IT applied suggested code changes
+ concat = a + 'O';
+ ^
+cmake-string-concatenation-use-cmstrcat.cxx:19:12: warning: use cmStrCat() instead of string concatenation [cmake-string-concatenation-use-cmstrcat]
+ concat = "This is a string literal" + b;
+ ^ ~
+ cmStrCat( , )
+cmake-string-concatenation-use-cmstrcat.cxx:19:12: note: FIX-IT applied suggested code changes
+cmake-string-concatenation-use-cmstrcat.cxx:19:39: note: FIX-IT applied suggested code changes
+ concat = "This is a string literal" + b;
+ ^
+cmake-string-concatenation-use-cmstrcat.cxx:19:42: note: FIX-IT applied suggested code changes
+ concat = "This is a string literal" + b;
+ ^
+cmake-string-concatenation-use-cmstrcat.cxx:20:12: warning: use cmStrCat() instead of string concatenation [cmake-string-concatenation-use-cmstrcat]
+ concat = 'O' + a;
+ ^ ~
+ cmStrCat( , )
+cmake-string-concatenation-use-cmstrcat.cxx:20:12: note: FIX-IT applied suggested code changes
+cmake-string-concatenation-use-cmstrcat.cxx:20:16: note: FIX-IT applied suggested code changes
+ concat = 'O' + a;
+ ^
+cmake-string-concatenation-use-cmstrcat.cxx:20:19: note: FIX-IT applied suggested code changes
+ concat = 'O' + a;
+ ^
+cmake-string-concatenation-use-cmstrcat.cxx:21:12: warning: use cmStrCat() instead of string concatenation [cmake-string-concatenation-use-cmstrcat]
+ concat = a + " and this is a string literal" + 'O' + b;
+ ^ ~ ~ ~
+ cmStrCat( , , , )
+cmake-string-concatenation-use-cmstrcat.cxx:21:12: note: FIX-IT applied suggested code changes
+cmake-string-concatenation-use-cmstrcat.cxx:21:14: note: FIX-IT applied suggested code changes
+ concat = a + " and this is a string literal" + 'O' + b;
+ ^
+cmake-string-concatenation-use-cmstrcat.cxx:21:48: note: FIX-IT applied suggested code changes
+ concat = a + " and this is a string literal" + 'O' + b;
+ ^
+cmake-string-concatenation-use-cmstrcat.cxx:21:54: note: FIX-IT applied suggested code changes
+ concat = a + " and this is a string literal" + 'O' + b;
+ ^
+cmake-string-concatenation-use-cmstrcat.cxx:21:57: note: FIX-IT applied suggested code changes
+ concat = a + " and this is a string literal" + 'O' + b;
+ ^
+cmake-string-concatenation-use-cmstrcat.cxx:23:10: warning: use cmStrCat() instead of string append [cmake-string-concatenation-use-cmstrcat]
+ concat += b;
+ ^~
+ = cmStrCat(concat, )
+cmake-string-concatenation-use-cmstrcat.cxx:23:10: note: FIX-IT applied suggested code changes
+cmake-string-concatenation-use-cmstrcat.cxx:23:14: note: FIX-IT applied suggested code changes
+ concat += b;
+ ^
+cmake-string-concatenation-use-cmstrcat.cxx:24:10: warning: use cmStrCat() instead of string append [cmake-string-concatenation-use-cmstrcat]
+ concat += " and this is a string literal";
+ ^~
+ = cmStrCat(concat, )
+cmake-string-concatenation-use-cmstrcat.cxx:24:10: note: FIX-IT applied suggested code changes
+cmake-string-concatenation-use-cmstrcat.cxx:24:44: note: FIX-IT applied suggested code changes
+ concat += " and this is a string literal";
+ ^
+cmake-string-concatenation-use-cmstrcat.cxx:25:10: warning: use cmStrCat() instead of string append [cmake-string-concatenation-use-cmstrcat]
+ concat += 'o';
+ ^~
+ = cmStrCat(concat, )
+cmake-string-concatenation-use-cmstrcat.cxx:25:10: note: FIX-IT applied suggested code changes
+cmake-string-concatenation-use-cmstrcat.cxx:25:16: note: FIX-IT applied suggested code changes
+ concat += 'o';
+ ^
+cmake-string-concatenation-use-cmstrcat.cxx:26:10: warning: use cmStrCat() instead of string append [cmake-string-concatenation-use-cmstrcat]
+ concat += b + " and this is a string literal " + 'o' + b;
+ ^~ ~ ~ ~
+ = cmStrCat(concat, , , , )
+cmake-string-concatenation-use-cmstrcat.cxx:26:10: note: FIX-IT applied suggested code changes
+cmake-string-concatenation-use-cmstrcat.cxx:26:15: note: FIX-IT applied suggested code changes
+ concat += b + " and this is a string literal " + 'o' + b;
+ ^
+cmake-string-concatenation-use-cmstrcat.cxx:26:50: note: FIX-IT applied suggested code changes
+ concat += b + " and this is a string literal " + 'o' + b;
+ ^
+cmake-string-concatenation-use-cmstrcat.cxx:26:56: note: FIX-IT applied suggested code changes
+ concat += b + " and this is a string literal " + 'o' + b;
+ ^
+cmake-string-concatenation-use-cmstrcat.cxx:26:59: note: FIX-IT applied suggested code changes
+ concat += b + " and this is a string literal " + 'o' + b;
+ ^
diff --git a/Utilities/ClangTidyModule/Tests/cmake-string-concatenation-use-cmstrcat.cxx b/Utilities/ClangTidyModule/Tests/cmake-string-concatenation-use-cmstrcat.cxx
new file mode 100644
index 0000000000..13a20ac2a6
--- /dev/null
+++ b/Utilities/ClangTidyModule/Tests/cmake-string-concatenation-use-cmstrcat.cxx
@@ -0,0 +1,36 @@
+#include <string>
+
+template <typename... Args>
+std::string cmStrCat(Args&&... args)
+{
+ return "";
+}
+
+std::string a = "This is a string variable";
+std::string b = " and this is a string variable";
+std::string concat;
+
+// Correction needed
+void test1()
+{
+ concat = a + b;
+ concat = a + " and this is a string literal";
+ concat = a + 'O';
+ concat = "This is a string literal" + b;
+ concat = 'O' + a;
+ concat = a + " and this is a string literal" + 'O' + b;
+
+ concat += b;
+ concat += " and this is a string literal";
+ concat += 'o';
+ concat += b + " and this is a string literal " + 'o' + b;
+}
+
+// No correction needed
+void test2()
+{
+ a = b;
+ a = "This is a string literal";
+ a = 'X';
+ cmStrCat(a, b);
+}