[Lldb-commits] [lldb] 9cdd68e - Recommit "[lldb/API] Overwrite variables with SBLaunchInfo::SetEnvironment(append=true)"

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Thu Jul 23 05:18:06 PDT 2020


Author: Pavel Labath
Date: 2020-07-23T14:17:57+02:00
New Revision: 9cdd68e7c1331efd3c0aec84ef925a1679a73771

URL: https://github.com/llvm/llvm-project/commit/9cdd68e7c1331efd3c0aec84ef925a1679a73771
DIFF: https://github.com/llvm/llvm-project/commit/9cdd68e7c1331efd3c0aec84ef925a1679a73771.diff

LOG: Recommit "[lldb/API] Overwrite variables with SBLaunchInfo::SetEnvironment(append=true)"

The patch was reverted 27d52cd86a2c because of failures in
TestWeakSymbols.py. These have now been addressed in D83552.

The original commit message was:
This function was documented to overwrite entries with D76111, which was
adding a couple of similar functions. However, this function (unlike the
functions added in that patch) was/is not actually overwriting variables
-- any pre-existing variables would get ignored.

This behavior does not seem to be intentional. In fact, before the refactor in
D41359, this function could introduce duplicate entries, which could
have very surprising effects both inside lldb and on other applications
(some applications would take the first value, some the second one; in
lldb, attempting to unset a variable could make the second variable
become active, etc.).

Overwriting seems to be the most reasonable behavior here, so change the
code to match documentation.

Differential Revision: https://reviews.llvm.org/D83306

Added: 
    

Modified: 
    lldb/source/API/SBLaunchInfo.cpp
    lldb/test/API/python_api/sbenvironment/TestSBEnvironment.py

Removed: 
    


################################################################################
diff  --git a/lldb/source/API/SBLaunchInfo.cpp b/lldb/source/API/SBLaunchInfo.cpp
index ba13072e8f9b..cda8134c9853 100644
--- a/lldb/source/API/SBLaunchInfo.cpp
+++ b/lldb/source/API/SBLaunchInfo.cpp
@@ -190,9 +190,10 @@ void SBLaunchInfo::SetEnvironment(const SBEnvironment &env, bool append) {
   LLDB_RECORD_METHOD(void, SBLaunchInfo, SetEnvironment,
                      (const lldb::SBEnvironment &, bool), env, append);
   Environment &refEnv = env.ref();
-  if (append)
-    m_opaque_sp->GetEnvironment().insert(refEnv.begin(), refEnv.end());
-  else
+  if (append) {
+    for (auto &KV : refEnv)
+      m_opaque_sp->GetEnvironment().insert_or_assign(KV.first(), KV.second);
+  } else
     m_opaque_sp->GetEnvironment() = refEnv;
   m_opaque_sp->RegenerateEnvp();
 }

diff  --git a/lldb/test/API/python_api/sbenvironment/TestSBEnvironment.py b/lldb/test/API/python_api/sbenvironment/TestSBEnvironment.py
index c1937f985e28..6389854ce58f 100644
--- a/lldb/test/API/python_api/sbenvironment/TestSBEnvironment.py
+++ b/lldb/test/API/python_api/sbenvironment/TestSBEnvironment.py
@@ -53,6 +53,11 @@ def test_launch_info(self):
         launch_info.SetEnvironment(env, append=True)
         self.assertEqual(launch_info.GetEnvironment().GetNumValues(), env_count + 1)
 
+        env.Set("FOO", "baz", overwrite=True)
+        launch_info.SetEnvironment(env, append=True)
+        self.assertEqual(launch_info.GetEnvironment().GetNumValues(), env_count + 1)
+        self.assertEqual(launch_info.GetEnvironment().Get("FOO"), "baz")
+
         # Make sure we can replace the launchInfo's environment
         env.Clear()
         env.Set("BAR", "foo", overwrite=True)
@@ -120,6 +125,11 @@ def test_creating_and_modifying_environment(self):
         env.SetEntries(entries, append=False)
         self.assertEqualEntries(env, ["X=x", "Y=y"])
 
+        entries.Clear()
+        entries.AppendList(["X=y", "Y=x"], 2)
+        env.SetEntries(entries, append=True)
+        self.assertEqualEntries(env, ["X=y", "Y=x"])
+
         # Test clear
         env.Clear()
         self.assertEqualEntries(env, [])


        


More information about the lldb-commits mailing list