blob: d50490fe87d2080e68fcf109153161ef1d971c25 [file] [log] [blame]
Johnny Chen3d57ee72010-11-11 23:29:54 +00001"""
2Test the printing of anonymous and named namespace variables.
3"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
Jim Ingham63dfc722012-09-22 00:05:11 +00009import lldbutil
Johnny Chen3d57ee72010-11-11 23:29:54 +000010
11class NamespaceTestCase(TestBase):
12
Greg Clayton4570d3e2013-12-10 23:19:29 +000013 mydir = TestBase.compute_mydir(__file__)
Johnny Chen3d57ee72010-11-11 23:29:54 +000014
Johnny Chend71ffbc2010-12-23 23:26:05 +000015 # rdar://problem/8668674
Robert Flack13c7ad92015-03-30 14:12:17 +000016 @skipUnlessDarwin
Johnny Chen24086bc2012-04-06 19:54:10 +000017 @dsym_test
Johnny Chen3d57ee72010-11-11 23:29:54 +000018 def test_with_dsym_and_run_command(self):
19 """Test that anonymous and named namespace variables display correctly."""
20 self.buildDsym()
21 self.namespace_variable_commands()
22
Johnny Chend71ffbc2010-12-23 23:26:05 +000023 # rdar://problem/8668674
Johnny Chen24086bc2012-04-06 19:54:10 +000024 @dwarf_test
Johnny Chen3d57ee72010-11-11 23:29:54 +000025 def test_with_dwarf_and_run_command(self):
26 """Test that anonymous and named namespace variables display correctly."""
27 self.buildDwarf()
28 self.namespace_variable_commands()
29
30 def setUp(self):
31 # Call super's setUp().
32 TestBase.setUp(self)
33 # Find the line numbers for declarations of namespace variables i and j.
34 self.line_var_i = line_number('main.cpp',
35 '// Find the line number for anonymous namespace variable i.')
36 self.line_var_j = line_number('main.cpp',
37 '// Find the line number for named namespace variable j.')
38 # And the line number to break at.
39 self.line_break = line_number('main.cpp',
40 '// Set break point at this line.')
41
42 def namespace_variable_commands(self):
43 """Test that anonymous and named namespace variables display correctly."""
44 self.runCmd("file a.out", CURRENT_EXECUTABLE_SET)
45
Jim Ingham63dfc722012-09-22 00:05:11 +000046 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.line_break, num_expected_locations=1, loc_exact=True)
Johnny Chen3d57ee72010-11-11 23:29:54 +000047
48 self.runCmd("run", RUN_SUCCEEDED)
49
50 # The stop reason of the thread should be breakpoint.
51 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
Greg Clayton7260f622011-04-18 08:33:37 +000052 substrs = ['stopped',
Johnny Chen3d57ee72010-11-11 23:29:54 +000053 'stop reason = breakpoint'])
54
Johnny Chen80e6db92010-11-17 00:52:41 +000055 # On Mac OS X, gcc 4.2 emits the wrong debug info with respect to types.
56 slist = ['(int) a = 12', 'anon_uint', 'a_uint', 'b_uint', 'y_uint']
57 if sys.platform.startswith("darwin") and self.getCompiler() in ['clang', 'llvm-gcc']:
58 slist = ['(int) a = 12',
Johnny Chen5e27d5e2011-11-01 18:46:52 +000059 '::my_uint_t', 'anon_uint = 0',
Johnny Chen80e6db92010-11-17 00:52:41 +000060 '(A::uint_t) a_uint = 1',
61 '(A::B::uint_t) b_uint = 2',
Johnny Chenb4d59f42010-11-18 20:20:18 +000062 '(Y::uint_t) y_uint = 3']
63
Johnny Chen80e6db92010-11-17 00:52:41 +000064 # 'frame variable' displays the local variables with type information.
Johnny Chen13e64a72010-11-15 19:10:53 +000065 self.expect('frame variable', VARIABLES_DISPLAYED_CORRECTLY,
Johnny Chen80e6db92010-11-17 00:52:41 +000066 substrs = slist)
Johnny Chen13e64a72010-11-15 19:10:53 +000067
Johnny Chen6ba9d1f2010-11-15 18:27:57 +000068 # 'frame variable' with basename 'i' should work.
Greg Clayton3bcdfc02012-12-04 00:32:51 +000069 self.expect("frame variable --show-declaration --show-globals i",
Johnny Chen3d57ee72010-11-11 23:29:54 +000070 startstr = "main.cpp:%d: (int) (anonymous namespace)::i = 3" % self.line_var_i)
71 # main.cpp:12: (int) (anonymous namespace)::i = 3
72
Johnny Chen6ba9d1f2010-11-15 18:27:57 +000073 # 'frame variable' with basename 'j' should work, too.
Greg Clayton3bcdfc02012-12-04 00:32:51 +000074 self.expect("frame variable --show-declaration --show-globals j",
Johnny Chen3d57ee72010-11-11 23:29:54 +000075 startstr = "main.cpp:%d: (int) A::B::j = 4" % self.line_var_j)
76 # main.cpp:19: (int) A::B::j = 4
77
Johnny Chen6ba9d1f2010-11-15 18:27:57 +000078 # 'frame variable' should support address-of operator.
79 self.runCmd("frame variable &i")
80
81 # 'frame variable' with fully qualified name 'A::B::j' should work.
Johnny Chend71ffbc2010-12-23 23:26:05 +000082 self.expect("frame variable A::B::j", VARIABLES_DISPLAYED_CORRECTLY,
Johnny Chen8a815152011-07-28 00:52:23 +000083 startstr = '(int) A::B::j = 4',
Enrico Granata4d93b8c2013-09-30 19:11:51 +000084 patterns = [' = 4'])
Johnny Chen6ba9d1f2010-11-15 18:27:57 +000085
Johnny Chen913af512010-11-15 18:40:06 +000086 # So should the anonymous namespace case.
Johnny Chend71ffbc2010-12-23 23:26:05 +000087 self.expect("frame variable '(anonymous namespace)::i'", VARIABLES_DISPLAYED_CORRECTLY,
Johnny Chen8a815152011-07-28 00:52:23 +000088 startstr = '(int) (anonymous namespace)::i = 3',
Enrico Granata4d93b8c2013-09-30 19:11:51 +000089 patterns = [' = 3'])
Johnny Chen913af512010-11-15 18:40:06 +000090
Johnny Chen6df2cd32010-11-12 01:00:56 +000091 # rdar://problem/8660275
92 # test/namespace: 'expression -- i+j' not working
Johnny Chend71ffbc2010-12-23 23:26:05 +000093 # This has been fixed.
Johnny Chendeac5232010-11-15 17:42:22 +000094 self.expect("expression -- i + j",
95 startstr = "(int) $0 = 7")
Johnny Chen064d7f52010-11-12 00:50:45 +000096 # (int) $0 = 7
Johnny Chen3d57ee72010-11-11 23:29:54 +000097
Johnny Chend71ffbc2010-12-23 23:26:05 +000098 self.runCmd("expression -- i")
99 self.runCmd("expression -- j")
100
Johnny Chen8ed80e62010-11-15 18:49:03 +0000101 # rdar://problem/8668674
102 # expression command with fully qualified namespace for a variable does not work
Johnny Chen098781b2011-08-01 21:30:30 +0000103 self.expect("expression -- ::i", VARIABLES_DISPLAYED_CORRECTLY,
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000104 patterns = [' = 3'])
Johnny Chen098781b2011-08-01 21:30:30 +0000105 self.expect("expression -- A::B::j", VARIABLES_DISPLAYED_CORRECTLY,
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000106 patterns = [' = 4'])
Johnny Chen913af512010-11-15 18:40:06 +0000107
Matt Kopec201284a2013-05-13 22:00:32 +0000108 # expression command with function in anonymous namespace
109 self.expect("expression -- myanonfunc(3)",
110 patterns = [' = 6'])
111
112 # global namespace qualification with function in anonymous namespace
113 self.expect("expression -- ::myanonfunc(4)",
114 patterns = [' = 8'])
115
116 self.expect("p myanonfunc",
117 patterns = ['\(anonymous namespace\)::myanonfunc\(int\)'])
Johnny Chen913af512010-11-15 18:40:06 +0000118
Siva Chandra462722d2015-03-27 00:10:04 +0000119 self.expect("p variadic_sum",
120 patterns = ['\(anonymous namespace\)::variadic_sum\(int, ...\)'])
121
Johnny Chen3d57ee72010-11-11 23:29:54 +0000122if __name__ == '__main__':
123 import atexit
124 lldb.SBDebugger.Initialize()
125 atexit.register(lambda: lldb.SBDebugger.Terminate())
126 unittest2.main()