[cfe-dev] How to get the string from the CFGStmt

redder_0210 redder_0210 at yeah.net
Fri Feb 17 08:42:23 PST 2012


I want to do something to the CFG of Clang. Now I can get the CFG of the C File.
AfterI got the CFG, I can get the Stmt of the CFG.Now the question is that how I can get the SourceManager through the CFG.
At 2012-02-17 17:10:40,"Manuel Klimek" <klimek at google.com> wrote:
>On Fri, Feb 17, 2012 at 10:05 AM, redder_0210 <redder_0210 at yeah.net> wrote:
>> Thanks for you replying.
>> The new question is that I cannot get the SourceManager only by the Stmt. So
>> the "SM.getSpellingLoc(Range.getBegin())" cannot work.
>> Thus, I want to how to get SourceManager from the Stmt or from the
>> SourceRange.
>
>You can't. You need to get the SourceManager from the context. I could
>help you more if you give a little more context on how you're hooking
>into clang.
>
>Cheers,
>/Manuel
>
>> Hope Best!
>> /Redder
>>
>> At 2012-02-13 17:25:26,"Manuel Klimek" <klimek at google.com> wrote:
>>>On Mon, Feb 13, 2012 at 5:48 AM, redder_0210 <redder_0210 at yeah.net> wrote:
>>>> Dear all,
>>>> I am interested in that how to translate from a statemnt to the
>>>> corresponding string.
>>>> For example,
>>>> in the CFG, there is a CFGStmt "int i = 0;",
>>>> now I want to get the string "int i = 0;",namely modify the type CFGStmt
>>>> to
>>>> the type string.
>>>
>>>CFGStmt has a getStmt() which gets you a Stmt, which has a function
>>>getSourceRange which gives you the SourceRange.
>>>When you have a SourceRange you can get a CharSourceRange by calling
>>>CharSourceRange::getTokenRange(...).
>>>Then you get the start location by calling
>>>SM.getSpellingLoc(Range.getBegin()) (where Range is the
>>>CharSourceRange).
>>>You can calculate the length of the text by calling getDecomposedLoc
>>>on the getBegin() and getEnd() of the spelling locations. You'll also
>>>need to add Lexer::MeasureTokenLength(EndLocation, ...) to the size,
>>>as the last element of a token range points to the begin of the last
>>>token.
>>>Finally you can call SM.getCharacterData with the begin and you'll
>>>have your text.
>>>
>>>Unfortunately this is a little harder than it could be, mostly due to
>>>the different types of locations. (Note that if there are for example
>>>macros involved, it matters whether you use getSpellingLocation or
>>>not).
>>>
>>>Cheers,
>>>/Manuel
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120218/183144ed/attachment.html>


More information about the cfe-dev mailing list