[cfe-dev] Rewriter::InsertText

Bruce Stephens bruce.r.stephens at gmail.com
Thu Aug 5 03:13:39 PDT 2010


Сергей Филиппов <rolenof-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org>
writes:

[...]

> void RewritingPass::Initialize(ASTContext &context)
> {
>     this->context = &context;
>     SourceLocation L;
>     mainFileId = context.getSourceManager().getMainFileID();
>     rewriter = new Rewriter(context.getSourceManager(), context.getLangOptions());
>     const llvm::StringRef preamble(getPreamble());
>     if (preamble.size() > 0)
>         rewriter->InsertText(context.getSourceManager().getLocForStartOfFile(mainFileId), preamble, true);
> }
>
> const std::string CastRewritePass::getPreamble() const
> {   
>     return " /* cast rewrite pass */\n\n";
> }
>
> after execution of RewritingPass::Initialize preamble changed it's not /* cast rewrite pass*/, but 0\230\bast rewrite pass*/.
> I found a solvation - use not string in getPreamble, but StringRef. If i do so, it works ok.

Does it also work if you give the result of getPreamble() a name?
Something like this:

   const std::string& temp(getPreamble());
   const llvm::StringRef preamble(temp);

If so, then obviously I'd suspect an issue with temporaries.  (I must
admit intuitively it looks like there shouldn't be an issue with that
code, but my intuition is probably not a good guide to C++.)




More information about the cfe-dev mailing list