[clang] [clang][Sema] Warn when macro operator mixes with outer operand due to precedence (PR #184924)
Rajveer Singh Bharadwaj via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 13 06:04:29 PDT 2026
================
@@ -15844,6 +15844,56 @@ static void DiagnoseShiftCompare(Sema &S, SourceLocation OpLoc,
SourceRange(OCE->getArg(1)->getBeginLoc(), RHSExpr->getEndLoc()));
}
+static bool operandEscapesImmediateMacroExpansion(const SourceManager &SM,
+ SourceLocation OpLoc,
+ SourceRange OperandRange) {
+
+ if (!OpLoc.isMacroID())
+ return false;
+
+ SourceLocation Begin = OperandRange.getBegin();
+ SourceLocation End = OperandRange.getEnd();
+
+ if (!Begin.isValid() || !End.isValid())
+ return false;
+
+ // If operand comes directly from file → definitely escapes
+ if (!Begin.isMacroID() || !End.isMacroID())
+ return true;
+
+ // Compare the immediate expansion location
+ SourceLocation OpExp = SM.getImmediateMacroCallerLoc(OpLoc);
+ SourceLocation BeginExp = SM.getImmediateMacroCallerLoc(Begin);
+ SourceLocation EndExp = SM.getImmediateMacroCallerLoc(End);
+
+ return BeginExp != OpExp || EndExp != OpExp;
+}
+
+static void DiagnoseMacroMixedOperator(Sema &Self, BinaryOperatorKind Opc,
+ SourceLocation OpLoc, Expr *LHSExpr,
+ Expr *RHSExpr) {
+ const SourceManager &SM = Self.getSourceManager();
+
+ if (!OpLoc.isMacroID())
+ return;
----------------
Rajveer100 wrote:
The case mentioned in https://github.com/llvm/llvm-project/pull/184924/changes#r2919832000 fails due to this, since here the `* 4` part falls outside hence we miss this case.
https://github.com/llvm/llvm-project/pull/184924
More information about the cfe-commits
mailing list