LLDB thinks function call is ambiguous, but it's not

Viewed 36

I'm trying to debug a C++ program. I'm on macOS, using CLion IDE, clang compiler, LLDB. I stop the program at a breakpoint (marked with >>):

UnicodeString unicodeFromFile(const std::string &file) {
    std::ifstream input(file, std::ios::binary);
    cout << "open? " << input.is_open() << endl;
>>  std::vector<char> bytes(...);

I want to run po input.is_open(). LLDB reports:

(lldb) po input.is_open()
error: expression failed to parse:
error: <user expression 0>:1:7: call to member function 'is_open' is ambiguous
input.is_open()
~~~~~~^~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/fstream:1169:10: candidate function
    bool is_open() const;
         ^

But it's obviously not ambiguous, since the previous line of code calls it without problem. And it's error message is only listing one function.

Previous to this, I had a different problem where it couldn't find the symbol at all. But I used the advice here, which got me to this point. That answer says to add this to to ~/.lldbinit:

settings set target.import-std-module true

My previous errors looked like:

(lldb) po input.is_open()
error: expression failed to parse:
error: Couldn't lookup symbols:
  __ZNKSt3__114basic_ifstreamIcNS_11char_traitsIcEEE7is_openEv
1 Answers

When I try this trivial case with the most recent Xcode (or with TOT lldb) calling the is_open works:

 > cat tryifstream.cpp 
#include <fstream>

int
main() {
  std::ifstream my_str("/tmp/tryifstream.cpp");
  bool is_it = my_str.is_open();
  return is_it ? 0 : 20; 
}
> clang++ -g -O0 /tmp/tryifstream.cpp -o /tmp/a.out
> lldb /tmp/a.out
(lldb) b s -p return
Breakpoint 1: where = a.out`main + 88 at tryifstream.cpp:7:10, address = 0x000000010000379c
(lldb) run
Process 38065 launched: '/tmp/a.out' (arm64)
Process 38065 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x000000010000379c a.out`main at tryifstream.cpp:7
   4    main() {
   5      std::ifstream my_str("/tmp/tryifstream.cpp");
   6      bool is_it = my_str.is_open();
-> 7      return is_it ? 0 : 20; 
                 ^
   8    }
Target 0: (a.out) stopped.
(lldb) v is_it
(bool) is_it = true
(lldb) expr my_str.is_open()
(bool) $0 = true

If this simple case fails for you, you might try upgrading your Xcode. Getting the lldb expression parser to handle all of std templates is an area of ongoing work and newer lldb's are generally better at it.

If that simple case works for you, but your "real" case still doesn't, and you can make your project or some reduced reproducer available to us, please file a bug with the llvm.org bug reporter, including a copy of your reproducer. It will be hard to figure out what's going wrong in this case w/o being able to trace through the lookups.

Related