blob: 4eaa2a7583dcd4ed78477769090d3502e16d5629 [file] [log] [blame]
Zachary Turnerff890da2015-10-19 23:45:41 +00001from __future__ import print_function
Zachary Turnerc1b7cd72015-11-05 19:22:28 +00002from __future__ import absolute_import
Zachary Turnerff890da2015-10-19 23:45:41 +00003
Zachary Turnerc1b7cd72015-11-05 19:22:28 +00004# System modules
Sean Callanan816cb3e2014-10-16 23:15:22 +00005import os
Sean Callanan816cb3e2014-10-16 23:15:22 +00006
Zachary Turnerc1b7cd72015-11-05 19:22:28 +00007# Third-party modules
8
9# LLDB modules
10import lldb
11from .lldbtest import *
12from . import lldbutil
Zachary Turner9a1a2942016-02-04 23:04:17 +000013from .decorators import *
Zachary Turnerc1b7cd72015-11-05 19:22:28 +000014
Sean Callanan816cb3e2014-10-16 23:15:22 +000015def source_type(filename):
16 _, extension = os.path.splitext(filename)
17 return {
18 '.c' : 'C_SOURCES',
19 '.cpp' : 'CXX_SOURCES',
20 '.cxx' : 'CXX_SOURCES',
21 '.cc' : 'CXX_SOURCES',
22 '.m' : 'OBJC_SOURCES',
23 '.mm' : 'OBJCXX_SOURCES'
24 }.get(extension, None)
25
Todd Fialad9be7532016-01-06 19:16:45 +000026
Sean Callanan816cb3e2014-10-16 23:15:22 +000027class CommandParser:
28 def __init__(self):
29 self.breakpoints = []
30
31 def parse_one_command(self, line):
32 parts = line.split('//%')
Sean Callanand950fa72014-10-17 00:39:37 +000033
34 command = None
35 new_breakpoint = True
36
37 if len(parts) == 2:
Todd Fialad9be7532016-01-06 19:16:45 +000038 command = parts[1].strip() # take off whitespace
Sean Callanand950fa72014-10-17 00:39:37 +000039 new_breakpoint = parts[0].strip() != ""
40
41 return (command, new_breakpoint)
Sean Callanan816cb3e2014-10-16 23:15:22 +000042
43 def parse_source_files(self, source_files):
44 for source_file in source_files:
45 file_handle = open(source_file)
46 lines = file_handle.readlines()
47 line_number = 0
Sean Callanand950fa72014-10-17 00:39:37 +000048 current_breakpoint = None # non-NULL means we're looking through whitespace to find additional commands
Sean Callanan816cb3e2014-10-16 23:15:22 +000049 for line in lines:
50 line_number = line_number + 1 # 1-based, so we do this first
Sean Callanand950fa72014-10-17 00:39:37 +000051 (command, new_breakpoint) = self.parse_one_command(line)
52
53 if new_breakpoint:
54 current_breakpoint = None
55
Sean Callanan816cb3e2014-10-16 23:15:22 +000056 if command != None:
Sean Callanand950fa72014-10-17 00:39:37 +000057 if current_breakpoint == None:
58 current_breakpoint = {}
59 current_breakpoint['file_name'] = source_file
60 current_breakpoint['line_number'] = line_number
61 current_breakpoint['command'] = command
62 self.breakpoints.append(current_breakpoint)
63 else:
64 current_breakpoint['command'] = current_breakpoint['command'] + "\n" + command
Sean Callanan816cb3e2014-10-16 23:15:22 +000065
66 def set_breakpoints(self, target):
67 for breakpoint in self.breakpoints:
68 breakpoint['breakpoint'] = target.BreakpointCreateByLocation(breakpoint['file_name'], breakpoint['line_number'])
69
70 def handle_breakpoint(self, test, breakpoint_id):
71 for breakpoint in self.breakpoints:
72 if breakpoint['breakpoint'].GetID() == breakpoint_id:
73 test.execute_user_command(breakpoint['command'])
74 return
75
Sean Callanan816cb3e2014-10-16 23:15:22 +000076class InlineTest(TestBase):
77 # Internal implementation
78
Greg Clayton70995582015-01-07 22:25:50 +000079 def getRerunArgs(self):
80 # The -N option says to NOT run a if it matches the option argument, so
81 # if we are using dSYM we say to NOT run dwarf (-N dwarf) and vice versa.
Siva Chandra311c7db2015-01-09 01:54:44 +000082 if self.using_dsym is None:
83 # The test was skipped altogether.
84 return ""
85 elif self.using_dsym:
Greg Clayton70995582015-01-07 22:25:50 +000086 return "-N dwarf %s" % (self.mydir)
87 else:
88 return "-N dsym %s" % (self.mydir)
Todd Fialad9be7532016-01-06 19:16:45 +000089
Greg Clayton70995582015-01-07 22:25:50 +000090 def BuildMakefile(self):
91 if os.path.exists("Makefile"):
92 return
Sean Callanan816cb3e2014-10-16 23:15:22 +000093
Greg Clayton70995582015-01-07 22:25:50 +000094 categories = {}
95
96 for f in os.listdir(os.getcwd()):
97 t = source_type(f)
98 if t:
Zachary Turner606e1e32015-10-23 17:53:51 +000099 if t in list(categories.keys()):
Greg Clayton70995582015-01-07 22:25:50 +0000100 categories[t].append(f)
101 else:
102 categories[t] = [f]
103
104 makefile = open("Makefile", 'w+')
105
106 level = os.sep.join([".."] * len(self.mydir.split(os.sep))) + os.sep + "make"
107
108 makefile.write("LEVEL = " + level + "\n")
109
Zachary Turner606e1e32015-10-23 17:53:51 +0000110 for t in list(categories.keys()):
Greg Clayton70995582015-01-07 22:25:50 +0000111 line = t + " := " + " ".join(categories[t])
112 makefile.write(line + "\n")
113
Zachary Turner606e1e32015-10-23 17:53:51 +0000114 if ('OBJCXX_SOURCES' in list(categories.keys())) or ('OBJC_SOURCES' in list(categories.keys())):
Greg Clayton70995582015-01-07 22:25:50 +0000115 makefile.write("LDFLAGS = $(CFLAGS) -lobjc -framework Foundation\n")
116
Zachary Turner606e1e32015-10-23 17:53:51 +0000117 if ('CXX_SOURCES' in list(categories.keys())):
Greg Clayton70995582015-01-07 22:25:50 +0000118 makefile.write("CXXFLAGS += -std=c++11\n")
119
120 makefile.write("include $(LEVEL)/Makefile.rules\n")
Sean Callananbca81c52016-01-26 01:15:57 +0000121 makefile.write("\ncleanup:\n\trm -f Makefile *.d\n\n")
Greg Clayton70995582015-01-07 22:25:50 +0000122 makefile.flush()
123 makefile.close()
124
Robert Flack13c7ad92015-03-30 14:12:17 +0000125 @skipUnlessDarwin
Sean Callanane17428a2014-10-28 20:23:20 +0000126 def __test_with_dsym(self):
Greg Clayton70995582015-01-07 22:25:50 +0000127 self.using_dsym = True
128 self.BuildMakefile()
129 self.buildDsym()
Sean Callanan816cb3e2014-10-16 23:15:22 +0000130 self.do_test()
131
Sean Callanane17428a2014-10-28 20:23:20 +0000132 def __test_with_dwarf(self):
Greg Clayton70995582015-01-07 22:25:50 +0000133 self.using_dsym = False
134 self.BuildMakefile()
135 self.buildDwarf()
Sean Callanan816cb3e2014-10-16 23:15:22 +0000136 self.do_test()
137
Enrico Granata238de512015-10-19 20:40:50 +0000138 def __test_with_dwo(self):
139 self.using_dsym = False
140 self.BuildMakefile()
141 self.buildDwo()
142 self.do_test()
143
Sean Callanan816cb3e2014-10-16 23:15:22 +0000144 def execute_user_command(self, __command):
Zachary Turner5821f792015-11-06 21:37:21 +0000145 exec(__command, globals(), locals())
Sean Callanan816cb3e2014-10-16 23:15:22 +0000146
147 def do_test(self):
148 exe_name = "a.out"
149 exe = os.path.join(os.getcwd(), exe_name)
150 source_files = [ f for f in os.listdir(os.getcwd()) if source_type(f) ]
151 target = self.dbg.CreateTarget(exe)
152
153 parser = CommandParser()
154 parser.parse_source_files(source_files)
155 parser.set_breakpoints(target)
156
157 process = target.LaunchSimple(None, None, os.getcwd())
158
159 while lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint):
160 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
161 breakpoint_id = thread.GetStopReasonDataAtIndex (0)
162 parser.handle_breakpoint(self, breakpoint_id)
163 process.Continue()
164
Sean Callanan816cb3e2014-10-16 23:15:22 +0000165
166 # Utilities for testcases
167
168 def check_expression (self, expression, expected_result, use_summary = True):
169 value = self.frame().EvaluateExpression (expression)
170 self.assertTrue(value.IsValid(), expression+"returned a valid value")
171 if self.TraceOn():
Zachary Turnerff890da2015-10-19 23:45:41 +0000172 print(value.GetSummary())
173 print(value.GetValue())
Sean Callanan816cb3e2014-10-16 23:15:22 +0000174 if use_summary:
175 answer = value.GetSummary()
176 else:
177 answer = value.GetValue()
178 report_str = "%s expected: %s got: %s"%(expression, expected_result, answer)
179 self.assertTrue(answer == expected_result, report_str)
180
Sean Callanane17428a2014-10-28 20:23:20 +0000181def ApplyDecoratorsToFunction(func, decorators):
182 tmp = func
183 if type(decorators) == list:
184 for decorator in decorators:
185 tmp = decorator(tmp)
186 elif hasattr(decorators, '__call__'):
187 tmp = decorators(tmp)
188 return tmp
189
190
191def MakeInlineTest(__file, __globals, decorators=None):
Sean Callanan816cb3e2014-10-16 23:15:22 +0000192 # Derive the test name from the current file name
193 file_basename = os.path.basename(__file)
194 InlineTest.mydir = TestBase.compute_mydir(__file)
195
196 test_name, _ = os.path.splitext(file_basename)
197 # Build the test case
Siva Chandra311c7db2015-01-09 01:54:44 +0000198 test = type(test_name, (InlineTest,), {'using_dsym': None})
Sean Callanan816cb3e2014-10-16 23:15:22 +0000199 test.name = test_name
Sean Callanane17428a2014-10-28 20:23:20 +0000200
201 test.test_with_dsym = ApplyDecoratorsToFunction(test._InlineTest__test_with_dsym, decorators)
202 test.test_with_dwarf = ApplyDecoratorsToFunction(test._InlineTest__test_with_dwarf, decorators)
Enrico Granata238de512015-10-19 20:40:50 +0000203 test.test_with_dwo = ApplyDecoratorsToFunction(test._InlineTest__test_with_dwo, decorators)
Sean Callanane17428a2014-10-28 20:23:20 +0000204
Sean Callanan816cb3e2014-10-16 23:15:22 +0000205 # Add the test case to the globals, and hide InlineTest
206 __globals.update({test_name : test})
Todd Fiala5bdbef642015-12-22 17:14:47 +0000207
208 # Store the name of the originating file.o
209 test.test_filename = __file
Enrico Granataab0e8312014-11-05 21:31:57 +0000210 return test
Sean Callanan816cb3e2014-10-16 23:15:22 +0000211