[llvm-dev] Location (in Source Code) of String Literal in MacroExpansion

chiasa.men via llvm-dev llvm-dev at lists.llvm.org
Thu Aug 26 01:02:57 PDT 2021


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)?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20210826/aba19521/attachment.html>


More information about the llvm-dev mailing list