From 9daa1012d4300b4bf987cdfe84a0d2e2972b8358 Mon Sep 17 00:00:00 2001 From: John Franklin Rickard Date: Thu, 29 Jan 2026 19:55:26 +0100 Subject: [PATCH] cmGlobalGenerator: Avoid string copies by returning view --- Source/cmCoreTryCompile.cxx | 4 ++-- Source/cmGlobalGenerator.cxx | 2 +- Source/cmGlobalGenerator.h | 2 +- Source/cmLocalVisualStudio7Generator.cxx | 4 ++-- Source/cmSourceFile.cxx | 4 ++-- Source/cmVisualStudio10TargetGenerator.cxx | 5 +++-- 6 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Source/cmCoreTryCompile.cxx b/Source/cmCoreTryCompile.cxx index d5ac27e8fe..6befcbf890 100644 --- a/Source/cmCoreTryCompile.cxx +++ b/Source/cmCoreTryCompile.cxx @@ -575,9 +575,9 @@ cm::optional 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 diff --git a/Source/cmGlobalGenerator.cxx b/Source/cmGlobalGenerator.cxx index e3bed8b1e1..a42afa4f1e 100644 --- a/Source/cmGlobalGenerator.cxx +++ b/Source/cmGlobalGenerator.cxx @@ -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 diff --git a/Source/cmGlobalGenerator.h b/Source/cmGlobalGenerator.h index 7838b192af..83f76340d1 100644 --- a/Source/cmGlobalGenerator.h +++ b/Source/cmGlobalGenerator.h @@ -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) diff --git a/Source/cmLocalVisualStudio7Generator.cxx b/Source/cmLocalVisualStudio7Generator.cxx index a34820bca4..3d44ae8626 100644 --- a/Source/cmLocalVisualStudio7Generator.cxx +++ b/Source/cmLocalVisualStudio7Generator.cxx @@ -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 diff --git a/Source/cmSourceFile.cxx b/Source/cmSourceFile.cxx index 822c880172..5f2cf3bf3e 100644 --- a/Source/cmSourceFile.cxx +++ b/Source/cmSourceFile.cxx @@ -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); } } diff --git a/Source/cmVisualStudio10TargetGenerator.cxx b/Source/cmVisualStudio10TargetGenerator.cxx index f6cfc3c4d8..364fd566c1 100644 --- a/Source/cmVisualStudio10TargetGenerator.cxx +++ b/Source/cmVisualStudio10TargetGenerator.cxx @@ -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) {