`stripComments(cook(...))` is a common pattern being written.
Without this patch, this has a use-after-free issue (cook returns a temporary
TokenStream object which has its own payload, but the payload is not
shared with the one returned by stripComments).
Reviewed By: sammccall
Differential Revision: https://reviews.llvm.org/D125311
return Storage[1];
}
+ /// Returns the shared payload.
+ std::shared_ptr<void> getPayload() const { return Payload; }
+ /// Adds the given payload to the stream.
+ void addPayload(std::shared_ptr<void> P) {
+ if (!Payload)
+ Payload = std::move(P);
+ else
+ Payload = std::make_shared<
+ std::pair<std::shared_ptr<void>, std::shared_ptr<void>>>(
+ std::move(P), std::move(Payload));
+ }
+
/// Print the tokens in this stream to the output stream.
///
/// The presence of newlines/spaces is preserved, but not the quantity.
auto CleanedStorage = std::make_shared<llvm::BumpPtrAllocator>();
clang::IdentifierTable Identifiers(LangOpts);
TokenStream Result(CleanedStorage);
-
+ Result.addPayload(Code.getPayload());
for (auto Tok : Code.tokens()) {
if (Tok.flag(LexFlags::NeedsCleaning)) {
// Remove escaped newlines and trigraphs.
}
TokenStream stripComments(const TokenStream &Input) {
- TokenStream Out;
+ TokenStream Out(Input.getPayload());
for (const Token &T : Input.tokens()) {
if (T.Kind == tok::comment)
continue;