[clang-tools-extra] Add clang-tidy check readability-math-missing-parentheses (PR #84481)

Bhuminjay Soni via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 11 03:44:51 PDT 2024


================
@@ -0,0 +1,167 @@
+//===--- MathMissingParenthesesCheck.cpp - clang-tidy ---------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "MathMissingParenthesesCheck.h"
+#include "../utils/ASTUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Preprocessor.h"
+#include <set>
+#include <stack>
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+void MathMissingParenthesesCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(binaryOperator(unless(hasParent(binaryOperator())),
+                                    hasDescendant(binaryOperator()))
+                         .bind("binOp"),
+                     this);
+}
+static int precedenceCheck(const char op) {
+  if (op == '/' || op == '*' || op == '%')
+    return 5;
+
+  else if (op == '+' || op == '-')
+    return 4;
+
+  else if (op == '&')
+    return 3;
+  else if (op == '^')
+    return 2;
+
+  else if (op == '|')
+    return 1;
+
+  else
+    return 0;
+}
+static bool isOperand(const char c) {
----------------
11happy wrote:

done


https://github.com/llvm/llvm-project/pull/84481


More information about the cfe-commits mailing list