<div dir="ltr">Hi Aleksei,<div><br></div><div>I had put break point on ASTContext Destructor but somehow, it was not hitting. </div><div><br></div><div>After that, I looked at the clang-query code and got a clue. [<b>Just Build AST</b>]</div><div><br></div><div><div>CommonOptionsParser OptionsParser(argc, argv, ToolingSampleCategory);</div><div>ClangTool tool(OptionsParser.getCompilations(), OptionsParser.getSourcePathList());</div><div><br></div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>std::vector<std::unique_ptr<ASTUnit>> ASTs;</div><div><b><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>if (tool.buildASTs(ASTs) == 0)</b></div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>{</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>cout << "successfully generated AST " << std::endl;</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>else</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>{</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>cout << "Failed to Generate AST " << std::endl;</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>return -1;</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>}</div></div><div><br></div><div>Thank you very much for giving suggestion.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 28, 2016 at 2:57 PM, Aleksei Sidorin <span dir="ltr"><<a href="mailto:a.sidorin@samsung.com" target="_blank">a.sidorin@samsung.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<div class="m_8799394728017370124moz-cite-prefix">Another suggestion is that ASTContext
that is used for running your tool dies before Tool.run() is
executed.<br>
<br>
In FrontendActionFactory::<wbr>runInvocation a CompilerInstance is
created. It has a smart pointer to the created ASTContext. But the
CompilerInstance is a local variable and it is destroyed within
all its data after invocation ends.<br>
You can try to set a breakpoint on the ASTContext destructor and
see the moment where it is destructed and the stack trace.<br>
<br>
<br>
<br>
27.12.2016 22:49, Apoorva Paneliya пишет:<br>
</div><div><div class="h5">
<blockquote type="cite">
<div dir="ltr">Hi Aleksei,
<div><br>
</div>
<div>I tried your below suggestion.</div>
<div><br>
</div>
<div><span style="font-size:12.8px">std::unique_ptr<</span><span style="font-size:12.8px">FrontendAction<wbr>Factory>
Factory =</span><br style="font-size:12.8px">
<span style="font-size:12.8px">
newFrontendActionFactory(&</span><span style="font-size:12.8px">func<wbr>Helper.MatchFinder);</span><br style="font-size:12.8px">
<span style="font-size:12.8px">Tool.run(Factory.get());</span><br>
</div>
<div><span style="font-size:12.8px"><br>
</span></div>
<div><span style="font-size:12.8px">unfortunately, result is
same (still, exception is generated).</span></div>
<div><span style="font-size:12.8px"><br>
</span></div>
<div><span style="font-size:12.8px">It is also a good idea to
check that your vector is non-empty before getting an
element. -> <b>Implemented.</b></span></div>
<div><span style="font-size:12.8px"><br>
</span></div>
<div><span style="font-size:12.8px">Let me know if you have any
other suggestion. </span></div>
<div><span style="font-size:12.8px"><br>
</span></div>
<div><span style="font-size:12.8px">thank you very much.!!</span></div>
<div><br>
</div>
<div><span style="font-size:12.8px"><br>
</span></div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Wed, Dec 28, 2016 at 12:06 AM,
Aleksei Sidorin <span dir="ltr"><<a href="mailto:a.sidorin@samsung.com" target="_blank">a.sidorin@samsung.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<div class="m_8799394728017370124m_-95797757030923449moz-cite-prefix">> <span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"></span>Tool.run(newFrontendActionFact<wbr>ory(&(funcHelper.MatchFinder))<wbr>.get());<br>
<br>
As I can guess, newFrontendActionFactory() returns
std::unique_ptr which is destructed immediately after
this statement is executed. So, it seems like you have a
memory use-after-free. Try this instead:<br>
<br>
std::unique_ptr<FrontendAction<wbr>Factory>
Factory =<br>
newFrontendActionFactory(&func<wbr>Helper.MatchFinder);<br>
Tool.run(Factory.get());<br>
<br>
Does it help?<br>
<br>
It is also a good idea to check that your vector is
non-empty before getting an element.<br>
<br>
<br>
<br>
27.12.2016 17:57, Apoorva Paneliya via cfe-dev пишет:<br>
</div>
<blockquote type="cite">
<div>
<div class="m_8799394728017370124h5">
<div dir="ltr">Hi there,
<div><br>
</div>
<div>Just now started with Clang. so, this might
silly question.</div>
<div><br>
</div>
<div>I am getting exception while calling <b>FunctionDecl->getNameA<wbr>sString()
or </b><b>FunctionDecl-></b><b>getName().str()</b><wbr>; </div>
<div><br>
</div>
<div>Exception Details:-</div>
<div><br>
</div>
<div>Type:- "Exception thrown: read access
violation."</div>
<div>
<div><br>
</div>
<div>Points to:- </div>
<div><br>
</div>
<div>File:- include\clang\AST\DeclarationN<wbr>ames.h</div>
<div><br>
</div>
<div><b>StoredNameKind getStoredNameKind() const
{</b></div>
<div><b> return
static_cast<StoredNameKind>(Pt<wbr>r
& PtrMask);</b></div>
<div><b> }</b></div>
</div>
<div><br>
</div>
<div>Actual Code </div>
<div><br>
</div>
<div><b>Main.cpp</b></div>
<div><br>
</div>
<div>static llvm::cl::OptionCategory
ToolingSampleCategory("Tooling Sample");<br>
</div>
<div>
<div><br>
</div>
<div>int main(int argc, const char **argv)</div>
<div>{</div>
<div><br>
</div>
<div>
<div> CommonOptionsParser
OptionsParser(argc, argv,
ToolingSampleCategory);</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>ClangTool
Tool(OptionsParser.getCompilat<wbr>ions(),</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>OptionsParser.getSourcePathLis<wbr>t());</div>
</div>
</div>
<div> <br>
</div>
<div> DeclarationMatcher funcMatcher =
functionDecl( ).bind("functionMatcher");</div>
<div><br>
</div>
<div>
<div> <b>FunctionHelper funcHelper;</b></div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>funcHelper.setNodeBindName("fu<wbr>nctionMatcher");</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>funcHelper.MatchFinder.addMatc<wbr>her(funcMatcher,
&funcHelper);</div>
<div><br>
</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>Tool.run(newFrontendActionFact<wbr>ory(&(funcHelper.MatchFinder))<wbr>.get());</div>
<div><br>
</div>
<div> <span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span></div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>llvm::SmallVector<const
clang::FunctionDecl*, 5U> allFuncDecl =
funcHelper.getAllFunctionDecl(<wbr>);</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>const
FunctionDecl* fnDecl = allFuncDecl[0];</div>
<div><b><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>cout
<< fnDecl->getName().str(); /* <font color="#ff0000">Exception Generated while
calling method.</font> If same method
called from run method then it works. */</b></div>
</div>
<div><b><br>
</b></div>
<div><b> </b>return 0;</div>
<div>}</div>
<div><br>
</div>
<div><br>
</div>
<div><b>FunctionalHelper.h</b></div>
<div><b><br>
</b></div>
<div><b><br>
</b></div>
<div>
<div> typedef struct
_FunctionInfo</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>{</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>std::string<span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>funcName;</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>std::string<span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>sourceFileName;</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>unsigned
int<span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>paramCount;</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>}FunctionInfo;</div>
<div><br>
</div>
<div><br>
</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span><b>class
FunctionHelper : public
clang::ast_matchers::MatchFind<wbr>er::MatchCallback</b>
{</div>
<div><br>
</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>std::vector<FunctionInfo><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>m_vec_FunctionInfo;</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>std::string<span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>m_str_NodeBindName;</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>llvm::SmallVector<const
clang::FunctionDecl*,5><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>m_vec_FunctionDecl;</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span></div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>private:</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>void
run(const clang::ast_matchers::MatchFind<wbr>er::MatchResult
&Result) override</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>{</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>const
clang::FunctionDecl* tempFuncDecl =
Result.Nodes.getNodeAs<clang::<wbr>FunctionDecl>(m_str_NodeBindNa<wbr>me.c_str());</div>
<div> </div>
<div><b> //
tempFuncDecl ->getName().str()<wbr>; <font color="#38761d">//from here, it's works.</font></b></div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span></div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>if
(tempFuncDecl)</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>{</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>FunctionInfo
tempFuncInfo;</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>fillFunctionInfo(tempFuncDecl,
tempFuncInfo);</div>
<div><br>
</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>m_vec_FunctionInfo.push_back(t<wbr>empFuncInfo);</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>m_vec_FunctionDecl.push_back(t<wbr>empFuncDecl);</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>}</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>}</div>
<div><br>
</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>public:</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>clang::ast_matchers::MatchFind<wbr>er<span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>MatchFinder;</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>void
setNodeBindName(std::string nodeBindName) {
m_str_NodeBindName = nodeBindName; };</div>
<div><br>
</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>std::vector<FunctionInfo>
getFuncInfoForAllMatchedNodes(<wbr>)</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>{<span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span></div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>return
m_vec_FunctionInfo;</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>}</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span></div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>llvm::SmallVector<const
clang::FunctionDecl*,5>
getAllFunctionDecl()</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>{</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>return
m_vec_FunctionDecl;</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>}</div>
<div><br>
</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>static
std::string getFunctionName(const
clang::FunctionDecl* funcDecl)</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>{</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>if
(!funcDecl) { return ""; }</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>return
funcDecl->getNameAsString();</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>}</div>
<div><br>
</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>static
std::string getSourceFileName(const
clang::FunctionDecl* funcDecl)</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>{</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>if
(!funcDecl) { return ""; }</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>return
funcDecl->getASTContext().getS<wbr>ourceManager().getFilename(<wbr>funcDecl->getLocation()).str()<wbr>;</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>}</div>
<div><br>
</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>static
unsigned int getParamsCount(const
clang::FunctionDecl* funcDecl)</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>{</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>if
(!funcDecl) { return 0; }</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>return
funcDecl->getNumParams();</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>}</div>
<div><br>
</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>static
void fillFunctionInfo(const
clang::FunctionDecl* funcDecl, FunctionInfo
&funcInfo)</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>{</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>if
(!funcDecl) { return; }</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>funcInfo.funcName
= FunctionHelper::getFunctionNam<wbr>e(funcDecl);</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>funcInfo.sourceFileName
= FunctionHelper::getSourceFileN<wbr>ame(funcDecl);</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>funcInfo.paramCount
= FunctionHelper::getParamsCount<wbr>(funcDecl);</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>}</div>
<div><span class="m_8799394728017370124m_-95797757030923449gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>};</div>
</div>
<div><br>
</div>
<div><br>
</div>
<div>Config:- 32bit, Debug</div>
<div>OS:- Windows</div>
<div>Compiled through VS2015.</div>
<div>Clang Version:- latest available. [ I guess
4.0]</div>
<div><br>
</div>
<div>So, Need help in resolving issue.</div>
<div>Thanks In Advance.!!</div>
<div><br>
</div>
<div>Regards,</div>
<div>Apoorva</div>
<div><br>
</div>
<div><br>
</div>
<div><br>
</div>
<div><br>
</div>
<div><br>
</div>
</div>
<br>
<fieldset class="m_8799394728017370124m_-95797757030923449mimeAttachmentHeader"></fieldset>
<br>
</div>
</div>
<pre>______________________________<wbr>_________________
cfe-dev mailing list
<a class="m_8799394728017370124m_-95797757030923449moz-txt-link-abbreviated" href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>
<a class="m_8799394728017370124m_-95797757030923449moz-txt-link-freetext" href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><span class="m_8799394728017370124HOEnZb"><font color="#888888">
</font></span></pre><span class="m_8799394728017370124HOEnZb"><font color="#888888">
</font></span></blockquote><span class="m_8799394728017370124HOEnZb"><font color="#888888">
<p>
</p>
<pre class="m_8799394728017370124m_-95797757030923449moz-signature" cols="72">--
Best regards,
Aleksei Sidorin
Software Engineer,
IMSWL-IMCG, SRR, Samsung Electronics
</pre>
</font></span></div>
</blockquote></div>
</div>
</blockquote>
<p>
</p><pre class="m_8799394728017370124moz-signature" cols="72">--
Best regards,
Aleksei Sidorin
Software Engineer,
IMSWL-IMCG, SRR, Samsung Electronics
</pre></div></div></div>
</blockquote></div><br></div>