[Lldb-commits] [PATCH] D27632: Add Formatv() versions of all our printf style formatting functions

Zachary Turner via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Fri Dec 9 21:16:56 PST 2016


zturner updated this revision to Diff 80992.
zturner added a comment.

Added a format provider for `FileSpec`.  Style syntax is documented in this patch.  Added some unit tests so you can see the output.

To answer Greg's earlier question about printing c-strings, `formatv("'{0}' '{1}' '{2}'", (const char *)nullptr, "", "test");` would print "'' '' 'test'";.  So yes that means that nullptr doesn't print "(null)".  It could be made to do so if desired, but if it's any consolation the goal is really to move away from c-strings, and StringRef doesn't have any concept of null.  There's just empty and not empty.

One nice thing about is that you can easily change the behavior of existing formatters using a mechanism which I've called "adapters".  You can see an example of a builtin adapter in this patch, where I use `fmt_repeat` to repeat a character 7 times.  To write an adapter for const char * that prints (null), you could do this:

  struct fmt_or_null {
    explicit fmt_or_null(const char *s) : s(s) {}
    void format(llvm::raw_ostream &Stream, StringRef Style) const {
      if (!s)
        Stream << "(null)";  // Override the default behavior for nullptr;
      else
        llvm::format_provider<const char *>::format(s, Stream, Style);  // Otherwise just use the default;
    }
    const char *s;
  };
  
  void foo() {
    const char *s = nullptr;
    std::string result = llvm::formatv("{0}", fmt_or_null(s));
  }


https://reviews.llvm.org/D27632

Files:
  include/lldb/Core/Error.h
  include/lldb/Core/Log.h
  include/lldb/Core/ModuleSpec.h
  include/lldb/Core/Stream.h
  include/lldb/Host/FileSpec.h
  include/lldb/Interpreter/CommandReturnObject.h
  source/Breakpoint/BreakpointOptions.cpp
  source/Commands/CommandObjectApropos.cpp
  source/Core/Log.cpp
  source/Host/common/FileSpec.cpp
  source/Symbol/ClangASTContext.cpp
  source/Target/Target.cpp
  unittests/Host/FileSpecTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27632.80992.patch
Type: text/x-patch
Size: 18080 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20161210/1b016c03/attachment-0001.bin>


More information about the lldb-commits mailing list