SourceLoc comparison

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

SourceLoc comparison

Richard Smith via cfe-dev
Hi,

When grabbing SourceLocs from different Preprocessors, they cannot be
compared with each other by using their comparison operator.

I assume this is because they have some kind of pointer identity and
equality just compares raw representations.

I attached a function that helped me solve this issue. Can you please
give feedback if I missed something existing or if there is an issue
with my approach? Or maybe the SourceManager/Preprocessor should not
even create new "duplicate" SrcLocs in this case?

Regards
Rafael

---

bool clutil::DeepSrcLocEqual(clang::SourceLocation lhs,
clang::SourceLocation rhs, const clang::SourceManager &SM)
{
     if (lhs == rhs)
         return true;

     if (SM.getExpansionLoc(lhs) != SM.getExpansionLoc(rhs))
         return false;
     if (SM.getSpellingLoc(lhs) != SM.getSpellingLoc(rhs))
         return false;

     clang::SourceLocation lhsMacro, rhsMacro;
     if (SM.isMacroArgExpansion(lhs, &lhsMacro))
     {
         if (!SM.isMacroArgExpansion(rhs, &rhsMacro))
             return false;
         if (!DeepSrcLocEqual(lhsMacro, rhsMacro, SM))
             return false;
     }

     return true;
}


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

smime.p7s (7K) Download Attachment