cmCustomCommand: reduce raw pointer usage

This commit is contained in:
Marc Chevrier
2026-09-14 18:24:48 +02:00
parent f5efe7a6b5
commit a53691b73a
15 changed files with 60 additions and 57 deletions
+5 -6
View File
@@ -9,6 +9,7 @@
#include <utility>
#include <cm/memory>
#include <cm/optional>
#include <cmext/string_view>
#include "cmCustomCommand.h"
@@ -44,8 +45,7 @@ bool cmAddCustomCommandCommand(std::vector<std::string> const& args,
std::string depfile;
std::string job_pool;
std::string job_server_aware;
std::string comment_buffer;
char const* comment = nullptr;
cm::optional<std::string> comment;
std::vector<std::string> depends;
std::vector<std::string> outputs;
std::vector<std::string> output;
@@ -369,7 +369,7 @@ bool cmAddCustomCommandCommand(std::vector<std::string> const& args,
byproducts.push_back(filename);
break;
case doing_comment:
if (!comment_buffer.empty()) {
if (comment && !comment->empty()) {
std::string const msg =
"COMMENT requires exactly one argument, but multiple values "
"or COMMENT keywords have been given.";
@@ -381,8 +381,7 @@ bool cmAddCustomCommandCommand(std::vector<std::string> const& args,
mf.IssuePolicyWarning(cmPolicies::CMP0175, msg);
}
}
comment_buffer = copy;
comment = comment_buffer.c_str();
comment = copy;
break;
default:
status.SetError("Wrong syntax. Unknown type of argument.");
@@ -477,7 +476,7 @@ bool cmAddCustomCommandCommand(std::vector<std::string> const& args,
cc->SetByproducts(byproducts);
cc->SetCommandLines(commandLines);
cc->SetComment(comment);
cc->SetWorkingDirectory(working.c_str());
cc->SetWorkingDirectory(working);
cc->SetEscapeOldStyle(!verbatim);
cc->SetUsesTerminal(uses_terminal);
cc->SetDepfile(depfile);
+4 -5
View File
@@ -5,6 +5,7 @@
#include <utility>
#include <cm/memory>
#include <cm/optional>
#include "cmCustomCommand.h"
#include "cmCustomCommandLines.h"
@@ -50,8 +51,7 @@ bool cmAddCustomTargetCommand(std::vector<std::string> const& args,
bool verbatim = false;
bool uses_terminal = false;
bool command_expand_lists = false;
std::string comment_buffer;
char const* comment = nullptr;
cm::optional<std::string> comment;
std::vector<std::string> sources;
std::string job_pool;
std::string job_server_aware;
@@ -143,8 +143,7 @@ bool cmAddCustomTargetCommand(std::vector<std::string> const& args,
depends.push_back(std::move(dep));
} break;
case doing_comment:
comment_buffer = copy;
comment = comment_buffer.c_str();
comment = copy;
break;
case doing_source:
sources.push_back(copy);
@@ -209,7 +208,7 @@ bool cmAddCustomTargetCommand(std::vector<std::string> const& args,
// Add the utility target to the makefile.
auto cc = cm::make_unique<cmCustomCommand>();
cc->SetWorkingDirectory(working_directory.c_str());
cc->SetWorkingDirectory(working_directory);
cc->SetByproducts(byproducts);
cc->SetDepends(depends);
cc->SetCommandLines(commandLines);
+4 -6
View File
@@ -77,16 +77,14 @@ void cmCustomCommand::SetCommandLines(cmCustomCommandLines commandLines)
this->CommandLines = std::move(commandLines);
}
char const* cmCustomCommand::GetComment() const
cm::optional<std::string> const& cmCustomCommand::GetComment() const
{
char const* no_comment = nullptr;
return this->HaveComment ? this->Comment.c_str() : no_comment;
return this->Comment;
}
void cmCustomCommand::SetComment(char const* comment)
void cmCustomCommand::SetComment(cm::optional<std::string> comment)
{
this->Comment = comment ? comment : "";
this->HaveComment = (comment != nullptr);
this->Comment = std::move(comment);
}
void cmCustomCommand::AppendCommands(cmCustomCommandLines const& commandLines)
+7 -6
View File
@@ -8,6 +8,8 @@
#include <utility>
#include <vector>
#include <cm/optional>
#include "cmCustomCommandLines.h"
#include "cmListFileCache.h"
#include "cmPolicies.h"
@@ -50,9 +52,9 @@ public:
return this->WorkingDirectory;
}
void SetWorkingDirectory(char const* workingDirectory)
void SetWorkingDirectory(std::string const& workingDirectory)
{
this->WorkingDirectory = (workingDirectory ? workingDirectory : "");
this->WorkingDirectory = workingDirectory;
}
/** Get the list of command lines. */
@@ -60,8 +62,8 @@ public:
void SetCommandLines(cmCustomCommandLines commandLines);
/** Get the comment string for the command. */
char const* GetComment() const;
void SetComment(char const* comment);
cm::optional<std::string> const& GetComment() const;
void SetComment(cm::optional<std::string> comment);
/** Get a value indicating if the command uses UTF-8 output pipes. */
bool GetStdPipesUTF8() const { return this->StdPipesUTF8; }
@@ -148,13 +150,12 @@ private:
cmListFileBacktrace Backtrace;
cmImplicitDependsList ImplicitDepends;
std::string Target;
std::string Comment;
cm::optional<std::string> Comment;
std::string WorkingDirectory;
std::string Depfile;
std::string JobPool;
std::string Role;
bool JobserverAware = false;
bool HaveComment = false;
bool EscapeAllowMakeVars = false;
bool EscapeOldStyle = true;
bool UsesTerminal = false;
+5 -5
View File
@@ -152,7 +152,7 @@ std::string EvaluateDepfile(std::string const& path,
return cge->Evaluate(lg, config);
}
std::string EvaluateComment(char const* comment,
std::string EvaluateComment(std::string const& comment,
cmGeneratorExpression const& ge,
cmLocalGenerator* lg, std::string const& config)
{
@@ -484,12 +484,12 @@ std::string cmCustomCommandGenerator::GetInternalDepfile() const
cm::optional<std::string> cmCustomCommandGenerator::GetComment() const
{
char const* comment = this->CC->GetComment();
if (!comment) {
if (!this->CC->GetComment()) {
return cm::nullopt;
}
if (!*comment) {
return std::string();
std::string const& comment = this->CC->GetComment().value();
if (comment.empty()) {
return comment;
}
cmGeneratorExpression ge(*this->LG->GetCMakeInstance(),
+12 -12
View File
@@ -3614,7 +3614,7 @@ void ModuleCompilationDatabaseCommandAction::operator()(
cc->SetBacktrace(lfbt);
cc->SetCommandLines(command_lines);
cc->SetWorkingDirectory(lg.GetBinaryDirectory().c_str());
cc->SetWorkingDirectory(lg.GetBinaryDirectory());
cc->SetDependsExplicitOnly(true);
cc->SetOutputs(this->Output);
if (!inputs.empty()) {
@@ -3646,7 +3646,7 @@ void ModuleCompilationDatabaseTargetAction::operator()(
std::unique_ptr<cmCustomCommand> cc)
{
cc->SetBacktrace(lfbt);
cc->SetWorkingDirectory(lg.GetBinaryDirectory().c_str());
cc->SetWorkingDirectory(lg.GetBinaryDirectory());
std::vector<std::string> target_inputs;
target_inputs.emplace_back(this->Output);
cc->SetDepends(target_inputs);
@@ -3698,7 +3698,7 @@ bool cmGlobalGenerator::AddBuildDatabaseTargets()
static cm::static_string_view TargetPrefix = "cmake_build_database"_s;
auto AddMergeTarget =
[&mf](std::string const& name, char const* comment,
[&mf](std::string const& name, std::string const& comment,
std::string const& output,
std::function<std::vector<std::string>()> inputs) {
// Add the custom command.
@@ -3732,13 +3732,13 @@ bool cmGlobalGenerator::AddBuildDatabaseTargets()
lang, ".json");
mf->GetOrCreateGeneratedSource(output);
AddMergeTarget(
cmStrCat(TargetPrefix, '-', lang), comment.c_str(), output,
cmStrCat(TargetPrefix, '-', lang), comment, output,
[this, lang]() { return this->PerLanguageModuleDbs[lang]; });
all_lang_paths.emplace_back(std::move(output));
}
// Add the overall target.
auto const* comment = "Combining module command databases";
std::string comment{ "Combining module command databases" };
auto output =
cmStrCat(mf->GetHomeOutputDirectory(), "/build_database.json");
mf->GetOrCreateGeneratedSource(output);
@@ -3758,8 +3758,8 @@ bool cmGlobalGenerator::AddBuildDatabaseTargets()
auto output = cmStrCat(mf->GetHomeOutputDirectory(), "/build_database_",
lang, '_', config, ".json");
mf->GetOrCreateGeneratedSource(output);
AddMergeTarget(cmStrCat(TargetPrefix, '-', lang, '-', config),
comment.c_str(), output, [this, config, lang]() {
AddMergeTarget(cmStrCat(TargetPrefix, '-', lang, '-', config), comment,
output, [this, config, lang]() {
return this->PerConfigModuleDbs[config][lang];
});
all_config_paths.emplace_back(std::move(output));
@@ -3770,8 +3770,8 @@ bool cmGlobalGenerator::AddBuildDatabaseTargets()
auto output = cmStrCat(mf->GetHomeOutputDirectory(), "/build_database_",
config, ".json");
mf->GetOrCreateGeneratedSource(output);
AddMergeTarget(cmStrCat(TargetPrefix, '-', config), comment.c_str(),
output, [all_config_paths]() { return all_config_paths; });
AddMergeTarget(cmStrCat(TargetPrefix, '-', config), comment, output,
[all_config_paths]() { return all_config_paths; });
}
// NMC considerations
@@ -3783,13 +3783,13 @@ bool cmGlobalGenerator::AddBuildDatabaseTargets()
lang, ".json");
mf->GetOrCreateGeneratedSource(output);
AddMergeTarget(
cmStrCat(TargetPrefix, '-', lang), comment.c_str(), output,
cmStrCat(TargetPrefix, '-', lang), comment, output,
[this, lang]() { return this->PerLanguageModuleDbs[lang]; });
all_config_paths.emplace_back(std::move(output));
}
// Add the overall target.
auto const* comment = "Combining all module command databases";
std::string comment{ "Combining all module command databases" };
auto output = cmStrCat(mf->GetHomeOutputDirectory(), "/build_database.json");
mf->GetOrCreateGeneratedSource(output);
AddMergeTarget(std::string(TargetPrefix), comment, output,
@@ -3844,7 +3844,7 @@ void cmGlobalGenerator::CreateGlobalTarget(GlobalTargetInfo const& gti,
// Store the custom command in the target.
cmCustomCommand cc;
cc.SetCommandLines(gti.CommandLines);
cc.SetWorkingDirectory(gti.WorkingDir.c_str());
cc.SetWorkingDirectory(gti.WorkingDir);
cc.SetStdPipesUTF8(gti.StdPipesUTF8);
cc.SetUsesTerminal(gti.UsesTerminal);
cc.SetRole(gti.Role);
+1
View File
@@ -9,6 +9,7 @@
#include <utility>
#include <cm/memory>
#include <cm/optional>
#include <cm/string>
#include <cm/string_view>
#include <cmext/algorithm>
+1 -1
View File
@@ -829,7 +829,7 @@ void cmGlobalXCodeGenerator::AddExtraTargets(
cc = cm::make_unique<cmCustomCommand>();
cc->SetCommandLines(legacyDependHelperCommandLines);
cc->SetComment("Depend check for xcode");
cc->SetWorkingDirectory(legacyDependHelperDir.c_str());
cc->SetWorkingDirectory(legacyDependHelperDir);
gen->AddCustomCommandToTarget(
target->GetName(), cmCustomCommandType::POST_BUILD, std::move(cc),
cmObjectLibraryCommands::Accept);
+1 -1
View File
@@ -3069,7 +3069,7 @@ void cmLocalGenerator::CopyPchCompilePdb(
auto cc = cm::make_unique<cmCustomCommand>();
cc->SetCommandLines(commandLines);
cc->SetComment(comment.c_str());
cc->SetComment(comment);
cc->SetStdPipesUTF8(true);
cc->AppendDepends(
{ reuseTarget->GetPchFile(config, language), copy_script });
+1 -1
View File
@@ -272,7 +272,7 @@ cmSourceFile* cmLocalVisualStudio7Generator::CreateVCProjBuildRule()
cc->SetMainDependency(makefileIn);
cc->SetDepends(listFiles);
cc->SetCommandLines(commandLines);
cc->SetComment(comment.c_str());
cc->SetComment(comment);
cc->SetEscapeOldStyle(false);
cc->SetStdPipesUTF8(true);
cc->SetUsesTerminal(true);
+1 -1
View File
@@ -1520,7 +1520,7 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement(
true, config);
localGen.AppendCustomCommandLines(ccg, *cmdLineLists[i]);
if (cc.GetComment()) {
auto cge = ge.Parse(cc.GetComment());
auto cge = ge.Parse(cc.GetComment().value());
cmdComments[i]->emplace_back(
cge->Evaluate(this->GetLocalGenerator(), config));
}
+3 -1
View File
@@ -11,6 +11,8 @@
#include <utility>
#include <vector>
#include <cm/optional>
#include "cmCustomCommand.h"
#include "cmCustomCommandGenerator.h"
#include "cmGeneratedFileStream.h"
@@ -96,7 +98,7 @@ void cmNinjaUtilityTargetGenerator::WriteUtilBuildStatements(
if (!commandDesc.empty()) {
commandDesc += "; ";
}
auto cge = ge.Parse(ci.GetComment());
auto cge = ge.Parse(ci.GetComment().value());
commandDesc += cge->Evaluate(this->GetLocalGenerator(), config);
}
util_outputs.Add(ccg.GetByproducts());
+1
View File
@@ -5,6 +5,7 @@
#include <utility>
#include <cm/memory>
#include <cm/optional>
#include <cm/string_view>
#include "cmCustomCommand.h"
+3 -2
View File
@@ -6,6 +6,7 @@
#include <utility>
#include <cm/memory>
#include <cm/optional>
#include "cmCustomCommand.h"
#include "cmDiagnostics.h"
@@ -171,9 +172,9 @@ void cmQtAutoGenGlobalInitializer::GetOrCreateGlobalTarget(
// Create utility target
auto cc = cm::make_unique<cmCustomCommand>();
cc->SetWorkingDirectory(makefile->GetHomeOutputDirectory().c_str());
cc->SetWorkingDirectory(makefile->GetHomeOutputDirectory());
cc->SetEscapeOldStyle(false);
cc->SetComment(comment.c_str());
cc->SetComment(comment);
cmTarget* target = localGen->AddUtilityCommand(name, true, std::move(cc));
localGen->AddGeneratorTarget(
cm::make_unique<cmGeneratorTarget>(target, localGen));
+11 -10
View File
@@ -19,6 +19,7 @@
#include <cm/algorithm>
#include <cm/iterator>
#include <cm/memory>
#include <cm/optional>
#include <cm/string_view>
#include <cmext/algorithm>
#include <cmext/string_view>
@@ -1593,7 +1594,7 @@ bool cmQtAutoGenInitializer::InitAutogenTarget()
cc->SetOutputs(timestampFileGenex);
cc->SetDepends(uicDependencies);
cc->SetComment("");
cc->SetWorkingDirectory(this->Dir.Work.c_str());
cc->SetWorkingDirectory(this->Dir.Work);
cc->SetEscapeOldStyle(false);
cc->SetStdPipesUTF8(stdPipesUTF8);
this->LocalGen->AddCustomCommandToOutput(std::move(cc));
@@ -1607,9 +1608,9 @@ bool cmQtAutoGenInitializer::InitAutogenTarget()
cmCustomCommand cc;
cc.SetByproducts(autogenByproducts);
cc.SetCommandLines(commandLines);
cc.SetComment(autogenComment.c_str());
cc.SetComment(autogenComment);
cc.SetBacktrace(this->Makefile->GetBacktrace());
cc.SetWorkingDirectory(this->Dir.Work.c_str());
cc.SetWorkingDirectory(this->Dir.Work);
cc.SetStdPipesUTF8(stdPipesUTF8);
cc.SetEscapeOldStyle(false);
cc.SetEscapeAllowMakeVars(true);
@@ -1671,7 +1672,7 @@ bool cmQtAutoGenInitializer::InitAutogenTarget()
cmStrCat(this->GenTarget->GetName(), "_autogen_timestamp_deps");
auto cc = cm::make_unique<cmCustomCommand>();
cc->SetWorkingDirectory(this->Dir.Work.c_str());
cc->SetWorkingDirectory(this->Dir.Work);
cc->SetDepends(dependencies);
cc->SetEscapeOldStyle(false);
timestampTarget = this->LocalGen->AddUtilityCommand(
@@ -1742,8 +1743,8 @@ bool cmQtAutoGenInitializer::InitAutogenTarget()
cc->SetByproducts(timestampByproducts);
cc->SetDepends(dependencies);
cc->SetCommandLines(commandLines);
cc->SetComment(autogenComment.c_str());
cc->SetWorkingDirectory(this->Dir.Work.c_str());
cc->SetComment(autogenComment);
cc->SetWorkingDirectory(this->Dir.Work);
cc->SetEscapeOldStyle(false);
cc->SetDepfile(depFile);
cc->SetStdPipesUTF8(stdPipesUTF8);
@@ -1763,12 +1764,12 @@ bool cmQtAutoGenInitializer::InitAutogenTarget()
} else {
// Create autogen target
auto cc = cm::make_unique<cmCustomCommand>();
cc->SetWorkingDirectory(this->Dir.Work.c_str());
cc->SetWorkingDirectory(this->Dir.Work);
cc->SetByproducts(autogenByproducts);
cc->SetDepends(dependencies);
cc->SetCommandLines(commandLines);
cc->SetEscapeOldStyle(false);
cc->SetComment(autogenComment.c_str());
cc->SetComment(autogenComment);
cmTarget* autogenTarget = this->LocalGen->AddUtilityCommand(
this->AutogenTarget.Name, true, std::move(cc));
// Create autogen generator target
@@ -1878,9 +1879,9 @@ bool cmQtAutoGenInitializer::InitRccTargets()
FileProjectRelativePath(this->Makefile, qrc.QrcFile));
auto cc = cm::make_unique<cmCustomCommand>();
cc->SetWorkingDirectory(this->Dir.Work.c_str());
cc->SetWorkingDirectory(this->Dir.Work);
cc->SetCommandLines(commandLines);
cc->SetComment(ccComment.c_str());
cc->SetComment(ccComment);
cc->SetStdPipesUTF8(true);
if (qrc.Generated || this->Rcc.GlobalTarget) {