cmMakefile: Let AddDefinition accept a value as cm::string_view

This changes `cmMakefile::AddDefinition` to take a `cm::string_view` as value
argument instead of a `const char *`.

Benefits are:
- `std::string` can be passed to `cmMakefile::AddDefinition` directly without
  the `c_str()` plus string length recomputation fallback.
- Lengths of literals passed to `cmMakefile::AddDefinition` can be computed at
  compile time.

In various sources uses of `cmMakefile::AddDefinition` are adapted to avoid
`std::string::c_str` calls and the `std::string` is passed directly.
Uses of `cmMakefile::AddDefinition`, where a `nullptr` `const char*` might
be passed to `cmMakefile::AddDefinition` are extended with `nullptr` checks.
This commit is contained in:
Sebastian Holtermann
2019-07-17 16:20:58 +02:00
parent f2ba968ef2
commit e91bfe440c
56 changed files with 226 additions and 256 deletions

View File

@@ -55,20 +55,20 @@ bool cmFunctionHelperCommand::operator()(
// set the value of argc
std::ostringstream strStream;
strStream << expandedArgs.size();
makefile.AddDefinition("ARGC", strStream.str().c_str());
makefile.AddDefinition("ARGC", strStream.str());
makefile.MarkVariableAsUsed("ARGC");
// set the values for ARGV0 ARGV1 ...
for (unsigned int t = 0; t < expandedArgs.size(); ++t) {
std::ostringstream tmpStream;
tmpStream << "ARGV" << t;
makefile.AddDefinition(tmpStream.str(), expandedArgs[t].c_str());
makefile.AddDefinition(tmpStream.str(), expandedArgs[t]);
makefile.MarkVariableAsUsed(tmpStream.str());
}
// define the formal arguments
for (unsigned int j = 1; j < this->Args.size(); ++j) {
makefile.AddDefinition(this->Args[j], expandedArgs[j - 1].c_str());
makefile.AddDefinition(this->Args[j], expandedArgs[j - 1]);
}
// define ARGV and ARGN
@@ -76,9 +76,9 @@ bool cmFunctionHelperCommand::operator()(
std::vector<std::string>::const_iterator eit =
expandedArgs.begin() + (this->Args.size() - 1);
std::string argnDef = cmJoin(cmMakeRange(eit, expandedArgs.end()), ";");
makefile.AddDefinition("ARGV", argvDef.c_str());
makefile.AddDefinition("ARGV", argvDef);
makefile.MarkVariableAsUsed("ARGV");
makefile.AddDefinition("ARGN", argnDef.c_str());
makefile.AddDefinition("ARGN", argnDef);
makefile.MarkVariableAsUsed("ARGN");
// Invoke all the functions that were collected in the block.