[cfe-dev] Segmentation fault when accessing a token

Daniel QuiƱones Lopez kaos09 at hotmail.com
Wed Jan 29 04:21:09 PST 2014


Hello,

I'm trying to acces to the SourceLocation of a token (a comma, specifically) in a declaration of this type:

class A{
public:
    int a, b;
};

For this, I'm using this function:

SourceLocation findTokenAfterLocation(SourceLocation loc, ASTContext* Ctx, char c) {
      SourceManager &SM = Ctx->getSourceManager();
      if (loc.isMacroID()) 
        if (!Lexer::isAtEndOfMacroExpansion(loc, SM,Ctx->getLangOpts(), &loc))
          return SourceLocation();
      loc = Lexer::getLocForEndOfToken(loc, /*Offset=*/0, SM, Ctx->getLangOpts());

      // Break down the source location.
      std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc);

      // Try to load the file buffer.
      bool invalidTemp = false;
      StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
      if (invalidTemp)
        return SourceLocation();

      const char *tokenBegin = file.data() + locInfo.second;

      // Lex from the start of the given location.
      Lexer lexer(SM.getLocForStartOfFile(locInfo.first), Ctx->getLangOpts(), file.begin(),  tokenBegin, file.end());
      Token tok;
      lexer.LexFromRawLexer(tok);

      switch(c){
    case ';': if (tok.isNot(tok::semi))
            return findTokenAfterLocation(tok.getLocation(), Ctx, c);
          break;
    case ',': if (tok.isNot(tok::comma))
            return findTokenAfterLocation(tok.getLocation(), Ctx, c);
          break;
    default: return SourceLocation();
         break;
      }

      return tok.getLocation();
    }

Which is adapted from "findLocationAfterSemi" from clang/lib/ARCMigrate/Transforms.cpp in order to find several tokens and it works fine in others programs in which is used. But I get a segmentation fault when I try this:

SourceLocation comma = findSymbolAfterLocation(f1->getLocation(), Context,',');

being f1 a FieldDecl*.
What can be happenning?
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140129/dd6205f9/attachment.html>


More information about the cfe-dev mailing list