diff --git a/llvm/include/llvm/Support/JSON.h b/llvm/include/llvm/Support/JSON.h --- a/llvm/include/llvm/Support/JSON.h +++ b/llvm/include/llvm/Support/JSON.h @@ -818,7 +818,13 @@ void attributeBegin(llvm::StringRef Key); void attributeEnd(); - private: + /// Inform the writer that something external is about to write a value to OS. + void rawValueBegin(); + /// Inform the writer that something external has finished writing a value to + /// OS. + void rawValueEnd(); + +private: void attributeImpl(llvm::StringRef Key, Block Contents) { attributeBegin(Key); Contents(); @@ -832,6 +838,8 @@ Singleton, // Top level, or object attribute. Array, Object, + /// Something outside this object is writing to OS such as a 3rd-party API. + ExternalStreamWriter, }; struct State { Context Ctx = Singleton; diff --git a/llvm/lib/Support/JSON.cpp b/llvm/lib/Support/JSON.cpp --- a/llvm/lib/Support/JSON.cpp +++ b/llvm/lib/Support/JSON.cpp @@ -636,6 +636,18 @@ Stack.back().HasValue = true; } +void llvm::json::OStream::rawValueBegin() { + valueBegin(); + Stack.emplace_back(); + Stack.back().Ctx = ExternalStreamWriter; +} + +void llvm::json::OStream::rawValueEnd() { + assert(Stack.back().Ctx == ExternalStreamWriter && + "Expected matching rawValueBegin"); + Stack.pop_back(); +} + void llvm::json::OStream::newline() { if (IndentSize) { OS.write('\n');