AddCacheEntry: Suppress raw pointer usage

stage/master/nightly/2023/05/31^2
Marc Chevrier 2023-05-28 16:27:03 +02:00
parent b0d1ddb723
commit 4fc322bab4
18 changed files with 98 additions and 86 deletions

View File

@ -359,19 +359,19 @@ void QCMake::setProperties(const QCMakePropertyList& newProps)
if (s.Type == QCMakeProperty::BOOL) {
this->CMakeInstance->AddCacheEntry(
s.Key.toStdString(), s.Value.toBool() ? "ON" : "OFF",
s.Help.toStdString().c_str(), cmStateEnums::BOOL);
s.Help.toStdString(), cmStateEnums::BOOL);
} else if (s.Type == QCMakeProperty::STRING) {
this->CMakeInstance->AddCacheEntry(
s.Key.toStdString(), s.Value.toString().toStdString(),
s.Help.toStdString().c_str(), cmStateEnums::STRING);
s.Help.toStdString(), cmStateEnums::STRING);
} else if (s.Type == QCMakeProperty::PATH) {
this->CMakeInstance->AddCacheEntry(
s.Key.toStdString(), s.Value.toString().toStdString(),
s.Help.toStdString().c_str(), cmStateEnums::PATH);
s.Help.toStdString(), cmStateEnums::PATH);
} else if (s.Type == QCMakeProperty::FILEPATH) {
this->CMakeInstance->AddCacheEntry(
s.Key.toStdString(), s.Value.toString().toStdString(),
s.Help.toStdString().c_str(), cmStateEnums::FILEPATH);
s.Help.toStdString(), cmStateEnums::FILEPATH);
}
}

View File

@ -14,6 +14,7 @@
#include "cmMakefile.h"
#include "cmSourceFile.h"
#include "cmState.h"
#include "cmValue.h"
#include "cmVersion.h"
#ifdef __QNX__
@ -78,25 +79,37 @@ static void CCONV cmAddCacheDefinition(void* arg, const char* name,
int type)
{
cmMakefile* mf = static_cast<cmMakefile*>(arg);
std::string valueString;
std::string docString;
cmValue v;
cmValue d;
if (value != nullptr) {
valueString = value;
v = cmValue{ valueString };
}
if (doc != nullptr) {
docString = doc;
d = cmValue{ docString };
}
switch (type) {
case CM_CACHE_BOOL:
mf->AddCacheDefinition(name, value, doc, cmStateEnums::BOOL);
mf->AddCacheDefinition(name, v, d, cmStateEnums::BOOL);
break;
case CM_CACHE_PATH:
mf->AddCacheDefinition(name, value, doc, cmStateEnums::PATH);
mf->AddCacheDefinition(name, v, d, cmStateEnums::PATH);
break;
case CM_CACHE_FILEPATH:
mf->AddCacheDefinition(name, value, doc, cmStateEnums::FILEPATH);
mf->AddCacheDefinition(name, v, d, cmStateEnums::FILEPATH);
break;
case CM_CACHE_STRING:
mf->AddCacheDefinition(name, value, doc, cmStateEnums::STRING);
mf->AddCacheDefinition(name, v, d, cmStateEnums::STRING);
break;
case CM_CACHE_INTERNAL:
mf->AddCacheDefinition(name, value, doc, cmStateEnums::INTERNAL);
mf->AddCacheDefinition(name, v, d, cmStateEnums::INTERNAL);
break;
case CM_CACHE_STATIC:
mf->AddCacheDefinition(name, value, doc, cmStateEnums::STATIC);
mf->AddCacheDefinition(name, v, d, cmStateEnums::STATIC);
break;
}
}

View File

@ -523,7 +523,7 @@ void cmCacheManager::PrintCache(std::ostream& out) const
}
void cmCacheManager::AddCacheEntry(const std::string& key, cmValue value,
const char* helpString,
cmValue helpString,
cmStateEnums::CacheEntryType type)
{
CacheEntry& e = this->Cache[key];
@ -543,7 +543,7 @@ void cmCacheManager::AddCacheEntry(const std::string& key, cmValue value,
}
e.SetProperty(
"HELPSTRING",
helpString ? std::string{ helpString }
helpString ? *helpString
: std::string{
"(This variable does not exist and should not be used)" });
}

