Writing tests with `clang::tooling::runToolOnCode`, output to `std::string`?

Can I skip the whole file I/O approach and have something like:

void MyMatcher::onEndOfTranslationUnit() {
  // Replace in place
  // TCRewriter.overwriteChangedFiles();

#ifdef PROJECT_NAME_DEBUG
  TCRewriter.getEditBuffer(TCRewriter.getSourceMgr().getMainFileID())
      .write(outs);
#endif /* PROJECT_NAME_DEBUG */
}

I’ve tried hacking together:

#ifdef PROJECT_NAME_DEBUG
#ifdef PROJECT_NAME_TEST
static std::string output_str("\0", 1716);
llvm::raw_string_ostream outs(output_str);
#else
using llvm::outs;
#endif /* PROJECT_NAME_TEST */
#endif /* PROJECT_NAME_DEBUG */

But I just this garbage in my output_str:

""N5clang12ast_matchers8internal25matcher_isBitFieldMatcherEN5clang12ast_matchers8internal16MatcherInterfaceINS_9FieldDeclEEEN5clang12ast_matchers8internal19DynMatcherInterfaceE

Also this idea doesn’t really work as multiple tests would be added to the one output_str

Rather than reading a file into a string and comparing that against another string, it would be nice to write tests like:

EXPECT_EQ(clang::tooling::runToolOnCodeRes(
    std::make_unique<MyPluginAction>(),
    "int f(int a){} f(6);",
),
    "int f(int a){} f(/*a=*/6);
);

Possibly there’s an alternative to clang::tooling::runToolOnCode that will work for this use-case?

Thanks for suggestions