<html>
<head>
</head>
<body class='hmmessage'><div dir='ltr'>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style>
<div dir="ltr">Thank you very much. Bad news, Mikhail. I tried your fix and it doesn't work by the moment. The problem is in this line:<br><br><blockquote>PresumedLoc PLoc = sm.getPresumedLoc(SpellingLoc)<br></blockquote>(note: the compiler gave me an error when I put PresumedLoc& PLoc, so I just delete &). <br><br>Just to discard that other things could be causing the problem, I have attempted the following:<br><br>* I don't call to HandleTranslationUnit before doing this, so the AST is not traversed twice. <br><br>* Instead of taking a MemberExpr, I have tried with a Stmt and a CXXMethodDecl. The same. However, taking the CXXRecordDecl that nested the statement and the method does worked. <br><br>* I have tried to assign the ASTContext as in the ASTDumper:<br><br><a class="code" href="http://clang.llvm.org/doxygen/classclang_1_1ASTContext.html">ASTContext</a> &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();<br>This was even worse. Failed directly in:<br><blockquote>SourceLocation SpellingLoc = sm.getSpellingLoc(FS->getSourceRange().getBegin());<br></blockquote><br>Anyway, I'm using ASTMatchers, so I update the ASTContext in each match:<br><br>void System::run(const MatchFinder::MatchResult &Result) {<br> Context = Result.Context; (Context is a field of the class System declared as ASTContext *Context;)<br> ...<br>}<br><br>* I have also tried to check that the object "SpellingLoc" wasn't invalid:<br><br>if(SpellingLoc.isValid()){<br> PresumedLoc PLoc = sm.getPresumedLoc(SpellingLoc);<br>}<br><br>The SpellingLoc object , however, is valid.<br><br>Sincerely, I don't know what else to try. This is the code as I have it right now:<br><br>void System::run(const MatchFinder::MatchResult &Result) {<br> Context = Result.Context; <br><br> const Stmt *FS = 0;<br> if( FS = Result.Nodes.getNodeAs<clang::Stmt>("statement")){<br><br> ...<br><br> SourceManager& sm = Context->getSourceManager();<br> SourceLocation SpellingLoc = sm.getSpellingLoc(FS->getSourceRange().getBegin());<br><br> if(SpellingLoc.isValid()){<br> PresumedLoc PLoc = sm.getPresumedLoc(SpellingLoc);<br><br> if (!PLoc.isInvalid()) {<br> std::pair<unsigned, const char *> location = <br> std::pair<unsigned, const char *>(PLoc.getLine(), PLoc.getFilename());<br> <br> getCoverage(location); <br> }<br> }<br> }<br>}<br><br>Perhaps something in the code of the program that I'm using to check this impedes the correct generation of the AST...<br><br><br><div><hr id="stopSpelling">From: mikhail.ramalho@gmail.com<br>Date: Tue, 1 Mar 2016 17:45:15 +0000<br>Subject: Re: [cfe-dev] FullSourceLoc, getSpellingLineNumber segmentation fault<br>To: pedretti_86@hotmail.com<br>CC: cfe-dev@lists.llvm.org<br><br><div dir="ltr">Hello,<div><br></div><div>I finally found the two commits the "fixed" the crashes, the first one might help you:</div><div><br></div><div>1. Use PresumedLoc instead of FullSourceLoc</div><div><br></div><div>Basically, it's the code used in <a href="http://clang.llvm.org/doxygen/ASTDumper_8cpp_source.html#l00571" target="_blank">ASTDumper</a>. </div><div><br></div><div>I have something like:</div><div><br></div><div> sm = &ASTContext->getSourceManager();<br></div><div><div> if(!sm)</div><div> return;</div></div><div><div><br></div><div> clang::SourceLocation SpellingLoc = sm->getSpellingLoc(decl.getSourceRange().getBegin());</div><div> clang::PresumedLoc &PLoc = sm->getPresumedLoc(SpellingLoc);</div></div><div><br></div><div> if (PLoc.isInvalid()) {</div><div> location.set_file("<invalid sloc>");</div><div> return;</div><div> }</div><div><br></div><div> location.set_line(PLoc.getLine());</div><div> location.set_file(PLoc.getFilename()); <br></div><div><br></div><div>where:</div><div><br></div><div>sm is a clang::SourceManager *sm;</div><div>ASTContext is a clang::ASTContext *ASTContext; </div><div>location is an internal class that holds location information.</div><div><br></div><div>One minor note is to always update your ASTContext (in your case, the object Context), since it changes from translation unit. <br></div><div><br></div><div>~</div><div><br></div><div>2. I was using virtual files, whose content was mapped into a local string, something like:</div><div><br></div><div>for(auto file : files)</div><div>{</div><div> std::string intrinsics = get_instrinsics();</div><div><br></div><div><div> clang::tooling::ClangTool Tool(Compilations, sources);</div><div> Tool.mapVirtualFile("/esbmc_intrinsics.h", intrinsics);</div></div><div><br></div><div> Tool.buildASTs(ASTs);<br></div><div>}</div><div><br></div><div>So I changed std::string to be a member of the class that calls this code.</div><div><br></div><div>~</div><div><br></div><div>Since you're not using virtual files, the second one shouldn't help, but try the first one and see if it fixes the problem.</div><div><br></div><div>I hope it helps.</div><div><br></div><div>Thank you,</div><div><br></div></div><div class="ecxgmail_extra"><br><div class="ecxgmail_quote">2016-03-01 14:46 GMT+00:00 victor <span dir="ltr"><<a href="mailto:pedretti_86@hotmail.com" target="_blank">pedretti_86@hotmail.com</a>></span>:<br><blockquote class="ecxgmail_quote" style="border-left:1px #ccc solid;padding-left:1ex;">
<div><div dir="ltr">Oh sorry! Forget about the previous message. It is not working (I had commented the line where getSpellingLineNumber was called). I definitely don't know how to sort out this issue. The problem is that the tool crashes when reaching that point: is there something I can do so that, at least, the tool doesn't crash? <br><br>Thanks and sorry again.<br><br><div><span><hr>From: <a href="mailto:mikhail.ramalho@gmail.com" target="_blank">mikhail.ramalho@gmail.com</a><br>Date: Mon, 29 Feb 2016 19:16:28 +0000<br>Subject: Re: [cfe-dev] FullSourceLoc, getSpellingLineNumber segmentation fault<br>To: <a href="mailto:pedretti_86@hotmail.com" target="_blank">pedretti_86@hotmail.com</a><br>CC: <a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br><br></span><div><div class="h5"><div dir="ltr">Hi,<div><br></div><div>I used to face a lot of crashes on this method, it would usually explode at some #__SSE2__ code in a static method called ComputeLineNumbers inside clang.</div><div><br></div><div>Are you using virtual files? Are you generating the AST more than once (I mean, calling Tool.BuildAST() more than once)?</div><div><br></div><div>I don't quite remember how I fixed, but I _think_ those two might have something to do with the problem.</div><div><br></div><div>Cheers,</div><div><br></div></div><div><br><div>2016-02-29 18:22 GMT+00:00 victor via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span>:<br><blockquote style="border-left:1px #ccc solid;padding-left:1ex;">
<div><div dir="ltr">Hi,<br><br>Does somebody know what can make the method getSpellingLineNumber from FullSourceLoc to generate a segmentation fault? <font style="font-size:12pt;" size="3">I'm using clang 3.6.</font><font style="font-size:12pt;" size="3"></font><br><br><a href="http://clang.llvm.org/doxygen/classclang_1_1FullSourceLoc.html#a52927fcf23cf662c209e5e996bf59917" target="_blank">http://clang.llvm.org/doxygen/classclang_1_1FullSourceLoc.html#a52927fcf23cf662c209e5e996bf59917</a><br><br>Before using this method, I have checked that the FullSourceLoc is valid. This is the code:<br><br> if(loc.isValid()){
<br> <br> if(!(loc.getFileID().isInvalid())){<br> <br> if(Context->getSourceManager().getFileEntryForID(loc.getFileID()) != NULL){<br><br> unsigned int = loc.getSpellingLineNumber();<br> }<br> }<br> }<br><br>I can't find anything special in the code which I'm using to check if my program works:<br><br> void DeleteNode( XMLNode* node ) {<br> node->_parent->DeleteChild( node );<br> }<br><br>And this is the tree:<br><br><font style="font-size:10pt;" size="2">| | |-CXXMethodDecl 0x26b4090 <line:1576:5, line:1578:5> line:1576:10 DeleteNode 'void (class tinyxml2::XMLNode *)'</font><font style="font-size:10pt;" size="2"><br></font><font style="font-size:10pt;" size="2">| | | |-ParmVarDecl 0x26b4010 <col:22, col:31> col:31 used node 'class tinyxml2::XMLNode *'</font><font style="font-size:10pt;" size="2"><br></font><font style="font-size:10pt;" size="2">| | | |-CompoundStmt 0x26c9500 <col:38, line:1578:5></font><font style="font-size:10pt;" size="2"><br></font><font style="font-size:10pt;" size="2">| | | | `-CXXMemberCallExpr 0x26c94b8 <line:1577:9, col:42> 'void'</font><font style="font-size:10pt;" size="2"><br></font><font style="font-size:10pt;" size="2">| | | | |-MemberExpr 0x26c9460 <col:9, col:24> '<bound member function type>' ->DeleteChild 0x2699420</font><font style="font-size:10pt;" size="2"><br></font><font style="font-size:10pt;" size="2">| | | | | `-ImplicitCastExpr 0x26c9448 <col:9, col:15> 'class tinyxml2::XMLNode *' <LValueToRValue></font><font style="font-size:10pt;" size="2"><br></font><font style="font-size:10pt;" size="2">| | | | | `-MemberExpr 0x26c9418 <col:9, col:15> 'class tinyxml2::XMLNode *' lvalue ->_parent 0x269a310</font><font style="font-size:10pt;" size="2"><br></font><font style="font-size:10pt;" size="2">| | | | | `-ImplicitCastExpr 0x26c9400 <col:9> 'class tinyxml2::XMLNode *' <LValueToRValue></font><font style="font-size:10pt;" size="2"><br></font><font style="font-size:10pt;" size="2">| | | | | `-DeclRefExpr 0x26c93d8 <col:9> 'class tinyxml2::XMLNode *' lvalue ParmVar 0x26b4010 'node' 'class tinyxml2::XMLNode *'</font><font style="font-size:10pt;" size="2"><br></font><font style="font-size:12pt;" size="3"><br></font><font style="font-size:12pt;" size="3">The object "loc" contains the location of the MemberExpr "_parent". </font><br><font style="font-size:12pt;" size="3"></font><br>Thanks.<br> </div></div>
<br>_______________________________________________<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>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div><div dir="ltr"><div><br></div><div>Mikhail Ramalho.</div></div></div>
</div></div></div></div> </div></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="ecxgmail_signature"><div dir="ltr"><div><br></div><div>Mikhail Ramalho.void Mutation::run(const MatchFinder::MatchResult &Result) {void Mutation::run(const MatchFinder::MatchResult &Result) {void Mutation::run(const MatchFinder::MatchResult &Result) {</div></div></div>
</div></div></div>
</div></body>
</html>