Merge topic 'listfile-string'

b4c6db0f52 cmListFile: Use `std::string` in place of `const char*`

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !11793
diff --git a/Source/cmListFileCache.cxx b/Source/cmListFileCache.cxx
index d3e6288..430062c 100644
--- a/Source/cmListFileCache.cxx
+++ b/Source/cmListFileCache.cxx
@@ -48,12 +48,12 @@
 {
 public:
   cmListFileParser(cmListFile* lf, cmListFileBacktrace lfbt,
-                   cmMessenger* messenger);
+                   cmMessenger* messenger, std::string const& filename);
   cmListFileParser(cmListFileParser const&) = delete;
   cmListFileParser& operator=(cmListFileParser const&) = delete;
 
-  bool ParseFile(char const* filename);
-  bool ParseString(cm::string_view str, char const* virtual_filename);
+  bool ParseFile();
+  bool ParseString(cm::string_view str);
 
 private:
   bool Parse();
@@ -75,7 +75,7 @@
   cmListFile* ListFile;
   cmListFileBacktrace Backtrace;
   cmMessenger* Messenger;
-  char const* FileName = nullptr;
+  std::string const& FileName;
   std::unique_ptr<cmListFileLexer, void (*)(cmListFileLexer*)> Lexer;
   std::string FunctionName;
   long FunctionLine;
@@ -84,10 +84,12 @@
 };
 
 cmListFileParser::cmListFileParser(cmListFile* lf, cmListFileBacktrace lfbt,
-                                   cmMessenger* messenger)
+                                   cmMessenger* messenger,
+                                   std::string const& filename)
   : ListFile(lf)
   , Backtrace(std::move(lfbt))
   , Messenger(messenger)
+  , FileName(filename)
   , Lexer(cmListFileLexer_New(), cmListFileLexer_Delete)
 {
 }
@@ -109,19 +111,20 @@
   cmSystemTools::SetFatalErrorOccurred();
 }
 
-bool cmListFileParser::ParseFile(char const* filename)
+bool cmListFileParser::ParseFile()
 {
-  this->FileName = filename;
+  std::string const* filename = &this->FileName;
 
 #ifdef _WIN32
   std::string expandedFileName = cmsys::Encoding::ToNarrow(
-    cmSystemTools::ConvertToWindowsExtendedPath(filename));
-  filename = expandedFileName.c_str();
+    cmSystemTools::ConvertToWindowsExtendedPath(*filename));
+  filename = &expandedFileName;
 #endif
 
   // Open the file.
   cmListFileLexer_BOM bom;
-  if (!cmListFileLexer_SetFileName(this->Lexer.get(), filename, &bom)) {
+  if (!cmListFileLexer_SetFileName(this->Lexer.get(), filename->c_str(),
+                                   &bom)) {
     this->IssueFileOpenError("cmListFileCache: error can not open file.");
     return false;
   }
@@ -137,11 +140,8 @@
   return this->Parse();
 }
 
