blob: a03f3c800d5880616fd02cd528a231d052a8485c [file] [log] [blame]
Zachary Turnerff890da2015-10-19 23:45:41 +00001from __future__ import print_function
2
Sean Callanan816cb3e2014-10-16 23:15:22 +00003import lldb
4from lldbtest import *
5import lldbutil
6import os
Sean Callanane17428a2014-10-28 20:23:20 +00007import sys
Sean Callanan816cb3e2014-10-16 23:15:22 +00008
9def source_type(filename):
10 _, extension = os.path.splitext(filename)
11 return {
12 '.c' : 'C_SOURCES',
13 '.cpp' : 'CXX_SOURCES',
14 '.cxx' : 'CXX_SOURCES',
15 '.cc' : 'CXX_SOURCES',
16 '.m' : 'OBJC_SOURCES',
17 '.mm' : 'OBJCXX_SOURCES'
18 }.get(extension, None)
19
20class CommandParser:
21 def __init__(self):
22 self.breakpoints = []
23
24 def parse_one_command(self, line):
25 parts = line.split('//%')
Sean Callanand950fa72014-10-17 00:39:37 +000026
27 command = None
28 new_breakpoint = True
29
30 if len(parts) == 2:
31 command = parts[1].strip() # take off whitespace
32 new_breakpoint = parts[0].strip() != ""
33
34 return (command, new_breakpoint)
Sean Callanan816cb3e2014-10-16 23:15:22 +000035
36 def parse_source_files(self, source_files):
37 for source_file in source_files:
38 file_handle = open(source_file)
39 lines = file_handle.readlines()
40 line_number = 0
Sean Callanand950fa72014-10-17 00:39:37 +000041 current_breakpoint = None # non-NULL means we're looking through whitespace to find additional commands
Sean Callanan816cb3e2014-10-16 23:15:22 +000042 for line in lines:
43 line_number = line_number + 1 # 1-based, so we do this first
Sean Callanand950fa72014-10-17 00:39:37 +000044 (command, new_breakpoint) = self.parse_one_command(line)
45
46 if new_breakpoint:
47 current_breakpoint = None
48
Sean Callanan816cb3e2014-10-16 23:15:22 +000049 if command != None:
Sean Callanand950fa72014-10-17 00:39:37 +000050 if current_breakpoint == None:
51 current_breakpoint = {}
52 current_breakpoint['file_name'] = source_file
53 current_breakpoint['line_number'] = line_number
54 current_breakpoint['command'] = command
55 self.breakpoints.append(current_breakpoint)
56 else:
57 current_breakpoint['command'] = current_breakpoint['command'] + "\n" + command
Sean Callanan816cb3e2014-10-16 23:15:22 +000058
59 def set_breakpoints(self, target):
60 for breakpoint in self.breakpoints:
61 breakpoint['breakpoint'] = target.BreakpointCreateByLocation(breakpoint['file_name'], breakpoint['line_number'])
62
63 def handle_breakpoint(self, test, breakpoint_id):
64 for breakpoint in self.breakpoints:
65 if breakpoint['breakpoint'].GetID() == breakpoint_id:
66 test.execute_user_command(breakpoint['command'])
67 return
68
Sean Callanan816cb3e2014-10-16 23:15:22 +000069class InlineTest(TestBase):
70 # Internal implementation
71
Greg Clayton70995582015-01-07 22:25:50 +000072 def getRerunArgs(self):
73 # The -N option says to NOT run a if it matches the option argument, so
74 # if we are using dSYM we say to NOT run dwarf (-N dwarf) and vice versa.
Siva Chandra311c7db2015-01-09 01:54:44 +000075 if self.using_dsym is None:
76 # The test was skipped altogether.
77 return ""
78 elif self.using_dsym:
Greg Clayton70995582015-01-07 22:25:50 +000079 return "-N dwarf %s" % (self.mydir)
80 else:
81 return "-N dsym %s" % (self.mydir)
82
83 def BuildMakefile(self):
84 if os.path.exists("Makefile"):
85 return
Sean Callanan816cb3e2014-10-16 23:15:22 +000086
Greg Clayton70995582015-01-07 22:25:50 +000087 categories = {}
88
89 for f in os.listdir(os.getcwd()):
90 t = source_type(f)
91 if t:
Zachary Turner606e1e32015-10-23 17:53:51 +000092 if t in list(categories.keys()):
Greg Clayton70995582015-01-07 22:25:50 +000093 categories[t].append(f)
94 else:
95 categories[t] = [f]
96
97 makefile = open("Makefile", 'w+')
98
99 level = os.sep.join([".."] * len(self.mydir.split(os.sep))) + os.sep + "make"
100
101 makefile.write("LEVEL = " + level + "\n")
102
Zachary Turner606e1e32015-10-23 17:53:51 +0000103 for t in list(categories.keys()):
Greg Clayton70995582015-01-07 22:25:50 +0000104 line = t + " := " + " ".join(categories[t])
105 makefile.write(line + "\n")
106
Zachary Turner606e1e32015-10-23 17:53:51 +0000107 if ('OBJCXX_SOURCES' in list(categories.keys())) or ('OBJC_SOURCES' in list(categories.keys())):
Greg Clayton70995582015-01-07 22:25:50 +0000108 makefile.write("LDFLAGS = $(CFLAGS) -lobjc -framework Foundation\n")
109
Zachary Turner606e1e32015-10-23 17:53:51 +0000110 if ('CXX_SOURCES' in list(categories.keys())):
Greg Clayton70995582015-01-07 22:25:50 +0000111 makefile.write("CXXFLAGS += -std=c++11\n")
112
113 makefile.write("include $(LEVEL)/Makefile.rules\n")
114 makefile.flush()
115 makefile.close()
116
Sean Callanan816cb3e2014-10-16 23:15:22 +0000117
Robert Flack13c7ad92015-03-30 14:12:17 +0000118 @skipUnlessDarwin
Sean Callanane17428a2014-10-28 20:23:20 +0000119 def __test_with_dsym(self):
Greg Clayton70995582015-01-07 22:25:50 +0000120 self.using_dsym = True
121 self.BuildMakefile()
122 self.buildDsym()
Sean Callanan816cb3e2014-10-16 23:15:22 +0000123 self.do_test()
124
Sean Callanane17428a2014-10-28 20:23:20 +0000125 def __test_with_dwarf(self):
Greg Clayton70995582015-01-07 22:25:50 +0000126 self.using_dsym = False
127 self.BuildMakefile()
128 self.buildDwarf()
Sean Callanan816cb3e2014-10-16 23:15:22 +0000129 self.do_test()
130
Enrico Granata238de512015-10-19 20:40:50 +0000131 def __test_with_dwo(self):
132 self.using_dsym = False
133 self.BuildMakefile()
134 self.buildDwo()
135 self.do_test()
136
Sean Callanan816cb3e2014-10-16 23:15:22 +0000137 def execute_user_command(self, __command):
138 exec __command in globals(), locals()
139
140 def do_test(self):
141 exe_name = "a.out"
142 exe = os.path.join(os.getcwd(), exe_name)
143 source_files = [ f for f in os.listdir(os.getcwd()) if source_type(f) ]
144 target = self.dbg.CreateTarget(exe)
145
146 parser = CommandParser()
147 parser.parse_source_files(source_files)
148 parser.set_breakpoints(target)
149
150 process = target.LaunchSimple(None, None, os.getcwd())
151
152 while lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint):
153 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
154 breakpoint_id = thread.GetStopReasonDataAtIndex (0)
155 parser.handle_breakpoint(self, breakpoint_id)
156 process.Continue()
157
Sean Callanan816cb3e2014-10-16 23:15:22 +0000158
159 # Utilities for testcases
160
161 def check_expression (self, expression, expected_result, use_summary = True):
162 value = self.frame().EvaluateExpression (expression)
163 self.assertTrue(value.IsValid(), expression+"returned a valid value")
164 if self.TraceOn():
Zachary Turnerff890da2015-10-19 23:45:41 +0000165 print(value.GetSummary())
166 print(value.GetValue())
Sean Callanan816cb3e2014-10-16 23:15:22 +0000167 if use_summary:
168 answer = value.GetSummary()
169 else:
170 answer = value.GetValue()
171 report_str = "%s expected: %s got: %s"%(expression, expected_result, answer)
172 self.assertTrue(answer == expected_result, report_str)
173
Sean Callanane17428a2014-10-28 20:23:20 +0000174def ApplyDecoratorsToFunction(func, decorators):
175 tmp = func
176 if type(decorators) == list:
177 for decorator in decorators:
178 tmp = decorator(tmp)
179 elif hasattr(decorators, '__call__'):
180 tmp = decorators(tmp)
181 return tmp
182
183
184def MakeInlineTest(__file, __globals, decorators=None):
Sean Callanan816cb3e2014-10-16 23:15:22 +0000185 # Derive the test name from the current file name
186 file_basename = os.path.basename(__file)
187 InlineTest.mydir = TestBase.compute_mydir(__file)
188
189 test_name, _ = os.path.splitext(file_basename)
190 # Build the test case
Siva Chandra311c7db2015-01-09 01:54:44 +0000191 test = type(test_name, (InlineTest,), {'using_dsym': None})
Sean Callanan816cb3e2014-10-16 23:15:22 +0000192 test.name = test_name
Sean Callanane17428a2014-10-28 20:23:20 +0000193
194 test.test_with_dsym = ApplyDecoratorsToFunction(test._InlineTest__test_with_dsym, decorators)
195 test.test_with_dwarf = ApplyDecoratorsToFunction(test._InlineTest__test_with_dwarf, decorators)
Enrico Granata238de512015-10-19 20:40:50 +0000196 test.test_with_dwo = ApplyDecoratorsToFunction(test._InlineTest__test_with_dwo, decorators)
Sean Callanane17428a2014-10-28 20:23:20 +0000197
Sean Callanan816cb3e2014-10-16 23:15:22 +0000198 # Add the test case to the globals, and hide InlineTest
199 __globals.update({test_name : test})
Enrico Granataab0e8312014-11-05 21:31:57 +0000200
201 return test
Sean Callanan816cb3e2014-10-16 23:15:22 +0000202