@@ -107,7 +107,27 @@ def has_branches_covered(line):
107107 line .metrics .max_covered_mcdc_pairs )
108108
109109 return count
110-
110+
111+ def get_function_name_line_number (file_path , function , initial_guess ):
112+
113+ with open (file_path ,"r" ) as fd :
114+ lines = fd .readlines ()
115+
116+ line_number_closest_so_far = initial_guess ;
117+ delta = 9999999999 ;
118+
119+ # print(function, line_number_closest_so_far, delta, initial_guess)
120+ for count , line in enumerate (reversed (lines [:initial_guess + 1 ])):
121+ if function in line .replace (" " ,"" ):
122+ line_num = initial_guess - count
123+ if abs (line_num - initial_guess ) < delta :
124+ line_number_closest_so_far = line_num
125+ delta = abs (line_num - initial_guess )
126+ # print(function, line_number_closest_so_far, delta, initial_guess)
127+
128+ # print(line_number_closest_so_far + 1,function)
129+ return line_number_closest_so_far + 1 ## add one since python starts from 0
130+
111131def runCoverageResultsMP (mpFile , verbose = False , testName = "" , source_root = "" ):
112132
113133 vcproj = VCProjectApi (mpFile )
@@ -136,9 +156,9 @@ def runGcovResults(api, verbose = False, testName = "", source_root = "") :
136156 fname = file .display_name
137157 fpath = file .display_path .rsplit ('.' ,1 )[0 ]
138158 fpath = os .path .relpath (fpath ,prj_dir ).replace ("\\ " ,"/" )
139-
159+
140160 fileDict [fpath ] = file
141-
161+
142162 output = ""
143163
144164 for path in sorted (fileDict .keys ()):
@@ -160,10 +180,11 @@ def runGcovResults(api, verbose = False, testName = "", source_root = "") :
160180 output += "TN:" + testName + "\n "
161181 new_path = new_path .replace ("\\ " ,"/" )
162182 output += "SF:" + new_path + "/" + file .name + "\n "
163-
183+
164184 for func in file .functions :
185+ func_name_line_number = get_function_name_line_number (file .display_path , func .name , func .start_line )
165186 fName = func .name + func .instrumented_functions [0 ].parameterized_name .replace (func .name ,"" ,1 )
166- FN .append ("FN:" + str (func . start_line ) + "," + fName )
187+ FN .append ("FN:" + str (func_name_line_number ) + "," + fName )
167188 if has_anything_covered (func ) > 0 :
168189 FNDA .append ("FNDA:1" + "," + fName )
169190 else :
@@ -174,16 +195,27 @@ def runGcovResults(api, verbose = False, testName = "", source_root = "") :
174195 line_branch = []
175196
176197 last_line = ""
198+ any_line_covered = 0
199+ any_return_found = False
200+ found_func_start = False
177201
178202 for line in func .iterate_coverage ():
179203 if has_any_coverage (line ):
180204 LF += 1
181205 if has_anything_covered (line ):
182206 lineCovered = "1"
183207 LH += 1
208+ any_line_covered += 1
184209 else :
185210 lineCovered = "0"
186- DA .append ("DA:" + str (line .line_number ) + "," + lineCovered )
211+
212+ if " return" in line .text :
213+ any_return_found = True
214+ if not found_func_start :
215+ DA .append ("DA:" + str (func_name_line_number ) + "," + lineCovered )
216+ found_func_start = True
217+ else :
218+ DA .append ("DA:" + str (line .line_number ) + "," + lineCovered )
187219
188220 last_line = line .text
189221
@@ -207,9 +239,12 @@ def runGcovResults(api, verbose = False, testName = "", source_root = "") :
207239 branch_number += 1
208240
209241
210- if "return" not in last_line :
242+ if True : # not any_return_found :
211243 if verbose : print ("counting last line: " , func .name , line .line_number ,last_line )
212- DA .append ("DA:" + str (line .line_number ) + ",1" )
244+ if any_line_covered > 0 :
245+ DA .append ("DA:" + str (line .line_number ) + ",1" )
246+ else :
247+ DA .append ("DA:" + str (line .line_number ) + ",0" )
213248 else :
214249 if verbose : print ("not counting last line: " , func .name , line .line_number ,last_line )
215250
0 commit comments