[cfe-dev] FullSourceLoc, getSpellingLineNumber segmentation fault

Mikhail Ramalho via cfe-dev cfe-dev at lists.llvm.org
Tue Mar 1 09:45:15 PST 2016


Hello,

I finally found the two commits the "fixed"  the crashes, the first one
might help you:

1. Use PresumedLoc instead of FullSourceLoc

Basically, it's the code used in ASTDumper
<http://clang.llvm.org/doxygen/ASTDumper_8cpp_source.html#l00571>.

I have something like:

  sm = &ASTContext->getSourceManager();
  if(!sm)
    return;

  clang::SourceLocation SpellingLoc =
sm->getSpellingLoc(decl.getSourceRange().getBegin());
  clang::PresumedLoc &PLoc = sm->getPresumedLoc(SpellingLoc);

  if (PLoc.isInvalid()) {
    location.set_file("<invalid sloc>");
    return;
  }

  location.set_line(PLoc.getLine());
  location.set_file(PLoc.getFilename());

where:

sm is a clang::SourceManager *sm;
ASTContext is a clang::ASTContext *ASTContext;
location is an internal class that holds location information.

One minor note is to always update your ASTContext (in your case, the
object Context), since it changes from translation unit.

~

2. I was using virtual files, whose content was mapped into a local string,
something like:

for(auto file : files)
{
  std::string intrinsics = get_instrinsics();

  clang::tooling::ClangTool Tool(Compilations, sources);
  Tool.mapVirtualFile("/esbmc_intrinsics.h", intrinsics);

  Tool.buildASTs(ASTs);
}

So I changed std::string to be a member of the class that calls this code.

~

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.

I hope it helps.

Thank you,


2016-03-01 14:46 GMT+00:00 victor <pedretti_86 at hotmail.com>:

> 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?
>
> Thanks and sorry again.
>
> ------------------------------
> From: mikhail.ramalho at gmail.com
> Date: Mon, 29 Feb 2016 19:16:28 +0000
> Subject: Re: [cfe-dev] FullSourceLoc, getSpellingLineNumber segmentation
> fault
> To: pedretti_86 at hotmail.com
> CC: cfe-dev at lists.llvm.org
>
> Hi,
>
> 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.
>
> Are you using virtual files? Are you generating the AST more than once (I
> mean, calling Tool.BuildAST() more than once)?
>
> I don't quite remember how I fixed, but I _think_ those two might have
> something to do with the problem.
>
> Cheers,
>
>
> 2016-02-29 18:22 GMT+00:00 victor via cfe-dev <cfe-dev at lists.llvm.org>:
>
> Hi,
>
> Does somebody know what can make the method getSpellingLineNumber from
> FullSourceLoc to generate a segmentation fault? I'm using clang 3.6.
>
>
> http://clang.llvm.org/doxygen/classclang_1_1FullSourceLoc.html#a52927fcf23cf662c209e5e996bf59917
>
> Before using this method, I have checked that the FullSourceLoc is valid.
> This is the code:
>
>  if(loc.isValid()){
>
>          if(!(loc.getFileID().isInvalid())){
>
>
> if(Context->getSourceManager().getFileEntryForID(loc.getFileID()) != NULL){
>
>                    unsigned int = loc.getSpellingLineNumber();
>             }
>          }
>   }
>
> I can't find anything special in the code which I'm using to check if my
> program works:
>
>     void DeleteNode( XMLNode* node )    {
>         node->_parent->DeleteChild( node );
>     }
>
> And this is the tree:
>
> | | |-CXXMethodDecl 0x26b4090 <line:1576:5, line:1578:5> line:1576:10
> DeleteNode 'void (class tinyxml2::XMLNode *)'
> | | | |-ParmVarDecl 0x26b4010 <col:22, col:31> col:31 used node 'class
> tinyxml2::XMLNode *'
> | | | |-CompoundStmt 0x26c9500 <col:38, line:1578:5>
> | | | | `-CXXMemberCallExpr 0x26c94b8 <line:1577:9, col:42> 'void'
> | | | |   |-MemberExpr 0x26c9460 <col:9, col:24> '<bound member function
> type>' ->DeleteChild 0x2699420
> | | | |   | `-ImplicitCastExpr 0x26c9448 <col:9, col:15> 'class
> tinyxml2::XMLNode *' <LValueToRValue>
> | | | |   |   `-MemberExpr 0x26c9418 <col:9, col:15> 'class
> tinyxml2::XMLNode *' lvalue ->_parent 0x269a310
> | | | |   |     `-ImplicitCastExpr 0x26c9400 <col:9> 'class
> tinyxml2::XMLNode *' <LValueToRValue>
> | | | |   |       `-DeclRefExpr 0x26c93d8 <col:9> 'class tinyxml2::XMLNode
> *' lvalue ParmVar 0x26b4010 'node' 'class tinyxml2::XMLNode *'
>
> The object "loc" contains the location of the MemberExpr "_parent".
>
> Thanks.
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
>
>
>
> --
>
> Mikhail Ramalho.
>



-- 

Mikhail Ramalho.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160301/45b5b505/attachment-0001.html>


More information about the cfe-dev mailing list