cmake::CreateProfilingEntry: Refactor to take lambda for args

This commit is contained in:
Kyle Edwards 2022-11-10 18:15:08 -05:00
parent 31893e8c8f
commit 553794e987
4 changed files with 36 additions and 35 deletions

View File

@ -376,7 +376,20 @@ public:
this->Makefile->ExecutionStatusStack.push_back(&status);
#if !defined(CMAKE_BOOTSTRAP)
this->ProfilingDataRAII =
this->Makefile->GetCMakeInstance()->CreateProfilingEntry(lff, lfc);
this->Makefile->GetCMakeInstance()->CreateProfilingEntry(
"script", lff.LowerCaseName(), [&lff, &lfc]() -> Json::Value {
Json::Value argsValue = Json::objectValue;
if (!lff.Arguments().empty()) {
std::string args;
for (auto const& a : lff.Arguments()) {
args = cmStrCat(args, args.empty() ? "" : " ", a.Value);
}
argsValue["functionArgs"] = args;
}
argsValue["location"] =
cmStrCat(lfc.FilePath, ':', std::to_string(lfc.Line));
return argsValue;
});
#endif
}

View File

@ -6,9 +6,6 @@
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <vector>
#include <cm/utility>
#include <cm3p/json/value.h>
#include <cm3p/json/writer.h>
@ -16,7 +13,6 @@
#include "cmsys/FStream.hxx"
#include "cmsys/SystemInformation.hxx"
#include "cmListFileCache.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
@ -47,22 +43,6 @@ cmMakefileProfilingData::~cmMakefileProfilingData() noexcept
}
}
void cmMakefileProfilingData::StartEntry(const cmListFileFunction& lff,
cmListFileContext const& lfc)
{
cm::optional<Json::Value> argsValue(cm::in_place, Json::objectValue);
if (!lff.Arguments().empty()) {
std::string args;
for (auto const& a : lff.Arguments()) {
args = cmStrCat(args, args.empty() ? "" : " ", a.Value);
}
(*argsValue)["functionArgs"] = args;
}
(*argsValue)["location"] =
cmStrCat(lfc.FilePath, ':', std::to_string(lfc.Line));
this->StartEntry("script", lff.LowerCaseName(), std::move(argsValue));
}
void cmMakefileProfilingData::StartEntry(const std::string& category,
const std::string& name,
cm::optional<Json::Value> args)
@ -127,6 +107,15 @@ void cmMakefileProfilingData::StopEntry()
}
}
cmMakefileProfilingData::RAII::RAII(cmMakefileProfilingData& data,
const std::string& category,
const std::string& name,
cm::optional<Json::Value> args)
: Data(&data)
{
this->Data->StartEntry(category, name, std::move(args));
}
cmMakefileProfilingData::RAII::RAII(RAII&& other) noexcept
: Data(other.Data)
{

View File

@ -3,7 +3,6 @@
#pragma once
#include <memory>
#include <string>
#include <utility>
#include <cm/optional>
@ -15,15 +14,11 @@ namespace Json {
class StreamWriter;
}
class cmListFileContext;
class cmListFileFunction;
class cmMakefileProfilingData
{
public:
cmMakefileProfilingData(const std::string&);
~cmMakefileProfilingData() noexcept;
void StartEntry(const cmListFileFunction& lff, cmListFileContext const& lfc);
void StartEntry(const std::string& category, const std::string& name,
cm::optional<Json::Value> args = cm::nullopt);
void StopEntry();
@ -35,12 +30,9 @@ public:
RAII(const RAII&) = delete;
RAII(RAII&&) noexcept;
template <typename... Args>
RAII(cmMakefileProfilingData& data, Args&&... args)
: Data(&data)
{
this->Data->StartEntry(std::forward<Args>(args)...);
}
RAII(cmMakefileProfilingData& data, const std::string& category,
const std::string& name,
cm::optional<Json::Value> args = cm::nullopt);
~RAII();

View File

@ -638,13 +638,20 @@ public:
cmMakefileProfilingData& GetProfilingOutput();
bool IsProfilingEnabled() const;
template <typename... Args>
cm::optional<cmMakefileProfilingData::RAII> CreateProfilingEntry(
Args&&... args)
const std::string& category, const std::string& name)
{
return this->CreateProfilingEntry(
category, name, []() -> cm::nullopt_t { return cm::nullopt; });
}
template <typename ArgsFunc>
cm::optional<cmMakefileProfilingData::RAII> CreateProfilingEntry(
const std::string& category, const std::string& name, ArgsFunc&& argsFunc)
{
if (this->IsProfilingEnabled()) {
return cm::make_optional<cmMakefileProfilingData::RAII>(
this->GetProfilingOutput(), std::forward<Args>(args)...);
this->GetProfilingOutput(), category, name, argsFunc());
}
return cm::nullopt;
}