1 // RUN: %check_clang_tidy %s llvm-twine-local %t
2
3 namespace llvm {
4 class Twine {
5 public:
6 Twine(const char *);
7 Twine(int);
8 Twine();
9 Twine &operator+(const Twine &);
10 };
11 }
12
13 using namespace llvm;
14
15 void foo(const Twine &x);
16
17 static Twine Moo = Twine("bark") + "bah";
18 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: twine variables are prone to use-after-free bugs
19 // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
20 // CHECK-FIXES: static std::string Moo = (Twine("bark") + "bah").str();
21
main()22 int main() {
23 const Twine t = Twine("a") + "b" + Twine(42);
24 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs
25 // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
26 // CHECK-FIXES: std::string t = (Twine("a") + "b" + Twine(42)).str();
27 foo(Twine("a") + "b");
28
29 Twine Prefix = false ? "__INT_FAST" : "__UINT_FAST";
30 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: twine variables are prone to use-after-free bugs
31 // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
32 // CHECK-FIXES: const char * Prefix = false ? "__INT_FAST" : "__UINT_FAST";
33
34 const Twine t2 = Twine();
35 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs
36 // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
37 // CHECK-FIXES: std::string t2 = (Twine()).str();
38 foo(Twine() + "b");
39
40 const Twine t3 = Twine(42);
41 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs
42 // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
43 // CHECK-FIXES: std::string t3 = (Twine(42)).str();
44
45 const Twine t4 = Twine(42) + "b";
46 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs
47 // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
48 // CHECK-FIXES: std::string t4 = (Twine(42) + "b").str();
49
50 const Twine t5 = Twine() + "b";
51 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs
52 // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
53 // CHECK-FIXES: std::string t5 = (Twine() + "b").str();
54
55 const Twine t6 = true ? Twine() : Twine(42);
56 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs
57 // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
58 // CHECK-FIXES: std::string t6 = (true ? Twine() : Twine(42)).str();
59
60 const Twine t7 = false ? Twine() : Twine("b");
61 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs
62 // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
63 // CHECK-FIXES: std::string t7 = (false ? Twine() : Twine("b")).str();
64 }
65