<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="font-size:0.75em;"><a href="https://gist.github.com/T35R6braPwgDJKq/6439fda090e4ee8440d726f8dafa4dbb#file-mectest-cpp">https://gist.github.com/T35R6braPwgDJKq/6439fda090e4ee8440d726f8dafa4dbb#file-mectest-cpp</a></span></p>
<br /><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">Above I modified the MacroExpansionContext unittest.</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">The relevant code:</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">//--------------</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">TEST_F(MacroExpansionContextTest, Custom) {</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">  // From the GCC website.</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">  const auto Ctx = getMacroExpansionContextFor(R"code(</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">#define CONCATMACRO(parm) L##parm</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">int f(wchar_t *b){}</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">int main()</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">{</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">    int a= f(CONCATMACRO("TEST"));</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">    int b= f(L"TEST");</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">    return 0;</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">}</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">)code");</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">    Ctx->dumpExpansionRanges();</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">    //> :6:14, :6:33</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">    Ctx->dumpExpandedTexts();</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">    //> :6:14 -> 'L"TEST"'</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">    printf("Exp: %s\n",Ctx->getExpandedText(at(6, 14)).getValue());</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">    //Exp: L"TEST"</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">    printf("Org: %s\n",Ctx->getOriginalText(at(6, 14)).getValue());</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">    //Org: CONCATMACRO("TEST"));</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">    //    int b= f(L"TEST");</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">    //    return 0;</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">    //}</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">    StringRef sourceText = clang::Lexer::getSourceText(CharSourceRange(SourceRange(at(6, 14),at(6, 33)),false),SourceMgr, LangOpts);</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">    printf("sourceText: %s\n",sourceText);</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">    // sourceText: CONCATMACRO("TEST"));</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">    //     int b= f(L"TEST");</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">    //     return 0;</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">    // }</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">}</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">//--------------</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">I am interested in getting the range for "TEST" in the SourceText. Since the CONCATMACRO is preprocessed to L"TEST" I thought the MacroExpansionContext would be of help.</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">It tells about the ExpansionRanges: 6:14, :6:33 which is CONCATMACRO("TEST")</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">What I would need is: 6:26, 6:32</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">Obviously I could just calculate a bit like:</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">range.begin (14) + macronameLen (11) +1 (opening parenthesis) = 26</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">to</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">range.end (33) - 1 (closing parenthesis) = 32</p>
<br /><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">But would that be reliable and is there no other way?</p>
<br /><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">Furthermore: Why do getSourceText/getOriginalText print the whole rest of the source code and not just from (6,14) to (6,33)?</p>
<br /></body>
</html>