[cfe-dev] End SourceLocation of macro

aniruddh rao aniruddhasrao at gmail.com
Fri Apr 15 10:51:41 PDT 2011


Thanks a lot Argyrios. This is exactly what I wanted.

Thanks,
Aniruddh

On Thu, Apr 14, 2011 at 4:58 PM, Argyrios Kyrtzidis <kyrtzidis at apple.com>wrote:

> On Apr 13, 2011, at 10:35 PM, aniruddh rao wrote:
>
> Hi,
>
> I am doing source analysis using clang and I need to identify certain
> function calls. Some of these calls are instantiated through macros.
>
> Eg:
>
> <1> #define MALLOC(x) malloc((x))
> <2>
> <3>int main() {
> <4>     char *p;
> <5>
> <6>     p = MALLOC(sizeof(char) *
> <7>                                  MAX_SIZE);
> <8> }
>
> In the above dummy example, I want to get the end location of malloc
> invocation. When MALLOC is expanded by the preprocessor, the two line
> invocation becomes single line and getEndLoc() on the CallExpr object gives
> line number 6. But I want to get the actual end location of the macro
> invocation in source file. In the example, it is line 7. Can anyone point me
> in the right direction to get this from AST?
>
>
>
> Let's say 'loc' is the SourceLocation that you know is in a macro
> (isMacroID() == true) and 'SM' is SourceManager, do this:
>
>   FileID FID = SM.getFileID(loc);
>   const SrcMgr::SLocEntry *Entry = &SM.getSLocEntry(FID);
>
>   while (Entry->getInstantiation().getInstantiationLocStart().isMacroID())
> {
>     loc = Entry->getInstantiation().getInstantiationLocStart();
>     FID = SM.getFileID(loc);
>     Entry = &SM.getSLocEntry(FID);
>   }
>
> Now
>
> Entry->getInstantiation().getInstantiationLocEnd()
>
> Will be the SourceLocation of ending ')' of the macro instantiation.
>
>
> Thanks,
> Aniruddh
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110415/b6ae2f32/attachment.html>


More information about the cfe-dev mailing list