cmGlobalGenerator: Avoid string copies by returning view

This commit is contained in:
John Franklin Rickard
2026-02-06 12:27:23 -05:00
committed by Brad King
parent e531e7420d
commit 9daa1012d4
6 changed files with 11 additions and 10 deletions
+2 -2
View File
@@ -575,9 +575,9 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode(
for (auto const& source : sources) {
auto const& si = source.first;
std::string ext = cmSystemTools::GetFilenameLastExtension(si);
std::string lang = gg->GetLanguageFromExtension(ext);
cm::string_view lang = gg->GetLanguageFromExtension(ext);
if (!lang.empty()) {
testLangs.insert(lang);
testLangs.insert(std::string(lang));
} else {
std::ostringstream err;
err << "Unknown extension \"" << ext
+1 -1
View File
@@ -1100,7 +1100,7 @@ std::string cmGlobalGenerator::GetLanguageOutputExtension(
return "";
}
std::string cmGlobalGenerator::GetLanguageFromExtension(
cm::string_view cmGlobalGenerator::GetLanguageFromExtension(
cm::string_view ext) const
{
// if there is an extension and it starts with . then move past the
+1 -1
View File
@@ -360,7 +360,7 @@ public:
bool GetToolSupportsColor() const { return this->ToolSupportsColor; }
//! return the language for the given extension
std::string GetLanguageFromExtension(cm::string_view ext) const;
cm::string_view GetLanguageFromExtension(cm::string_view ext) const;
//! is an extension to be ignored
bool IgnoreFile(cm::string_view ext) const;
//! What is the preference for linkers and this language (None or Preferred)
+2 -2
View File
@@ -1486,8 +1486,8 @@ cmLocalVisualStudio7GeneratorFCInfo::cmLocalVisualStudio7GeneratorFCInfo(
std::string configUpper = cmSystemTools::UpperCase(config);
cmLVS7GFileConfig fc;
std::string lang =
lg->GlobalGenerator->GetLanguageFromExtension(sf.GetExtension());
std::string lang = std::string(
lg->GlobalGenerator->GetLanguageFromExtension(sf.GetExtension()));
std::string const& sourceLang = lg->GetSourceFileLanguage(sf);
bool needForceLang = false;
// source file does not match its extension language
+2 -2
View File
@@ -287,9 +287,9 @@ void cmSourceFile::CheckLanguage(cm::string_view ext)
// Try to identify the source file language from the extension.
cmMakefile const* mf = this->Location.GetMakefile();
cmGlobalGenerator* gg = mf->GetGlobalGenerator();
std::string l = gg->GetLanguageFromExtension(ext);
cm::string_view l = gg->GetLanguageFromExtension(ext);
if (!l.empty()) {
this->Language = std::move(l);
this->Language = std::string(l);
}
}
+3 -2
View File
@@ -2835,8 +2835,9 @@ void cmVisualStudio10TargetGenerator::OutputSourceSpecificFlags(
// Force language if the file extension does not match.
// Note that MSVC treats the upper-case '.C' extension as C and not C++.
std::string const ext = sf.GetExtension();
std::string const extLang =
ext == "C"_s ? "C" : this->GlobalGenerator->GetLanguageFromExtension(ext);
cm::string_view const extLang = ext == "C"_s
? "C"_s
: this->GlobalGenerator->GetLanguageFromExtension(ext);
std::string lang = this->LocalGenerator->GetSourceFileLanguage(sf);
char const* compileAs = nullptr;
if (lang != extLang) {