[cfe-dev] SourceLoc comparison

Rafael·Stahl via cfe-dev cfe-dev at lists.llvm.org
Thu Dec 13 06:02:23 PST 2018


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?



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;

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 5449 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20181213/5c970eec/attachment.bin>

More information about the cfe-dev mailing list