CMake/Source/cmGetSourceFilePropertyCommand.cxx
Sebastian Holtermann e91bfe440c 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.
2019-07-24 11:11:25 +02:00

44 lines
1.2 KiB
C++

/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmGetSourceFilePropertyCommand.h"
#include "cmMakefile.h"
#include "cmSourceFile.h"
class cmExecutionStatus;
// cmSetSourceFilePropertyCommand
bool cmGetSourceFilePropertyCommand::InitialPass(
std::vector<std::string> const& args, cmExecutionStatus&)
{
if (args.size() != 3) {
this->SetError("called with incorrect number of arguments");
return false;
}
std::string const& var = args[0];
std::string const& file = args[1];
cmSourceFile* sf = this->Makefile->GetSource(file);
// for the location we must create a source file first
if (!sf && args[2] == "LOCATION") {
sf = this->Makefile->CreateSource(file);
}
if (sf) {
if (args[2] == "LANGUAGE") {
this->Makefile->AddDefinition(var, sf->GetLanguage());
return true;
}
const char* prop = nullptr;
if (!args[2].empty()) {
prop = sf->GetPropertyForUser(args[2]);
}
if (prop) {
this->Makefile->AddDefinition(var, prop);
return true;
}
}
this->Makefile->AddDefinition(var, "NOTFOUND");
return true;
}