-bool cmListFileParser::ParseString(cm::string_view str,
-                                   char const* virtual_filename)
+bool cmListFileParser::ParseString(cm::string_view str)
 {
-  this->FileName = virtual_filename;
-
   if (!cmListFileLexer_SetString(this->Lexer.get(), str.data(),
                                  str.length())) {
     this->IssueFileOpenError("cmListFileCache: cannot allocate buffer.");
@@ -422,7 +422,7 @@
 
 } // anonymous namespace
 
-bool cmListFile::ParseFile(char const* filename, cmMessenger* messenger,
+bool cmListFile::ParseFile(std::string const& filename, cmMessenger* messenger,
                            cmListFileBacktrace const& lfbt)
 {
   if (!cmSystemTools::FileExists(filename) ||
@@ -430,28 +430,17 @@
     return false;
   }
 
-  bool parseError = false;
-
-  {
-    cmListFileParser parser(this, lfbt, messenger);
-    parseError = !parser.ParseFile(filename);
-  }
-
-  return !parseError;
+  cmListFileParser parser(this, lfbt, messenger, filename);
+  return parser.ParseFile();
 }
 
-bool cmListFile::ParseString(cm::string_view str, char const* virtual_filename,
+bool cmListFile::ParseString(cm::string_view str,
+                             std::string const& virtual_filename,
                              cmMessenger* messenger,
                              cmListFileBacktrace const& lfbt)
 {
-  bool parseError = false;
-
-  {
-    cmListFileParser parser(this, lfbt, messenger);
-    parseError = !parser.ParseString(str, virtual_filename);
-  }
-
-  return !parseError;
+  cmListFileParser parser(this, lfbt, messenger, virtual_filename);
+  return parser.ParseString(str);
 }
 
 #include "cmStack.tcc"
diff --git a/Source/cmListFileCache.h b/Source/cmListFileCache.h
index 32093d2..d4db74c 100644
--- a/Source/cmListFileCache.h
+++ b/Source/cmListFileCache.h
@@ -239,10 +239,10 @@
 
 struct cmListFile
 {
-  bool ParseFile(char const* path, cmMessenger* messenger,
+  bool ParseFile(std::string const& path, cmMessenger* messenger,
                  cmListFileBacktrace const& lfbt);
 
-  bool ParseString(cm::string_view str, char const* virtual_filename,
+  bool ParseString(cm::string_view str, std::string const& virtual_filename,
                    cmMessenger* messenger, cmListFileBacktrace const& lfbt);
 
   std::vector<cmListFileFunction> Functions;
diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx
index 49c1d0b..83f7fbd 100644
--- a/Source/cmMakefile.cxx
+++ b/Source/cmMakefile.cxx
@@ -682,7 +682,7 @@
 #endif
 
   cmListFile listFile;
-  if (!listFile.ParseFile(filenametoread.c_str(), this->GetMessenger(),
+  if (!listFile.ParseFile(filenametoread, this->GetMessenger(),
                           this->Backtrace)) {
 #ifdef CMake_ENABLE_DEBUGGER
     if (this->GetCMakeInstance()->GetDebugAdapter()) {
@@ -805,7 +805,7 @@
 #endif
 
   cmListFile listFile;
-  if (!listFile.ParseFile(filenametoread.c_str(), this->GetMessenger(),
+  if (!listFile.ParseFile(filenametoread, this->GetMessenger(),
                           this->Backtrace)) {
 #ifdef CMake_ENABLE_DEBUGGER
     if (this->GetCMakeInstance()->GetDebugAdapter()) {
@@ -840,8 +840,8 @@
   ListFileScope scope(this, filenametoread);
 
   cmListFile listFile;
-  if (!listFile.ParseString(content, virtualFileName.c_str(),
-                            this->GetMessenger(), this->Backtrace)) {
+  if (!listFile.ParseString(content, virtualFileName, this->GetMessenger(),
+                            this->Backtrace)) {
     return false;
   }
 
@@ -1594,7 +1594,7 @@
 #endif
 
   cmListFile listFile;
-  if (!listFile.ParseFile(currentStart.c_str(), this->GetMessenger(),
+  if (!listFile.ParseFile(currentStart, this->GetMessenger(),
                           this->Backtrace)) {
 #ifdef CMake_ENABLE_DEBUGGER
     if (this->GetCMakeInstance()->GetDebugAdapter()) {
diff --git a/Tests/CMakeLib/testDebugger.h b/Tests/CMakeLib/testDebugger.h
index c27086d..f08e5a9 100644
--- a/Tests/CMakeLib/testDebugger.h
+++ b/Tests/CMakeLib/testDebugger.h
@@ -72,8 +72,8 @@
     Client->bind(server2client, client2server);
     Debugger->bind(client2server, server2client);
   }
-  std::vector<cmListFileFunction> CreateListFileFunctions(char const* str,
-                                                          char const* filename)
+  std::vector<cmListFileFunction> CreateListFileFunctions(
+    char const* str, std::string const& filename)
   {
     cmMessenger messenger;
     cmListFileBacktrace backtrace;
diff --git a/Tests/CMakeLib/testDebuggerBreakpointManager.cxx b/Tests/CMakeLib/testDebuggerBreakpointManager.cxx
index 3fff39e..527053d 100644
--- a/Tests/CMakeLib/testDebuggerBreakpointManager.cxx
+++ b/Tests/CMakeLib/testDebuggerBreakpointManager.cxx
@@ -75,8 +75,7 @@
   helper.bind();
   std::string sourcePath = "C:/CMakeLists.txt";
   std::vector<cmListFileFunction> functions = helper.CreateListFileFunctions(
-    "# Comment1\nset(var1 foo)\n# Comment2\nset(var2\nbar)\n",
-    sourcePath.c_str());
+    "# Comment1\nset(var1 foo)\n# Comment2\nset(var2\nbar)\n", sourcePath);
 
   breakpointManager.SourceFileLoaded(sourcePath, functions);
   dap::SetBreakpointsRequest setBreakpointRequest;
@@ -149,8 +148,7 @@
   sourceBreakpoints[4].line = 5;
   setBreakpointRequest.breakpoints = sourceBreakpoints;
   std::vector<cmListFileFunction> functions = helper.CreateListFileFunctions(
-    "# Comment1\nset(var1 foo)\n# Comment2\nset(var2\nbar)\n",
-    sourcePath.c_str());
+    "# Comment1\nset(var1 foo)\n# Comment2\nset(var2\nbar)\n", sourcePath);
   auto got = helper.Client->send(setBreakpointRequest).get();
 
   // Act