[prev in list] [next in list] [prev in thread] [next in thread]
List: cfe-dev
Subject: [cfe-dev] Location (in Source Code) of String Literal in MacroExpansion
From: "chiasa.men via cfe-dev" <cfe-dev () lists ! llvm ! org>
Date: 2021-08-26 8:02:57
Message-ID: 9426298.kXHlkqGcj9 () march
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
https://gist.github.com/T35R6braPwgDJKq/6439fda090e4ee8440d726f8dafa4dbb#file-mectest-cpp
Above I modified the MacroExpansionContext unittest.
The relevant code:
//--------------
TEST_F(MacroExpansionContextTest, Custom) {
// From the GCC website.
const auto Ctx = getMacroExpansionContextFor(R"code(
#define CONCATMACRO(parm) L##parm
int f(wchar_t *b){}
int main()
{
int a= f(CONCATMACRO("TEST"));
int b= f(L"TEST");
return 0;
}
)code");
Ctx->dumpExpansionRanges();
//> :6:14, :6:33
Ctx->dumpExpandedTexts();
//> :6:14 -> 'L"TEST"'
printf("Exp: %s\n",Ctx->getExpandedText(at(6, 14)).getValue());
//Exp: L"TEST"
printf("Org: %s\n",Ctx->getOriginalText(at(6, 14)).getValue());
//Org: CONCATMACRO("TEST"));
// int b= f(L"TEST");
// return 0;
//}
StringRef sourceText =
clang::Lexer::getSourceText(CharSourceRange(SourceRange(at(6, 14),at(6,
33)),false),SourceMgr, LangOpts);
printf("sourceText: %s\n",sourceText);
// sourceText: CONCATMACRO("TEST"));
// int b= f(L"TEST");
// return 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.
It tells about the ExpansionRanges: 6:14, :6:33 which is CONCATMACRO("TEST")
What I would need is: 6:26, 6:32
Obviously I could just calculate a bit like:
range.begin (14) + macronameLen (11) +1 (opening parenthesis) = 26
to
range.end (33) - 1 (closing parenthesis) = 32
But would that be reliable and is there no other way?
Furthermore: Why do getSourceText/getOriginalText print the whole rest of the
source code and not just from (6,14) to (6,33)?
[Attachment #5 (unknown)]
<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-me \
ctest-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>
[Attachment #6 (text/plain)]
_______________________________________________
cfe-dev mailing list
cfe-dev@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic