| | 1 | | #pragma rtGlobals=3 |
| | 2 | | #pragma TextEncoding="UTF-8" |
| | 3 | | #pragma rtFunctionErrors=1 |
| | 4 | | #pragma version=1.10 |
| | 5 | | #pragma ModuleName=IUTF_Tracing_Cobertura |
| | 6 | |
|
| | 7 | | #undef UTF_ALLOW_TRACING |
| | 8 | | #if Exists("TUFXOP_Version") |
| | 9 | |
|
| | 10 | | #if IgorVersion() >= 10.00 |
| | 11 | | #define UTF_ALLOW_TRACING |
| | 12 | | #elif (IgorVersion() >= 9.00) && (NumberByKey("BUILD", IgorInfo(0)) >= 38812) |
| | 13 | | #define UTF_ALLOW_TRACING |
| | 14 | | #endif |
| | 15 | |
|
| | 16 | | #endif |
| | 17 | |
|
| | 18 | | #ifdef UTF_ALLOW_TRACING |
| | 19 | |
|
| | 20 | | // file size limit to show a warning banner. Some Cobertura consumers like Gitlab have a hardcoded |
| | 21 | | // limit after which no cobertura files can no longer be read. The limit for Gitlab is at 10 MB but |
| | 22 | | // this will show the warning a bit early to leave the user some room to take some preparation. |
| | 23 | | static Constant FILESIZE_WARNING_LIMIT = 8000000 |
| | 24 | |
|
| | 25 | | // Empty function signature without any arguments. |
| | 26 | | static StrConstant EMPTY_FUNC_SIGNATURE = "()" |
| | 27 | |
|
| | 28 | | /// @brief The Cobertura information for one procedure file. This is needed to generate a valid |
| | 29 | | /// Cobertura file. |
| | 30 | | /// |
| | 31 | | /// sourcePath: The source path where the procedure file can be found. The source path can be any |
| | 32 | | /// directory, the procedure file just have to be in any sub-directory in it. This is usually |
| | 33 | | /// one of theuser defined values. |
| | 34 | | /// packageName: A package name is a part of the directory path from the source path to the |
| | 35 | | /// procedure file. The package name doesn't contain the name of the procedure file itself. If |
| | 36 | | /// the procedure file is located directly in the source path the package name is an empty |
| | 37 | | /// string. |
| | 38 | | /// classFileName: The class file name is the relative path from the source path to the procedure |
| | 39 | | /// file. |
| | 40 | | /// className: The class name is like the class file name. The file extension is removed and all |
| | 41 | | /// directory delimiters are replaced with dots. |
| | 42 | | Structure IUTF_Cobertura_ProcInfo |
| | 43 | | string sourcePath |
| | 44 | | string packageName |
| | 45 | | string classFileName |
| | 46 | | string className |
| | 47 | | EndStructure |
| | 48 | |
|
| | 49 | | /// @brief Contains the coverage metrics about a section of the code (this could be a function/macro |
| | 50 | | /// or the whole file). |
| | 51 | | /// |
| | 52 | | /// lineRate: The result of lineCovered/lineValid. If lineValid is 0 lineRate is set to 1. |
| | 53 | | /// lineCovered: The number of valid lines that has at least one hit. |
| | 54 | | /// lineValid: The number of lines that are marked as instrumented and therefore user code. |
| | 55 | | /// branchRate: The result of branchCovered/branchValid. If branchValid is 0 branchRate is set to 1. |
| | 56 | | /// branchCovered: The number of valid branches that has at least one hit. |
| | 57 | | /// branchValid: The number of branches that could be instrumented. A branch is a point where the |
| | 58 | | /// user code branches and can execute different parts depending on a condition (like an |
| | 59 | | /// if-statement). |
| | 60 | | Structure IUTF_Cobertura_Metrics |
| | 61 | | variable lineRate |
| | 62 | | variable lineCovered |
| | 63 | | variable lineValid |
| | 64 | | variable branchRate |
| | 65 | | variable branchCovered |
| | 66 | | variable branchValid |
| | 67 | | EndStructure |
| | 68 | |
|
| | 69 | | // Convert a file path suitable for Cobertura XML. This will replace all back-slash with a |
| | 70 | | // forward-slash. |
| | 71 | | // This is similar to ParseFilePath(5, path, "/", 0, 0) which isn't supported in Igor 9. |
| 5 | 72 | | static Function/S FilePathToXml(string path) |
| 5 | 73 | | path = ReplaceString("\\", path, "/") |
| 5 | 74 | | path = IUTF_Utils_Xml#ToXmlToken(path) |
| | 75 | |
|
| 5 | 76 | | return path |
| 5 | 77 | | End |
| | 78 | |
|
| | 79 | | // Returns the Cobertura class name of a relative file name. This will trim the file extension and |
| | 80 | | // replace directory delimiter with a dots. |
| 40 | 81 | | static Function/S GetClassName(string fileName) |
| 40 | 82 | | string className |
| | 83 | |
|
| 40 | 84 | | string dir = ParseFilePath(1, fileName, "\\", 1, 0) |
| 40 | 85 | | string rawName = ParseFilePath(3, fileName, "\\", 0, 0) |
| 40 | 86 | | sprintf className, "%s%s", ReplaceString("\\", dir, "."), rawName |
| | 87 | |
|
| 40 | 88 | | return className |
| 40 | 89 | | End |
| | 90 | |
|
| | 91 | | /// @brief Fill info with the required path information of a procedure file for the cobertura output |
| | 92 | | /// |
| | 93 | | /// @param procPath The full path of the procedure file |
| | 94 | | /// @param sources A wave with valid source paths. It will use the first entry which contains |
| | 95 | | /// procPath. If no source contains procPath the parent directory of procPath will |
| | 96 | | /// be used as source. |
| | 97 | | /// |
| | 98 | | /// @retval info The information that can be used for the Cobertura output. |
| 20 | 99 | | static Function [STRUCT IUTF_Cobertura_ProcInfo info] GetProcInfo(string procPath, WAVE/T sources) |
| 20 | 100 | | variable i, length |
| | 101 | |
|
| 20 | 102 | | string dirPath = IUTF_Utils_Paths#GetDirPathOfFile(procPath) |
| 20 | 103 | | variable size = DimSize(sources, UTF_ROW) |
| | 104 | |
|
| 20 | 105 | | procPath = ParseFilePath(5, procPath, "\\", 0, 0) |
| | 106 | |
|
| 20 | 107 | | for(i = 0; i < size; i += 1) |
| 30 | 108 | | if(!IUTF_Utils_Strings#IsPrefix(dirPath, sources[i])) |
| 15 | 109 | | continue |
| 15 | 110 | | endif |
| | 111 | |
|
| 15 | 112 | | info.sourcePath = RemoveEnding(sources[i], "\\") |
| 15 | 113 | | info.packageName = RemoveEnding(dirPath[strlen(sources[i]), Inf], "\\") |
| 15 | 114 | | info.classFileName = procPath[strlen(sources[i]), Inf] |
| 15 | 115 | | info.className = GetClassName(info.classFileName) |
| | 116 | |
|
| 15 | 117 | | return |
| 0 | 118 | | endfor |
| | 119 | |
|
| 5 | 120 | | info.sourcePath = RemoveEnding(dirPath, "\\") |
| 5 | 121 | | info.packageName = "" |
| 5 | 122 | | info.classFileName = procPath[strlen(dirPath), Inf] |
| 5 | 123 | | info.className = GetClassName(info.classFileName) |
| 20 | 124 | | End |
| | 125 | |
|
| | 126 | | /// @brief Creates the lines report for the Cobertura output. At the same time it will generate some |
| | 127 | | /// metrics which can be used at other places in the Cobertura output. |
| | 128 | | /// |
| | 129 | | /// @param indent The indent string that is prefixed for each output line. |
| | 130 | | /// @param lineStart the inclusive line index where the report should start |
| | 131 | | /// @param lineEnd the exclusive line index where the report should end |
| | 132 | | /// @param procIndex The index of the procedure file |
| | 133 | | /// @param totals The totals wave with the data from the instrumentation run |
| | 134 | | /// @param marker The wave with can tell which line in the source file was instrumented |
| | 135 | | /// |
| | 136 | | /// @retval report The report XML |
| | 137 | | /// @retval metrics The collected metrics for the specified lines section and procedure file |
| 15 | 138 | | static Function [string report, STRUCT IUTF_Cobertura_Metrics metrics] GetLinesReport(string indent, variable lineStart, |
| 15 | 139 | | string line, coverage |
| 15 | 140 | | variable i, execC, nobranchC, branchC, sum |
| | 141 | |
|
| 15 | 142 | | variable totalLines, coveredLines, totalBranches, coveredBranches |
| | 143 | |
|
| 15 | 144 | | report = indent + "<lines>\n" |
| | 145 | |
|
| 15 | 146 | | for(i = lineStart; i < lineEnd; i += 1) |
| 40 | 147 | | if(!marker[i][%INSTR]) |
| 10 | 148 | | continue |
| 30 | 149 | | endif |
| | 150 | |
|
| 30 | 151 | | execC = totals[i][0][procIndex] |
| 30 | 152 | | nobranchC = totals[i][1][procIndex] |
| 30 | 153 | | branchC = totals[i][2][procIndex] |
| | 154 | |
|
| 30 | 155 | | totalLines += 1 |
| 30 | 156 | | coveredLines += execC > 0 |
| | 157 | |
|
| 30 | 158 | | if(noBranchC + branchC) |
| 5 | 159 | | sum = (nobranchC > 0) + (branchC > 0) |
| 5 | 160 | | sprintf coverage, "%g%% (%d/2)", sum * 50, sum |
| 5 | 161 | | totalBranches += 2 |
| 5 | 162 | | coveredBranches += sum |
| | 163 | |
|
| 5 | 164 | | sprintf line, "%s\t<line number=\"%d\" hits=\"%d\" branch=\"true\" condition-coverage=\"%s\">\n", indent, i + 1, e |
| 5 | 165 | | report += line |
| 5 | 166 | | report += indent + "\t\t<conditions>\n" |
| | 167 | |
|
| 5 | 168 | | sprintf line, "%s\t\t\t<condition number=\"0\" type=\"jump\" coverage=\"%g%%\"/>\n", indent, sum * 50 |
| 5 | 169 | | report += line |
| | 170 | |
|
| 5 | 171 | | report += indent + "\t\t</conditions>\n" |
| 5 | 172 | | report += indent + "\t</line>\n" |
| 25 | 173 | | else |
| 25 | 174 | | sprintf line, "%s\t<line number=\"%d\" hits=\"%d\" branch=\"false\"/>\n", indent, i + 1, execC |
| 25 | 175 | | report += line |
| 30 | 176 | | endif |
| 30 | 177 | | endfor |
| | 178 | |
|
| 15 | 179 | | report += indent + "</lines>\n" |
| | 180 | |
|
| 15 | 181 | | metrics.lineCovered = coveredLines |
| 15 | 182 | | metrics.lineValid = totalLines |
| 15 | 183 | | metrics.lineRate = totalLines == 0 ? 1 : coveredLines / totalLines |
| 15 | 184 | | metrics.branchCovered = coveredBranches |
| 15 | 185 | | metrics.branchValid = totalBranches |
| 15 | 186 | | metrics.branchRate = totalBranches == 0 ? 1 : coveredBranches / totalBranches |
| 15 | 187 | | End |
| | 188 | |
|
| | 189 | | /// @brief Creates a report of a specific function inside a procedure file. |
| | 190 | | /// |
| | 191 | | /// @param funcName The name of the function |
| | 192 | | /// @param funcStart The inclusive line index where the function starts |
| | 193 | | /// @param funcEnd The exclusive line index where the function ends |
| | 194 | | /// @param procName The name of the procedure file |
| | 195 | | /// @param procIndex The index of the procedure file |
| | 196 | | /// @param totals The totals wave with the data from the instrumentation run |
| | 197 | | /// @param marker The wave with can tell which line in the source file was instrumented |
| | 198 | | /// |
| | 199 | | /// @returns The report XML |
| 0 | 200 | | static Function/S GetFunctionReport(string funcName, variable funcStart, variable funcEnd, string procName, variable pro |
| 0 | 201 | | string report, linesReport, line, fullFuncName, msg |
| 0 | 202 | | STRUCT IUTF_Cobertura_Metrics metrics |
| 0 | 203 | | variable err, complexity, complexIndex |
| | 204 | |
|
| 0 | 205 | | complexIndex = FindDimLabel(marker, UTF_COLUMN, "COMPLEX") |
| 0 | 206 | | WaveStats/M=1/Q/Z/RMD=[funcStart, funcEnd - 1][complexIndex, complexIndex] marker |
| 0 | 207 | | complexity = V_sum |
| | 208 | |
|
| 0 | 209 | | [linesReport, metrics] = GetLinesReport("\t\t\t\t\t\t\t", funcStart, funcEnd, procIndex, totals, marker) |
| | 210 | |
|
| 0 | 211 | | fullFuncName = IUTF_Basics#getFullFunctionName(err, funcName, procName) |
| 0 | 212 | | if(err) |
| 0 | 213 | | // possibly a macro which has no full function name |
| 0 | 214 | | fullFuncName = funcName |
| 0 | 215 | | endif |
| | 216 | |
|
| 0 | 217 | | sprintf line, "\t\t\t\t\t\t<method name=\"%s\" signature=\"%s\" line-rate=\"%f\" branch-rate=\"%f\" complexity=\"%d\"> |
| 0 | 218 | | report = line |
| | 219 | |
|
| 0 | 220 | | report += linesReport |
| | 221 | |
|
| 0 | 222 | | report += "\t\t\t\t\t\t</method>\n" |
| 0 | 223 | | return report |
| 0 | 224 | | End |
| | 225 | |
|
| | 226 | | /// @brief Creates a report of a procedure file |
| | 227 | | /// |
| | 228 | | /// @brief procName The name of the procedure file |
| | 229 | | /// @brief procPath The path to the procedure file |
| | 230 | | /// @brief procIndex The index of the procedure file |
| | 231 | | /// @param totals The totals wave with the data from the instrumentation run |
| | 232 | | /// @param marker The wave with can tell which line in the source file was instrumented |
| | 233 | | /// @param sources The source wave which contains all valid source paths |
| | 234 | | /// |
| | 235 | | /// @returns The report XML |
| 0 | 236 | | static Function/S GetProcedureReport(string procName, string procPath, variable procIndex, WAVE totals, WAVE marker, WAV |
| 0 | 237 | | string msg, report, line |
| 0 | 238 | | variable i, funcCount, funcEnd, epochTime, complexity, complexIndex |
| 0 | 239 | | STRUCT IUTF_Cobertura_ProcInfo info |
| 0 | 240 | | STRUCT IUTF_Cobertura_Metrics metrics |
| 0 | 241 | | string linesReport |
| | 242 | |
|
| 0 | 243 | | [info] = GetProcInfo(procPath, sources) |
| 0 | 244 | | WAVE/WAVE funcLocations = IUTF_Tracing#GetFuncLocations() |
| 0 | 245 | | WAVE/T procFuncNames = funcLocations[procIndex][%FUNCLIST] |
| 0 | 246 | | WAVE procFuncLines = funcLocations[procIndex][%FUNCSTART] |
| 0 | 247 | | funcCount = DimSize(procFuncNames, UTF_ROW) |
| | 248 | |
|
| 0 | 249 | | complexIndex = FindDimLabel(marker, UTF_COLUMN, "COMPLEX") |
| 0 | 250 | | WaveStats/M=1/Q/Z/RMD=[][complexIndex, complexIndex] marker |
| 0 | 251 | | complexity = V_sum |
| | 252 | |
|
| 0 | 253 | | [linesReport, metrics] = GetLinesReport("\t\t\t\t\t", 0, DimSize(marker, UTF_ROW), procIndex, totals, marker) |
| | 254 | |
|
| 0 | 255 | | // DateTime since unix epoch and in UTC. This is the short version of: |
| 0 | 256 | | // (<current datetime> - <utc offset>) - (<epoch datetime> - <utc offset>) |
| 0 | 257 | | epochTime = DateTime - Date2Secs(1970, 1, 1) |
| | 258 | |
|
| 0 | 259 | | report = "<?xml version=\"1.0\"?>\n" |
| 0 | 260 | | report += "<!DOCTYPE coverage SYSTEM \"https://cobertura.sourceforge.net/xml/coverage-04.dtd\">\n" |
| 0 | 261 | | report += "<!--Cobertura coverage report generated by the Igor Pro package \"igortest\"-->\n" |
| 0 | 262 | | sprintf line, "<coverage line-rate=\"%f\" branch-rate=\"%f\" lines-covered=\"%d\" lines-valid=\"%d\" branches-covered= |
| 0 | 263 | | metrics.lineRate, metrics.branchRate, metrics.lineCovered, metrics.lineValid, metrics.branchCovered, metrics.b |
| 0 | 264 | | report += line |
| | 265 | |
|
| 0 | 266 | | report += "\t<sources>\n" |
| 0 | 267 | | sprintf line, "\t\t<source>%s</source>\n", FilePathToXml(info.sourcePath) |
| 0 | 268 | | report += line |
| 0 | 269 | | report += "\t</sources>\n" |
| | 270 | |
|
| 0 | 271 | | report += "\t<packages>\n" |
| 0 | 272 | | sprintf line, "\t\t<package name=\"%s\" line-rate=\"%f\" branch-rate=\"%f\" complexity=\"%d\">\n", FilePathToXml(info. |
| 0 | 273 | | report += line |
| | 274 | |
|
| 0 | 275 | | report += "\t\t\t<classes>\n" |
| 0 | 276 | | sprintf line, "\t\t\t\t<class name=\"%s\" filename=\"%s\" line-rate=\"%f\" branch-rate=\"%f\" complexity=\"%d\">\n", I |
| 0 | 277 | | report += line |
| | 278 | |
|
| 0 | 279 | | report += "\t\t\t\t\t<methods>\n" |
| 0 | 280 | | for(i = 0; i < funcCount; i += 1) |
| 0 | 281 | | if(i < funcCount - 1) |
| 0 | 282 | | funcEnd = procFuncLines[i + 1] |
| 0 | 283 | | else |
| 0 | 284 | | funcEnd = DimSize(marker, UTF_ROW) |
| 0 | 285 | | endif |
| 0 | 286 | | report += GetFunctionReport(procFuncNames[i], procFuncLines[i], funcEnd, procName, procIndex, totals, marker) |
| 0 | 287 | | endfor |
| 0 | 288 | | report += "\t\t\t\t\t</methods>\n" |
| | 289 | |
|
| 0 | 290 | | report += linesReport |
| | 291 | |
|
| 0 | 292 | | report += "\t\t\t\t</class>\n" |
| 0 | 293 | | report += "\t\t\t</classes>\n" |
| | 294 | |
|
| 0 | 295 | | report += "\t\t</package>\n" |
| 0 | 296 | | report += "\t</packages>\n" |
| | 297 | |
|
| 0 | 298 | | report += "</coverage>\n" |
| 0 | 299 | | return report |
| 0 | 300 | | End |
| | 301 | |
|
| | 302 | | /// @brief Generate the cobertura report for each instrumented procedure file and write each report |
| | 303 | | /// to the home directory of the experiment. |
| | 304 | | /// |
| | 305 | | /// @param sources A comma delimited list of source paths that should be used for the cobertura |
| | 306 | | /// generation. If this string is empty it will use the current home directory as |
| | 307 | | /// source path. |
| | 308 | | /// @param outDir The output path for the generated files. If this string is empty it will use the |
| | 309 | | /// current home directory. |
| 6 | 310 | | static Function PrintReport(string sources, string outDir) |
| 6 | 311 | | variable i, procCount, nameIndex, pathIndex |
| 6 | 312 | | string name, path, report, msg |
| | 313 | |
|
| 6 | 314 | | WAVE/T procs = IUTF_Tracing#GetTracedProcedureInfos() |
| 6 | 315 | | procCount = IUTF_Utils_Vector#GetLength(procs) |
| 6 | 316 | | nameIndex = FindDimLabel(procs, UTF_COLUMN, "NAME") |
| 6 | 317 | | pathIndex = FindDimLabel(procs, UTF_COLUMN, "PATH") |
| | 318 | |
|
| 6 | 319 | | if(!IUTF_Tracing_Analytics#HasTracingData()) |
| 0 | 320 | | IUTF_Reporting#ReportErrorAndAbort("Bug: No tracing data exists") |
| 0 | 321 | | return NaN |
| 6 | 322 | | endif |
| | 323 | |
|
| 6 | 324 | | WAVE totals = IUTF_Tracing_Analytics#GetTotals() |
| 0 | 325 | | if(!DimSize(totals, UTF_ROW)) |
| 0 | 326 | | // this can happen after stored Experiment is loaded to a fresh instance of Igor |
| 0 | 327 | | IUTF_Reporting#ReportErrorAndAbort("Bug: TUFXOP has no data. Try to rerun tracing to get new data.") |
| 0 | 328 | | return NaN |
| 0 | 329 | | endif |
| | 330 | |
|
| 0 | 331 | | WAVE/WAVE instrMarker = IUTF_Tracing#GetInstrumentedMarker() |
| | 332 | |
|
| 0 | 333 | | if(IUTF_Utils#IsEmpty(sources)) |
| 0 | 334 | | Make/T/N=1/FREE=1 wvSources |
| 0 | 335 | | wvSources[0] = IUTF_Utils_Paths#GetHomePath() |
| 0 | 336 | | else |
| 0 | 337 | | WAVE/T wvSources = ListToTextWave(sources, ",") |
| 0 | 338 | | endif |
| 0 | 339 | | wvSources[] = ParseFilePath(2, ParseFilePath(5, wvSources[p], "\\", 0, 0), "\\", 0, 0) |
| | 340 | |
|
| 0 | 341 | | printf "Generate Cobertura reports" |
| | 342 | |
|
| 0 | 343 | | for(i = 0; i < procCount; i += 1) |
| 0 | 344 | | name = procs[i][nameIndex] |
| 0 | 345 | | path = procs[i][pathIndex] |
| 0 | 346 | | WAVE/Z marker = instrMarker[i] |
| | 347 | |
|
| 0 | 348 | | if(IUTF_Utils#IsEmpty(path) || !WaveExists(marker)) |
| 0 | 349 | | continue |
| 0 | 350 | | endif |
| | 351 | |
|
| 0 | 352 | | printf "." |
| | 353 | |
|
| 0 | 354 | | report = GetProcedureReport(name, path, i, totals, marker, wvSources) |
| | 355 | |
|
| 0 | 356 | | if(strlen(report) >= FILESIZE_WARNING_LIMIT) |
| 0 | 357 | | sprintf msg, "WARNING! The report size of \"%s\" (%.2W1PB) exceed suggested maximum file size of %.2W1PB.", name, |
| 0 | 358 | | IUTF_Reporting#IUTF_PrintStatusMessage(msg) |
| 0 | 359 | | sprintf msg, "WARNING! Some Cobertura consumer like Gitlab could have issues reading such large files." |
| 0 | 360 | | IUTF_Reporting#IUTF_PrintStatusMessage(msg) |
| 0 | 361 | | sprintf msg, "WARNING! Try to split your procedure file into multiple smaller ones." |
| 0 | 362 | | IUTF_Reporting#IUTF_PrintStatusMessage(msg) |
| 0 | 363 | | endif |
| | 364 | |
|
| 0 | 365 | | IUTF_Utils_XML#WriteXML("Cobertura_", report, outDir = outDir) |
| 0 | 366 | | endfor |
| | 367 | |
|
| 0 | 368 | | printf "\n" |
| 0 | 369 | | IUTF_Reporting#IUTF_PrintStatusMessage("Cobertura export finished.") |
| 6 | 370 | | End |
| | 371 | |
|
| | 372 | | #endif // UTF_ALLOW_TRACING |