Debian Codegraph @

gcc/testsuite/gdc.test/runnable/test17559.d

raw
1 // REQUIRED_ARGS: -g
2 // REQUIRED_ARGS(linux freebsd openbsd dragonflybsd): -L-export-dynamic
3 // PERMUTE_ARGS:
4 // DISABLED: osx
5
6 import core.stdc.stdio;
7
8 void main()
9 {
10 fun(1);
11 fun(2);
12 fun(3);
13 #line 30
14 fun(4);
15
16 foo(1, 10);
17 foo(2, 10);
18 foo(3, 10);
19 #line 40
20 foo(4, 10);
21 }
22
23 void fun(int n, int defParam = 10)
24 {
25 try
26 {
27 if (n == 4)
28 throw new Exception("fun");
29 }
30 catch(Exception e)
31 {
32 string s = e.toString();
33 printf("%.*s\n", cast(int)s.length, s.ptr);
34 int line = lineInMain(e.toString());
35 assert(line >= 30 && line <= 32); // return address might be next statement
36 }
37 }
38
39 void foo(int n, int m)
40 {
41 try
42 {
43 if (n == 4)
44 throw new Exception("foo");
45 }
46 catch(Exception e)
47 {
48 string s = e.toString();
49 printf("%.*s\n", cast(int)s.length, s.ptr);
50 int line = lineInMain(e.toString());
51 assert(line >= 40 && line <= 41); // return address might be next statement
52 }
53 }
54
55 int lineInMain(string msg)
56 {
57 // find line number of _Dmain in stack trace
58 // on linux: file.d:line _Dmain [addr]
59 // on windows: addr in _Dmain at file.d(line)
60 int line = 0;
61 bool mainFound = false;
62 for (size_t pos = 0; pos + 6 < msg.length; pos++)
63 {
64 if (msg[pos] == '\n')
65 {
66 line = 0;
67 mainFound = false;
68 }
69 else if ((msg[pos] == ':' || msg[pos] == '(') && line == 0)
70 {
71 for (pos++; pos < msg.length && msg[pos] >= '0' && msg[pos] <= '9'; pos++)
72 line = line * 10 + msg[pos] - '0';
73 if (line > 0 && mainFound)
74 return line;
75 }
76 else if (msg[pos .. pos + 6] == "_Dmain" || msg[pos .. pos + 6] == "D main")
77 {
78 mainFound = true;
79 if (line > 0 && mainFound)
80 return line;
81 }
82 }
83 return 0;
84 }