@@ -153,43 +153,46 @@ Coverage::CoverageMap LlvmCoverageTool::getCoverageInfo() const {
153153 LOG_S (ERROR) << " Can't found coverage.json at " << covJsonPath.string ();
154154 throw CoverageGenerationException (" Can't found coverage.json at " + covJsonPath.string ());
155155 }
156- LOG_S (INFO) << " Reading coverage.json" ;
157-
158- nlohmann::json coverageJson = JsonUtils::getJsonFromFile (covJsonPath);
159-
160- // Parsing is based on LLVM coverage mapping format
161- ExecUtils::doWorkWithProgress (
162- coverageJson.at (" data" ), progressWriter, " Reading coverage.json" ,
163- [&coverageMap](const nlohmann::json &data) {
164- for (const nlohmann::json &function : data.at (" functions" )) {
165- std::string filename = function.at (" filenames" ).at (0 );
166- // no need to show coverage for gtest library
167- if (Paths::isGtest (filename)) {
168- continue ;
169- }
170- for (const nlohmann::json ®ion : function.at (" regions" )) {
171- // In an LLVM coverage mapping format a region is an array with line and
172- // character position
173- FileCoverage::SourcePosition startPosition{ region.at (0 ).get <uint32_t >() - 1 ,
174- region.at (1 ).get <uint32_t >() - 1 };
175- FileCoverage::SourcePosition endPosition{ region.at (2 ).get <uint32_t >() - 1 ,
176- region.at (3 ).get <uint32_t >() - 1 };
177- FileCoverage::SourceRange sourceRange{ startPosition, endPosition };
178- // The 4th element in LLVM coverage mapping format of a region
179- if (region.at (4 ).get <int >() == 0 ) {
180- coverageMap[filename].uncoveredRanges .push_back (sourceRange);
181- } else if (region.at (4 ).get <int >() >= 1 ) {
182- coverageMap[filename].coveredRanges .push_back (sourceRange);
156+ try {
157+ LOG_S (INFO) << " Reading coverage.json" ;
158+ nlohmann::json coverageJson = JsonUtils::getJsonFromFile (covJsonPath);
159+
160+ // Parsing is based on LLVM coverage mapping format
161+ ExecUtils::doWorkWithProgress (
162+ coverageJson.at (" data" ), progressWriter, " Reading coverage.json" ,
163+ [&coverageMap](const nlohmann::json &data) {
164+ for (const nlohmann::json &function: data.at (" functions" )) {
165+ std::string filename = function.at (" filenames" ).at (0 );
166+ // no need to show coverage for gtest library
167+ if (Paths::isGtest (filename)) {
168+ continue ;
169+ }
170+ for (const nlohmann::json ®ion: function.at (" regions" )) {
171+ // In an LLVM coverage mapping format a region is an array with line and
172+ // character position
173+ FileCoverage::SourcePosition startPosition{region.at (0 ).get <uint32_t >() - 1 ,
174+ region.at (1 ).get <uint32_t >() - 1 };
175+ FileCoverage::SourcePosition endPosition{region.at (2 ).get <uint32_t >() - 1 ,
176+ region.at (3 ).get <uint32_t >() - 1 };
177+ FileCoverage::SourceRange sourceRange{startPosition, endPosition};
178+ // The 4th element in LLVM coverage mapping format of a region
179+ if (region.at (4 ).get <int >() == 0 ) {
180+ coverageMap[filename].uncoveredRanges .push_back (sourceRange);
181+ } else if (region.at (4 ).get <int >() >= 1 ) {
182+ coverageMap[filename].coveredRanges .push_back (sourceRange);
183+ }
184+ }
183185 }
184- }
185- }
186- });
186+ });
187187
188- for (const auto &item : coverageMap) {
189- countLineCoverage (coverageMap, item.first );
190- }
188+ for (const auto &item: coverageMap) {
189+ countLineCoverage (coverageMap, item.first );
190+ }
191191
192- return coverageMap;
192+ return coverageMap;
193+ } catch (const std::exception &e) {
194+ throw CoverageGenerationException (" Can't parse coverage.json at " + covJsonPath.string ());
195+ }
193196}
194197
195198void LlvmCoverageTool::countLineCoverage (Coverage::CoverageMap &coverageMap,
@@ -223,9 +226,18 @@ void LlvmCoverageTool::checkLineForPartial(Coverage::FileCoverage::SourceLine li
223226}
224227
225228nlohmann::json LlvmCoverageTool::getTotals () const {
226- fs::path covJsonPath = Paths::getCoverageJsonPath (projectContext);
227- nlohmann::json coverageJson = JsonUtils::getJsonFromFile (covJsonPath);
228- return coverageJson.at (" data" ).back ().at (" totals" );
229+ try {
230+ fs::path covJsonPath = Paths::getCoverageJsonPath (projectContext);
231+ nlohmann::json coverageJson = JsonUtils::getJsonFromFile (covJsonPath);
232+ return coverageJson.at (" data" ).back ().at (" totals" );
233+ } catch (const std::exception &e) {
234+ return {{
235+ " lines" , {
236+ {" count" , 0 },
237+ {" covered" , 0 },
238+ {" percent" , (double ) 0.0 }
239+ }}};
240+ }
229241}
230242
231243
0 commit comments