<div dir="ltr"><div>Generally, the clang approach is to not change and save the AST, but change the source code based on AST info.</div><div>A good high level explanation is here:</div><a href="https://eli.thegreenplace.net/2014/07/29/ast-matchers-and-clang-refactoring-tools">https://eli.thegreenplace.net/2014/07/29/ast-matchers-and-clang-refactoring-tools</a><br><div><br></div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, May 24, 2018 at 4:25 AM 罗 金才 via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">




<div dir="ltr">
<div id="m_4508374698286542241divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif" dir="ltr">
<p style="margin-top:0;margin-bottom:0"></p>
<div style="font-family:微软雅黑,Tahoma;font-size:14px;font-variant-numeric:normal;font-variant-east-asian:normal;line-height:21px">
Hello Develpers,</div>
<div style="font-family:微软雅黑,Tahoma;font-size:14px;font-variant-numeric:normal;font-variant-east-asian:normal;line-height:21px">
     I want to add a filedDec to <span style="font-size:10.5pt;line-height:1.5;background-color:window">CXXRecordDecl  and save it . my code as foloow , but how to save it?</span></div>
<div style="font-family:微软雅黑,Tahoma;font-size:14px;font-variant-numeric:normal;font-variant-east-asian:normal;line-height:21px">
<span style="background-color:rgba(0,0,0,0)">bool </span><span style="color:inherit!important;background-color:rgba(0,0,0,0);font-size:10.5pt;line-height:1.5">MyASTVisitor::</span><span style="color:inherit!important;background-color:rgba(0,0,0,0);font-size:10.5pt;line-height:1.5">VisitCXXRecordDecl(CXXRecordDecl *Declaration) {</span></div>
<div style="font-family:微软雅黑,Tahoma;font-size:14px;font-variant-numeric:normal;font-variant-east-asian:normal;line-height:21px">
<span style="background-color:rgba(0,0,0,0)"><span style="white-space:pre-wrap"></span><span style="white-space:pre-wrap"></span>DeclContext *declContext = Declaration->getDeclContext();<br>
<span style="white-space:pre-wrap"></span><span style="white-space:pre-wrap"></span>SourceManager &sourceManager_ = TheRewriter.getSourceMgr();<br>
<br>
<span style="white-space:pre-wrap"></span><span style="white-space:pre-wrap"></span>clang::ASTContext& astContext = Declaration->getASTContext();<br>
<br>
<span style="white-space:pre-wrap"></span><span style="white-space:pre-wrap"></span>SourceLocation startLoc = Declaration->getLocStart();<br>
<span style="white-space:pre-wrap"></span><span style="white-space:pre-wrap"></span>const char* startBuf = sourceManager_.getCharacterData(startLoc);<br>
<span style="white-space:pre-wrap"></span><span style="white-space:pre-wrap"></span>const char* classStartBuf = strchr(startBuf, '{')+1;<br>
<span style="white-space:pre-wrap"></span><span style="white-space:pre-wrap"></span>SourceLocation insertStartLoc = Declaration->getLocStart().getLocWithOffset(std::distance(startBuf, classStartBuf));<br>
<br>
<span style="white-space:pre-wrap"></span><span style="white-space:pre-wrap"></span>llvm::APInt apInt(32, 42);<br>
<span style="white-space:pre-wrap"></span><span style="white-space:pre-wrap"></span>clang::IntegerLiteral* intergerLiteral = clang::IntegerLiteral::Create(astContext, apInt, astContext.IntTy, insertStartLoc);<br>
<br>
<span style="white-space:pre-wrap"></span><span style="white-space:pre-wrap"></span>clang::TypeSourceInfo* typeSourceInfo = astContext.CreateTypeSourceInfo(astContext.IntTy);<br>
<br>
<span style="white-space:pre-wrap"></span><span style="white-space:pre-wrap"></span>clang::IdentifierInfo& id = astContext.Idents.get("test_");<br>
<span style="white-space:pre-wrap"></span><span style="white-space:pre-wrap"></span>clang::FieldDecl* fieldDecl = clang::FieldDecl::Create(astContext, Declaration, insertStartLoc, insertStartLoc, &id, astContext.IntTy, typeSourceInfo, intergerLiteral, false, clang::InClassInitStyle::ICIS_NoInit);<br>
<br>
<span style="white-space:pre-wrap"></span><span style="white-space:pre-wrap"></span>fieldDecl->setAccess(clang::AccessSpecifier::AS_private);<br>
<br>
<span style="white-space:pre-wrap"></span><span style="white-space:pre-wrap"></span>Declaration->addDecl(fieldDecl);<br>
<br>
<span style="white-space:pre-wrap"></span><span style="white-space:pre-wrap"></span>return true;<br>
<span style="white-space:pre-wrap"></span>}</span></div>
<div style="font-family:微软雅黑,Tahoma;font-size:14px;font-variant-numeric:normal;font-variant-east-asian:normal;line-height:21px">
<span style="background-color:rgba(0,0,0,0)"><br>
</span></div>
<div style="font-family:微软雅黑,Tahoma;font-size:14px;font-variant-numeric:normal;font-variant-east-asian:normal;line-height:21px">
<div style="line-height:normal;font-family:Calibri,Helvetica,sans-serif;font-size:12pt">
Yours truly<br>
</div>
<div style="line-height:normal;font-family:Calibri,Helvetica,sans-serif;font-size:12pt">
<br>
</div>
<div style="line-height:normal;font-family:Calibri,Helvetica,sans-serif;font-size:12pt">
Robert</div>
</div>
<br>
<p></p>
</div>
</div>

_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div>