[cfe-dev] Modification of a StringLiteral (SemaConsumer - RecursiveASTVisitor - TreeTransform)

Timothée Vandeput timothee.vandeput at gmail.com
Fri May 30 10:52:49 PDT 2014


Hello Richard,

 *The AST is neither designed nor intended to support in-place modification
> -- it's supposed to be essentially immutable once created. TreeTransform is
> a better approach (since it creates new AST rather than updating existing
> AST nodes in-place).*
>

This is what I have been reading here on previous messages, hence our
attempt to use TreeTransform.


> *In your overridden TransformStringLiteral, how are you creating the new
> StringLiteral? We don't seem to have any relevant functions on Sema for
> this, so I think you'd need to compute the relevant type yourself and call
> StringLiteral::Create.*
>

This is what we tried, here is the code snippet of our TreeTransform class :

*class HmIdTransform : public TreeTransform<HmIdTransform>*
*{*
* Sema& m_Sema;*
* ASTContext& m_Context;*
* std::string m_NewString;*

*public:*
* HmIdTransform(Sema& semaRef, std::string newString)*
* : TreeTransform(semaRef)*
* , m_Sema(semaRef)*
* , m_Context(semaRef.getASTContext())*
* , m_NewString(newString)*
* {*

* }*

* bool AlwaysRebuild() { return true; }*

* ExprResult TransformStringLiteral(StringLiteral* strLiteral)*
* {*
* QualType StrType = m_Context.getConstantArrayType(m_Context.CharTy,
llvm::APInt(32, m_NewString.size() + 1), ArrayType::Normal, 0);*

* return m_Sema.Owned(StringLiteral::Create(m_Context, m_NewString,
StringLiteral::Ascii, false, StrType, strLiteral->getLocStart()));*
* }*
*};*

Here is an extract of our RecursiveASTVisitor::TraverseFieldDecl method :

*bool TraverseFieldDecl(FieldDecl* decl)*
*{*
* if(decl->hasInClassInitializer())*
* {*
* Expr* initializer = decl->getInClassInitializer();*

* if(initializer && initializer->children())*
* {*
* HmIdTransform initTransform(m_CI.getSema(), "ModifiedString");*

* initTransform.TransformInitializer(initializer, true);*
* }*
* }*

* return true;*
*}*

Now, it seems the node doesn't get rebuild, do we have to invoke a method
to rebuild the modified node?
I though the TreeTransform would do it (knowing that it has a reference to
the Sema) but I guess I am mistaken.

Thanks for your help !
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140530/500d7902/attachment.html>


More information about the cfe-dev mailing list