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 void bar(Twine x);
17 
18 static Twine Moo = Twine("bark") + "bah";
19 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: twine variables are prone to use-after-free bugs
20 // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
21 // CHECK-FIXES: static std::string Moo = (Twine("bark") + "bah").str();
22 
main()23 int main() {
24   const Twine t = Twine("a") + "b" + Twine(42);
25 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs
26 // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
27 // CHECK-FIXES: std::string t = (Twine("a") + "b" + Twine(42)).str();
28   foo(Twine("a") + "b");
29 
30   Twine Prefix = false ? "__INT_FAST" : "__UINT_FAST";
31 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: twine variables are prone to use-after-free bugs
32 // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
33 // CHECK-FIXES: const char * Prefix = false ? "__INT_FAST" : "__UINT_FAST";
34 
35   const Twine t2 = Twine();
36 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs
37 // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
38 // CHECK-FIXES: std::string t2 = (Twine()).str();
39   foo(Twine() + "b");
40 
41   const Twine t3 = Twine(42);
42 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs
43 // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
44 // CHECK-FIXES: std::string t3 = (Twine(42)).str();
45 
46   const Twine t4 = Twine(42) + "b";
47 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs
48 // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
49 // CHECK-FIXES: std::string t4 = (Twine(42) + "b").str();
50 
51   const Twine t5 = Twine() + "b";
52 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs
53 // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
54 // CHECK-FIXES: std::string t5 = (Twine() + "b").str();
55 
56   const Twine t6 = true ? Twine() : Twine(42);
57 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs
58 // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
59 // CHECK-FIXES: std::string t6 = (true ? Twine() : Twine(42)).str();
60 
61   const Twine t7 = false ? Twine() : Twine("b");
62 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: twine variables are prone to use-after-free bugs
63 // CHECK-MESSAGES: note: FIX-IT applied suggested code changes
64 // CHECK-FIXES: std::string t7 = (false ? Twine() : Twine("b")).str();
65 }
66