[llvm] [llvm-cov] Add a SourcesFile flag (PR #80646)
Farzon Lotfi via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 5 05:50:31 PST 2024
https://github.com/farzonl updated https://github.com/llvm/llvm-project/pull/80646
>From 61e18316ba4ca9810a96269141ba3f1705a3c667 Mon Sep 17 00:00:00 2001
From: Farzon Lotfi <farzon at farzon.com>
Date: Mon, 5 Feb 2024 03:38:25 -0500
Subject: [PATCH] [llvm-cov] Add a SourcesFile for providing sources on
character limited terminals
---
llvm/docs/CommandGuide/llvm-cov.rst | 15 ++++++++++
llvm/tools/llvm-cov/CodeCoverage.cpp | 41 ++++++++++++++++++++++++++++
2 files changed, 56 insertions(+)
diff --git a/llvm/docs/CommandGuide/llvm-cov.rst b/llvm/docs/CommandGuide/llvm-cov.rst
index 968f3c452f558..ba592eb5404bf 100644
--- a/llvm/docs/CommandGuide/llvm-cov.rst
+++ b/llvm/docs/CommandGuide/llvm-cov.rst
@@ -380,6 +380,11 @@ OPTIONS
Fail if an object file cannot be found for a binary ID present in the profile,
neither on the command line nor via binary ID lookup.
+.. option:: -sourcesFile
+
+ A json file of source files used as an alternative to -sources for
+ terminals with character limits.
+
.. program:: llvm-cov report
.. _llvm-cov-report:
@@ -470,6 +475,11 @@ OPTIONS
Fail if an object file cannot be found for a binary ID present in the profile,
neither on the command line nor via binary ID lookup.
+.. option:: -sourcesFile
+
+ A json file of source files used as an alternative to -sources for
+ terminals with character limits.
+
.. program:: llvm-cov export
.. _llvm-cov-export:
@@ -562,6 +572,11 @@ OPTIONS
Fail if an object file cannot be found for a binary ID present in the profile,
neither on the command line nor via binary ID lookup.
+.. option:: -sourcesFile
+
+ A json file of source files used as an alternative to -sources for
+ terminals with character limits.
+
CONVERT-FOR-TESTING COMMAND
---------------------------
diff --git a/llvm/tools/llvm-cov/CodeCoverage.cpp b/llvm/tools/llvm-cov/CodeCoverage.cpp
index 049e89d1a2300..7d2d8fba9ab6d 100644
--- a/llvm/tools/llvm-cov/CodeCoverage.cpp
+++ b/llvm/tools/llvm-cov/CodeCoverage.cpp
@@ -31,6 +31,7 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Format.h"
+#include "llvm/Support/JSON.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Process.h"
@@ -685,6 +686,9 @@ int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
cl::list<std::string> InputSourceFiles("sources", cl::Positional,
cl::desc("<Source files>"));
+ cl::opt<std::string> InputSourcesFile(
+ "sourcesFile", cl::Positional, cl::desc("<A json file of source files>"));
+
cl::opt<bool> DebugDumpCollectedPaths(
"dump-collected-paths", cl::Optional, cl::Hidden,
cl::desc("Show the collected paths to source files"));
@@ -958,6 +962,43 @@ int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
for (const std::string &File : InputSourceFiles)
collectPaths(File);
+ if (!InputSourcesFile.empty()) {
+ ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
+ MemoryBuffer::getFile(InputSourcesFile);
+
+ if (!Buf)
+ return 1;
+
+ // Parse the JSON document
+ llvm::Expected<llvm::json::Value> jsonDocument =
+ llvm::json::parse(Buf.get()->getBuffer());
+
+ if (!jsonDocument) {
+ error("Error parsing Sources JSON File: ",
+ llvm::toString(jsonDocument.takeError()));
+ return 1;
+ }
+
+ // Access the JSON data
+ const llvm::json::Object *jsonObject = jsonDocument->getAsObject();
+ if (!jsonObject) {
+ error("Expected Sources JSON to be inclosed in an object");
+ return 1;
+ }
+ auto jsonArray = jsonObject->getArray("sourcesFile");
+ if (!jsonArray) {
+ error("Expected Sources JSON to list sources under the 'sourcesFile' "
+ "key.");
+ return 1;
+ }
+ for (size_t i = 0; i < jsonArray->size(); ++i) {
+ std::optional<llvm::StringRef> File =
+ jsonArray[i].data()->getAsString();
+ if (File)
+ collectPaths(File->str());
+ }
+ }
+
if (DebugDumpCollectedPaths) {
for (const std::string &SF : SourceFiles)
outs() << SF << '\n';
More information about the llvm-commits
mailing list