Skip to content

Commit 15a5e2d

Browse files
committed
updates for lcov
1 parent b9feb41 commit 15a5e2d

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

src/main/resources/scripts/generate_lcov.py

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
111131
def 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

src/main/resources/scripts/generate_xml.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ def __init__(self, FullManageProjectName, verbose, teePrint, use_cte):
9999

100100
if self.teePrint is None:
101101
self.teePrint = tee_print.TeePrint()
102-
self.teePrint.teePrint("BaseGenerateXml called incorrectly, no teePrint")
103102
#
104103
# BaseGenerateXml - calculate coverage value
105104
#
@@ -802,7 +801,8 @@ def generate_local_results(self, results, key):
802801
self.report_failed_only,
803802
self.print_exc,
804803
self.useStartLine,
805-
self.teePrint)
804+
self.teePrint,
805+
self.use_cte)
806806

807807
localXML.topLevelAPI = self.api
808808
localXML.noResults = self.noResults

src/main/resources/scripts/vcast_exec.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def __init__(self, args):
7070
self.pclp_input = args.pclp_input
7171

7272
self.html_base_dir = args.html_base_dir
73+
self.use_cte = args.use_cte
7374

7475
if args.exit_with_failed_count == 'not present':
7576
self.useJunitFailCountPct = False
@@ -214,7 +215,7 @@ def runJunitMetrics(self):
214215
self.useStartLine = False
215216

216217
self.failed_count, self.passed_count = generate_results.buildReports(self.FullMP,self.level,self.environment,
217-
True, self.timing, xml_data_dir = self.xml_data_dir, useStartLine=self.useStartLine)
218+
True, self.timing, xml_data_dir = self.xml_data_dir, useStartLine=self.useStartLine, teePrint = None, use_cte = self.use_cte)
218219

219220
# calculate the failed percentage
220221
if (self.failed_count + self.passed_count > 0):
@@ -340,6 +341,7 @@ def runExec(self):
340341
metricsGroup.add_argument('--cobertura_extended', help='Generate coverage results in extended Cobertura xml format', action="store_true", default = False)
341342
metricsGroup.add_argument('--lcov', help='Generate coverage results in an LCOV format', action="store_true", default = False)
342343
metricsGroup.add_argument('--junit', help='Generate test results in Junit xml format', action="store_true", default = False)
344+
metricsGroup.add_argument('--junit_use_cte_for_classname', help=argparse.SUPPRESS, action="store_true", dest="use_cte")
343345
metricsGroup.add_argument('--sonarqube', help='Generate test results in SonarQube Generic test execution report format (CppUnit)', action="store_true", default = False)
344346
metricsGroup.add_argument('--pclp_input', help='Generate static analysis results from PC-lint Plus XML file to generic static analysis format (codequality)', action="store", default = None)
345347
metricsGroup.add_argument('--pclp_output_html', help='Generate static analysis results from PC-lint Plus XML file to an HTML output', action="store", default = "pclp_findings.html")

0 commit comments

Comments
 (0)