<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">> <span class="gmail-Apple-tab-span" style="white-space:pre"></span>Tool.run(newFrontendActionFactory(&(funcHelper.MatchFinder)).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<FrontendActionFactory> Factory =<br>
newFrontendActionFactory(&funcHelper.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
cite="mid:CAABjOmx0ffPrJbgdsnJ7KztYHCGgvHnMSDed6M1_fwO05SrKag@mail.gmail.com"
type="cite">
<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->getNameAsString()
or </b><b>FunctionDecl-></b><b>getName().str()</b>; </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\DeclarationNames.h</div>
<div><br>
</div>
<div><b>StoredNameKind getStoredNameKind() const {</b></div>
<div><b> return static_cast<StoredNameKind>(Ptr &
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="gmail-Apple-tab-span" style="white-space:pre"> </span>ClangTool
Tool(OptionsParser.getCompilations(),</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>OptionsParser.getSourcePathList());</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="gmail-Apple-tab-span" style="white-space:pre"> </span>funcHelper.setNodeBindName("functionMatcher");</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>funcHelper.MatchFinder.addMatcher(funcMatcher,
&funcHelper);</div>
<div><br>
</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>Tool.run(newFrontendActionFactory(&(funcHelper.MatchFinder)).get());</div>
<div><br>
</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>llvm::SmallVector<const
clang::FunctionDecl*, 5U> allFuncDecl =
funcHelper.getAllFunctionDecl();</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>const
FunctionDecl* fnDecl = allFuncDecl[0];</div>
<div><b><span class="gmail-Apple-tab-span" style="white-space:pre"> </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="gmail-Apple-tab-span" style="white-space:pre"> </span>{</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>std::string<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>funcName;</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>std::string<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>sourceFileName;</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>unsigned
int<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>paramCount;</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>}FunctionInfo;</div>
<div><br>
</div>
<div><br>
</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span><b>class
FunctionHelper : public
clang::ast_matchers::MatchFinder::MatchCallback</b> {</div>
<div><br>
</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>std::vector<FunctionInfo><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>m_vec_FunctionInfo;</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>std::string<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>m_str_NodeBindName;</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>llvm::SmallVector<const
clang::FunctionDecl*,5><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>m_vec_FunctionDecl;</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>private:</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>void
run(const clang::ast_matchers::MatchFinder::MatchResult
&Result) override</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>const
clang::FunctionDecl* tempFuncDecl =
Result.Nodes.getNodeAs<clang::FunctionDecl>(m_str_NodeBindName.c_str());</div>
<div> </div>
<div><b> //
tempFuncDecl ->getName().str(); <font color="#38761d">//from
here, it's works.</font></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>if
(tempFuncDecl)</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>FunctionInfo
tempFuncInfo;</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>fillFunctionInfo(tempFuncDecl,
tempFuncInfo);</div>
<div><br>
</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>m_vec_FunctionInfo.push_back(tempFuncInfo);</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>m_vec_FunctionDecl.push_back(tempFuncDecl);</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>}</div>
<div><br>
</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>public:</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>clang::ast_matchers::MatchFinder<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>MatchFinder;</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>void
setNodeBindName(std::string nodeBindName) {
m_str_NodeBindName = nodeBindName; };</div>
<div><br>
</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>std::vector<FunctionInfo>
getFuncInfoForAllMatchedNodes()</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>{<span class="gmail-Apple-tab-span" style="white-space:pre"> </span></div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>return
m_vec_FunctionInfo;</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></div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>llvm::SmallVector<const
clang::FunctionDecl*,5> getAllFunctionDecl()</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>return
m_vec_FunctionDecl;</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>}</div>
<div><br>
</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>static
std::string getFunctionName(const clang::FunctionDecl*
funcDecl)</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>if
(!funcDecl) { return ""; }</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>return
funcDecl->getNameAsString();</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>}</div>
<div><br>
</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>static
std::string getSourceFileName(const clang::FunctionDecl*
funcDecl)</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>if
(!funcDecl) { return ""; }</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>return
funcDecl->getASTContext().getSourceManager().getFilename(funcDecl->getLocation()).str();</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>}</div>
<div><br>
</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>static
unsigned int getParamsCount(const clang::FunctionDecl*
funcDecl)</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>if
(!funcDecl) { return 0; }</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>return
funcDecl->getNumParams();</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>}</div>
<div><br>
</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>static
void fillFunctionInfo(const clang::FunctionDecl* funcDecl,
FunctionInfo &funcInfo)</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>if
(!funcDecl) { return; }</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>funcInfo.funcName
= FunctionHelper::getFunctionName(funcDecl);</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>funcInfo.sourceFileName
= FunctionHelper::getSourceFileName(funcDecl);</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>funcInfo.paramCount
= FunctionHelper::getParamsCount(funcDecl);</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>};</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="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
cfe-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>
<a class="moz-txt-link-freetext" href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a>
</pre>
</blockquote>
<br>
<p><br>
</p>
<pre class="moz-signature" cols="72">--
Best regards,
Aleksei Sidorin
Software Engineer,
IMSWL-IMCG, SRR, Samsung Electronics
</pre>
</body>
</html>