View File

@ -173,20 +173,19 @@ public:
unsigned int GetCacheMinorVersion() const { return this->CacheMinorVersion; }
//! Add an entry into the cache
void AddCacheEntry(const std::string& key, const char* value,
const char* helpString, cmStateEnums::CacheEntryType type)
{
this->AddCacheEntry(key,
value ? cmValue(std::string(value)) : cmValue(nullptr),
helpString, type);
}
void AddCacheEntry(const std::string& key, const std::string& value,
const char* helpString, cmStateEnums::CacheEntryType type)
const std::string& helpString,
cmStateEnums::CacheEntryType type)
{
this->AddCacheEntry(key, cmValue(value), helpString, type);
this->AddCacheEntry(key, cmValue{ value }, cmValue{ helpString }, type);
}
void AddCacheEntry(const std::string& key, cmValue value,
const char* helpString,
const std::string& helpString,
cmStateEnums::CacheEntryType type)
{
this->AddCacheEntry(key, value, cmValue{ helpString }, type);
}
void AddCacheEntry(const std::string& key, cmValue value, cmValue helpString,
cmStateEnums::CacheEntryType type);
//! Remove an entry from the cache

View File

@ -257,7 +257,7 @@ void cmExtraEclipseCDT4Generator::AddEnvVar(std::ostream& out,
// The variable is in the env, but not in the cache. Use it and put it
// in the cache
valueToUse = envVarValue;
mf->AddCacheDefinition(cacheEntryName, valueToUse, cacheEntryName.c_str(),
mf->AddCacheDefinition(cacheEntryName, valueToUse, cacheEntryName,
cmStateEnums::STRING, true);
mf->GetCMakeInstance()->SaveCache(lg.GetBinaryDirectory());
} else if (!envVarSet && cacheValue) {
@ -272,9 +272,8 @@ void cmExtraEclipseCDT4Generator::AddEnvVar(std::ostream& out,
valueToUse = *cacheValue;
if (valueToUse.find(envVarValue) == std::string::npos) {
valueToUse = envVarValue;
mf->AddCacheDefinition(cacheEntryName, valueToUse,
cacheEntryName.c_str(), cmStateEnums::STRING,
true);
mf->AddCacheDefinition(cacheEntryName, valueToUse, cacheEntryName,
cmStateEnums::STRING, true);
mf->GetCMakeInstance()->SaveCache(lg.GetBinaryDirectory());
}
}

View File

@ -509,7 +509,7 @@ void cmFindBase::NormalizeFindResult()
// value.
if (value != *existingValue || this->AlreadyInCacheWithoutMetaInfo) {
this->Makefile->GetCMakeInstance()->AddCacheEntry(
this->VariableName, value, this->VariableDocumentation.c_str(),
this->VariableName, value, this->VariableDocumentation,
this->VariableType);
if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) ==
cmPolicies::NEW) {
@ -534,7 +534,7 @@ void cmFindBase::NormalizeFindResult()
if (this->StoreResultInCache) {
if (this->AlreadyInCacheWithoutMetaInfo) {
this->Makefile->AddCacheDefinition(this->VariableName, "",
this->VariableDocumentation.c_str(),
this->VariableDocumentation,
this->VariableType);
if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) ==
cmPolicies::NEW &&
@ -564,7 +564,7 @@ void cmFindBase::StoreFindResult(const std::string& value)
if (!value.empty()) {
if (this->StoreResultInCache) {
this->Makefile->AddCacheDefinition(this->VariableName, value,
this->VariableDocumentation.c_str(),
this->VariableDocumentation,
this->VariableType, force);
if (updateNormalVariable &&
this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
@ -580,7 +580,7 @@ void cmFindBase::StoreFindResult(const std::string& value)
auto notFound = cmStrCat(this->VariableName, "-NOTFOUND");
if (this->StoreResultInCache) {
this->Makefile->AddCacheDefinition(this->VariableName, notFound,
this->VariableDocumentation.c_str(),
this->VariableDocumentation,
this->VariableType, force);
if (updateNormalVariable &&
this->Makefile->IsNormalDefinitionSet(this->VariableName)) {

View File

@ -1728,7 +1728,7 @@ void cmFindPackageCommand::SetConfigDirCacheVariable(const std::string& value)
std::string const help =
cmStrCat("The directory containing a CMake configuration file for ",
this->Name, '.');
this->Makefile->AddCacheDefinition(this->Variable, value, help.c_str(),
this->Makefile->AddCacheDefinition(this->Variable, value, help,
cmStateEnums::PATH, true);
if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) ==
cmPolicies::NEW &&

View File

@ -1357,11 +1357,9 @@ void cmGlobalGenerator::Configure()
// update the cache entry for the number of local generators, this is used
// for progress
char num[100];
snprintf(num, sizeof(num), "%d", static_cast<int>(this->Makefiles.size()));
this->GetCMakeInstance()->AddCacheEntry("CMAKE_NUMBER_OF_MAKEFILES", num,
"number of local generators",
cmStateEnums::INTERNAL);
this->GetCMakeInstance()->AddCacheEntry(
"CMAKE_NUMBER_OF_MAKEFILES", std::to_string(this->Makefiles.size()),
"number of local generators", cmStateEnums::INTERNAL);
auto endTime = std::chrono::steady_clock::now();

View File

@ -1939,8 +1939,8 @@ void cmMakefile::AddDefinitionBool(const std::string& name, bool value)
this->AddDefinition(name, value ? "ON" : "OFF");
}
void cmMakefile::AddCacheDefinition(const std::string& name, const char* value,
const char* doc,
void cmMakefile::AddCacheDefinition(const std::string& name, cmValue value,
cmValue doc,
cmStateEnums::CacheEntryType type,
bool force)
{
@ -1954,22 +1954,20 @@ void cmMakefile::AddCacheDefinition(const std::string& name, const char* value,
// if this is not a force, then use the value from the cache
// if it is a force, then use the value being passed in
if (!force) {
value = existingValue->c_str();
value = existingValue;
}
if (type == cmStateEnums::PATH || type == cmStateEnums::FILEPATH) {
nvalue = value ? value : "";
cmList files(nvalue);
cmList files(value);
for (auto& file : files) {
if (!cmIsOff(file)) {
file = cmSystemTools::CollapseFullPath(file);
}
}
nvalue = files.to_string();
value = cmValue{ nvalue };
this->GetCMakeInstance()->AddCacheEntry(name, nvalue, doc, type);
nvalue = *this->GetState()->GetInitializedCacheValue(name);
value = nvalue.c_str();
this->GetCMakeInstance()->AddCacheEntry(name, value, doc, type);
value = this->GetState()->GetInitializedCacheValue(name);
}
}
this->GetCMakeInstance()->AddCacheEntry(name, value, doc, type);

View File

@ -302,14 +302,23 @@ public:
*/
void AddDefinitionBool(const std::string& name, bool);
//! Add a definition to this makefile and the global cmake cache.
void AddCacheDefinition(const std::string& name, const char* value,
const char* doc, cmStateEnums::CacheEntryType type,
void AddCacheDefinition(const std::string& name, cmValue value, cmValue doc,
cmStateEnums::CacheEntryType type,
bool force = false);
void AddCacheDefinition(const std::string& name, const std::string& value,
const char* doc, cmStateEnums::CacheEntryType type,
void AddCacheDefinition(const std::string& name, cmValue value,
const std::string& doc,
cmStateEnums::CacheEntryType type,
bool force = false)
{
this->AddCacheDefinition(name, value.c_str(), doc, type, force);
this->AddCacheDefinition(name, value, cmValue{ doc }, type, force);
}
void AddCacheDefinition(const std::string& name, const std::string& value,
const std::string& doc,
cmStateEnums::CacheEntryType type,
bool force = false)
{
this->AddCacheDefinition(name, cmValue{ value }, cmValue{ doc }, type,
force);
}
/**

View File

@ -83,7 +83,8 @@ bool cmMarkAsAdvancedCommand(std::vector<std::string> const& args,
if (oldBehavior) {
if (!state->GetCacheEntryValue(variable)) {
status.GetMakefile().GetCMakeInstance()->AddCacheEntry(
variable, nullptr, nullptr, cmStateEnums::UNINITIALIZED);
variable, cmValue{ nullptr }, cmValue{ nullptr },
cmStateEnums::UNINITIALIZED);
overwrite = true;
}
}

View File

@ -67,7 +67,7 @@ bool cmOptionCommand(std::vector<std::string> const& args,
}
bool init = cmIsOn(initialValue);
status.GetMakefile().AddCacheDefinition(args[0], init ? "ON" : "OFF",
args[1].c_str(), cmStateEnums::BOOL);
args[1], cmStateEnums::BOOL);
if (status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0077) !=
cmPolicies::NEW &&
status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0126) ==

View File

@ -79,8 +79,8 @@ bool cmSetCommand(std::vector<std::string> const& args,
bool force = false; // optional
bool parentScope = false;
cmStateEnums::CacheEntryType type =
cmStateEnums::STRING; // required if cache
const char* docstring = nullptr; // required if cache
cmStateEnums::STRING; // required if cache
cmValue docstring; // required if cache
unsigned int ignoreLastArgs = 0;
// look for PARENT_SCOPE argument
@ -131,7 +131,7 @@ bool cmSetCommand(std::vector<std::string> const& args,
// ensure that the type is actually converting to a string.
type = cmStateEnums::STRING;
}
docstring = args[cacheStart + 2].c_str();
docstring = cmValue{ args[cacheStart + 2] };
}
// see if this is already in the cache
@ -150,8 +150,8 @@ bool cmSetCommand(std::vector<std::string> const& args,
// if it is meant to be in the cache then define it in the cache
if (cache) {
status.GetMakefile().AddCacheDefinition(variable, value, docstring, type,
force);
status.GetMakefile().AddCacheDefinition(variable, cmValue{ value },
docstring, type, force);
} else {
// add the definition
status.GetMakefile().AddDefinition(variable, value);

View File

@ -209,7 +209,7 @@ bool cmState::GetCacheEntryPropertyAsBool(std::string const& key,
}
void cmState::AddCacheEntry(const std::string& key, cmValue value,
const char* helpString,
const std::string& helpString,
cmStateEnums::CacheEntryType type)
{
this->CacheManager->AddCacheEntry(key, value, helpString, type);

View File

@ -254,7 +254,7 @@ public:
private:
friend class cmake;
void AddCacheEntry(const std::string& key, cmValue value,
const char* helpString,
const std::string& helpString,
cmStateEnums::CacheEntryType type);
bool DoWriteGlobVerifyTarget() const;

View File

@ -2,7 +2,6 @@
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmTryRunCommand.h"
#include <cstdio>
#include <stdexcept>
#include <cm/optional>
@ -293,11 +292,9 @@ void TryRunCommandImpl::RunExecutable(const std::string& runArgs,
workDir ? workDir->c_str() : nullptr, cmSystemTools::OUTPUT_NONE,
cmDuration::zero());
// set the run var
char retChar[16];
const char* retStr;
std::string retStr;
if (worked) {
snprintf(retChar, sizeof(retChar), "%i", retVal);
retStr = retChar;
retStr = std::to_string(retVal);
} else {
retStr = "FAILED_TO_RUN";
}
@ -351,7 +348,7 @@ void TryRunCommandImpl::DoNotRunExecutable(
detailsString);
this->Makefile->AddCacheDefinition(this->RunResultVariable,
"PLEASE_FILL_OUT-FAILED_TO_RUN",
comment.c_str(), cmStateEnums::STRING);
comment, cmStateEnums::STRING);
cmState* state = this->Makefile->GetState();
cmValue existingValue = state->GetCacheEntryValue(this->RunResultVariable);
@ -372,9 +369,9 @@ void TryRunCommandImpl::DoNotRunExecutable(
"would have printed on stdout on its target platform.\n",
detailsString);
this->Makefile->AddCacheDefinition(
internalRunOutputStdOutName, "PLEASE_FILL_OUT-NOTFOUND",
comment.c_str(), cmStateEnums::STRING);
this->Makefile->AddCacheDefinition(internalRunOutputStdOutName,
"PLEASE_FILL_OUT-NOTFOUND", comment,
cmStateEnums::STRING);
cmState* state = this->Makefile->GetState();
cmValue existing =
state->GetCacheEntryValue(internalRunOutputStdOutName);
@ -394,9 +391,9 @@ void TryRunCommandImpl::DoNotRunExecutable(
"would have printed on stderr on its target platform.\n",
detailsString);
this->Makefile->AddCacheDefinition(
internalRunOutputStdErrName, "PLEASE_FILL_OUT-NOTFOUND",
comment.c_str(), cmStateEnums::STRING);
this->Makefile->AddCacheDefinition(internalRunOutputStdErrName,
"PLEASE_FILL_OUT-NOTFOUND", comment,
cmStateEnums::STRING);
cmState* state = this->Makefile->GetState();
cmValue existing =
state->GetCacheEntryValue(internalRunOutputStdErrName);
@ -416,9 +413,9 @@ void TryRunCommandImpl::DoNotRunExecutable(
"would have printed on stdout and stderr on its target platform.\n",
detailsString);
this->Makefile->AddCacheDefinition(
internalRunOutputName, "PLEASE_FILL_OUT-NOTFOUND", comment.c_str(),
cmStateEnums::STRING);
this->Makefile->AddCacheDefinition(internalRunOutputName,
"PLEASE_FILL_OUT-NOTFOUND", comment,
cmStateEnums::STRING);
cmState* state = this->Makefile->GetState();
cmValue existing = state->GetCacheEntryValue(internalRunOutputName);
if (existing) {

View File

@ -2205,7 +2205,7 @@ int cmake::HandleDeleteCacheVariables(const std::string& var)
this->LoadCache();
// restore the changed compilers
for (SaveCacheEntry const& i : saved) {
this->AddCacheEntry(i.key, i.value, i.help.c_str(), i.type);
this->AddCacheEntry(i.key, i.value, i.help, i.type);
}
cmSystemTools::Message(warning.str());
// avoid reconfigure if there were errors
@ -2775,7 +2775,7 @@ int cmake::Generate()
}
void cmake::AddCacheEntry(const std::string& key, cmValue value,
const char* helpString, int type)
cmValue helpString, int type)
{
this->State->AddCacheEntry(key, value, helpString,
static_cast<cmStateEnums::CacheEntryType>(type));

View File

@ -328,20 +328,18 @@ public:
*/
cmValue GetCacheDefinition(const std::string&) const;
//! Add an entry into the cache
void AddCacheEntry(const std::string& key, const char* value,
const char* helpString, int type)
{
this->AddCacheEntry(key,
value ? cmValue(std::string(value)) : cmValue(nullptr),
helpString, type);
}
void AddCacheEntry(const std::string& key, const std::string& value,
const char* helpString, int type)
const std::string& helpString, int type)
{
this->AddCacheEntry(key, cmValue(value), helpString, type);
this->AddCacheEntry(key, cmValue{ value }, cmValue{ helpString }, type);
}
void AddCacheEntry(const std::string& key, cmValue value,
const char* helpString, int type);
const std::string& helpString, int type)
{
this->AddCacheEntry(key, value, cmValue{ helpString }, type);
}
void AddCacheEntry(const std::string& key, cmValue value, cmValue helpString,
int type);
bool DoWriteGlobVerifyTarget() const;
std::string const& GetGlobVerifyScript() const;