LLDB preloads some dependent modules when creating the target. On
Windows, when the target is run and the dependent DLLs are actually
loaded by the process, ProcessWindows::OnLoadDll calls
DynamicLoaderWindowsDYLD::OnLoadModule which in turn calls
Target::GetOrCreateModule to try to load the DLL module. However, it
did not properly handle the case of getting an already-preloaded module,
and as a result any dependent DLLs preloaded by LLDB will end up being
duplicated in the target module list and causing some commands to return
repeated results (e.g. target modules lookup).
This issue does not seem to affect Linux, because the codepath it uses,
which goes through DynamicLoader::FindModuleViaTarget, has an
additional check to find existing modules before calling
Target::GetOrCreateModule.
This patch adds an early return to Target::GetOrCreateModule for when
it finds a pre-existing module already in the target module list to fix
the issue for Windows.
Unfortunately, this aspect of the test doesn't work as is on Windows.
By default (in msvc builds), clang invokes link.exe here, but link.exe can't take %t.shlib.dll as input file to the linker, as it requires an import library.
If we would assume that lld is available, we could run the linking command with -fuse-ld=lld - however that on its own isn't enough, as lld only allows linking against DLLs when run with the -lldmingw flag. We can add -fuse-ld=lld -Wl,-lldmingw here to fix that, but that would break testing in mingw environments, as -lldmingw isn't a valid option on the mingw lld driver.
Likewise, we could create a proper import library by adding -Wl,-implib:%t.shlib.lib on the first command above, but that doesn't work in mingw mode either, as it would have to be -Wl,--out-implib=%t.shlib.lib instead.
In practice, running the lldb tests in mingw mode is not entirely supported, while they do pass cleanly in MSVC mode (and there's a buildbot testing this configuration) - but I would like to work towards making things work better in the mingw configuration too.
There's a different substitution, %build, which invokes the lldb/test/Shell/helpers/build.py script, which abstracts a bunch of boilerplate details mostly relevant for windows targets (like creating PDB debug info files); the easiest at this point would probably be to extend that script with options for creating import libraries.
For testing with mingw, I'm currently using this out of tree patch for that script too:
(This is a bit hacky, since it uses the build type of the python interpreter to switch the preference between clang-cl and clang.)
Alternatively, we could maybe add a separate substitution that expands into either -implib: or --out-implib=, but I'm not sure if we can guess which one will be the right one either, while the build.py helper script probably can do better.