[llvm] [llvm-cov] Add support for baseline coverage (PR #117910)

via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 22 12:15:57 PDT 2025


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff ccc18ca1479dee73b0fdafa75d9403c50c17ee28 bd087fc8762a19e25c0bde6e9f1a0b8136a3761d --extensions h,cpp -- llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h llvm/lib/ProfileData/Coverage/CoverageMapping.cpp llvm/tools/llvm-cov/CodeCoverage.cpp llvm/unittests/ProfileData/CoverageMappingTest.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
index ebe5dc8021..272c42bc68 100644
--- a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
+++ b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
@@ -973,8 +973,7 @@ Error CoverageMapping::loadFromReaders(
     std::optional<std::reference_wrapper<IndexedInstrProfReader>>
         &ProfileReader,
     CoverageMapping &Coverage) {
-  assert(!Coverage.SingleByteCoverage ||
-         !ProfileReader ||
+  assert(!Coverage.SingleByteCoverage || !ProfileReader ||
          *Coverage.SingleByteCoverage ==
              ProfileReader.value().get().hasSingleByteCoverage());
   if (ProfileReader) {
diff --git a/llvm/tools/llvm-cov/CodeCoverage.cpp b/llvm/tools/llvm-cov/CodeCoverage.cpp
index 313005451d..1cf4cbc5c2 100644
--- a/llvm/tools/llvm-cov/CodeCoverage.cpp
+++ b/llvm/tools/llvm-cov/CodeCoverage.cpp
@@ -1131,15 +1131,15 @@ int CodeCoverageTool::doShow(int argc, const char **argv,
     }
 
 <<<<<<< HEAD
-  if (ShowCreatedTime) {
-    auto ModifiedTime = Status.getLastModificationTime();
-    std::string ModifiedTimeStr = to_string(ModifiedTime);
-    size_t found = ModifiedTimeStr.rfind(':');
-    ViewOpts.CreatedTimeStr =
-        (found != std::string::npos)
-            ? "Created: " + ModifiedTimeStr.substr(0, found)
-            : "Created: " + ModifiedTimeStr;
-  }
+    if (ShowCreatedTime) {
+      auto ModifiedTime = Status.getLastModificationTime();
+      std::string ModifiedTimeStr = to_string(ModifiedTime);
+      size_t found = ModifiedTimeStr.rfind(':');
+      ViewOpts.CreatedTimeStr =
+          (found != std::string::npos)
+              ? "Created: " + ModifiedTimeStr.substr(0, found)
+              : "Created: " + ModifiedTimeStr;
+    }
 =======
     ModifiedTime = Status.getLastModificationTime();
   } else {
@@ -1152,94 +1152,94 @@ int CodeCoverageTool::doShow(int argc, const char **argv,
                                 : "Created: " + ModifiedTimeStr;
 >>>>>>> 2ee0a53151 ([llvm-cov] Add support for baseline coverage)
 
-  auto Coverage = load();
-  if (!Coverage)
-    return 1;
-
-  auto Printer = CoveragePrinter::create(ViewOpts);
+    auto Coverage = load();
+    if (!Coverage)
+      return 1;
 
-  if (SourceFiles.empty() && !HadSourceFiles)
-    // Get the source files from the function coverage mapping.
-    for (StringRef Filename : Coverage->getUniqueSourceFiles()) {
-      if (!IgnoreFilenameFilters.matchesFilename(Filename))
-        SourceFiles.push_back(std::string(Filename));
-    }
+    auto Printer = CoveragePrinter::create(ViewOpts);
 
-  // Create an index out of the source files.
-  if (ViewOpts.hasOutputDirectory()) {
-    if (Error E = Printer->createIndexFile(SourceFiles, *Coverage, Filters)) {
-      error("could not create index file!", toString(std::move(E)));
-      return 1;
-    }
-  }
+    if (SourceFiles.empty() && !HadSourceFiles)
+      // Get the source files from the function coverage mapping.
+      for (StringRef Filename : Coverage->getUniqueSourceFiles()) {
+        if (!IgnoreFilenameFilters.matchesFilename(Filename))
+          SourceFiles.push_back(std::string(Filename));
+      }
 
-  if (!Filters.empty()) {
-    // Build the map of filenames to functions.
-    std::map<llvm::StringRef, std::vector<const FunctionRecord *>>
-        FilenameFunctionMap;
-    for (const auto &SourceFile : SourceFiles)
-      for (const auto &Function : Coverage->getCoveredFunctions(SourceFile))
-        if (Filters.matches(*Coverage, Function))
-          FilenameFunctionMap[SourceFile].push_back(&Function);
-
-    // Only print filter matching functions for each file.
-    for (const auto &FileFunc : FilenameFunctionMap) {
-      StringRef File = FileFunc.first;
-      const auto &Functions = FileFunc.second;
-
-      auto OSOrErr = Printer->createViewFile(File, /*InToplevel=*/false);
-      if (Error E = OSOrErr.takeError()) {
-        error("could not create view file!", toString(std::move(E)));
+    // Create an index out of the source files.
+    if (ViewOpts.hasOutputDirectory()) {
+      if (Error E = Printer->createIndexFile(SourceFiles, *Coverage, Filters)) {
+        error("could not create index file!", toString(std::move(E)));
         return 1;
       }
-      auto OS = std::move(OSOrErr.get());
-
-      bool ShowTitle = ViewOpts.hasOutputDirectory();
-      for (const auto *Function : Functions) {
-        auto FunctionView = createFunctionView(*Function, *Coverage);
-        if (!FunctionView) {
-          warning("Could not read coverage for '" + Function->Name + "'.");
-          continue;
+    }
+
+    if (!Filters.empty()) {
+      // Build the map of filenames to functions.
+      std::map<llvm::StringRef, std::vector<const FunctionRecord *>>
+          FilenameFunctionMap;
+      for (const auto &SourceFile : SourceFiles)
+        for (const auto &Function : Coverage->getCoveredFunctions(SourceFile))
+          if (Filters.matches(*Coverage, Function))
+            FilenameFunctionMap[SourceFile].push_back(&Function);
+
+      // Only print filter matching functions for each file.
+      for (const auto &FileFunc : FilenameFunctionMap) {
+        StringRef File = FileFunc.first;
+        const auto &Functions = FileFunc.second;
+
+        auto OSOrErr = Printer->createViewFile(File, /*InToplevel=*/false);
+        if (Error E = OSOrErr.takeError()) {
+          error("could not create view file!", toString(std::move(E)));
+          return 1;
+        }
+        auto OS = std::move(OSOrErr.get());
+
+        bool ShowTitle = ViewOpts.hasOutputDirectory();
+        for (const auto *Function : Functions) {
+          auto FunctionView = createFunctionView(*Function, *Coverage);
+          if (!FunctionView) {
+            warning("Could not read coverage for '" + Function->Name + "'.");
+            continue;
+          }
+          FunctionView->print(*OS.get(), /*WholeFile=*/false,
+                              /*ShowSourceName=*/true, ShowTitle);
+          ShowTitle = false;
         }
-        FunctionView->print(*OS.get(), /*WholeFile=*/false,
-                            /*ShowSourceName=*/true, ShowTitle);
-        ShowTitle = false;
+
+        Printer->closeViewFile(std::move(OS));
       }
+      return 0;
+    }
 
-      Printer->closeViewFile(std::move(OS));
+    // Show files
+    bool ShowFilenames =
+        (SourceFiles.size() != 1) || ViewOpts.hasOutputDirectory() ||
+        (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML);
+
+    ThreadPoolStrategy S = hardware_concurrency(ViewOpts.NumThreads);
+    if (ViewOpts.NumThreads == 0) {
+      // If NumThreads is not specified, create one thread for each input, up to
+      // the number of hardware cores.
+      S = heavyweight_hardware_concurrency(SourceFiles.size());
+      S.Limit = true;
     }
-    return 0;
-  }
 
-  // Show files
-  bool ShowFilenames =
-      (SourceFiles.size() != 1) || ViewOpts.hasOutputDirectory() ||
-      (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML);
-
-  ThreadPoolStrategy S = hardware_concurrency(ViewOpts.NumThreads);
-  if (ViewOpts.NumThreads == 0) {
-    // If NumThreads is not specified, create one thread for each input, up to
-    // the number of hardware cores.
-    S = heavyweight_hardware_concurrency(SourceFiles.size());
-    S.Limit = true;
-  }
+    if (!ViewOpts.hasOutputDirectory() || S.ThreadsRequested == 1) {
+      for (const std::string &SourceFile : SourceFiles)
+        writeSourceFileView(SourceFile, Coverage.get(), Printer.get(),
+                            ShowFilenames);
+    } else {
+      // In -output-dir mode, it's safe to use multiple threads to print files.
+      DefaultThreadPool Pool(S);
+      for (const std::string &SourceFile : SourceFiles)
+        Pool.async(&CodeCoverageTool::writeSourceFileView, this, SourceFile,
+                   Coverage.get(), Printer.get(), ShowFilenames);
+      Pool.wait();
+    }
 
-  if (!ViewOpts.hasOutputDirectory() || S.ThreadsRequested == 1) {
-    for (const std::string &SourceFile : SourceFiles)
-      writeSourceFileView(SourceFile, Coverage.get(), Printer.get(),
-                          ShowFilenames);
-  } else {
-    // In -output-dir mode, it's safe to use multiple threads to print files.
-    DefaultThreadPool Pool(S);
-    for (const std::string &SourceFile : SourceFiles)
-      Pool.async(&CodeCoverageTool::writeSourceFileView, this, SourceFile,
-                 Coverage.get(), Printer.get(), ShowFilenames);
-    Pool.wait();
+    return 0;
   }
 
-  return 0;
-}
-
 int CodeCoverageTool::doReport(int argc, const char **argv,
                                CommandLineParserType commandLineParser) {
   cl::opt<bool> ShowFunctionSummaries(

``````````

</details>


https://github.com/llvm/llvm-project/pull/117910


More information about the llvm-commits mailing list