================
@@ -847,11 +869,104 @@ static bool areExprsMacroAndNonMacro(const Expr 
*&LhsExpr,
   if (!LhsExpr || !RhsExpr)
     return false;
 
-  SourceLocation LhsLoc = LhsExpr->getExprLoc();
-  SourceLocation RhsLoc = RhsExpr->getExprLoc();
+  const SourceLocation LhsLoc = LhsExpr->getExprLoc();
+  const SourceLocation RhsLoc = RhsExpr->getExprLoc();
 
   return LhsLoc.isMacroID() != RhsLoc.isMacroID();
 }
+
+static bool areStringsSameIgnoreSpaces(const llvm::StringRef *Left,
+                                       const llvm::StringRef *Right) {
+  if (Left == Right)
+    return true;
+  if (Left->compare(*Right) == 0) {
+    return true;
+  }
+  // Do running index comparison
+  size_t LIdx = 0;
+  size_t RIdx = 0;
+  const char *LData = Left->data();
+  const char *RData = Right->data();
+  while (LIdx < Left->size() && RIdx < Right->size()) {
+
+    // Skip spaces and tabs from both strings
+    while (LIdx < Left->size() && (LData[LIdx] == ' ' || LData[LIdx] == '\t')) 
{
+      LIdx += 1;
+    }
+    while (RIdx < Right->size() &&
+           (RData[RIdx] == ' ' || RData[RIdx] == '\t')) {
+      RIdx += 1;
+    }
+
+    // If all not tabs and spaces are the same then return true
+    if (LIdx >= Left->size() && RIdx >= Right->size())
+      return true;
+
+    // If any characters differs then return false
+    else if (LData[LIdx] != RData[RIdx])
+      return false;
+
+    // If current char is the same ==> continue search
+    else {
+      LIdx += 1;
+      RIdx += 1;
+    }
+  }
+  // Shortest string is processed: issue a verdict
+  return LIdx >= Left->size() && RIdx >= Right->size();
+}
+
+static bool areExprsSameMacroOrLiteral(const BinaryOperator *BinOp,
+                                       const ASTContext *Context) {
+
+  if (!BinOp)
+    return false;
+
+  const Expr *Lhs = BinOp->getLHS();
+  const Expr *Rhs = BinOp->getRHS();
+  const SourceManager &SM = Context->getSourceManager();
+
+  const SourceRange Lsr = Lhs->getSourceRange();
+  const SourceRange Rsr = Rhs->getSourceRange();
+  if (Lsr.getBegin().isMacroID()) {
+    // Left is macro so right macro too
+    if (Rsr.getBegin().isMacroID()) {
+      // Both sides are macros so they are same macro or literal
+      const llvm::StringRef L = Lexer::getSourceText(
+          CharSourceRange::getTokenRange(Lsr), SM, LangOptions(), 0);
+      const llvm::StringRef R = Lexer::getSourceText(
+          CharSourceRange::getTokenRange(Rsr), SM, LangOptions(), 0);
+      return areStringsSameIgnoreSpaces(&L, &R);
+    }
+    // Left is macro but right is not so they are not same macro or literal
+    return false;
+  } else {
----------------
PiotrZSL wrote:

no need for else, previous if ends with return.

https://github.com/llvm/llvm-project/pull/122841
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to