summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test/clang-tidy
diff options
context:
space:
mode:
authorBenjamin Kramer <benny.kra@googlemail.com>2014-07-08 14:32:17 +0000
committerBenjamin Kramer <benny.kra@googlemail.com>2014-07-08 14:32:17 +0000
commit190e2cfd7459b5acc30f12702137ce33913b85b4 (patch)
treecd97d25268318ea65f973576a3fa124afe2da34c /clang-tools-extra/test/clang-tidy
parenteb893a1fd688362a7809525c3b8761b77090f048 (diff)
downloadbcm5719-llvm-190e2cfd7459b5acc30f12702137ce33913b85b4.tar.gz
bcm5719-llvm-190e2cfd7459b5acc30f12702137ce33913b85b4.zip
[clang-tidy] Add a little checker for Twine locals in LLVM.
Those often cause use after free bugs and should be generally avoided. Technically it is safe to have a Twine with >=2 components in a variable but I don't think it is a good pattern to follow. The almost trivial checker comes with elaborated fix-it hints that turn the Twine into a std::string if necessary and otherwise fall back to the original type if the Twine is created from a single value. llvm-svn: 212535
Diffstat (limited to 'clang-tools-extra/test/clang-tidy')
-rw-r--r--clang-tools-extra/test/clang-tidy/llvm-twine-local.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/llvm-twine-local.cpp b/clang-tools-extra/test/clang-tidy/llvm-twine-local.cpp
new file mode 100644
index 00000000000..0f134a5500a
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/llvm-twine-local.cpp
@@ -0,0 +1,35 @@
+// RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
+// RUN: clang-tidy %t.cpp -checks='-*,llvm-twine-local' -fix -- > %t.msg 2>&1
+// RUN: FileCheck -input-file=%t.cpp %s
+// RUN: FileCheck -input-file=%t.msg -check-prefix=CHECK-MESSAGES %s
+
+namespace llvm {
+class Twine {
+public:
+ Twine(const char *);
+ Twine(int);
+ Twine &operator+(const Twine &);
+};
+}
+
+using namespace llvm;
+
+void foo(const Twine &x);
+
+static Twine Moo = Twine("bark") + "bah";
+// CHECK-MASSAGES: twine variables are prone to use after free bugs
+// CHECK-MESSAGES: note: FIX-IT applied suggested code changes
+// CHECK: static std::string Moo = (Twine("bark") + "bah").str();
+
+int main() {
+ const Twine t = Twine("a") + "b" + Twine(42);
+// CHECK-MASSAGES: twine variables are prone to use after free bugs
+// CHECK-MESSAGES: note: FIX-IT applied suggested code changes
+// CHECK: std::string t = (Twine("a") + "b" + Twine(42)).str();
+ foo(Twine("a") + "b");
+
+ Twine Prefix = false ? "__INT_FAST" : "__UINT_FAST";
+// CHECK-MASSAGES: twine variables are prone to use after free bugs
+// CHECK-MESSAGES: note: FIX-IT applied suggested code changes
+// CHECK: const char * Prefix = false ? "__INT_FAST" : "__UINT_FAST";
+}
OpenPOWER on IntegriCloud