<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>Hello,<br><br>I'm trying to acces to the SourceLocation of a token (a comma, specifically) in a declaration of this type:<br><br><blockquote>class A{<br>public:<br> int a, b;<br>};<br><br></blockquote>For this, I'm using this function:<br><br><blockquote>SourceLocation findTokenAfterLocation(SourceLocation loc, ASTContext* Ctx, char c) {<br> SourceManager &SM = Ctx->getSourceManager();<br> if (loc.isMacroID()) <br> if (!Lexer::isAtEndOfMacroExpansion(loc, SM,Ctx->getLangOpts(), &loc))<br> return SourceLocation();<br> loc = Lexer::getLocForEndOfToken(loc, /*Offset=*/0, SM, Ctx->getLangOpts());<br><br> // Break down the source location.<br> std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc);<br><br> // Try to load the file buffer.<br> bool invalidTemp = false;<br> StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);<br> if (invalidTemp)<br> return SourceLocation();<br><br> const char *tokenBegin = file.data() + locInfo.second;<br><br> // Lex from the start of the given location.<br> Lexer lexer(SM.getLocForStartOfFile(locInfo.first), Ctx->getLangOpts(), file.begin(), tokenBegin, file.end());<br> Token tok;<br> lexer.LexFromRawLexer(tok);<br><br> switch(c){<br> case ';': if (tok.isNot(tok::semi))<br> return findTokenAfterLocation(tok.getLocation(), Ctx, c);<br> break;<br> case ',': if (tok.isNot(tok::comma))<br> return findTokenAfterLocation(tok.getLocation(), Ctx, c);<br> break;<br> default: return SourceLocation();<br> break;<br> }<br><br> return tok.getLocation();<br> }<br><br></blockquote>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:<br><br><blockquote>SourceLocation comma = findSymbolAfterLocation(f1->getLocation(), Context,',');<br><br></blockquote>being f1 a FieldDecl*.<br>What can be happenning?<br> </div></body>
</html>