[clang-tools-extra] [clang-tidy] fix false-negative for macros in `readability-math-missing-parentheses` (PR #90279)

2024-04-26 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy approved this pull request.

LGTM. 
Thank you for CCing me.

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


[clang] Add new flag -Wpointer-integer-ordered-compare (PR #88489)

2024-04-26 Thread Bhuminjay Soni via cfe-commits

11happy wrote:

Humble ping! 

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


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

2024-04-23 Thread Bhuminjay Soni via cfe-commits

11happy wrote:

hello @5chmidti I am sorry for my delayed response quite busy with my academic 
work last week, I have added this change
```
if (EndLoc.isInvalid())
  return;
 ```
 this works fine for those macros.
 Thank you

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


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

2024-04-23 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/84481

>From 8fdf6306085ed4cf0f77b7e718e374e9f65fedf9 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Fri, 8 Mar 2024 19:02:47 +0530
Subject: [PATCH 01/14] add clang-tidy check
 readability-math-missing-parentheses

Signed-off-by: 11happy 
---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../MathMissingParenthesesCheck.cpp   | 167 ++
 .../readability/MathMissingParenthesesCheck.h |  31 
 .../readability/ReadabilityTidyModule.cpp |   3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |   6 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../readability/math-missing-parentheses.rst  |  19 ++
 .../readability/math-missing-parentheses.cpp  |  42 +
 8 files changed, 270 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/math-missing-parentheses.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index a6c8cbd8eb448a..0d4fa095501dfb 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -27,6 +27,7 @@ add_clang_library(clangTidyReadabilityModule
   IsolateDeclarationCheck.cpp
   MagicNumbersCheck.cpp
   MakeMemberFunctionConstCheck.cpp
+  MathMissingParenthesesCheck.cpp
   MisleadingIndentationCheck.cpp
   MisplacedArrayIndexCheck.cpp
   NamedParameterCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
new file mode 100644
index 00..d9574a9fb7a476
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
@@ -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 
+#include 
+
+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) {
+  if (c >= 'a' && c <= 'z')
+return true;
+  else if (c >= 'A' && c <= 'Z')
+return true;
+  else if (c >= '0' && c <= '9')
+return true;
+  else if (c == '$')
+return true;
+  else
+return false;
+}
+static bool conditionForNegative(const std::string s, int i,
+ const std::string CurStr) {
+  if (CurStr[0] == '-') {
+if (i == 0) {
+  return true;
+} else {
+  while (s[i - 1] == ' ') {
+i--;
+  }
+  if (!isOperand(s[i - 1])) {
+return true;
+  } else {
+return false;
+  }
+}
+  } else {
+return false;
+  }
+}
+static std::string getOperationOrder(std::string s, std::set ) 
{
+  std::stack StackOne;
+  std::string TempStr = "";
+  for (int i = 0; i < s.length(); i++) {
+std::string CurStr = "";
+CurStr += s[i];
+if (CurStr == " ")
+  continue;
+else {
+  if (isOperand(CurStr[0]) || conditionForNegative(s, i, CurStr)) {
+while (i < s.length() && (isOperand(s[i]) || s[i] == '-')) {
+  if (s[i] == '-') {
+TempStr += "$";
+  } else {
+TempStr += CurStr;
+  }
+  i++;
+  CurStr = s[i];
+}
+TempStr += " ";
+  } else if (CurStr == "(") {
+StackOne.push("(");
+  } else if (CurStr == ")") {
+while (StackOne.top() != "(") {
+  TempStr += StackOne.top();
+  StackOne.pop();
+}
+StackOne.pop();
+  } else {
+while (!StackOne.empty() && 

[clang] Add new flag -Wpointer-integer-ordered-compare (PR #88489)

2024-04-18 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -Wno-pointer-integer-compare 
-Wpointer-integer-ordered-compare -fsyntax-only -verify=pointer-integer-ordered 
%s
+// RUN: %clang_cc1 -Wpointer-integer-compare 
-Wno-pointer-integer-ordered-compare -fsyntax-only -verify=pointer-integer %s
+
+void test1(int *a){
+int b = 1;
+short c = 1;
+if(c=0; // pointer-integer-ordered-warning{{ordered comparison 
between pointer and zero ('int *' and 'int') is an extension}}
+}
+
+int test3(int *a){
+return a>=1; // pointer-integer-ordered-warning{{ordered comparison 
between pointer and integer ('int *' and 'int')}}
+}
+
+int test4(int *a){
+return a>1; // pointer-integer-ordered-warning{{ordered comparison between 
pointer and integer ('int *' and 'int')}}
+}
+int test5(int *a){

11happy wrote:

done

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


[clang] Add new flag -Wpointer-integer-ordered-compare (PR #88489)

2024-04-18 Thread Bhuminjay Soni via cfe-commits


@@ -245,6 +245,12 @@ Modified Compiler Flags
f3 *c = (f3 *)x;
  }
 
+- Added a new diagnostic flag ``-Wpointer-integer-ordered-compare`` which is
+  grouped under ``-Wpointer-integer-compare`` and moved previously
+  ungrouped diagnostics 
``ext_typecheck_ordered_comparison_of_pointer_integer``,
+  ``ext_typecheck_ordered_comparison_of_pointer_and_zero`` under
+  ``-Wpointer-integer-ordered-compare``. Fixes #GH88090

11happy wrote:

done

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


[clang] Add new flag -Wpointer-integer-ordered-compare (PR #88489)

2024-04-18 Thread Bhuminjay Soni via cfe-commits


@@ -714,10 +714,11 @@ def HeaderHygiene : DiagGroup<"header-hygiene">;
 def DuplicateDeclSpecifier : DiagGroup<"duplicate-decl-specifier">;
 def CompareDistinctPointerType : DiagGroup<"compare-distinct-pointer-types">;
 def GNUUnionCast : DiagGroup<"gnu-union-cast">;
+def PointerIntegerOrderedCompare : 
DiagGroup<"pointer-integer-ordered-compare">;
+def PointerIntegerCompare : 
DiagGroup<"pointer-integer-compare",[PointerIntegerOrderedCompare]>;
 def GNUVariableSizedTypeNotAtEnd : 
DiagGroup<"gnu-variable-sized-type-not-at-end">;
 def Varargs : DiagGroup<"varargs">;
 def XorUsedAsPow : DiagGroup<"xor-used-as-pow">;
-

11happy wrote:

done

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


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

2024-04-18 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/84481

>From 8fdf6306085ed4cf0f77b7e718e374e9f65fedf9 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Fri, 8 Mar 2024 19:02:47 +0530
Subject: [PATCH 01/13] add clang-tidy check
 readability-math-missing-parentheses

Signed-off-by: 11happy 
---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../MathMissingParenthesesCheck.cpp   | 167 ++
 .../readability/MathMissingParenthesesCheck.h |  31 
 .../readability/ReadabilityTidyModule.cpp |   3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |   6 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../readability/math-missing-parentheses.rst  |  19 ++
 .../readability/math-missing-parentheses.cpp  |  42 +
 8 files changed, 270 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/math-missing-parentheses.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index a6c8cbd8eb448a..0d4fa095501dfb 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -27,6 +27,7 @@ add_clang_library(clangTidyReadabilityModule
   IsolateDeclarationCheck.cpp
   MagicNumbersCheck.cpp
   MakeMemberFunctionConstCheck.cpp
+  MathMissingParenthesesCheck.cpp
   MisleadingIndentationCheck.cpp
   MisplacedArrayIndexCheck.cpp
   NamedParameterCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
new file mode 100644
index 00..d9574a9fb7a476
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
@@ -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 
+#include 
+
+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) {
+  if (c >= 'a' && c <= 'z')
+return true;
+  else if (c >= 'A' && c <= 'Z')
+return true;
+  else if (c >= '0' && c <= '9')
+return true;
+  else if (c == '$')
+return true;
+  else
+return false;
+}
+static bool conditionForNegative(const std::string s, int i,
+ const std::string CurStr) {
+  if (CurStr[0] == '-') {
+if (i == 0) {
+  return true;
+} else {
+  while (s[i - 1] == ' ') {
+i--;
+  }
+  if (!isOperand(s[i - 1])) {
+return true;
+  } else {
+return false;
+  }
+}
+  } else {
+return false;
+  }
+}
+static std::string getOperationOrder(std::string s, std::set ) 
{
+  std::stack StackOne;
+  std::string TempStr = "";
+  for (int i = 0; i < s.length(); i++) {
+std::string CurStr = "";
+CurStr += s[i];
+if (CurStr == " ")
+  continue;
+else {
+  if (isOperand(CurStr[0]) || conditionForNegative(s, i, CurStr)) {
+while (i < s.length() && (isOperand(s[i]) || s[i] == '-')) {
+  if (s[i] == '-') {
+TempStr += "$";
+  } else {
+TempStr += CurStr;
+  }
+  i++;
+  CurStr = s[i];
+}
+TempStr += " ";
+  } else if (CurStr == "(") {
+StackOne.push("(");
+  } else if (CurStr == ")") {
+while (StackOne.top() != "(") {
+  TempStr += StackOne.top();
+  StackOne.pop();
+}
+StackOne.pop();
+  } else {
+while (!StackOne.empty() && 

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

2024-04-18 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,97 @@
+//===--- 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 "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+void MathMissingParenthesesCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(binaryOperator(unless(hasParent(binaryOperator())),
+unless(isAssignmentOperator()),
+unless(isComparisonOperator()),
+unless(hasAnyOperatorName("&&", "||")),
+hasDescendant(binaryOperator()))
+ .bind("binOp"),
+ this);
+}
+
+static int getPrecedence(const BinaryOperator *BinOp) {
+  if (!BinOp)
+return 0;
+  switch (BinOp->getOpcode()) {
+  case BO_Mul:
+  case BO_Div:
+  case BO_Rem:
+return 5;
+  case BO_Add:
+  case BO_Sub:
+return 4;
+  case BO_And:
+return 3;
+  case BO_Xor:
+return 2;
+  case BO_Or:
+return 1;
+  default:
+return 0;
+  }
+}
+static void addParantheses(const BinaryOperator *BinOp,
+   const BinaryOperator *ParentBinOp,
+   ClangTidyCheck *Check,
+   const clang::SourceManager ,
+   const clang::LangOptions ) {
+  if (!BinOp)
+return;
+
+  int Precedence1 = getPrecedence(BinOp);
+  int Precedence2 = getPrecedence(ParentBinOp);
+
+  if (ParentBinOp != nullptr && Precedence1 != Precedence2) {
+const clang::SourceLocation StartLoc = BinOp->getBeginLoc();
+const clang::SourceLocation EndLoc =
+clang::Lexer::getLocForEndOfToken(BinOp->getEndLoc(), 0, SM, LangOpts);
+
+auto Diag =
+Check->diag(StartLoc,
+"'%0' has higher precedence than '%1'; add parentheses to "
+"explicitly specify the order of operations")
+<< (Precedence1 > Precedence2 ? BinOp->getOpcodeStr()
+  : ParentBinOp->getOpcodeStr())
+<< (Precedence1 > Precedence2 ? ParentBinOp->getOpcodeStr()
+  : BinOp->getOpcodeStr());
+
+Diag << FixItHint::CreateInsertion(StartLoc, "(");
+Diag << FixItHint::CreateInsertion(EndLoc, ")");
+Diag << SourceRange(StartLoc, EndLoc);

11happy wrote:

done

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


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

2024-04-18 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/84481

>From 8fdf6306085ed4cf0f77b7e718e374e9f65fedf9 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Fri, 8 Mar 2024 19:02:47 +0530
Subject: [PATCH 01/12] add clang-tidy check
 readability-math-missing-parentheses

Signed-off-by: 11happy 
---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../MathMissingParenthesesCheck.cpp   | 167 ++
 .../readability/MathMissingParenthesesCheck.h |  31 
 .../readability/ReadabilityTidyModule.cpp |   3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |   6 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../readability/math-missing-parentheses.rst  |  19 ++
 .../readability/math-missing-parentheses.cpp  |  42 +
 8 files changed, 270 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/math-missing-parentheses.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index a6c8cbd8eb448a..0d4fa095501dfb 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -27,6 +27,7 @@ add_clang_library(clangTidyReadabilityModule
   IsolateDeclarationCheck.cpp
   MagicNumbersCheck.cpp
   MakeMemberFunctionConstCheck.cpp
+  MathMissingParenthesesCheck.cpp
   MisleadingIndentationCheck.cpp
   MisplacedArrayIndexCheck.cpp
   NamedParameterCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
new file mode 100644
index 00..d9574a9fb7a476
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
@@ -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 
+#include 
+
+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) {
+  if (c >= 'a' && c <= 'z')
+return true;
+  else if (c >= 'A' && c <= 'Z')
+return true;
+  else if (c >= '0' && c <= '9')
+return true;
+  else if (c == '$')
+return true;
+  else
+return false;
+}
+static bool conditionForNegative(const std::string s, int i,
+ const std::string CurStr) {
+  if (CurStr[0] == '-') {
+if (i == 0) {
+  return true;
+} else {
+  while (s[i - 1] == ' ') {
+i--;
+  }
+  if (!isOperand(s[i - 1])) {
+return true;
+  } else {
+return false;
+  }
+}
+  } else {
+return false;
+  }
+}
+static std::string getOperationOrder(std::string s, std::set ) 
{
+  std::stack StackOne;
+  std::string TempStr = "";
+  for (int i = 0; i < s.length(); i++) {
+std::string CurStr = "";
+CurStr += s[i];
+if (CurStr == " ")
+  continue;
+else {
+  if (isOperand(CurStr[0]) || conditionForNegative(s, i, CurStr)) {
+while (i < s.length() && (isOperand(s[i]) || s[i] == '-')) {
+  if (s[i] == '-') {
+TempStr += "$";
+  } else {
+TempStr += CurStr;
+  }
+  i++;
+  CurStr = s[i];
+}
+TempStr += " ";
+  } else if (CurStr == "(") {
+StackOne.push("(");
+  } else if (CurStr == ")") {
+while (StackOne.top() != "(") {
+  TempStr += StackOne.top();
+  StackOne.pop();
+}
+StackOne.pop();
+  } else {
+while (!StackOne.empty() && 

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

2024-04-18 Thread Bhuminjay Soni via cfe-commits

11happy wrote:

Hello @PiotrZSL , I ran it on simple codebases & it looks good. I havent tried 
it on llvm codebase. it was taking a lot of time on my pc. 

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


[clang] Add new flag -Wpointer-integer-ordered-compare (PR #88489)

2024-04-18 Thread Bhuminjay Soni via cfe-commits

11happy wrote:

humble reminder @AaronBallman , @shafik 

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


[clang] Add new flag -Wpointer-integer-ordered-compare (PR #88489)

2024-04-18 Thread Bhuminjay Soni via cfe-commits

11happy wrote:

Hello @vincent-mailhol thank you for the review I have made the suggested 
changed.


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


[clang] Add new flag -Wpointer-integer-ordered-compare (PR #88489)

2024-04-18 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/88489

>From bc08f479ba483e1582304ca103dbd92481108f12 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Fri, 12 Apr 2024 14:56:42 +0530
Subject: [PATCH 1/3] add new flag -Wpointer-integer-ordered-compare

Signed-off-by: 11happy 
---
 clang/docs/ReleaseNotes.rst   |  6 
 clang/include/clang/Basic/DiagnosticGroups.td |  3 +-
 .../clang/Basic/DiagnosticSemaKinds.td|  8 +++--
 clang/test/Misc/warning-flags.c   |  5 ++--
 clang/test/Sema/compare_pointers.c| 30 +++
 5 files changed, 45 insertions(+), 7 deletions(-)
 create mode 100644 clang/test/Sema/compare_pointers.c

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 93318871fa9f6e..e205088c5cd440 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -245,6 +245,12 @@ Modified Compiler Flags
f3 *c = (f3 *)x;
  }
 
+- Added a new diagnostic flag ``-Wpointer-integer-ordered-compare`` which is
+  grouped under ``-Wpointer-integer-compare`` and moved previously
+  ungrouped diagnostics 
``ext_typecheck_ordered_comparison_of_pointer_integer``,
+  ``ext_typecheck_ordered_comparison_of_pointer_and_zero`` under
+  ``-Wpointer-integer-ordered-compare``. Fixes #GH88090
+
 
 Removed Compiler Flags
 -
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 47747d8704b6c8..2cf56574650839 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -714,10 +714,11 @@ def HeaderHygiene : DiagGroup<"header-hygiene">;
 def DuplicateDeclSpecifier : DiagGroup<"duplicate-decl-specifier">;
 def CompareDistinctPointerType : DiagGroup<"compare-distinct-pointer-types">;
 def GNUUnionCast : DiagGroup<"gnu-union-cast">;
+def PointerIntegerOrderedCompare : 
DiagGroup<"pointer-integer-ordered-compare">;
+def PointerIntegerCompare : 
DiagGroup<"pointer-integer-compare",[PointerIntegerOrderedCompare]>;
 def GNUVariableSizedTypeNotAtEnd : 
DiagGroup<"gnu-variable-sized-type-not-at-end">;
 def Varargs : DiagGroup<"varargs">;
 def XorUsedAsPow : DiagGroup<"xor-used-as-pow">;
-
 def Unsequenced : DiagGroup<"unsequenced">;
 // GCC name for -Wunsequenced
 def : DiagGroup<"sequence-point", [Unsequenced]>;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 180e913155d67c..a5632a3f6f61da 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -7272,9 +7272,11 @@ def err_typecheck_sub_ptr_compatible : Error<
   "%diff{$ and $ are not pointers to compatible types|"
   "pointers to incompatible types}0,1">;
 def ext_typecheck_ordered_comparison_of_pointer_integer : ExtWarn<
-  "ordered comparison between pointer and integer (%0 and %1)">;
+  "ordered comparison between pointer and integer (%0 and %1)">,
+  InGroup;
 def ext_typecheck_ordered_comparison_of_pointer_and_zero : Extension<
-  "ordered comparison between pointer and zero (%0 and %1) is an extension">;
+  "ordered comparison between pointer and zero (%0 and %1) is an extension">,
+  InGroup;
 def err_typecheck_ordered_comparison_of_pointer_and_zero : Error<
   "ordered comparison between pointer and zero (%0 and %1)">;
 def err_typecheck_three_way_comparison_of_pointer_and_zero : Error<
@@ -7299,7 +7301,7 @@ def err_typecheck_comparison_of_fptr_to_void : Error<
   "equality comparison between function pointer and void pointer (%0 and %1)">;
 def ext_typecheck_comparison_of_pointer_integer : ExtWarn<
   "comparison between pointer and integer (%0 and %1)">,
-  InGroup>;
+  InGroup;
 def err_typecheck_comparison_of_pointer_integer : Error<
   "comparison between pointer and integer (%0 and %1)">;
 def ext_typecheck_comparison_of_distinct_pointers : ExtWarn<
diff --git a/clang/test/Misc/warning-flags.c b/clang/test/Misc/warning-flags.c
index dd73331913c6f6..8c0e151613bd82 100644
--- a/clang/test/Misc/warning-flags.c
+++ b/clang/test/Misc/warning-flags.c
@@ -18,7 +18,7 @@ This test serves two purposes:
 
 The list of warnings below should NEVER grow.  It should gradually shrink to 0.
 
-CHECK: Warnings without flags (66):
+CHECK: Warnings without flags (65):
 
 CHECK-NEXT:   ext_expected_semi_decl_list
 CHECK-NEXT:   ext_explicit_specialization_storage_class
@@ -28,7 +28,6 @@ CHECK-NEXT:   ext_plain_complex
 CHECK-NEXT:   ext_template_arg_extra_parens
 CHECK-NEXT:   ext_template_spec_extra_headers
 CHECK-NEXT:   ext_typecheck_cond_incompatible_operands
-CHECK-NEXT:   ext_typecheck_ordered_comparison_of_pointer_integer
 CHECK-NEXT:   ext_using_undefined_std
 CHECK-NEXT:   pp_invalid_string_literal
 CHECK-NEXT:   pp_out_of_date_dependency
@@ -89,4 +88,4 @@ CHECK-NEXT:   warn_weak_import
 
 The list of warnings in -Wpedantic should NEVER grow.
 
-CHECK: Number in -Wpedantic (not 

[clang] Add new flag -Wpointer-integer-ordered-compare (PR #88489)

2024-04-12 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/88489

>From bc08f479ba483e1582304ca103dbd92481108f12 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Fri, 12 Apr 2024 14:56:42 +0530
Subject: [PATCH 1/2] add new flag -Wpointer-integer-ordered-compare

Signed-off-by: 11happy 
---
 clang/docs/ReleaseNotes.rst   |  6 
 clang/include/clang/Basic/DiagnosticGroups.td |  3 +-
 .../clang/Basic/DiagnosticSemaKinds.td|  8 +++--
 clang/test/Misc/warning-flags.c   |  5 ++--
 clang/test/Sema/compare_pointers.c| 30 +++
 5 files changed, 45 insertions(+), 7 deletions(-)
 create mode 100644 clang/test/Sema/compare_pointers.c

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 93318871fa9f6e..e205088c5cd440 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -245,6 +245,12 @@ Modified Compiler Flags
f3 *c = (f3 *)x;
  }
 
+- Added a new diagnostic flag ``-Wpointer-integer-ordered-compare`` which is
+  grouped under ``-Wpointer-integer-compare`` and moved previously
+  ungrouped diagnostics 
``ext_typecheck_ordered_comparison_of_pointer_integer``,
+  ``ext_typecheck_ordered_comparison_of_pointer_and_zero`` under
+  ``-Wpointer-integer-ordered-compare``. Fixes #GH88090
+
 
 Removed Compiler Flags
 -
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 47747d8704b6c8..2cf56574650839 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -714,10 +714,11 @@ def HeaderHygiene : DiagGroup<"header-hygiene">;
 def DuplicateDeclSpecifier : DiagGroup<"duplicate-decl-specifier">;
 def CompareDistinctPointerType : DiagGroup<"compare-distinct-pointer-types">;
 def GNUUnionCast : DiagGroup<"gnu-union-cast">;
+def PointerIntegerOrderedCompare : 
DiagGroup<"pointer-integer-ordered-compare">;
+def PointerIntegerCompare : 
DiagGroup<"pointer-integer-compare",[PointerIntegerOrderedCompare]>;
 def GNUVariableSizedTypeNotAtEnd : 
DiagGroup<"gnu-variable-sized-type-not-at-end">;
 def Varargs : DiagGroup<"varargs">;
 def XorUsedAsPow : DiagGroup<"xor-used-as-pow">;
-
 def Unsequenced : DiagGroup<"unsequenced">;
 // GCC name for -Wunsequenced
 def : DiagGroup<"sequence-point", [Unsequenced]>;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 180e913155d67c..a5632a3f6f61da 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -7272,9 +7272,11 @@ def err_typecheck_sub_ptr_compatible : Error<
   "%diff{$ and $ are not pointers to compatible types|"
   "pointers to incompatible types}0,1">;
 def ext_typecheck_ordered_comparison_of_pointer_integer : ExtWarn<
-  "ordered comparison between pointer and integer (%0 and %1)">;
+  "ordered comparison between pointer and integer (%0 and %1)">,
+  InGroup;
 def ext_typecheck_ordered_comparison_of_pointer_and_zero : Extension<
-  "ordered comparison between pointer and zero (%0 and %1) is an extension">;
+  "ordered comparison between pointer and zero (%0 and %1) is an extension">,
+  InGroup;
 def err_typecheck_ordered_comparison_of_pointer_and_zero : Error<
   "ordered comparison between pointer and zero (%0 and %1)">;
 def err_typecheck_three_way_comparison_of_pointer_and_zero : Error<
@@ -7299,7 +7301,7 @@ def err_typecheck_comparison_of_fptr_to_void : Error<
   "equality comparison between function pointer and void pointer (%0 and %1)">;
 def ext_typecheck_comparison_of_pointer_integer : ExtWarn<
   "comparison between pointer and integer (%0 and %1)">,
-  InGroup>;
+  InGroup;
 def err_typecheck_comparison_of_pointer_integer : Error<
   "comparison between pointer and integer (%0 and %1)">;
 def ext_typecheck_comparison_of_distinct_pointers : ExtWarn<
diff --git a/clang/test/Misc/warning-flags.c b/clang/test/Misc/warning-flags.c
index dd73331913c6f6..8c0e151613bd82 100644
--- a/clang/test/Misc/warning-flags.c
+++ b/clang/test/Misc/warning-flags.c
@@ -18,7 +18,7 @@ This test serves two purposes:
 
 The list of warnings below should NEVER grow.  It should gradually shrink to 0.
 
-CHECK: Warnings without flags (66):
+CHECK: Warnings without flags (65):
 
 CHECK-NEXT:   ext_expected_semi_decl_list
 CHECK-NEXT:   ext_explicit_specialization_storage_class
@@ -28,7 +28,6 @@ CHECK-NEXT:   ext_plain_complex
 CHECK-NEXT:   ext_template_arg_extra_parens
 CHECK-NEXT:   ext_template_spec_extra_headers
 CHECK-NEXT:   ext_typecheck_cond_incompatible_operands
-CHECK-NEXT:   ext_typecheck_ordered_comparison_of_pointer_integer
 CHECK-NEXT:   ext_using_undefined_std
 CHECK-NEXT:   pp_invalid_string_literal
 CHECK-NEXT:   pp_out_of_date_dependency
@@ -89,4 +88,4 @@ CHECK-NEXT:   warn_weak_import
 
 The list of warnings in -Wpedantic should NEVER grow.
 
-CHECK: Number in -Wpedantic (not 

[clang] Add new flag -Wpointer-integer-ordered-compare (PR #88489)

2024-04-12 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy created 
https://github.com/llvm/llvm-project/pull/88489

**Overview:**
This pull request fixes #88090 where a false negative issue related to 
`-Wpointer-integer-compare` failing on comparison between a pointer and zero.

**Testing:**
- Tested the updated code.
- Verified that other functionalities remain unaffected.

**Dependencies:**
- No dependencies on other pull requests.

**CC:**
- @AaronBallman , @shafik 



>From bc08f479ba483e1582304ca103dbd92481108f12 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Fri, 12 Apr 2024 14:56:42 +0530
Subject: [PATCH] add new flag -Wpointer-integer-ordered-compare

Signed-off-by: 11happy 
---
 clang/docs/ReleaseNotes.rst   |  6 
 clang/include/clang/Basic/DiagnosticGroups.td |  3 +-
 .../clang/Basic/DiagnosticSemaKinds.td|  8 +++--
 clang/test/Misc/warning-flags.c   |  5 ++--
 clang/test/Sema/compare_pointers.c| 30 +++
 5 files changed, 45 insertions(+), 7 deletions(-)
 create mode 100644 clang/test/Sema/compare_pointers.c

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 93318871fa9f6e..e205088c5cd440 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -245,6 +245,12 @@ Modified Compiler Flags
f3 *c = (f3 *)x;
  }
 
+- Added a new diagnostic flag ``-Wpointer-integer-ordered-compare`` which is
+  grouped under ``-Wpointer-integer-compare`` and moved previously
+  ungrouped diagnostics 
``ext_typecheck_ordered_comparison_of_pointer_integer``,
+  ``ext_typecheck_ordered_comparison_of_pointer_and_zero`` under
+  ``-Wpointer-integer-ordered-compare``. Fixes #GH88090
+
 
 Removed Compiler Flags
 -
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 47747d8704b6c8..2cf56574650839 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -714,10 +714,11 @@ def HeaderHygiene : DiagGroup<"header-hygiene">;
 def DuplicateDeclSpecifier : DiagGroup<"duplicate-decl-specifier">;
 def CompareDistinctPointerType : DiagGroup<"compare-distinct-pointer-types">;
 def GNUUnionCast : DiagGroup<"gnu-union-cast">;
+def PointerIntegerOrderedCompare : 
DiagGroup<"pointer-integer-ordered-compare">;
+def PointerIntegerCompare : 
DiagGroup<"pointer-integer-compare",[PointerIntegerOrderedCompare]>;
 def GNUVariableSizedTypeNotAtEnd : 
DiagGroup<"gnu-variable-sized-type-not-at-end">;
 def Varargs : DiagGroup<"varargs">;
 def XorUsedAsPow : DiagGroup<"xor-used-as-pow">;
-
 def Unsequenced : DiagGroup<"unsequenced">;
 // GCC name for -Wunsequenced
 def : DiagGroup<"sequence-point", [Unsequenced]>;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 180e913155d67c..a5632a3f6f61da 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -7272,9 +7272,11 @@ def err_typecheck_sub_ptr_compatible : Error<
   "%diff{$ and $ are not pointers to compatible types|"
   "pointers to incompatible types}0,1">;
 def ext_typecheck_ordered_comparison_of_pointer_integer : ExtWarn<
-  "ordered comparison between pointer and integer (%0 and %1)">;
+  "ordered comparison between pointer and integer (%0 and %1)">,
+  InGroup;
 def ext_typecheck_ordered_comparison_of_pointer_and_zero : Extension<
-  "ordered comparison between pointer and zero (%0 and %1) is an extension">;
+  "ordered comparison between pointer and zero (%0 and %1) is an extension">,
+  InGroup;
 def err_typecheck_ordered_comparison_of_pointer_and_zero : Error<
   "ordered comparison between pointer and zero (%0 and %1)">;
 def err_typecheck_three_way_comparison_of_pointer_and_zero : Error<
@@ -7299,7 +7301,7 @@ def err_typecheck_comparison_of_fptr_to_void : Error<
   "equality comparison between function pointer and void pointer (%0 and %1)">;
 def ext_typecheck_comparison_of_pointer_integer : ExtWarn<
   "comparison between pointer and integer (%0 and %1)">,
-  InGroup>;
+  InGroup;
 def err_typecheck_comparison_of_pointer_integer : Error<
   "comparison between pointer and integer (%0 and %1)">;
 def ext_typecheck_comparison_of_distinct_pointers : ExtWarn<
diff --git a/clang/test/Misc/warning-flags.c b/clang/test/Misc/warning-flags.c
index dd73331913c6f6..8c0e151613bd82 100644
--- a/clang/test/Misc/warning-flags.c
+++ b/clang/test/Misc/warning-flags.c
@@ -18,7 +18,7 @@ This test serves two purposes:
 
 The list of warnings below should NEVER grow.  It should gradually shrink to 0.
 
-CHECK: Warnings without flags (66):
+CHECK: Warnings without flags (65):
 
 CHECK-NEXT:   ext_expected_semi_decl_list
 CHECK-NEXT:   ext_explicit_specialization_storage_class
@@ -28,7 +28,6 @@ CHECK-NEXT:   ext_plain_complex
 CHECK-NEXT:   ext_template_arg_extra_parens
 CHECK-NEXT:   ext_template_spec_extra_headers
 CHECK-NEXT:   

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

2024-04-08 Thread Bhuminjay Soni via cfe-commits

11happy wrote:

Humble reminder! @PiotrZSL

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


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

2024-04-01 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/84481

>From 8fdf6306085ed4cf0f77b7e718e374e9f65fedf9 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Fri, 8 Mar 2024 19:02:47 +0530
Subject: [PATCH 01/11] add clang-tidy check
 readability-math-missing-parentheses

Signed-off-by: 11happy 
---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../MathMissingParenthesesCheck.cpp   | 167 ++
 .../readability/MathMissingParenthesesCheck.h |  31 
 .../readability/ReadabilityTidyModule.cpp |   3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |   6 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../readability/math-missing-parentheses.rst  |  19 ++
 .../readability/math-missing-parentheses.cpp  |  42 +
 8 files changed, 270 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/math-missing-parentheses.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index a6c8cbd8eb448a..0d4fa095501dfb 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -27,6 +27,7 @@ add_clang_library(clangTidyReadabilityModule
   IsolateDeclarationCheck.cpp
   MagicNumbersCheck.cpp
   MakeMemberFunctionConstCheck.cpp
+  MathMissingParenthesesCheck.cpp
   MisleadingIndentationCheck.cpp
   MisplacedArrayIndexCheck.cpp
   NamedParameterCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
new file mode 100644
index 00..d9574a9fb7a476
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
@@ -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 
+#include 
+
+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) {
+  if (c >= 'a' && c <= 'z')
+return true;
+  else if (c >= 'A' && c <= 'Z')
+return true;
+  else if (c >= '0' && c <= '9')
+return true;
+  else if (c == '$')
+return true;
+  else
+return false;
+}
+static bool conditionForNegative(const std::string s, int i,
+ const std::string CurStr) {
+  if (CurStr[0] == '-') {
+if (i == 0) {
+  return true;
+} else {
+  while (s[i - 1] == ' ') {
+i--;
+  }
+  if (!isOperand(s[i - 1])) {
+return true;
+  } else {
+return false;
+  }
+}
+  } else {
+return false;
+  }
+}
+static std::string getOperationOrder(std::string s, std::set ) 
{
+  std::stack StackOne;
+  std::string TempStr = "";
+  for (int i = 0; i < s.length(); i++) {
+std::string CurStr = "";
+CurStr += s[i];
+if (CurStr == " ")
+  continue;
+else {
+  if (isOperand(CurStr[0]) || conditionForNegative(s, i, CurStr)) {
+while (i < s.length() && (isOperand(s[i]) || s[i] == '-')) {
+  if (s[i] == '-') {
+TempStr += "$";
+  } else {
+TempStr += CurStr;
+  }
+  i++;
+  CurStr = s[i];
+}
+TempStr += " ";
+  } else if (CurStr == "(") {
+StackOne.push("(");
+  } else if (CurStr == ")") {
+while (StackOne.top() != "(") {
+  TempStr += StackOne.top();
+  StackOne.pop();
+}
+StackOne.pop();
+  } else {
+while (!StackOne.empty() && 

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

2024-04-01 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/84481

>From 8fdf6306085ed4cf0f77b7e718e374e9f65fedf9 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Fri, 8 Mar 2024 19:02:47 +0530
Subject: [PATCH 01/11] add clang-tidy check
 readability-math-missing-parentheses

Signed-off-by: 11happy 
---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../MathMissingParenthesesCheck.cpp   | 167 ++
 .../readability/MathMissingParenthesesCheck.h |  31 
 .../readability/ReadabilityTidyModule.cpp |   3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |   6 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../readability/math-missing-parentheses.rst  |  19 ++
 .../readability/math-missing-parentheses.cpp  |  42 +
 8 files changed, 270 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/math-missing-parentheses.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index a6c8cbd8eb448a..0d4fa095501dfb 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -27,6 +27,7 @@ add_clang_library(clangTidyReadabilityModule
   IsolateDeclarationCheck.cpp
   MagicNumbersCheck.cpp
   MakeMemberFunctionConstCheck.cpp
+  MathMissingParenthesesCheck.cpp
   MisleadingIndentationCheck.cpp
   MisplacedArrayIndexCheck.cpp
   NamedParameterCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
new file mode 100644
index 00..d9574a9fb7a476
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
@@ -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 
+#include 
+
+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) {
+  if (c >= 'a' && c <= 'z')
+return true;
+  else if (c >= 'A' && c <= 'Z')
+return true;
+  else if (c >= '0' && c <= '9')
+return true;
+  else if (c == '$')
+return true;
+  else
+return false;
+}
+static bool conditionForNegative(const std::string s, int i,
+ const std::string CurStr) {
+  if (CurStr[0] == '-') {
+if (i == 0) {
+  return true;
+} else {
+  while (s[i - 1] == ' ') {
+i--;
+  }
+  if (!isOperand(s[i - 1])) {
+return true;
+  } else {
+return false;
+  }
+}
+  } else {
+return false;
+  }
+}
+static std::string getOperationOrder(std::string s, std::set ) 
{
+  std::stack StackOne;
+  std::string TempStr = "";
+  for (int i = 0; i < s.length(); i++) {
+std::string CurStr = "";
+CurStr += s[i];
+if (CurStr == " ")
+  continue;
+else {
+  if (isOperand(CurStr[0]) || conditionForNegative(s, i, CurStr)) {
+while (i < s.length() && (isOperand(s[i]) || s[i] == '-')) {
+  if (s[i] == '-') {
+TempStr += "$";
+  } else {
+TempStr += CurStr;
+  }
+  i++;
+  CurStr = s[i];
+}
+TempStr += " ";
+  } else if (CurStr == "(") {
+StackOne.push("(");
+  } else if (CurStr == ")") {
+while (StackOne.top() != "(") {
+  TempStr += StackOne.top();
+  StackOne.pop();
+}
+StackOne.pop();
+  } else {
+while (!StackOne.empty() && 

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

2024-04-01 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,112 @@
+//===--- 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 "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+void MathMissingParenthesesCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(binaryOperator(unless(hasParent(binaryOperator())),

11happy wrote:

done

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


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

2024-04-01 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,81 @@
+// RUN: %check_clang_tidy %s readability-math-missing-parentheses %t
+
+int foo(){
+return 5;
+}
+
+int bar(){
+return 4;
+}
+
+class fun{
+public:  
+int A;
+double B;
+fun(){
+A = 5;
+B = 5.4;
+}
+};
+
+void f(){
+//CHECK-MESSAGES: :[[@LINE+2]]:17: warning: '*' has higher precedence than 
'+'; add parentheses to make the precedence of operations explicit 
[readability-math-missing-parentheses]
+//CHECK-FIXES: int a = 1 + (2 * 3);
+int a = 1 + 2 * 3; 
+
+int b = 1 + 2 + 3; // No warning
+
+int c = 1 * 2 * 3; // No warning
+
+//CHECK-MESSAGES: :[[@LINE+3]]:17: warning: '*' has higher precedence than 
'+'; add parentheses to make the precedence of operations explicit 
[readability-math-missing-parentheses]
+//CHECK-MESSAGES: :[[@LINE+2]]:25: warning: '/' has higher precedence than 
'-'; add parentheses to make the precedence of operations explicit 
[readability-math-missing-parentheses]
+//CHECK-FIXES: int d = 1 + (2 * 3) - (4 / 5);
+int d = 1 + 2 * 3 - 4 / 5;

11happy wrote:

done

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


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

2024-04-01 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,112 @@
+//===--- 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 "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+void MathMissingParenthesesCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(binaryOperator(unless(hasParent(binaryOperator())),
+unless(allOf(hasOperatorName("&&"),
+ hasOperatorName("||"))),
+hasDescendant(binaryOperator()))
+ .bind("binOp"),
+ this);
+}
+
+static int getPrecedence(const BinaryOperator *BinOp) {
+  if (!BinOp)
+return 0;
+  switch (BinOp->getOpcode()) {
+  case BO_Mul:
+  case BO_Div:
+  case BO_Rem:
+return 5;
+  case BO_Add:
+  case BO_Sub:
+return 4;
+  case BO_And:
+return 3;
+  case BO_Xor:
+return 2;
+  case BO_Or:
+return 1;
+  default:
+return 0;
+  }
+}
+static bool addParantheses(
+const BinaryOperator *BinOp, const BinaryOperator *ParentBinOp,
+std::vector<
+std::pair>>
+,
+const clang::SourceManager , const clang::LangOptions ) {
+  bool NeedToDiagnose = false;
+  if (!BinOp)
+return NeedToDiagnose;
+
+  if (ParentBinOp != nullptr &&
+  getPrecedence(BinOp) != getPrecedence(ParentBinOp)) {
+NeedToDiagnose = true;
+const clang::SourceLocation StartLoc = BinOp->getBeginLoc();
+clang::SourceLocation EndLoc =
+clang::Lexer::getLocForEndOfToken(BinOp->getEndLoc(), 0, SM, LangOpts);
+Insertions.push_back(
+{clang::SourceRange(StartLoc, EndLoc), {BinOp, ParentBinOp}});
+  }
+
+  NeedToDiagnose |= addParantheses(
+  dyn_cast(BinOp->getLHS()->IgnoreImpCasts()), BinOp,
+  Insertions, SM, LangOpts);
+  NeedToDiagnose |= addParantheses(
+  dyn_cast(BinOp->getRHS()->IgnoreImpCasts()), BinOp,
+  Insertions, SM, LangOpts);
+  return NeedToDiagnose;
+}
+
+void MathMissingParenthesesCheck::check(
+const MatchFinder::MatchResult ) {
+  const auto *BinOp = Result.Nodes.getNodeAs("binOp");
+  std::vector<
+  std::pair>>
+  Insertions;
+  const clang::SourceLocation StartLoc = BinOp->getBeginLoc();
+  const SourceManager  = *Result.SourceManager;
+  const clang::LangOptions  = Result.Context->getLangOpts();
+
+  if (addParantheses(BinOp, nullptr, Insertions, SM, LO)) {
+for (const auto  : Insertions) {
+  const clang::BinaryOperator *BinOp1 = Insertion.second.first;
+  const clang::BinaryOperator *BinOp2 = Insertion.second.second;
+
+  int Precedence1 = getPrecedence(BinOp1);
+  int Precedence2 = getPrecedence(BinOp2);
+
+  auto Diag = diag(Insertion.first.getBegin(),
+   "'%0' has higher precedence than '%1'; add parentheses "
+   "to make the precedence of operations explicit")

11happy wrote:

done

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


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

2024-04-01 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,25 @@
+.. title:: clang-tidy - readability-math-missing-parentheses
+
+readability-math-missing-parentheses
+
+
+Check for missing parentheses in mathematical expressions that involve 
operators
+of different priorities. Parentheses in mathematical expressions clarify the 
order

11happy wrote:

done

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


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

2024-04-01 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,112 @@
+//===--- 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 "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+void MathMissingParenthesesCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(binaryOperator(unless(hasParent(binaryOperator())),
+unless(allOf(hasOperatorName("&&"),
+ hasOperatorName("||"))),
+hasDescendant(binaryOperator()))
+ .bind("binOp"),
+ this);
+}
+
+static int getPrecedence(const BinaryOperator *BinOp) {
+  if (!BinOp)
+return 0;
+  switch (BinOp->getOpcode()) {
+  case BO_Mul:
+  case BO_Div:
+  case BO_Rem:
+return 5;
+  case BO_Add:
+  case BO_Sub:
+return 4;
+  case BO_And:
+return 3;
+  case BO_Xor:
+return 2;
+  case BO_Or:
+return 1;
+  default:
+return 0;
+  }
+}
+static bool addParantheses(
+const BinaryOperator *BinOp, const BinaryOperator *ParentBinOp,
+std::vector<
+std::pair>>
+,
+const clang::SourceManager , const clang::LangOptions ) {
+  bool NeedToDiagnose = false;
+  if (!BinOp)
+return NeedToDiagnose;
+
+  if (ParentBinOp != nullptr &&
+  getPrecedence(BinOp) != getPrecedence(ParentBinOp)) {
+NeedToDiagnose = true;
+const clang::SourceLocation StartLoc = BinOp->getBeginLoc();
+clang::SourceLocation EndLoc =
+clang::Lexer::getLocForEndOfToken(BinOp->getEndLoc(), 0, SM, LangOpts);
+Insertions.push_back(
+{clang::SourceRange(StartLoc, EndLoc), {BinOp, ParentBinOp}});

11happy wrote:

done

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


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

2024-04-01 Thread Bhuminjay Soni via cfe-commits


@@ -129,6 +129,12 @@ New checks
   Replaces certain conditional statements with equivalent calls to
   ``std::min`` or ``std::max``.
 
+- New :doc:`readability-math-missing-parentheses

11happy wrote:

done

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


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

2024-04-01 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/84481

>From 8fdf6306085ed4cf0f77b7e718e374e9f65fedf9 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Fri, 8 Mar 2024 19:02:47 +0530
Subject: [PATCH 01/10] add clang-tidy check
 readability-math-missing-parentheses

Signed-off-by: 11happy 
---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../MathMissingParenthesesCheck.cpp   | 167 ++
 .../readability/MathMissingParenthesesCheck.h |  31 
 .../readability/ReadabilityTidyModule.cpp |   3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |   6 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../readability/math-missing-parentheses.rst  |  19 ++
 .../readability/math-missing-parentheses.cpp  |  42 +
 8 files changed, 270 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/math-missing-parentheses.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index a6c8cbd8eb448a..0d4fa095501dfb 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -27,6 +27,7 @@ add_clang_library(clangTidyReadabilityModule
   IsolateDeclarationCheck.cpp
   MagicNumbersCheck.cpp
   MakeMemberFunctionConstCheck.cpp
+  MathMissingParenthesesCheck.cpp
   MisleadingIndentationCheck.cpp
   MisplacedArrayIndexCheck.cpp
   NamedParameterCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
new file mode 100644
index 00..d9574a9fb7a476
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
@@ -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 
+#include 
+
+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) {
+  if (c >= 'a' && c <= 'z')
+return true;
+  else if (c >= 'A' && c <= 'Z')
+return true;
+  else if (c >= '0' && c <= '9')
+return true;
+  else if (c == '$')
+return true;
+  else
+return false;
+}
+static bool conditionForNegative(const std::string s, int i,
+ const std::string CurStr) {
+  if (CurStr[0] == '-') {
+if (i == 0) {
+  return true;
+} else {
+  while (s[i - 1] == ' ') {
+i--;
+  }
+  if (!isOperand(s[i - 1])) {
+return true;
+  } else {
+return false;
+  }
+}
+  } else {
+return false;
+  }
+}
+static std::string getOperationOrder(std::string s, std::set ) 
{
+  std::stack StackOne;
+  std::string TempStr = "";
+  for (int i = 0; i < s.length(); i++) {
+std::string CurStr = "";
+CurStr += s[i];
+if (CurStr == " ")
+  continue;
+else {
+  if (isOperand(CurStr[0]) || conditionForNegative(s, i, CurStr)) {
+while (i < s.length() && (isOperand(s[i]) || s[i] == '-')) {
+  if (s[i] == '-') {
+TempStr += "$";
+  } else {
+TempStr += CurStr;
+  }
+  i++;
+  CurStr = s[i];
+}
+TempStr += " ";
+  } else if (CurStr == "(") {
+StackOne.push("(");
+  } else if (CurStr == ")") {
+while (StackOne.top() != "(") {
+  TempStr += StackOne.top();
+  StackOne.pop();
+}
+StackOne.pop();
+  } else {
+while (!StackOne.empty() && 

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

2024-03-29 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,86 @@
+//===--- 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 "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+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 getPrecedence(const BinaryOperator *BinOp) {
+  if (!BinOp)
+return 0;
+  switch (BinOp->getOpcode()) {
+  case BO_Mul:
+  case BO_Div:
+  case BO_Rem:
+return 5;
+  case BO_Add:
+  case BO_Sub:
+return 4;
+  case BO_And:
+return 3;
+  case BO_Xor:
+return 2;
+  case BO_Or:
+return 1;
+  default:
+return 0;
+  }
+}
+static bool addParantheses(const BinaryOperator *BinOp,
+   const BinaryOperator *ParentBinOp,
+   std::vector ) {
+  bool NeedToDiagnose = false;
+  if (!BinOp)
+return NeedToDiagnose;
+
+  if (ParentBinOp != nullptr &&
+  getPrecedence(BinOp) != getPrecedence(ParentBinOp)) {
+NeedToDiagnose = true;
+const clang::SourceLocation StartLoc = BinOp->getBeginLoc();
+const clang::SourceLocation EndLoc = 
BinOp->getEndLoc().getLocWithOffset(1);
+Insertions.push_back(clang::SourceRange(StartLoc, EndLoc));
+  }
+
+  NeedToDiagnose |= addParantheses(
+  dyn_cast(BinOp->getLHS()->IgnoreImpCasts()), BinOp,
+  Insertions);
+  NeedToDiagnose |= addParantheses(
+  dyn_cast(BinOp->getRHS()->IgnoreImpCasts()), BinOp,
+  Insertions);
+  return NeedToDiagnose;
+}
+
+void MathMissingParenthesesCheck::check(
+const MatchFinder::MatchResult ) {
+  const auto *BinOp = Result.Nodes.getNodeAs("binOp");
+  std::vector Insertions;
+  const clang::SourceLocation StartLoc = BinOp->getBeginLoc();
+
+  if (addParantheses(BinOp, nullptr, Insertions)) {
+const auto  = diag(
+StartLoc, "add parantheses to clarify the precedence of operations");

11happy wrote:

done

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


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

2024-03-29 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,86 @@
+//===--- 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 "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+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 getPrecedence(const BinaryOperator *BinOp) {
+  if (!BinOp)
+return 0;
+  switch (BinOp->getOpcode()) {
+  case BO_Mul:
+  case BO_Div:
+  case BO_Rem:
+return 5;
+  case BO_Add:
+  case BO_Sub:
+return 4;
+  case BO_And:
+return 3;
+  case BO_Xor:
+return 2;
+  case BO_Or:
+return 1;
+  default:
+return 0;
+  }
+}
+static bool addParantheses(const BinaryOperator *BinOp,
+   const BinaryOperator *ParentBinOp,
+   std::vector ) {
+  bool NeedToDiagnose = false;
+  if (!BinOp)
+return NeedToDiagnose;
+
+  if (ParentBinOp != nullptr &&
+  getPrecedence(BinOp) != getPrecedence(ParentBinOp)) {
+NeedToDiagnose = true;
+const clang::SourceLocation StartLoc = BinOp->getBeginLoc();
+const clang::SourceLocation EndLoc = 
BinOp->getEndLoc().getLocWithOffset(1);
+Insertions.push_back(clang::SourceRange(StartLoc, EndLoc));
+  }
+
+  NeedToDiagnose |= addParantheses(
+  dyn_cast(BinOp->getLHS()->IgnoreImpCasts()), BinOp,
+  Insertions);
+  NeedToDiagnose |= addParantheses(
+  dyn_cast(BinOp->getRHS()->IgnoreImpCasts()), BinOp,
+  Insertions);
+  return NeedToDiagnose;
+}
+
+void MathMissingParenthesesCheck::check(
+const MatchFinder::MatchResult ) {
+  const auto *BinOp = Result.Nodes.getNodeAs("binOp");
+  std::vector Insertions;
+  const clang::SourceLocation StartLoc = BinOp->getBeginLoc();
+
+  if (addParantheses(BinOp, nullptr, Insertions)) {
+const auto  = diag(

11happy wrote:

done

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


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

2024-03-29 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,86 @@
+//===--- 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 "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+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 getPrecedence(const BinaryOperator *BinOp) {
+  if (!BinOp)
+return 0;
+  switch (BinOp->getOpcode()) {
+  case BO_Mul:
+  case BO_Div:
+  case BO_Rem:
+return 5;
+  case BO_Add:
+  case BO_Sub:
+return 4;
+  case BO_And:
+return 3;
+  case BO_Xor:
+return 2;
+  case BO_Or:
+return 1;
+  default:
+return 0;
+  }
+}
+static bool addParantheses(const BinaryOperator *BinOp,
+   const BinaryOperator *ParentBinOp,
+   std::vector ) {
+  bool NeedToDiagnose = false;
+  if (!BinOp)
+return NeedToDiagnose;
+
+  if (ParentBinOp != nullptr &&
+  getPrecedence(BinOp) != getPrecedence(ParentBinOp)) {
+NeedToDiagnose = true;
+const clang::SourceLocation StartLoc = BinOp->getBeginLoc();
+const clang::SourceLocation EndLoc = 
BinOp->getEndLoc().getLocWithOffset(1);

11happy wrote:

done

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


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

2024-03-29 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/84481

>From 8fdf6306085ed4cf0f77b7e718e374e9f65fedf9 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Fri, 8 Mar 2024 19:02:47 +0530
Subject: [PATCH 1/9] add clang-tidy check readability-math-missing-parentheses

Signed-off-by: 11happy 
---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../MathMissingParenthesesCheck.cpp   | 167 ++
 .../readability/MathMissingParenthesesCheck.h |  31 
 .../readability/ReadabilityTidyModule.cpp |   3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |   6 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../readability/math-missing-parentheses.rst  |  19 ++
 .../readability/math-missing-parentheses.cpp  |  42 +
 8 files changed, 270 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/math-missing-parentheses.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index a6c8cbd8eb448a..0d4fa095501dfb 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -27,6 +27,7 @@ add_clang_library(clangTidyReadabilityModule
   IsolateDeclarationCheck.cpp
   MagicNumbersCheck.cpp
   MakeMemberFunctionConstCheck.cpp
+  MathMissingParenthesesCheck.cpp
   MisleadingIndentationCheck.cpp
   MisplacedArrayIndexCheck.cpp
   NamedParameterCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
new file mode 100644
index 00..d9574a9fb7a476
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
@@ -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 
+#include 
+
+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) {
+  if (c >= 'a' && c <= 'z')
+return true;
+  else if (c >= 'A' && c <= 'Z')
+return true;
+  else if (c >= '0' && c <= '9')
+return true;
+  else if (c == '$')
+return true;
+  else
+return false;
+}
+static bool conditionForNegative(const std::string s, int i,
+ const std::string CurStr) {
+  if (CurStr[0] == '-') {
+if (i == 0) {
+  return true;
+} else {
+  while (s[i - 1] == ' ') {
+i--;
+  }
+  if (!isOperand(s[i - 1])) {
+return true;
+  } else {
+return false;
+  }
+}
+  } else {
+return false;
+  }
+}
+static std::string getOperationOrder(std::string s, std::set ) 
{
+  std::stack StackOne;
+  std::string TempStr = "";
+  for (int i = 0; i < s.length(); i++) {
+std::string CurStr = "";
+CurStr += s[i];
+if (CurStr == " ")
+  continue;
+else {
+  if (isOperand(CurStr[0]) || conditionForNegative(s, i, CurStr)) {
+while (i < s.length() && (isOperand(s[i]) || s[i] == '-')) {
+  if (s[i] == '-') {
+TempStr += "$";
+  } else {
+TempStr += CurStr;
+  }
+  i++;
+  CurStr = s[i];
+}
+TempStr += " ";
+  } else if (CurStr == "(") {
+StackOne.push("(");
+  } else if (CurStr == ")") {
+while (StackOne.top() != "(") {
+  TempStr += StackOne.top();
+  StackOne.pop();
+}
+StackOne.pop();
+  } else {
+while (!StackOne.empty() && 

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

2024-03-26 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,25 @@
+.. title:: clang-tidy - readability-math-missing-parentheses
+
+readability-math-missing-parentheses
+
+
+Check for missing parentheses in mathematical expressions that involve 
operators
+of different priorities. Parentheses in mathematical expressions clarify the 
order
+of operations, especially with different-priority operators. Lengthy or 
multiline
+expressions can obscure this order, leading to coding errors. IDEs can aid 
clarity
+by highlighting parentheses. Explicitly using parentheses also clarify what 
the 

11happy wrote:

done

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


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

2024-03-26 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/84481

>From 8fdf6306085ed4cf0f77b7e718e374e9f65fedf9 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Fri, 8 Mar 2024 19:02:47 +0530
Subject: [PATCH 1/8] add clang-tidy check readability-math-missing-parentheses

Signed-off-by: 11happy 
---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../MathMissingParenthesesCheck.cpp   | 167 ++
 .../readability/MathMissingParenthesesCheck.h |  31 
 .../readability/ReadabilityTidyModule.cpp |   3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |   6 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../readability/math-missing-parentheses.rst  |  19 ++
 .../readability/math-missing-parentheses.cpp  |  42 +
 8 files changed, 270 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/math-missing-parentheses.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index a6c8cbd8eb448a..0d4fa095501dfb 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -27,6 +27,7 @@ add_clang_library(clangTidyReadabilityModule
   IsolateDeclarationCheck.cpp
   MagicNumbersCheck.cpp
   MakeMemberFunctionConstCheck.cpp
+  MathMissingParenthesesCheck.cpp
   MisleadingIndentationCheck.cpp
   MisplacedArrayIndexCheck.cpp
   NamedParameterCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
new file mode 100644
index 00..d9574a9fb7a476
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
@@ -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 
+#include 
+
+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) {
+  if (c >= 'a' && c <= 'z')
+return true;
+  else if (c >= 'A' && c <= 'Z')
+return true;
+  else if (c >= '0' && c <= '9')
+return true;
+  else if (c == '$')
+return true;
+  else
+return false;
+}
+static bool conditionForNegative(const std::string s, int i,
+ const std::string CurStr) {
+  if (CurStr[0] == '-') {
+if (i == 0) {
+  return true;
+} else {
+  while (s[i - 1] == ' ') {
+i--;
+  }
+  if (!isOperand(s[i - 1])) {
+return true;
+  } else {
+return false;
+  }
+}
+  } else {
+return false;
+  }
+}
+static std::string getOperationOrder(std::string s, std::set ) 
{
+  std::stack StackOne;
+  std::string TempStr = "";
+  for (int i = 0; i < s.length(); i++) {
+std::string CurStr = "";
+CurStr += s[i];
+if (CurStr == " ")
+  continue;
+else {
+  if (isOperand(CurStr[0]) || conditionForNegative(s, i, CurStr)) {
+while (i < s.length() && (isOperand(s[i]) || s[i] == '-')) {
+  if (s[i] == '-') {
+TempStr += "$";
+  } else {
+TempStr += CurStr;
+  }
+  i++;
+  CurStr = s[i];
+}
+TempStr += " ";
+  } else if (CurStr == "(") {
+StackOne.push("(");
+  } else if (CurStr == ")") {
+while (StackOne.top() != "(") {
+  TempStr += StackOne.top();
+  StackOne.pop();
+}
+StackOne.pop();
+  } else {
+while (!StackOne.empty() && 

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

2024-03-25 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,87 @@
+//===--- 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 "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+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 getPrecedence(const BinaryOperator *BinOp) {
+  if (!BinOp)
+return 0;
+  switch (BinOp->getOpcode()) {
+  case BO_Mul:
+  case BO_Div:
+  case BO_Rem:
+return 5;
+  case BO_Add:
+  case BO_Sub:
+return 4;
+  case BO_And:
+return 3;
+  case BO_Xor:
+return 2;
+  case BO_Or:
+return 1;
+  default:
+return 0;
+  }
+}
+static bool addParantheses(const BinaryOperator *BinOp,
+   const BinaryOperator *ParentBinOp,
+   std::vector ) {
+  bool NeedToDiagnose = false;
+  if (!BinOp)
+return NeedToDiagnose;
+
+  if (ParentBinOp != nullptr &&
+  getPrecedence(BinOp) != getPrecedence(ParentBinOp)) {
+NeedToDiagnose = true;
+const clang::SourceLocation StartLoc = BinOp->getBeginLoc();
+const clang::SourceLocation EndLoc = 
BinOp->getEndLoc().getLocWithOffset(1);
+Insertions.push_back(clang::SourceRange(StartLoc, EndLoc));
+  }
+
+  NeedToDiagnose |= addParantheses(
+  dyn_cast(BinOp->getLHS()->IgnoreImpCasts()), BinOp,
+  Insertions);
+  NeedToDiagnose |= addParantheses(
+  dyn_cast(BinOp->getRHS()->IgnoreImpCasts()), BinOp,
+  Insertions);
+  return NeedToDiagnose;
+}
+
+void MathMissingParenthesesCheck::check(
+const MatchFinder::MatchResult ) {
+  const auto *BinOp = Result.Nodes.getNodeAs("binOp");
+  bool NeedToDiagnose = false;

11happy wrote:

done

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


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

2024-03-25 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,87 @@
+//===--- 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 "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+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 getPrecedence(const BinaryOperator *BinOp) {
+  if (!BinOp)
+return 0;
+  switch (BinOp->getOpcode()) {
+  case BO_Mul:
+  case BO_Div:
+  case BO_Rem:
+return 5;
+  case BO_Add:
+  case BO_Sub:
+return 4;
+  case BO_And:
+return 3;
+  case BO_Xor:
+return 2;
+  case BO_Or:
+return 1;
+  default:
+return 0;
+  }
+}
+static bool addParantheses(const BinaryOperator *BinOp,
+   const BinaryOperator *ParentBinOp,
+   std::vector ) {
+  bool NeedToDiagnose = false;
+  if (!BinOp)
+return NeedToDiagnose;
+
+  if (ParentBinOp != nullptr &&
+  getPrecedence(BinOp) != getPrecedence(ParentBinOp)) {
+NeedToDiagnose = true;
+const clang::SourceLocation StartLoc = BinOp->getBeginLoc();
+const clang::SourceLocation EndLoc = 
BinOp->getEndLoc().getLocWithOffset(1);
+Insertions.push_back(clang::SourceRange(StartLoc, EndLoc));
+  }
+
+  NeedToDiagnose |= addParantheses(
+  dyn_cast(BinOp->getLHS()->IgnoreImpCasts()), BinOp,
+  Insertions);
+  NeedToDiagnose |= addParantheses(
+  dyn_cast(BinOp->getRHS()->IgnoreImpCasts()), BinOp,
+  Insertions);
+  return NeedToDiagnose;
+}
+
+void MathMissingParenthesesCheck::check(
+const MatchFinder::MatchResult ) {
+  const auto *BinOp = Result.Nodes.getNodeAs("binOp");
+  bool NeedToDiagnose = false;
+  std::vector Insertions;
+  const clang::SourceLocation StartLoc = BinOp->getBeginLoc();
+
+  if (addParantheses(BinOp, nullptr, Insertions)) {
+auto const  = diag(

11happy wrote:

done

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


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

2024-03-25 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,87 @@
+//===--- 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 "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+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 getPrecedence(const BinaryOperator *BinOp) {
+  if (!BinOp)
+return 0;
+  switch (BinOp->getOpcode()) {
+  case BO_Mul:
+  case BO_Div:
+  case BO_Rem:
+return 5;
+  case BO_Add:
+  case BO_Sub:
+return 4;
+  case BO_And:
+return 3;
+  case BO_Xor:
+return 2;
+  case BO_Or:
+return 1;
+  default:
+return 0;
+  }
+}
+static bool addParantheses(const BinaryOperator *BinOp,
+   const BinaryOperator *ParentBinOp,
+   std::vector ) {
+  bool NeedToDiagnose = false;
+  if (!BinOp)
+return NeedToDiagnose;
+
+  if (ParentBinOp != nullptr &&
+  getPrecedence(BinOp) != getPrecedence(ParentBinOp)) {
+NeedToDiagnose = true;
+const clang::SourceLocation StartLoc = BinOp->getBeginLoc();
+const clang::SourceLocation EndLoc = 
BinOp->getEndLoc().getLocWithOffset(1);
+Insertions.push_back(clang::SourceRange(StartLoc, EndLoc));
+  }
+
+  NeedToDiagnose |= addParantheses(
+  dyn_cast(BinOp->getLHS()->IgnoreImpCasts()), BinOp,
+  Insertions);
+  NeedToDiagnose |= addParantheses(
+  dyn_cast(BinOp->getRHS()->IgnoreImpCasts()), BinOp,
+  Insertions);
+  return NeedToDiagnose;
+}
+
+void MathMissingParenthesesCheck::check(
+const MatchFinder::MatchResult ) {
+  const auto *BinOp = Result.Nodes.getNodeAs("binOp");
+  bool NeedToDiagnose = false;
+  std::vector Insertions;
+  const clang::SourceLocation StartLoc = BinOp->getBeginLoc();
+
+  if (addParantheses(BinOp, nullptr, Insertions)) {
+auto const  = diag(
+StartLoc, "add parantheses to clarify the precedence of operations");
+for (const auto  : Insertions) {
+  Diag << FixItHint::CreateInsertion(Insertion.getBegin(), "(");
+  Diag << FixItHint::CreateInsertion(Insertion.getEnd(), ")");
+  Diag << SourceRange(Insertion.getBegin(), Insertion.getEnd());

11happy wrote:

done

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


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

2024-03-25 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/84481

>From 8fdf6306085ed4cf0f77b7e718e374e9f65fedf9 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Fri, 8 Mar 2024 19:02:47 +0530
Subject: [PATCH 1/7] add clang-tidy check readability-math-missing-parentheses

Signed-off-by: 11happy 
---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../MathMissingParenthesesCheck.cpp   | 167 ++
 .../readability/MathMissingParenthesesCheck.h |  31 
 .../readability/ReadabilityTidyModule.cpp |   3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |   6 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../readability/math-missing-parentheses.rst  |  19 ++
 .../readability/math-missing-parentheses.cpp  |  42 +
 8 files changed, 270 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/math-missing-parentheses.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index a6c8cbd8eb448a..0d4fa095501dfb 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -27,6 +27,7 @@ add_clang_library(clangTidyReadabilityModule
   IsolateDeclarationCheck.cpp
   MagicNumbersCheck.cpp
   MakeMemberFunctionConstCheck.cpp
+  MathMissingParenthesesCheck.cpp
   MisleadingIndentationCheck.cpp
   MisplacedArrayIndexCheck.cpp
   NamedParameterCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
new file mode 100644
index 00..d9574a9fb7a476
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
@@ -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 
+#include 
+
+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) {
+  if (c >= 'a' && c <= 'z')
+return true;
+  else if (c >= 'A' && c <= 'Z')
+return true;
+  else if (c >= '0' && c <= '9')
+return true;
+  else if (c == '$')
+return true;
+  else
+return false;
+}
+static bool conditionForNegative(const std::string s, int i,
+ const std::string CurStr) {
+  if (CurStr[0] == '-') {
+if (i == 0) {
+  return true;
+} else {
+  while (s[i - 1] == ' ') {
+i--;
+  }
+  if (!isOperand(s[i - 1])) {
+return true;
+  } else {
+return false;
+  }
+}
+  } else {
+return false;
+  }
+}
+static std::string getOperationOrder(std::string s, std::set ) 
{
+  std::stack StackOne;
+  std::string TempStr = "";
+  for (int i = 0; i < s.length(); i++) {
+std::string CurStr = "";
+CurStr += s[i];
+if (CurStr == " ")
+  continue;
+else {
+  if (isOperand(CurStr[0]) || conditionForNegative(s, i, CurStr)) {
+while (i < s.length() && (isOperand(s[i]) || s[i] == '-')) {
+  if (s[i] == '-') {
+TempStr += "$";
+  } else {
+TempStr += CurStr;
+  }
+  i++;
+  CurStr = s[i];
+}
+TempStr += " ";
+  } else if (CurStr == "(") {
+StackOne.push("(");
+  } else if (CurStr == ")") {
+while (StackOne.top() != "(") {
+  TempStr += StackOne.top();
+  StackOne.pop();
+}
+StackOne.pop();
+  } else {
+while (!StackOne.empty() && 

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

2024-03-25 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,25 @@
+.. title:: clang-tidy - readability-math-missing-parentheses
+
+readability-math-missing-parentheses
+
+
+Check for missing parentheses in mathematical expressions that involve 
operators
+of different priorities. Parentheses in mathematical expressions clarify the 
order
+of operations, especially with different-priority operators. Lengthy or 
multiline
+expressions can obscure this order, leading to coding errors. IDEs can aid 
clarity
+by highlighting parentheses. Explicitly using parentheses also clarify what 
the 

11happy wrote:

done

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


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

2024-03-24 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/84481

>From 8fdf6306085ed4cf0f77b7e718e374e9f65fedf9 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Fri, 8 Mar 2024 19:02:47 +0530
Subject: [PATCH 1/6] add clang-tidy check readability-math-missing-parentheses

Signed-off-by: 11happy 
---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../MathMissingParenthesesCheck.cpp   | 167 ++
 .../readability/MathMissingParenthesesCheck.h |  31 
 .../readability/ReadabilityTidyModule.cpp |   3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |   6 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../readability/math-missing-parentheses.rst  |  19 ++
 .../readability/math-missing-parentheses.cpp  |  42 +
 8 files changed, 270 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/math-missing-parentheses.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index a6c8cbd8eb448a..0d4fa095501dfb 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -27,6 +27,7 @@ add_clang_library(clangTidyReadabilityModule
   IsolateDeclarationCheck.cpp
   MagicNumbersCheck.cpp
   MakeMemberFunctionConstCheck.cpp
+  MathMissingParenthesesCheck.cpp
   MisleadingIndentationCheck.cpp
   MisplacedArrayIndexCheck.cpp
   NamedParameterCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
new file mode 100644
index 00..d9574a9fb7a476
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
@@ -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 
+#include 
+
+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) {
+  if (c >= 'a' && c <= 'z')
+return true;
+  else if (c >= 'A' && c <= 'Z')
+return true;
+  else if (c >= '0' && c <= '9')
+return true;
+  else if (c == '$')
+return true;
+  else
+return false;
+}
+static bool conditionForNegative(const std::string s, int i,
+ const std::string CurStr) {
+  if (CurStr[0] == '-') {
+if (i == 0) {
+  return true;
+} else {
+  while (s[i - 1] == ' ') {
+i--;
+  }
+  if (!isOperand(s[i - 1])) {
+return true;
+  } else {
+return false;
+  }
+}
+  } else {
+return false;
+  }
+}
+static std::string getOperationOrder(std::string s, std::set ) 
{
+  std::stack StackOne;
+  std::string TempStr = "";
+  for (int i = 0; i < s.length(); i++) {
+std::string CurStr = "";
+CurStr += s[i];
+if (CurStr == " ")
+  continue;
+else {
+  if (isOperand(CurStr[0]) || conditionForNegative(s, i, CurStr)) {
+while (i < s.length() && (isOperand(s[i]) || s[i] == '-')) {
+  if (s[i] == '-') {
+TempStr += "$";
+  } else {
+TempStr += CurStr;
+  }
+  i++;
+  CurStr = s[i];
+}
+TempStr += " ";
+  } else if (CurStr == "(") {
+StackOne.push("(");
+  } else if (CurStr == ")") {
+while (StackOne.top() != "(") {
+  TempStr += StackOne.top();
+  StackOne.pop();
+}
+StackOne.pop();
+  } else {
+while (!StackOne.empty() && 

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

2024-03-24 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/84481

>From 8fdf6306085ed4cf0f77b7e718e374e9f65fedf9 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Fri, 8 Mar 2024 19:02:47 +0530
Subject: [PATCH 1/5] add clang-tidy check readability-math-missing-parentheses

Signed-off-by: 11happy 
---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../MathMissingParenthesesCheck.cpp   | 167 ++
 .../readability/MathMissingParenthesesCheck.h |  31 
 .../readability/ReadabilityTidyModule.cpp |   3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |   6 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../readability/math-missing-parentheses.rst  |  19 ++
 .../readability/math-missing-parentheses.cpp  |  42 +
 8 files changed, 270 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/math-missing-parentheses.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index a6c8cbd8eb448a..0d4fa095501dfb 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -27,6 +27,7 @@ add_clang_library(clangTidyReadabilityModule
   IsolateDeclarationCheck.cpp
   MagicNumbersCheck.cpp
   MakeMemberFunctionConstCheck.cpp
+  MathMissingParenthesesCheck.cpp
   MisleadingIndentationCheck.cpp
   MisplacedArrayIndexCheck.cpp
   NamedParameterCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
new file mode 100644
index 00..d9574a9fb7a476
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
@@ -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 
+#include 
+
+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) {
+  if (c >= 'a' && c <= 'z')
+return true;
+  else if (c >= 'A' && c <= 'Z')
+return true;
+  else if (c >= '0' && c <= '9')
+return true;
+  else if (c == '$')
+return true;
+  else
+return false;
+}
+static bool conditionForNegative(const std::string s, int i,
+ const std::string CurStr) {
+  if (CurStr[0] == '-') {
+if (i == 0) {
+  return true;
+} else {
+  while (s[i - 1] == ' ') {
+i--;
+  }
+  if (!isOperand(s[i - 1])) {
+return true;
+  } else {
+return false;
+  }
+}
+  } else {
+return false;
+  }
+}
+static std::string getOperationOrder(std::string s, std::set ) 
{
+  std::stack StackOne;
+  std::string TempStr = "";
+  for (int i = 0; i < s.length(); i++) {
+std::string CurStr = "";
+CurStr += s[i];
+if (CurStr == " ")
+  continue;
+else {
+  if (isOperand(CurStr[0]) || conditionForNegative(s, i, CurStr)) {
+while (i < s.length() && (isOperand(s[i]) || s[i] == '-')) {
+  if (s[i] == '-') {
+TempStr += "$";
+  } else {
+TempStr += CurStr;
+  }
+  i++;
+  CurStr = s[i];
+}
+TempStr += " ";
+  } else if (CurStr == "(") {
+StackOne.push("(");
+  } else if (CurStr == ")") {
+while (StackOne.top() != "(") {
+  TempStr += StackOne.top();
+  StackOne.pop();
+}
+StackOne.pop();
+  } else {
+while (!StackOne.empty() && 

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

2024-03-24 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,68 @@
+//===--- 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 "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+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);
+}
+
+void addParantheses(
+const BinaryOperator *BinOp, const BinaryOperator *ParentBinOp,
+bool ,
+std::vector>
+) {
+  if (!BinOp)
+return;
+
+  if (ParentBinOp != nullptr &&
+  ParentBinOp->getOpcode() != BinOp->getOpcode()) {
+NeedToDiagnose = true;
+  }
+  const clang::SourceLocation StartLoc = BinOp->getBeginLoc();
+  const clang::SourceLocation EndLoc = BinOp->getEndLoc().getLocWithOffset(1);
+  Insertions.push_back({StartLoc, EndLoc});
+  addParantheses(dyn_cast(BinOp->getLHS()->IgnoreImpCasts()),
+ BinOp, NeedToDiagnose, Insertions);
+  addParantheses(dyn_cast(BinOp->getRHS()->IgnoreImpCasts()),
+ BinOp, NeedToDiagnose, Insertions);
+}
+
+void MathMissingParenthesesCheck::check(
+const MatchFinder::MatchResult ) {
+  const auto *BinOp = Result.Nodes.getNodeAs("binOp");
+  bool NeedToDiagnose = false;
+  std::vector>
+  Insertions;
+  addParantheses(BinOp, nullptr, NeedToDiagnose, Insertions);
+  const clang::SourceLocation StartLoc = BinOp->getBeginLoc();
+  const clang::SourceLocation EndLoc = BinOp->getEndLoc().getLocWithOffset(1);
+  const clang::SourceRange range(StartLoc, EndLoc);

11happy wrote:

done

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


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

2024-03-24 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,68 @@
+//===--- 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 "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+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);
+}
+
+void addParantheses(
+const BinaryOperator *BinOp, const BinaryOperator *ParentBinOp,
+bool ,
+std::vector>
+) {
+  if (!BinOp)
+return;
+
+  if (ParentBinOp != nullptr &&
+  ParentBinOp->getOpcode() != BinOp->getOpcode()) {
+NeedToDiagnose = true;
+  }
+  const clang::SourceLocation StartLoc = BinOp->getBeginLoc();
+  const clang::SourceLocation EndLoc = BinOp->getEndLoc().getLocWithOffset(1);
+  Insertions.push_back({StartLoc, EndLoc});
+  addParantheses(dyn_cast(BinOp->getLHS()->IgnoreImpCasts()),
+ BinOp, NeedToDiagnose, Insertions);
+  addParantheses(dyn_cast(BinOp->getRHS()->IgnoreImpCasts()),
+ BinOp, NeedToDiagnose, Insertions);
+}
+
+void MathMissingParenthesesCheck::check(
+const MatchFinder::MatchResult ) {
+  const auto *BinOp = Result.Nodes.getNodeAs("binOp");
+  bool NeedToDiagnose = false;
+  std::vector>
+  Insertions;
+  addParantheses(BinOp, nullptr, NeedToDiagnose, Insertions);
+  const clang::SourceLocation StartLoc = BinOp->getBeginLoc();
+  const clang::SourceLocation EndLoc = BinOp->getEndLoc().getLocWithOffset(1);
+  const clang::SourceRange range(StartLoc, EndLoc);
+  if (!Insertions.empty()) {
+Insertions.erase(Insertions.begin());
+  }
+  if (NeedToDiagnose) {
+auto const  = diag(
+StartLoc, "add parantheses to clarify the precedence of operations");
+for (const auto  : Insertions) {
+  Diag << FixItHint::CreateInsertion(Insertion.first, "(");
+  Diag << FixItHint::CreateInsertion(Insertion.second, ")");

11happy wrote:

done

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


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

2024-03-24 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,20 @@
+.. title:: clang-tidy - readability-math-missing-parentheses
+
+readability-math-missing-parentheses
+
+
+Check for mising parantheses in mathematical expressions that involve operators
+of different priorities.
+

11happy wrote:

done

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


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

2024-03-24 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,20 @@
+.. title:: clang-tidy - readability-math-missing-parentheses
+
+readability-math-missing-parentheses
+
+
+Check for mising parantheses in mathematical expressions that involve operators

11happy wrote:

done

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


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

2024-03-24 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,31 @@
+//===--- MathMissingParenthesesCheck.h - clang-tidy -*- C++ 
-*-===//
+//
+// 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
+//
+//===--===//
+
+#ifndef 
LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MATHMISSINGPARENTHESESCHECK_H
+#define 
LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MATHMISSINGPARENTHESESCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::readability {
+
+/// Checks for mathematical expressions that involve operators of different
+/// priorities.
+///
+/// For the user-facing documentation see:
+/// 
http://clang.llvm.org/extra/clang-tidy/checks/readability/math-missing-parentheses.html
+class MathMissingParenthesesCheck : public ClangTidyCheck {
+public:
+  MathMissingParenthesesCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult ) override;

11happy wrote:

will it be necessary to add this currently working fine?

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


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

2024-03-24 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,20 @@
+.. title:: clang-tidy - readability-math-missing-parentheses
+
+readability-math-missing-parentheses
+
+
+Check for mising parantheses in mathematical expressions that involve operators
+of different priorities.
+
+Before:
+
+.. code-block:: c++
+
+  int x = 1 + 2 * 3 - 4 / 5;
+
+
+After:
+
+.. code-block:: c++
+
+  int x = (1 + (2 * 3)) - (4 / 5);

11happy wrote:

done

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


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

2024-03-24 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,31 @@
+//===--- MathMissingParenthesesCheck.h - clang-tidy -*- C++ 
-*-===//
+//
+// 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
+//
+//===--===//
+
+#ifndef 
LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MATHMISSINGPARENTHESESCHECK_H
+#define 
LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MATHMISSINGPARENTHESESCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::readability {
+
+/// Checks for mathematical expressions that involve operators of different
+/// priorities.

11happy wrote:

done

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


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

2024-03-24 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,68 @@
+//===--- 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 "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+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);
+}
+
+void addParantheses(
+const BinaryOperator *BinOp, const BinaryOperator *ParentBinOp,
+bool ,
+std::vector>
+) {
+  if (!BinOp)
+return;
+
+  if (ParentBinOp != nullptr &&
+  ParentBinOp->getOpcode() != BinOp->getOpcode()) {
+NeedToDiagnose = true;
+  }

11happy wrote:

done

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


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

2024-03-24 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,76 @@
+// RUN: %check_clang_tidy %s readability-math-missing-parentheses %t
+
+int foo(){
+return 5;
+}
+
+int bar(){
+return 4;
+}
+
+class fun{
+public:  
+int A;
+double B;
+fun(){
+A = 5;
+B = 5.4;
+}
+};
+
+void f(){
+//CHECK-MESSAGES: :[[@LINE+2]]:13: warning: add parantheses to clarify the 
precedence of operations [readability-math-missing-parentheses]
+//CHECK-FIXES: int a = 1 + (2 * 3);
+int a = 1 + 2 * 3; 
+
+int b = 1 + 2 + 3; // No warning
+
+int c = 1 * 2 * 3; // No warning
+
+//CHECK-MESSAGES: :[[@LINE+2]]:13: warning: add parantheses to clarify the 
precedence of operations [readability-math-missing-parentheses]
+//CHECK-FIXES: int d = (1 + (2 * 3)) - (4 / 5);
+int d = 1 + 2 * 3 - 4 / 5;

11happy wrote:

done

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


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

2024-03-24 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,68 @@
+//===--- 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 "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+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);
+}
+
+void addParantheses(
+const BinaryOperator *BinOp, const BinaryOperator *ParentBinOp,
+bool ,

11happy wrote:

done

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


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

2024-03-24 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,68 @@
+//===--- 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 "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+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);
+}
+
+void addParantheses(
+const BinaryOperator *BinOp, const BinaryOperator *ParentBinOp,
+bool ,
+std::vector>

11happy wrote:

done

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


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

2024-03-24 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/84481

>From 8fdf6306085ed4cf0f77b7e718e374e9f65fedf9 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Fri, 8 Mar 2024 19:02:47 +0530
Subject: [PATCH 1/5] add clang-tidy check readability-math-missing-parentheses

Signed-off-by: 11happy 
---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../MathMissingParenthesesCheck.cpp   | 167 ++
 .../readability/MathMissingParenthesesCheck.h |  31 
 .../readability/ReadabilityTidyModule.cpp |   3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |   6 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../readability/math-missing-parentheses.rst  |  19 ++
 .../readability/math-missing-parentheses.cpp  |  42 +
 8 files changed, 270 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/math-missing-parentheses.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index a6c8cbd8eb448a..0d4fa095501dfb 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -27,6 +27,7 @@ add_clang_library(clangTidyReadabilityModule
   IsolateDeclarationCheck.cpp
   MagicNumbersCheck.cpp
   MakeMemberFunctionConstCheck.cpp
+  MathMissingParenthesesCheck.cpp
   MisleadingIndentationCheck.cpp
   MisplacedArrayIndexCheck.cpp
   NamedParameterCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
new file mode 100644
index 00..d9574a9fb7a476
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
@@ -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 
+#include 
+
+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) {
+  if (c >= 'a' && c <= 'z')
+return true;
+  else if (c >= 'A' && c <= 'Z')
+return true;
+  else if (c >= '0' && c <= '9')
+return true;
+  else if (c == '$')
+return true;
+  else
+return false;
+}
+static bool conditionForNegative(const std::string s, int i,
+ const std::string CurStr) {
+  if (CurStr[0] == '-') {
+if (i == 0) {
+  return true;
+} else {
+  while (s[i - 1] == ' ') {
+i--;
+  }
+  if (!isOperand(s[i - 1])) {
+return true;
+  } else {
+return false;
+  }
+}
+  } else {
+return false;
+  }
+}
+static std::string getOperationOrder(std::string s, std::set ) 
{
+  std::stack StackOne;
+  std::string TempStr = "";
+  for (int i = 0; i < s.length(); i++) {
+std::string CurStr = "";
+CurStr += s[i];
+if (CurStr == " ")
+  continue;
+else {
+  if (isOperand(CurStr[0]) || conditionForNegative(s, i, CurStr)) {
+while (i < s.length() && (isOperand(s[i]) || s[i] == '-')) {
+  if (s[i] == '-') {
+TempStr += "$";
+  } else {
+TempStr += CurStr;
+  }
+  i++;
+  CurStr = s[i];
+}
+TempStr += " ";
+  } else if (CurStr == "(") {
+StackOne.push("(");
+  } else if (CurStr == ")") {
+while (StackOne.top() != "(") {
+  TempStr += StackOne.top();
+  StackOne.pop();
+}
+StackOne.pop();
+  } else {
+while (!StackOne.empty() && 

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

2024-03-20 Thread Bhuminjay Soni via cfe-commits

11happy wrote:

> Not bad, still few issues left, mainly with `getOpcode`.
Thanks for your review!
Update: I will update the PR with the requested changes after 24 as I have mid 
semester exams from 21-24.

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


[clang] fix unnecessary warning when using bitand with boolean operators (PR #81976)

2024-03-16 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/81976

>From 07c73f087a430f5115ecdfec23730c5afcab37f3 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Fri, 16 Feb 2024 14:26:36 +0530
Subject: [PATCH 1/3] fix unnecessary warning when using bitand with boolean

Signed-off-by: 11happy 
---
 clang/lib/Sema/SemaChecking.cpp | 24 ++--
 clang/test/Sema/warn-bitwise-and-bool.c |  4 ++--
 clang/test/Sema/warn-bitwise-or-bool.c  |  4 ++--
 3 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 502b24bcdf8b42..e43892cbf35890 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -16191,12 +16191,24 @@ static void AnalyzeImplicitConversions(
 BO->getRHS()->isKnownToHaveBooleanValue() &&
 BO->getLHS()->HasSideEffects(S.Context) &&
 BO->getRHS()->HasSideEffects(S.Context)) {
-  S.Diag(BO->getBeginLoc(), diag::warn_bitwise_instead_of_logical)
-  << (BO->getOpcode() == BO_And ? "&" : "|") << OrigE->getSourceRange()
-  << FixItHint::CreateReplacement(
- BO->getOperatorLoc(),
- (BO->getOpcode() == BO_And ? "&&" : "||"));
-  S.Diag(BO->getBeginLoc(), diag::note_cast_operand_to_int);
+  clang::SourceManager  = S.getSourceManager();
+  clang::LangOptions LO = S.getLangOpts();
+  clang::SourceLocation BLoc = BO->getOperatorLoc();
+  clang::SourceLocation ELoc =
+  clang::Lexer::getLocForEndOfToken(BLoc, 0, SM, LO);
+  llvm::StringRef SR = clang::Lexer::getSourceText(
+  clang::CharSourceRange::getTokenRange(BLoc, ELoc), SM, LO);
+
+  if (SR.str() == "&" || SR.str() == "|") {
+
+S.Diag(BO->getBeginLoc(), diag::warn_bitwise_instead_of_logical)
+<< (BO->getOpcode() == BO_And ? "&" : "|")
+<< OrigE->getSourceRange()
+<< FixItHint::CreateReplacement(
+   BO->getOperatorLoc(),
+   (BO->getOpcode() == BO_And ? "&&" : "||"));
+S.Diag(BO->getBeginLoc(), diag::note_cast_operand_to_int);
+  }
 }
 
   // For conditional operators, we analyze the arguments as if they
diff --git a/clang/test/Sema/warn-bitwise-and-bool.c 
b/clang/test/Sema/warn-bitwise-and-bool.c
index 6bec1be1abdef6..23e7afcc59f8aa 100644
--- a/clang/test/Sema/warn-bitwise-and-bool.c
+++ b/clang/test/Sema/warn-bitwise-and-bool.c
@@ -45,8 +45,8 @@ void test(boolean a, boolean b, int *p, volatile int *q, int 
i) {
   b = bar() & (i > 4);
   b = (i == 7) & foo();
 #ifdef __cplusplus
-  b = foo() bitand bar(); // expected-warning {{use of bitwise '&' with 
boolean operands}}
-  // expected-note@-1 {{cast one or both operands to 
int to silence this warning}}
+  b = foo() bitand bar(); // Ok, no warning expected
+  
 #endif
 
   if (foo() & bar())  // expected-warning {{use of bitwise '&' with 
boolean operands}}
diff --git a/clang/test/Sema/warn-bitwise-or-bool.c 
b/clang/test/Sema/warn-bitwise-or-bool.c
index ae86790901aac5..e84f59cf8f766a 100644
--- a/clang/test/Sema/warn-bitwise-or-bool.c
+++ b/clang/test/Sema/warn-bitwise-or-bool.c
@@ -45,8 +45,8 @@ void test(boolean a, boolean b, int *p, volatile int *q, int 
i) {
   b = bar() | (i > 4);
   b = (i == 7) | foo();
 #ifdef __cplusplus
-  b = foo() bitor bar();  // expected-warning {{use of bitwise '|' with 
boolean operands}}
-  // expected-note@-1 {{cast one or both operands to 
int to silence this warning}}
+  b = foo() bitor bar();  //Ok, no warning expected
+  
 #endif
 
   if (foo() | bar())  // expected-warning {{use of bitwise '|' with 
boolean operands}}

>From a0ba9e05bb9d0443f386fd57eef3b4e6e2bd1217 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Sat, 16 Mar 2024 16:11:00 +0530
Subject: [PATCH 2/3] add release notes and add tests

Signed-off-by: 11happy 
---
 clang/docs/ReleaseNotes.rst |  5 +
 clang/lib/Sema/SemaChecking.cpp | 16 +---
 clang/test/Sema/warn-bitwise-and-bool.c |  6 ++
 clang/test/Sema/warn-bitwise-or-bool.c  |  4 
 4 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 254e0a9cb72979..177f424abaa73c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -106,6 +106,11 @@ Improvements to Clang's diagnostics
 - Clang now applies syntax highlighting to the code snippets it
   prints.
 
+- Clang now does not warn in cases where bitand operator is 
+  intentionally used with boolean operands, distinguishing it
+  from potential typographical errors or unintended bitwise operations.
+  Fixes (`#77601 `)
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/lib/Sema/SemaChecking.cpp 

[clang] fix unnecessary warning when using bitand with boolean operators (PR #81976)

2024-03-16 Thread Bhuminjay Soni via cfe-commits

11happy wrote:

currently the macro definition for both `&` and `bitand` is not giving any 
warning however should the `&` one warn? or current working is correct 
behaviour as expected?

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


[clang] fix unnecessary warning when using bitand with boolean operators (PR #81976)

2024-03-16 Thread Bhuminjay Soni via cfe-commits


@@ -45,8 +45,8 @@ void test(boolean a, boolean b, int *p, volatile int *q, int 
i) {
   b = bar() & (i > 4);
   b = (i == 7) & foo();
 #ifdef __cplusplus
-  b = foo() bitand bar(); // expected-warning {{use of bitwise '&' with 
boolean operands}}
-  // expected-note@-1 {{cast one or both operands to 
int to silence this warning}}
+  b = foo() bitand bar(); // Ok, no warning expected
+  

11happy wrote:

done

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


[clang] fix unnecessary warning when using bitand with boolean operators (PR #81976)

2024-03-16 Thread Bhuminjay Soni via cfe-commits


@@ -16191,12 +16191,24 @@ static void AnalyzeImplicitConversions(
 BO->getRHS()->isKnownToHaveBooleanValue() &&
 BO->getLHS()->HasSideEffects(S.Context) &&
 BO->getRHS()->HasSideEffects(S.Context)) {
-  S.Diag(BO->getBeginLoc(), diag::warn_bitwise_instead_of_logical)
-  << (BO->getOpcode() == BO_And ? "&" : "|") << OrigE->getSourceRange()
-  << FixItHint::CreateReplacement(
- BO->getOperatorLoc(),
- (BO->getOpcode() == BO_And ? "&&" : "||"));
-  S.Diag(BO->getBeginLoc(), diag::note_cast_operand_to_int);
+  clang::SourceManager  = S.getSourceManager();
+  clang::LangOptions LO = S.getLangOpts();
+  clang::SourceLocation BLoc = BO->getOperatorLoc();
+  clang::SourceLocation ELoc =
+  clang::Lexer::getLocForEndOfToken(BLoc, 0, SM, LO);
+  llvm::StringRef SR = clang::Lexer::getSourceText(
+  clang::CharSourceRange::getTokenRange(BLoc, ELoc), SM, LO);
+
+  if (SR.str() == "&" || SR.str() == "|") {
+
+S.Diag(BO->getBeginLoc(), diag::warn_bitwise_instead_of_logical)

11happy wrote:

done

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


[clang] fix unnecessary warning when using bitand with boolean operators (PR #81976)

2024-03-16 Thread Bhuminjay Soni via cfe-commits


@@ -16191,12 +16191,24 @@ static void AnalyzeImplicitConversions(
 BO->getRHS()->isKnownToHaveBooleanValue() &&
 BO->getLHS()->HasSideEffects(S.Context) &&
 BO->getRHS()->HasSideEffects(S.Context)) {
-  S.Diag(BO->getBeginLoc(), diag::warn_bitwise_instead_of_logical)
-  << (BO->getOpcode() == BO_And ? "&" : "|") << OrigE->getSourceRange()
-  << FixItHint::CreateReplacement(
- BO->getOperatorLoc(),
- (BO->getOpcode() == BO_And ? "&&" : "||"));
-  S.Diag(BO->getBeginLoc(), diag::note_cast_operand_to_int);
+  clang::SourceManager  = S.getSourceManager();
+  clang::LangOptions LO = S.getLangOpts();
+  clang::SourceLocation BLoc = BO->getOperatorLoc();
+  clang::SourceLocation ELoc =
+  clang::Lexer::getLocForEndOfToken(BLoc, 0, SM, LO);
+  llvm::StringRef SR = clang::Lexer::getSourceText(
+  clang::CharSourceRange::getTokenRange(BLoc, ELoc), SM, LO);

11happy wrote:

done

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


[clang] fix unnecessary warning when using bitand with boolean operators (PR #81976)

2024-03-16 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/81976

>From 07c73f087a430f5115ecdfec23730c5afcab37f3 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Fri, 16 Feb 2024 14:26:36 +0530
Subject: [PATCH 1/2] fix unnecessary warning when using bitand with boolean

Signed-off-by: 11happy 
---
 clang/lib/Sema/SemaChecking.cpp | 24 ++--
 clang/test/Sema/warn-bitwise-and-bool.c |  4 ++--
 clang/test/Sema/warn-bitwise-or-bool.c  |  4 ++--
 3 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 502b24bcdf8b42..e43892cbf35890 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -16191,12 +16191,24 @@ static void AnalyzeImplicitConversions(
 BO->getRHS()->isKnownToHaveBooleanValue() &&
 BO->getLHS()->HasSideEffects(S.Context) &&
 BO->getRHS()->HasSideEffects(S.Context)) {
-  S.Diag(BO->getBeginLoc(), diag::warn_bitwise_instead_of_logical)
-  << (BO->getOpcode() == BO_And ? "&" : "|") << OrigE->getSourceRange()
-  << FixItHint::CreateReplacement(
- BO->getOperatorLoc(),
- (BO->getOpcode() == BO_And ? "&&" : "||"));
-  S.Diag(BO->getBeginLoc(), diag::note_cast_operand_to_int);
+  clang::SourceManager  = S.getSourceManager();
+  clang::LangOptions LO = S.getLangOpts();
+  clang::SourceLocation BLoc = BO->getOperatorLoc();
+  clang::SourceLocation ELoc =
+  clang::Lexer::getLocForEndOfToken(BLoc, 0, SM, LO);
+  llvm::StringRef SR = clang::Lexer::getSourceText(
+  clang::CharSourceRange::getTokenRange(BLoc, ELoc), SM, LO);
+
+  if (SR.str() == "&" || SR.str() == "|") {
+
+S.Diag(BO->getBeginLoc(), diag::warn_bitwise_instead_of_logical)
+<< (BO->getOpcode() == BO_And ? "&" : "|")
+<< OrigE->getSourceRange()
+<< FixItHint::CreateReplacement(
+   BO->getOperatorLoc(),
+   (BO->getOpcode() == BO_And ? "&&" : "||"));
+S.Diag(BO->getBeginLoc(), diag::note_cast_operand_to_int);
+  }
 }
 
   // For conditional operators, we analyze the arguments as if they
diff --git a/clang/test/Sema/warn-bitwise-and-bool.c 
b/clang/test/Sema/warn-bitwise-and-bool.c
index 6bec1be1abdef6..23e7afcc59f8aa 100644
--- a/clang/test/Sema/warn-bitwise-and-bool.c
+++ b/clang/test/Sema/warn-bitwise-and-bool.c
@@ -45,8 +45,8 @@ void test(boolean a, boolean b, int *p, volatile int *q, int 
i) {
   b = bar() & (i > 4);
   b = (i == 7) & foo();
 #ifdef __cplusplus
-  b = foo() bitand bar(); // expected-warning {{use of bitwise '&' with 
boolean operands}}
-  // expected-note@-1 {{cast one or both operands to 
int to silence this warning}}
+  b = foo() bitand bar(); // Ok, no warning expected
+  
 #endif
 
   if (foo() & bar())  // expected-warning {{use of bitwise '&' with 
boolean operands}}
diff --git a/clang/test/Sema/warn-bitwise-or-bool.c 
b/clang/test/Sema/warn-bitwise-or-bool.c
index ae86790901aac5..e84f59cf8f766a 100644
--- a/clang/test/Sema/warn-bitwise-or-bool.c
+++ b/clang/test/Sema/warn-bitwise-or-bool.c
@@ -45,8 +45,8 @@ void test(boolean a, boolean b, int *p, volatile int *q, int 
i) {
   b = bar() | (i > 4);
   b = (i == 7) | foo();
 #ifdef __cplusplus
-  b = foo() bitor bar();  // expected-warning {{use of bitwise '|' with 
boolean operands}}
-  // expected-note@-1 {{cast one or both operands to 
int to silence this warning}}
+  b = foo() bitor bar();  //Ok, no warning expected
+  
 #endif
 
   if (foo() | bar())  // expected-warning {{use of bitwise '|' with 
boolean operands}}

>From a0ba9e05bb9d0443f386fd57eef3b4e6e2bd1217 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Sat, 16 Mar 2024 16:11:00 +0530
Subject: [PATCH 2/2] add release notes and add tests

Signed-off-by: 11happy 
---
 clang/docs/ReleaseNotes.rst |  5 +
 clang/lib/Sema/SemaChecking.cpp | 16 +---
 clang/test/Sema/warn-bitwise-and-bool.c |  6 ++
 clang/test/Sema/warn-bitwise-or-bool.c  |  4 
 4 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 254e0a9cb72979..177f424abaa73c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -106,6 +106,11 @@ Improvements to Clang's diagnostics
 - Clang now applies syntax highlighting to the code snippets it
   prints.
 
+- Clang now does not warn in cases where bitand operator is 
+  intentionally used with boolean operands, distinguishing it
+  from potential typographical errors or unintended bitwise operations.
+  Fixes (`#77601 `)
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/lib/Sema/SemaChecking.cpp 

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

2024-03-15 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,68 @@
+//===--- 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 "clang/Rewrite/Core/Rewriter.h"
+#include 
+using namespace std;
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+void MathMissingParenthesesCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(binaryOperator(unless(hasParent(binaryOperator())),
+hasDescendant(binaryOperator()))

11happy wrote:

I agree with you should be restricted to `+-*/%`

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


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

2024-03-15 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,68 @@
+//===--- 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"

11happy wrote:

done

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


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

2024-03-15 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,68 @@
+//===--- 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"

11happy wrote:

done

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


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

2024-03-15 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,68 @@
+//===--- 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 "clang/Rewrite/Core/Rewriter.h"
+#include 
+using namespace std;

11happy wrote:

done

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


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

2024-03-15 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,68 @@
+//===--- 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 "clang/Rewrite/Core/Rewriter.h"
+#include 
+using namespace std;
+
+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);
+}
+
+void addParantheses(const BinaryOperator *BinOp, clang::Rewriter ,
+const BinaryOperator *ParentBinOp, bool ) {
+  if (!BinOp)
+return;
+  if (ParentBinOp != nullptr &&
+  ParentBinOp->getOpcode() != BinOp->getOpcode()) {
+NeedToDiagnose = true;
+  }
+  clang::SourceLocation StartLoc = BinOp->getLHS()->getBeginLoc();
+  clang::SourceLocation EndLoc = BinOp->getRHS()->getEndLoc();

11happy wrote:

done

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


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

2024-03-15 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,68 @@
+//===--- 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 "clang/Rewrite/Core/Rewriter.h"
+#include 
+using namespace std;
+
+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);
+}
+
+void addParantheses(const BinaryOperator *BinOp, clang::Rewriter ,
+const BinaryOperator *ParentBinOp, bool ) {
+  if (!BinOp)
+return;
+  if (ParentBinOp != nullptr &&
+  ParentBinOp->getOpcode() != BinOp->getOpcode()) {
+NeedToDiagnose = true;
+  }
+  clang::SourceLocation StartLoc = BinOp->getLHS()->getBeginLoc();
+  clang::SourceLocation EndLoc = BinOp->getRHS()->getEndLoc();
+  Rewrite.InsertText(StartLoc, "(");
+  Rewrite.InsertTextAfterToken(EndLoc, ")");
+  addParantheses(dyn_cast(BinOp->getLHS()), Rewrite, BinOp,
+ NeedToDiagnose);
+  addParantheses(dyn_cast(BinOp->getRHS()), Rewrite, BinOp,
+ NeedToDiagnose);

11happy wrote:

done

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


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

2024-03-15 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,74 @@
+// RUN: %check_clang_tidy %s readability-math-missing-parentheses %t
+
+// FIXME: Add something that triggers the check here.
+
+int foo(){
+return 5;
+}
+
+int bar(){
+return 4;
+}
+
+class fun{
+public:  
+int A;
+double B;
+fun(){
+A = 5;
+B = 5.4;
+}
+};
+
+void f(){
+//CHECK-MESSAGES: :[[@LINE+2]]:13: warning: add parantheses to clarify the 
precedence of operations [readability-math-missing-parentheses]
+//CHECK-FIXES: int a = (1 + (2 * 3));
+int a = 1 + 2 * 3; 
+
+int b = 1 + 2 + 3; // No warning
+
+int c = 1 * 2 * 3; // No warning
+
+//CHECK-MESSAGES: :[[@LINE+2]]:13: warning: add parantheses to clarify the 
precedence of operations [readability-math-missing-parentheses]
+//CHECK-FIXES: int d = ((1 + (2 * 3)) - (4 / 5));
+int d = 1 + 2 * 3 - 4 / 5;
+
+//CHECK-MESSAGES: :[[@LINE+2]]:13: warning: add parantheses to clarify the 
precedence of operations [readability-math-missing-parentheses]
+//CHECK-FIXES: int e = ((1 & (2 + 3)) | (4 * 5));
+int e = 1 & 2 + 3 | 4 * 5;
+
+//CHECK-MESSAGES: :[[@LINE+2]]:13: warning: add parantheses to clarify the 
precedence of operations [readability-math-missing-parentheses]
+//CHECK-FIXES: int f = ((1 * -2) + 4);
+int f = 1 * -2 + 4;
+
+//CHECK-MESSAGES: :[[@LINE+2]]:13: warning: add parantheses to clarify the 
precedence of operations [readability-math-missing-parentheses]
+//CHECK-FIXES: int g = 1 * 2) * 3) + 4) + 5);
+int g = 1 * 2 * 3 + 4 + 5;
+
+// CHECK-MESSAGES: :[[@LINE+2]]:13: warning: add parantheses to clarify 
the precedence of operations [readability-math-missing-parentheses]
+// CHECK-FIXES: int h = ((120 & (2 + 3)) | (22 * 5));
+int h = 120 & 2 + 3 | 22 * 5;
+
+int i = 1 & 2 & 3; // No warning
+
+int j = 1 | 2 | 3; // No warning
+
+int k = 1 ^ 2 ^ 3; // No warning
+
+// CHECK-MESSAGES: :[[@LINE+2]]:13: warning: add parantheses to clarify 
the precedence of operations [readability-math-missing-parentheses]
+// CHECK-FIXES: int l = ((1 + 2) ^ 3);
+int l = 1 + 2 ^ 3;
+
+// CHECK-MESSAGES: :[[@LINE+2]]:13: warning: add parantheses to clarify 
the precedence of operations [readability-math-missing-parentheses]
+// CHECK-FIXES: int m = ((2 * foo()) + bar());
+int m = 2 * foo() + bar();
+
+// CHECK-MESSAGES: :[[@LINE+2]]:13: warning: add parantheses to clarify 
the precedence of operations [readability-math-missing-parentheses]
+// CHECK-FIXES: int n = ((1.05 * foo()) + double(bar()));
+int n = 1.05 * foo() + double(bar());
+
+// CHECK-MESSAGES: :[[@LINE+3]]:13: warning: add parantheses to clarify 
the precedence of operations [readability-math-missing-parentheses]
+// CHECK-FIXES: int o = (1 + (obj.A * 3)) + obj.B;
+fun obj;
+int o = 1 + obj.A * 3 + obj.B; 
+}

11happy wrote:

done

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


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

2024-03-15 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,68 @@
+//===--- 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 "clang/Rewrite/Core/Rewriter.h"
+#include 
+using namespace std;
+
+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);
+}
+
+void addParantheses(const BinaryOperator *BinOp, clang::Rewriter ,
+const BinaryOperator *ParentBinOp, bool ) {
+  if (!BinOp)
+return;
+  if (ParentBinOp != nullptr &&
+  ParentBinOp->getOpcode() != BinOp->getOpcode()) {
+NeedToDiagnose = true;
+  }
+  clang::SourceLocation StartLoc = BinOp->getLHS()->getBeginLoc();
+  clang::SourceLocation EndLoc = BinOp->getRHS()->getEndLoc();
+  Rewrite.InsertText(StartLoc, "(");
+  Rewrite.InsertTextAfterToken(EndLoc, ")");
+  addParantheses(dyn_cast(BinOp->getLHS()), Rewrite, BinOp,
+ NeedToDiagnose);
+  addParantheses(dyn_cast(BinOp->getRHS()), Rewrite, BinOp,
+ NeedToDiagnose);
+}
+
+void MathMissingParenthesesCheck::check(
+const MatchFinder::MatchResult ) {
+  const auto *BinOp = Result.Nodes.getNodeAs("binOp");
+  if (!BinOp)
+return;

11happy wrote:

done

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


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

2024-03-15 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,19 @@
+.. title:: clang-tidy - readability-math-missing-parentheses
+
+readability-math-missing-parentheses
+
+
+Checks for mathematical expressions that involve operators of different 
priorities.

11happy wrote:

done

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


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

2024-03-15 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,68 @@
+//===--- 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 "clang/Rewrite/Core/Rewriter.h"
+#include 
+using namespace std;
+
+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);
+}
+
+void addParantheses(const BinaryOperator *BinOp, clang::Rewriter ,
+const BinaryOperator *ParentBinOp, bool ) {
+  if (!BinOp)
+return;
+  if (ParentBinOp != nullptr &&
+  ParentBinOp->getOpcode() != BinOp->getOpcode()) {
+NeedToDiagnose = true;
+  }
+  clang::SourceLocation StartLoc = BinOp->getLHS()->getBeginLoc();
+  clang::SourceLocation EndLoc = BinOp->getRHS()->getEndLoc();
+  Rewrite.InsertText(StartLoc, "(");
+  Rewrite.InsertTextAfterToken(EndLoc, ")");
+  addParantheses(dyn_cast(BinOp->getLHS()), Rewrite, BinOp,
+ NeedToDiagnose);
+  addParantheses(dyn_cast(BinOp->getRHS()), Rewrite, BinOp,
+ NeedToDiagnose);
+}
+
+void MathMissingParenthesesCheck::check(
+const MatchFinder::MatchResult ) {
+  const auto *BinOp = Result.Nodes.getNodeAs("binOp");
+  if (!BinOp)
+return;
+  clang::SourceManager  = *Result.SourceManager;
+  clang::LangOptions LO = Result.Context->getLangOpts();

11happy wrote:

done

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


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

2024-03-15 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,68 @@
+//===--- 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 "clang/Rewrite/Core/Rewriter.h"
+#include 
+using namespace std;
+
+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);
+}
+
+void addParantheses(const BinaryOperator *BinOp, clang::Rewriter ,
+const BinaryOperator *ParentBinOp, bool ) {
+  if (!BinOp)
+return;
+  if (ParentBinOp != nullptr &&
+  ParentBinOp->getOpcode() != BinOp->getOpcode()) {
+NeedToDiagnose = true;
+  }
+  clang::SourceLocation StartLoc = BinOp->getLHS()->getBeginLoc();
+  clang::SourceLocation EndLoc = BinOp->getRHS()->getEndLoc();
+  Rewrite.InsertText(StartLoc, "(");
+  Rewrite.InsertTextAfterToken(EndLoc, ")");
+  addParantheses(dyn_cast(BinOp->getLHS()), Rewrite, BinOp,
+ NeedToDiagnose);
+  addParantheses(dyn_cast(BinOp->getRHS()), Rewrite, BinOp,
+ NeedToDiagnose);
+}
+
+void MathMissingParenthesesCheck::check(
+const MatchFinder::MatchResult ) {
+  const auto *BinOp = Result.Nodes.getNodeAs("binOp");
+  if (!BinOp)
+return;
+  clang::SourceManager  = *Result.SourceManager;
+  clang::LangOptions LO = Result.Context->getLangOpts();
+  Rewriter Rewrite(SM, LO);
+  bool NeedToDiagnose = false;
+  addParantheses(BinOp, Rewrite, nullptr, NeedToDiagnose);
+  clang::SourceLocation StartLoc = BinOp->getLHS()->getBeginLoc();
+  clang::SourceLocation EndLoc =
+  BinOp->getRHS()->getEndLoc().getLocWithOffset(1);

11happy wrote:

done

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


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

2024-03-15 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,74 @@
+// RUN: %check_clang_tidy %s readability-math-missing-parentheses %t
+
+// FIXME: Add something that triggers the check here.

11happy wrote:

done

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


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

2024-03-15 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,68 @@
+//===--- 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 "clang/Rewrite/Core/Rewriter.h"
+#include 
+using namespace std;
+
+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);
+}
+
+void addParantheses(const BinaryOperator *BinOp, clang::Rewriter ,
+const BinaryOperator *ParentBinOp, bool ) {
+  if (!BinOp)
+return;
+  if (ParentBinOp != nullptr &&
+  ParentBinOp->getOpcode() != BinOp->getOpcode()) {
+NeedToDiagnose = true;
+  }
+  clang::SourceLocation StartLoc = BinOp->getLHS()->getBeginLoc();
+  clang::SourceLocation EndLoc = BinOp->getRHS()->getEndLoc();
+  Rewrite.InsertText(StartLoc, "(");
+  Rewrite.InsertTextAfterToken(EndLoc, ")");
+  addParantheses(dyn_cast(BinOp->getLHS()), Rewrite, BinOp,
+ NeedToDiagnose);
+  addParantheses(dyn_cast(BinOp->getRHS()), Rewrite, BinOp,
+ NeedToDiagnose);
+}
+
+void MathMissingParenthesesCheck::check(
+const MatchFinder::MatchResult ) {
+  const auto *BinOp = Result.Nodes.getNodeAs("binOp");
+  if (!BinOp)
+return;
+  clang::SourceManager  = *Result.SourceManager;
+  clang::LangOptions LO = Result.Context->getLangOpts();
+  Rewriter Rewrite(SM, LO);
+  bool NeedToDiagnose = false;
+  addParantheses(BinOp, Rewrite, nullptr, NeedToDiagnose);
+  clang::SourceLocation StartLoc = BinOp->getLHS()->getBeginLoc();
+  clang::SourceLocation EndLoc =
+  BinOp->getRHS()->getEndLoc().getLocWithOffset(1);
+  clang::SourceRange range(StartLoc, EndLoc);
+  std::string NewExpression = Rewrite.getRewrittenText(range);
+  if (NeedToDiagnose) {
+diag(StartLoc, "add parantheses to clarify the precedence of operations")
+<< FixItHint::CreateReplacement(range, NewExpression);

11happy wrote:

done

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


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

2024-03-15 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/84481

>From 8fdf6306085ed4cf0f77b7e718e374e9f65fedf9 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Fri, 8 Mar 2024 19:02:47 +0530
Subject: [PATCH 1/4] add clang-tidy check readability-math-missing-parentheses

Signed-off-by: 11happy 
---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../MathMissingParenthesesCheck.cpp   | 167 ++
 .../readability/MathMissingParenthesesCheck.h |  31 
 .../readability/ReadabilityTidyModule.cpp |   3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |   6 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../readability/math-missing-parentheses.rst  |  19 ++
 .../readability/math-missing-parentheses.cpp  |  42 +
 8 files changed, 270 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/math-missing-parentheses.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index a6c8cbd8eb448a..0d4fa095501dfb 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -27,6 +27,7 @@ add_clang_library(clangTidyReadabilityModule
   IsolateDeclarationCheck.cpp
   MagicNumbersCheck.cpp
   MakeMemberFunctionConstCheck.cpp
+  MathMissingParenthesesCheck.cpp
   MisleadingIndentationCheck.cpp
   MisplacedArrayIndexCheck.cpp
   NamedParameterCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
new file mode 100644
index 00..d9574a9fb7a476
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
@@ -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 
+#include 
+
+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) {
+  if (c >= 'a' && c <= 'z')
+return true;
+  else if (c >= 'A' && c <= 'Z')
+return true;
+  else if (c >= '0' && c <= '9')
+return true;
+  else if (c == '$')
+return true;
+  else
+return false;
+}
+static bool conditionForNegative(const std::string s, int i,
+ const std::string CurStr) {
+  if (CurStr[0] == '-') {
+if (i == 0) {
+  return true;
+} else {
+  while (s[i - 1] == ' ') {
+i--;
+  }
+  if (!isOperand(s[i - 1])) {
+return true;
+  } else {
+return false;
+  }
+}
+  } else {
+return false;
+  }
+}
+static std::string getOperationOrder(std::string s, std::set ) 
{
+  std::stack StackOne;
+  std::string TempStr = "";
+  for (int i = 0; i < s.length(); i++) {
+std::string CurStr = "";
+CurStr += s[i];
+if (CurStr == " ")
+  continue;
+else {
+  if (isOperand(CurStr[0]) || conditionForNegative(s, i, CurStr)) {
+while (i < s.length() && (isOperand(s[i]) || s[i] == '-')) {
+  if (s[i] == '-') {
+TempStr += "$";
+  } else {
+TempStr += CurStr;
+  }
+  i++;
+  CurStr = s[i];
+}
+TempStr += " ";
+  } else if (CurStr == "(") {
+StackOne.push("(");
+  } else if (CurStr == ")") {
+while (StackOne.top() != "(") {
+  TempStr += StackOne.top();
+  StackOne.pop();
+}
+StackOne.pop();
+  } else {
+while (!StackOne.empty() && 

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

2024-03-15 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/84481

>From 8fdf6306085ed4cf0f77b7e718e374e9f65fedf9 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Fri, 8 Mar 2024 19:02:47 +0530
Subject: [PATCH 1/3] add clang-tidy check readability-math-missing-parentheses

Signed-off-by: 11happy 
---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../MathMissingParenthesesCheck.cpp   | 167 ++
 .../readability/MathMissingParenthesesCheck.h |  31 
 .../readability/ReadabilityTidyModule.cpp |   3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |   6 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../readability/math-missing-parentheses.rst  |  19 ++
 .../readability/math-missing-parentheses.cpp  |  42 +
 8 files changed, 270 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/math-missing-parentheses.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index a6c8cbd8eb448a..0d4fa095501dfb 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -27,6 +27,7 @@ add_clang_library(clangTidyReadabilityModule
   IsolateDeclarationCheck.cpp
   MagicNumbersCheck.cpp
   MakeMemberFunctionConstCheck.cpp
+  MathMissingParenthesesCheck.cpp
   MisleadingIndentationCheck.cpp
   MisplacedArrayIndexCheck.cpp
   NamedParameterCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
new file mode 100644
index 00..d9574a9fb7a476
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
@@ -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 
+#include 
+
+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) {
+  if (c >= 'a' && c <= 'z')
+return true;
+  else if (c >= 'A' && c <= 'Z')
+return true;
+  else if (c >= '0' && c <= '9')
+return true;
+  else if (c == '$')
+return true;
+  else
+return false;
+}
+static bool conditionForNegative(const std::string s, int i,
+ const std::string CurStr) {
+  if (CurStr[0] == '-') {
+if (i == 0) {
+  return true;
+} else {
+  while (s[i - 1] == ' ') {
+i--;
+  }
+  if (!isOperand(s[i - 1])) {
+return true;
+  } else {
+return false;
+  }
+}
+  } else {
+return false;
+  }
+}
+static std::string getOperationOrder(std::string s, std::set ) 
{
+  std::stack StackOne;
+  std::string TempStr = "";
+  for (int i = 0; i < s.length(); i++) {
+std::string CurStr = "";
+CurStr += s[i];
+if (CurStr == " ")
+  continue;
+else {
+  if (isOperand(CurStr[0]) || conditionForNegative(s, i, CurStr)) {
+while (i < s.length() && (isOperand(s[i]) || s[i] == '-')) {
+  if (s[i] == '-') {
+TempStr += "$";
+  } else {
+TempStr += CurStr;
+  }
+  i++;
+  CurStr = s[i];
+}
+TempStr += " ";
+  } else if (CurStr == "(") {
+StackOne.push("(");
+  } else if (CurStr == ")") {
+while (StackOne.top() != "(") {
+  TempStr += StackOne.top();
+  StackOne.pop();
+}
+StackOne.pop();
+  } else {
+while (!StackOne.empty() && 

[clang] Diagnose misuse of the cleanup attribute (PR #80040)

2024-03-13 Thread Bhuminjay Soni via cfe-commits

11happy wrote:

> It looks like it passed on your last commit but you have a conflict now which 
> you need to resolve.
> 
> Can you merge or do you need help with that?

I tried to resolve the merge conflict but the resolve editor is just stuck on 
loading on Vs Code may be as it have 2 conflicts?

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


[clang] [clang-tools-extra] [llvm] Diagnose misuse of the cleanup attribute (PR #80040)

2024-03-13 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/80040

error: too big or took too long to generate
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2024-03-11 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,42 @@
+// RUN: %check_clang_tidy %s readability-math-missing-parentheses %t
+
+// FIXME: Add something that triggers the check here.
+void f(){
+//CHECK-MESSAGES: :[[@LINE+2]]:13: warning: add parantheses to clarify the 
precedence of operations [readability-math-missing-parentheses]
+//CHECK-FIXES: int a = 1 + (2 * 3);
+int a = 1 + 2 * 3; 
+
+int b = 1 + 2 + 3; // No warning
+
+int c = 1 * 2 * 3; // No warning
+
+//CHECK-MESSAGES: :[[@LINE+2]]:13: warning: add parantheses to clarify the 
precedence of operations [readability-math-missing-parentheses]
+//CHECK-FIXES: int d = (1 + (2 * 3)) - (4 / 5);
+int d = 1 + 2 * 3 - 4 / 5;
+
+//CHECK-MESSAGES: :[[@LINE+2]]:13: warning: add parantheses to clarify the 
precedence of operations [readability-math-missing-parentheses]
+//CHECK-FIXES: int e = (1 & (2 + 3)) | (4 * 5);
+int e = 1 & 2 + 3 | 4 * 5;
+
+//CHECK-MESSAGES: :[[@LINE+2]]:13: warning: add parantheses to clarify the 
precedence of operations [readability-math-missing-parentheses]
+//CHECK-FIXES: int f = (1 * -2) + 4;
+int f = 1 * -2 + 4;
+
+//CHECK-MESSAGES: :[[@LINE+2]]:13: warning: add parantheses to clarify the 
precedence of operations [readability-math-missing-parentheses]
+//CHECK-FIXES: int g = (((1 * 2) * 3) + 4) + 5;
+int g = 1 * 2 * 3 + 4 + 5;
+
+// CHECK-MESSAGES: :[[@LINE+2]]:13: warning: add parantheses to clarify 
the precedence of operations [readability-math-missing-parentheses]
+// CHECK-FIXES: int h = (120 & (2 + 3)) | (22 * 5);
+int h = 120 & 2 + 3 | 22 * 5;
+
+int i = 1 & 2 & 3; // No warning
+
+int j = 1 | 2 | 3; // No warning
+
+int k = 1 ^ 2 ^ 3; // No warning
+
+// CHECK-MESSAGES: :[[@LINE+2]]:13: warning: add parantheses to clarify 
the precedence of operations [readability-math-missing-parentheses]
+// CHECK-FIXES: int l = (1 + 2) ^ 3;
+int l = 1 + 2 ^ 3;
+}

11happy wrote:

added tests

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


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

2024-03-11 Thread Bhuminjay Soni via cfe-commits


@@ -100,6 +100,12 @@ Improvements to clang-tidy
 New checks
 ^^
 
+- New :doc:`readability-math-missing-parentheses
+  ` check.
+
+  Checks for mathematical expressions that involve operators
+  of different priorities.

11happy wrote:

done

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


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

2024-03-11 Thread Bhuminjay Soni via cfe-commits


@@ -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 
+#include 
+
+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) {
+  if (c >= 'a' && c <= 'z')
+return true;
+  else if (c >= 'A' && c <= 'Z')
+return true;
+  else if (c >= '0' && c <= '9')
+return true;
+  else if (c == '$')
+return true;
+  else
+return false;
+}
+static bool conditionForNegative(const std::string s, int i,
+ const std::string CurStr) {
+  if (CurStr[0] == '-') {
+if (i == 0) {
+  return true;
+} else {
+  while (s[i - 1] == ' ') {
+i--;
+  }
+  if (!isOperand(s[i - 1])) {
+return true;
+  } else {
+return false;
+  }
+}
+  } else {
+return false;
+  }
+}
+static std::string getOperationOrder(std::string s, std::set ) 
{
+  std::stack StackOne;
+  std::string TempStr = "";
+  for (int i = 0; i < s.length(); i++) {
+std::string CurStr = "";
+CurStr += s[i];
+if (CurStr == " ")
+  continue;
+else {
+  if (isOperand(CurStr[0]) || conditionForNegative(s, i, CurStr)) {
+while (i < s.length() && (isOperand(s[i]) || s[i] == '-')) {
+  if (s[i] == '-') {
+TempStr += "$";
+  } else {
+TempStr += CurStr;
+  }
+  i++;
+  CurStr = s[i];
+}
+TempStr += " ";
+  } else if (CurStr == "(") {
+StackOne.push("(");
+  } else if (CurStr == ")") {
+while (StackOne.top() != "(") {
+  TempStr += StackOne.top();
+  StackOne.pop();
+}
+StackOne.pop();
+  } else {
+while (!StackOne.empty() && precedenceCheck(CurStr[0]) <=
+precedenceCheck((StackOne.top())[0])) {
+  TempStr += StackOne.top();
+  StackOne.pop();
+}
+StackOne.push(CurStr);
+  }
+}
+  }
+  while (!StackOne.empty()) {
+TempStr += StackOne.top();
+StackOne.pop();
+  }
+  std::stack StackTwo;
+  for (int i = 0; i < TempStr.length(); i++) {
+if (TempStr[i] == ' ')
+  continue;
+else if (isOperand(TempStr[i])) {
+  std::string CurStr = "";
+  while (i < TempStr.length() && isOperand(TempStr[i])) {
+if (TempStr[i] == '$') {
+  CurStr += "-";
+} else {
+  CurStr += TempStr[i];
+}
+i++;
+  }
+  StackTwo.push(CurStr);
+} else {
+  std::string OperandOne = StackTwo.top();
+  StackTwo.pop();
+  std::string OperandTwo = StackTwo.top();
+  StackTwo.pop();
+  Operators.insert(TempStr[i]);
+  StackTwo.push("(" + OperandTwo + " " + TempStr[i] + " " + OperandOne +
+")");
+}
+  }
+  return StackTwo.top();
+}
+void MathMissingParenthesesCheck::check(
+const MatchFinder::MatchResult ) {
+  const auto *BinOp = Result.Nodes.getNodeAs("binOp");
+  if (!BinOp)
+return;
+  clang::SourceManager  = *Result.SourceManager;
+  clang::LangOptions LO = Result.Context->getLangOpts();
+  clang::CharSourceRange Range =
+  clang::CharSourceRange::getTokenRange(BinOp->getSourceRange());
+  std::string Expression = clang::Lexer::getSourceText(Range, SM, LO).str();

11happy wrote:

done

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


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

2024-03-11 Thread Bhuminjay Soni via cfe-commits


@@ -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 
+#include 
+
+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) {
+  if (c >= 'a' && c <= 'z')
+return true;
+  else if (c >= 'A' && c <= 'Z')
+return true;
+  else if (c >= '0' && c <= '9')
+return true;
+  else if (c == '$')
+return true;
+  else
+return false;
+}
+static bool conditionForNegative(const std::string s, int i,
+ const std::string CurStr) {
+  if (CurStr[0] == '-') {
+if (i == 0) {
+  return true;
+} else {
+  while (s[i - 1] == ' ') {
+i--;
+  }
+  if (!isOperand(s[i - 1])) {
+return true;
+  } else {
+return false;
+  }
+}
+  } else {
+return false;
+  }
+}
+static std::string getOperationOrder(std::string s, std::set ) 
{

11happy wrote:

done

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


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

2024-03-11 Thread Bhuminjay Soni via cfe-commits


@@ -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 
+#include 
+
+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;
+}

11happy wrote:

done

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


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

2024-03-11 Thread Bhuminjay Soni via cfe-commits


@@ -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 
+#include 
+
+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
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add new flag -Wreturn-mismatch (PR #82872)

2024-03-11 Thread Bhuminjay Soni via cfe-commits

11happy wrote:

Are there any more changes for this PR required from my end?
Thank you

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


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

2024-03-11 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/84481

>From 8fdf6306085ed4cf0f77b7e718e374e9f65fedf9 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Fri, 8 Mar 2024 19:02:47 +0530
Subject: [PATCH 1/2] add clang-tidy check readability-math-missing-parentheses

Signed-off-by: 11happy 
---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../MathMissingParenthesesCheck.cpp   | 167 ++
 .../readability/MathMissingParenthesesCheck.h |  31 
 .../readability/ReadabilityTidyModule.cpp |   3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |   6 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../readability/math-missing-parentheses.rst  |  19 ++
 .../readability/math-missing-parentheses.cpp  |  42 +
 8 files changed, 270 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/math-missing-parentheses.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index a6c8cbd8eb448a..0d4fa095501dfb 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -27,6 +27,7 @@ add_clang_library(clangTidyReadabilityModule
   IsolateDeclarationCheck.cpp
   MagicNumbersCheck.cpp
   MakeMemberFunctionConstCheck.cpp
+  MathMissingParenthesesCheck.cpp
   MisleadingIndentationCheck.cpp
   MisplacedArrayIndexCheck.cpp
   NamedParameterCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
new file mode 100644
index 00..d9574a9fb7a476
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
@@ -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 
+#include 
+
+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) {
+  if (c >= 'a' && c <= 'z')
+return true;
+  else if (c >= 'A' && c <= 'Z')
+return true;
+  else if (c >= '0' && c <= '9')
+return true;
+  else if (c == '$')
+return true;
+  else
+return false;
+}
+static bool conditionForNegative(const std::string s, int i,
+ const std::string CurStr) {
+  if (CurStr[0] == '-') {
+if (i == 0) {
+  return true;
+} else {
+  while (s[i - 1] == ' ') {
+i--;
+  }
+  if (!isOperand(s[i - 1])) {
+return true;
+  } else {
+return false;
+  }
+}
+  } else {
+return false;
+  }
+}
+static std::string getOperationOrder(std::string s, std::set ) 
{
+  std::stack StackOne;
+  std::string TempStr = "";
+  for (int i = 0; i < s.length(); i++) {
+std::string CurStr = "";
+CurStr += s[i];
+if (CurStr == " ")
+  continue;
+else {
+  if (isOperand(CurStr[0]) || conditionForNegative(s, i, CurStr)) {
+while (i < s.length() && (isOperand(s[i]) || s[i] == '-')) {
+  if (s[i] == '-') {
+TempStr += "$";
+  } else {
+TempStr += CurStr;
+  }
+  i++;
+  CurStr = s[i];
+}
+TempStr += " ";
+  } else if (CurStr == "(") {
+StackOne.push("(");
+  } else if (CurStr == ")") {
+while (StackOne.top() != "(") {
+  TempStr += StackOne.top();
+  StackOne.pop();
+}
+StackOne.pop();
+  } else {
+while (!StackOne.empty() && 

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

2024-03-08 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy created 
https://github.com/llvm/llvm-project/pull/84481

**Overview:**
This pull request fixes #80850  where author suggests adding a readability 
check to detect missing parentheses around mathematical expressions when 
operators of different priorities are used. 

**Testing:**
- Tested the updated code.
- Verified that other functionalities remain unaffected.
![Screenshot from 2024-03-08 
19-06-11](https://github.com/llvm/llvm-project/assets/76656712/fa59a019-b357-4b55-b317-3b16014c31d7)



**Dependencies:**
- No dependencies on other pull requests.

**CC:**
-  @PiotrZSL 


>From 8fdf6306085ed4cf0f77b7e718e374e9f65fedf9 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Fri, 8 Mar 2024 19:02:47 +0530
Subject: [PATCH] add clang-tidy check readability-math-missing-parentheses

Signed-off-by: 11happy 
---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../MathMissingParenthesesCheck.cpp   | 167 ++
 .../readability/MathMissingParenthesesCheck.h |  31 
 .../readability/ReadabilityTidyModule.cpp |   3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |   6 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../readability/math-missing-parentheses.rst  |  19 ++
 .../readability/math-missing-parentheses.cpp  |  42 +
 8 files changed, 270 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/math-missing-parentheses.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index a6c8cbd8eb448a..0d4fa095501dfb 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -27,6 +27,7 @@ add_clang_library(clangTidyReadabilityModule
   IsolateDeclarationCheck.cpp
   MagicNumbersCheck.cpp
   MakeMemberFunctionConstCheck.cpp
+  MathMissingParenthesesCheck.cpp
   MisleadingIndentationCheck.cpp
   MisplacedArrayIndexCheck.cpp
   NamedParameterCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
new file mode 100644
index 00..d9574a9fb7a476
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
@@ -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 
+#include 
+
+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) {
+  if (c >= 'a' && c <= 'z')
+return true;
+  else if (c >= 'A' && c <= 'Z')
+return true;
+  else if (c >= '0' && c <= '9')
+return true;
+  else if (c == '$')
+return true;
+  else
+return false;
+}
+static bool conditionForNegative(const std::string s, int i,
+ const std::string CurStr) {
+  if (CurStr[0] == '-') {
+if (i == 0) {
+  return true;
+} else {
+  while (s[i - 1] == ' ') {
+i--;
+  }
+  if (!isOperand(s[i - 1])) {
+return true;
+  } else {
+return false;
+  }
+}
+  } else {
+return false;
+  }
+}
+static std::string getOperationOrder(std::string s, std::set ) 
{
+  std::stack StackOne;
+  std::string TempStr = "";
+  for (int i = 0; i < s.length(); i++) {
+std::string CurStr = "";
+CurStr += s[i];
+if (CurStr == " ")
+  continue;
+else {
+  if (isOperand(CurStr[0]) || conditionForNegative(s, i, CurStr)) {
+while (i < s.length() && (isOperand(s[i]) || s[i] == '-')) 

[clang] Add new flag -Wreturn-mismatch (PR #82872)

2024-03-05 Thread Bhuminjay Soni via cfe-commits


@@ -94,6 +94,7 @@ Deprecated Compiler Flags
 
 Modified Compiler Flags
 ---
+- The `-Wreturn-type` flag has been modified to split some of its 
functionality into `-Wreturn-mismatch` flag.

11happy wrote:

done

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


[clang] Add new flag -Wreturn-mismatch (PR #82872)

2024-03-05 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -Wno-return-type -Wreturn-mismatch -fsyntax-only -verify %s
+
+// Test that -Wreturn-mismatch is enabled and -Wreturn-type is disabled.
+
+int foo(void) __attribute__((noreturn));
+int bar(void);
+
+void test1() {

11happy wrote:

done

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


[clang] Add new flag -Wreturn-mismatch (PR #82872)

2024-03-05 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -Wno-return-type -Wreturn-mismatch -fsyntax-only -verify %s
+
+// Test that -Wreturn-mismatch is enabled and -Wreturn-type is disabled.
+
+int foo(void) __attribute__((noreturn));
+int bar(void);
+
+void test1() {
+  return 1; // expected-warning{{void function 'test1' should not return a 
value}}
+}
+
+int test2() { 
+return; // expected-warning{{non-void function 'test2' should return a 
value}}
+} 
+
+int test3() { 
+// no-warning

11happy wrote:

done

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


[clang] Add new flag -Wreturn-mismatch (PR #82872)

2024-03-05 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -Wno-return-type -Wreturn-mismatch -fsyntax-only -verify %s
+
+// Test that -Wreturn-mismatch is enabled and -Wreturn-type is disabled.
+
+int foo(void) __attribute__((noreturn));
+int bar(void);
+
+void test1() {
+  return 1; // expected-warning{{void function 'test1' should not return a 
value}}
+}
+
+int test2() { 
+return; // expected-warning{{non-void function 'test2' should return a 
value}}

11happy wrote:

done

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


[clang] Add new flag -Wreturn-mismatch (PR #82872)

2024-03-05 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -Wno-return-type -Wreturn-mismatch -fsyntax-only -verify %s
+
+// Test that -Wreturn-mismatch is enabled and -Wreturn-type is disabled.
+
+int foo(void) __attribute__((noreturn));
+int bar(void);
+
+void test1() {
+  return 1; // expected-warning{{void function 'test1' should not return a 
value}}

11happy wrote:

done

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


[clang] Add new flag -Wreturn-mismatch (PR #82872)

2024-03-05 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -Wno-return-type -Wreturn-mismatch -fsyntax-only -verify %s

11happy wrote:

done

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


[clang] Add new flag -Wreturn-mismatch (PR #82872)

2024-03-05 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -Wreturn-type -Wno-return-mismatch -fsyntax-only -verify %s

11happy wrote:

done

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


[clang] Add new flag -Wreturn-mismatch (PR #82872)

2024-03-05 Thread Bhuminjay Soni via cfe-commits


@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -Wno-return-type -Wreturn-mismatch -fsyntax-only -verify %s
+
+// Test that -Wreturn-mismatch is enabled and -Wreturn-type is disabled.
+
+int foo(void) __attribute__((noreturn));
+int bar(void);
+
+void test1() {
+  return 1; // expected-warning{{void function 'test1' should not return a 
value}}
+}
+
+int test2() { 
+return; // expected-warning{{non-void function 'test2' should return a 
value}}
+} 
+
+int test3() { 
+// no-warning
+} 
+
+int test4() {
+(void)(bar() || foo()); // no-warning
+} 
+
+void test5() {
+} // no-warning
+
+int test6() {
+  return 0; // no-warning
+}

11happy wrote:

done

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


[clang] Add new flag -Wreturn-mismatch (PR #82872)

2024-03-05 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/82872

>From 557ee75d60f0fdb4dd2b353c819b4f22f71b46d7 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Sat, 24 Feb 2024 13:50:30 +0530
Subject: [PATCH 1/3] add new flag -Wreturn-mismatch

Signed-off-by: 11happy 
---
 clang/include/clang/Basic/DiagnosticGroups.td| 1 +
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 6 +++---
 clang/test/Analysis/null-deref-ps.c  | 4 ++--
 clang/test/CodeGen/statements.c  | 2 +-
 clang/test/CodeGen/volatile-1.c  | 2 +-
 clang/test/Sema/return-silent.c  | 2 +-
 clang/test/Sema/return.c | 2 +-
 7 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 6765721ae7002c..7a786a24083583 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -607,6 +607,7 @@ def RedundantMove : DiagGroup<"redundant-move">;
 def Register : DiagGroup<"register", [DeprecatedRegister]>;
 def ReturnTypeCLinkage : DiagGroup<"return-type-c-linkage">;
 def ReturnType : DiagGroup<"return-type", [ReturnTypeCLinkage]>;
+def ReturnMismatch : DiagGroup<"return-mismatch">;
 def BindToTemporaryCopy : DiagGroup<"bind-to-temporary-copy",
 [CXX98CompatBindToTemporaryCopy]>;
 def SelfAssignmentField : DiagGroup<"self-assign-field">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 24d32cb87c89e2..7057b4b2123671 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10212,14 +10212,14 @@ def warn_second_parameter_to_va_arg_never_compatible 
: Warning<
 
 def warn_return_missing_expr : Warning<
   "non-void %select{function|method}1 %0 should return a value">, DefaultError,
-  InGroup;
+  InGroup;
 def ext_return_missing_expr : ExtWarn<
   "non-void %select{function|method}1 %0 should return a value">, DefaultError,
-  InGroup;
+  InGroup;
 def ext_return_has_expr : ExtWarn<
   "%select{void function|void method|constructor|destructor}1 %0 "
   "should not return a value">,
-  DefaultError, InGroup;
+  DefaultError, InGroup;
 def ext_return_has_void_expr : Extension<
   "void %select{function|method|block}1 %0 should not return void expression">;
 def err_return_init_list : Error<
diff --git a/clang/test/Analysis/null-deref-ps.c 
b/clang/test/Analysis/null-deref-ps.c
index d80de15c05a3fe..2682ad02ee37f9 100644
--- a/clang/test/Analysis/null-deref-ps.c
+++ b/clang/test/Analysis/null-deref-ps.c
@@ -1,5 +1,5 @@
-// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion 
-Wno-strict-prototypes -Wno-tautological-constant-compare 
-Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core 
-std=gnu99 -analyzer-purge=none -verify %s -Wno-error=return-type
-// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion 
-Wno-strict-prototypes -Wno-tautological-constant-compare 
-Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core 
-std=gnu99 -verify %s -Wno-error=return-type
+// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion 
-Wno-strict-prototypes -Wno-tautological-constant-compare 
-Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core 
-std=gnu99 -analyzer-purge=none -verify %s -Wno-error=return-type 
-Wno-error=return-mismatch
+// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion 
-Wno-strict-prototypes -Wno-tautological-constant-compare 
-Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core 
-std=gnu99 -verify %s -Wno-error=return-type -Wno-error=return-mismatch
 
 typedef unsigned uintptr_t;
 
diff --git a/clang/test/CodeGen/statements.c b/clang/test/CodeGen/statements.c
index 07ae075d6d807e..76f4f254dfd1dc 100644
--- a/clang/test/CodeGen/statements.c
+++ b/clang/test/CodeGen/statements.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -Wno-error=return-type -Wno-error=int-conversion %s 
-emit-llvm-only
+// RUN: %clang_cc1 -Wno-error=return-type -Wno-error=return-mismatch 
-Wno-error=int-conversion %s -emit-llvm-only
 // REQUIRES: LP64
 
 // Mismatched type between return and function result.
diff --git a/clang/test/CodeGen/volatile-1.c b/clang/test/CodeGen/volatile-1.c
index cd919c78989fe2..80c40d21cf593e 100644
--- a/clang/test/CodeGen/volatile-1.c
+++ b/clang/test/CodeGen/volatile-1.c
@@ -1,5 +1,5 @@
 // XFAIL: target=aarch64-pc-windows-msvc
-// RUN: %clang_cc1 -Wno-return-type -Wno-unused-value -emit-llvm %s -w -o - | 
FileCheck %s
+// RUN: %clang_cc1 -Wno-return-type -Wno-return-mismatch -Wno-unused-value 
-emit-llvm %s -w -o - | FileCheck %s
 
 // CHECK: @i = {{(dso_local )?}}global [[INT:i[0-9]+]] 0
 volatile int i, j, k;
diff --git 

[clang] Add new flag -Wreturn-mismatch (PR #82872)

2024-03-05 Thread Bhuminjay Soni via cfe-commits


@@ -1,5 +1,5 @@
 // XFAIL: target=aarch64-pc-windows-msvc
-// RUN: %clang_cc1 -Wno-return-type -Wno-unused-value -emit-llvm %s -w -o - | 
FileCheck %s
+// RUN: %clang_cc1 -Wno-return-type -Wno-return-mismatch -Wno-unused-value 
-emit-llvm %s -w -o - | FileCheck %s

11happy wrote:

same as above. done.

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


[clang] Add new flag -Wreturn-mismatch (PR #82872)

2024-03-05 Thread Bhuminjay Soni via cfe-commits


@@ -1,5 +1,5 @@
-// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion 
-Wno-strict-prototypes -Wno-tautological-constant-compare 
-Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core 
-std=gnu99 -analyzer-purge=none -verify %s -Wno-error=return-type
-// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion 
-Wno-strict-prototypes -Wno-tautological-constant-compare 
-Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core 
-std=gnu99 -verify %s -Wno-error=return-type
+// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion 
-Wno-strict-prototypes -Wno-tautological-constant-compare 
-Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core 
-std=gnu99 -analyzer-purge=none -verify %s -Wno-error=return-type 
-Wno-error=return-mismatch

11happy wrote:

sorry earlier I created a new seperate return-mismatch & tests were failing 
corrected it.

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


[clang] Add new flag -Wreturn-mismatch (PR #82872)

2024-03-05 Thread Bhuminjay Soni via cfe-commits


@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -triple x86_64-apple-darwin9 %s -fsyntax-only 
-Wignored-qualifiers -Wno-error=return-type -Wno-error=implicit-int -verify 
-fblocks -Wno-unreachable-code -Wno-unused-value -Wno-strict-prototypes
+// RUN: %clang_cc1 -triple x86_64-apple-darwin9 %s -fsyntax-only 
-Wignored-qualifiers -Wno-error=return-type -Wno-error=return-mismatch 
-Wno-error=implicit-int -verify -fblocks -Wno-unreachable-code 
-Wno-unused-value -Wno-strict-prototypes
 
 // clang emits the following warning by default.
 // With GCC, -pedantic, -Wreturn-type or -Wall are required to produce the

11happy wrote:

done 

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


[clang] Add new flag -Wreturn-mismatch (PR #82872)

2024-03-05 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy updated 
https://github.com/llvm/llvm-project/pull/82872

>From 557ee75d60f0fdb4dd2b353c819b4f22f71b46d7 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Sat, 24 Feb 2024 13:50:30 +0530
Subject: [PATCH 1/2] add new flag -Wreturn-mismatch

Signed-off-by: 11happy 
---
 clang/include/clang/Basic/DiagnosticGroups.td| 1 +
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 6 +++---
 clang/test/Analysis/null-deref-ps.c  | 4 ++--
 clang/test/CodeGen/statements.c  | 2 +-
 clang/test/CodeGen/volatile-1.c  | 2 +-
 clang/test/Sema/return-silent.c  | 2 +-
 clang/test/Sema/return.c | 2 +-
 7 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 6765721ae7002c..7a786a24083583 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -607,6 +607,7 @@ def RedundantMove : DiagGroup<"redundant-move">;
 def Register : DiagGroup<"register", [DeprecatedRegister]>;
 def ReturnTypeCLinkage : DiagGroup<"return-type-c-linkage">;
 def ReturnType : DiagGroup<"return-type", [ReturnTypeCLinkage]>;
+def ReturnMismatch : DiagGroup<"return-mismatch">;
 def BindToTemporaryCopy : DiagGroup<"bind-to-temporary-copy",
 [CXX98CompatBindToTemporaryCopy]>;
 def SelfAssignmentField : DiagGroup<"self-assign-field">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 24d32cb87c89e2..7057b4b2123671 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10212,14 +10212,14 @@ def warn_second_parameter_to_va_arg_never_compatible 
: Warning<
 
 def warn_return_missing_expr : Warning<
   "non-void %select{function|method}1 %0 should return a value">, DefaultError,
-  InGroup;
+  InGroup;
 def ext_return_missing_expr : ExtWarn<
   "non-void %select{function|method}1 %0 should return a value">, DefaultError,
-  InGroup;
+  InGroup;
 def ext_return_has_expr : ExtWarn<
   "%select{void function|void method|constructor|destructor}1 %0 "
   "should not return a value">,
-  DefaultError, InGroup;
+  DefaultError, InGroup;
 def ext_return_has_void_expr : Extension<
   "void %select{function|method|block}1 %0 should not return void expression">;
 def err_return_init_list : Error<
diff --git a/clang/test/Analysis/null-deref-ps.c 
b/clang/test/Analysis/null-deref-ps.c
index d80de15c05a3fe..2682ad02ee37f9 100644
--- a/clang/test/Analysis/null-deref-ps.c
+++ b/clang/test/Analysis/null-deref-ps.c
@@ -1,5 +1,5 @@
-// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion 
-Wno-strict-prototypes -Wno-tautological-constant-compare 
-Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core 
-std=gnu99 -analyzer-purge=none -verify %s -Wno-error=return-type
-// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion 
-Wno-strict-prototypes -Wno-tautological-constant-compare 
-Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core 
-std=gnu99 -verify %s -Wno-error=return-type
+// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion 
-Wno-strict-prototypes -Wno-tautological-constant-compare 
-Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core 
-std=gnu99 -analyzer-purge=none -verify %s -Wno-error=return-type 
-Wno-error=return-mismatch
+// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion 
-Wno-strict-prototypes -Wno-tautological-constant-compare 
-Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core 
-std=gnu99 -verify %s -Wno-error=return-type -Wno-error=return-mismatch
 
 typedef unsigned uintptr_t;
 
diff --git a/clang/test/CodeGen/statements.c b/clang/test/CodeGen/statements.c
index 07ae075d6d807e..76f4f254dfd1dc 100644
--- a/clang/test/CodeGen/statements.c
+++ b/clang/test/CodeGen/statements.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -Wno-error=return-type -Wno-error=int-conversion %s 
-emit-llvm-only
+// RUN: %clang_cc1 -Wno-error=return-type -Wno-error=return-mismatch 
-Wno-error=int-conversion %s -emit-llvm-only
 // REQUIRES: LP64
 
 // Mismatched type between return and function result.
diff --git a/clang/test/CodeGen/volatile-1.c b/clang/test/CodeGen/volatile-1.c
index cd919c78989fe2..80c40d21cf593e 100644
--- a/clang/test/CodeGen/volatile-1.c
+++ b/clang/test/CodeGen/volatile-1.c
@@ -1,5 +1,5 @@
 // XFAIL: target=aarch64-pc-windows-msvc
-// RUN: %clang_cc1 -Wno-return-type -Wno-unused-value -emit-llvm %s -w -o - | 
FileCheck %s
+// RUN: %clang_cc1 -Wno-return-type -Wno-return-mismatch -Wno-unused-value 
-emit-llvm %s -w -o - | FileCheck %s
 
 // CHECK: @i = {{(dso_local )?}}global [[INT:i[0-9]+]] 0
 volatile int i, j, k;
diff --git 

[clang] Add new flag -Wreturn-mismatch (PR #82872)

2024-03-02 Thread Bhuminjay Soni via cfe-commits

11happy wrote:

Its a Humble Ping!

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


[clang] fix unnecessary warning when using bitand with boolean operators (PR #81976)

2024-02-24 Thread Bhuminjay Soni via cfe-commits

11happy wrote:

Humble Ping!

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


[clang] Add new flag -Wreturn-mismatch (PR #82872)

2024-02-24 Thread Bhuminjay Soni via cfe-commits

https://github.com/11happy created 
https://github.com/llvm/llvm-project/pull/82872

**Overview:**
This pull request fixes #72116 where a new flag is introduced for compatibility 
with GCC 14, the functionality of -Wreturn-type is modified to split some of 
its behaviors into -Wreturn-mismatch 

**Testing:**
- Tested the updated code.
- Verified that other functionalities remain unaffected.
![Screenshot from 2024-02-24 
13-56-24](https://github.com/llvm/llvm-project/assets/76656712/3a0303c1-22e5-4b74-a674-6b772752ed2e)
- Sample working:
```
int foo(int x){
  x = 1;
  return;
}
```
![Screenshot from 2024-02-24 
14-01-21](https://github.com/llvm/llvm-project/assets/76656712/2352588b-17be-40b5-869e-66fe3af83a95)




**Dependencies:**
- No dependencies on other pull requests.



>From 557ee75d60f0fdb4dd2b353c819b4f22f71b46d7 Mon Sep 17 00:00:00 2001
From: 11happy 
Date: Sat, 24 Feb 2024 13:50:30 +0530
Subject: [PATCH] add new flag -Wreturn-mismatch

Signed-off-by: 11happy 
---
 clang/include/clang/Basic/DiagnosticGroups.td| 1 +
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 6 +++---
 clang/test/Analysis/null-deref-ps.c  | 4 ++--
 clang/test/CodeGen/statements.c  | 2 +-
 clang/test/CodeGen/volatile-1.c  | 2 +-
 clang/test/Sema/return-silent.c  | 2 +-
 clang/test/Sema/return.c | 2 +-
 7 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 6765721ae7002c..7a786a24083583 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -607,6 +607,7 @@ def RedundantMove : DiagGroup<"redundant-move">;
 def Register : DiagGroup<"register", [DeprecatedRegister]>;
 def ReturnTypeCLinkage : DiagGroup<"return-type-c-linkage">;
 def ReturnType : DiagGroup<"return-type", [ReturnTypeCLinkage]>;
+def ReturnMismatch : DiagGroup<"return-mismatch">;
 def BindToTemporaryCopy : DiagGroup<"bind-to-temporary-copy",
 [CXX98CompatBindToTemporaryCopy]>;
 def SelfAssignmentField : DiagGroup<"self-assign-field">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 24d32cb87c89e2..7057b4b2123671 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10212,14 +10212,14 @@ def warn_second_parameter_to_va_arg_never_compatible 
: Warning<
 
 def warn_return_missing_expr : Warning<
   "non-void %select{function|method}1 %0 should return a value">, DefaultError,
-  InGroup;
+  InGroup;
 def ext_return_missing_expr : ExtWarn<
   "non-void %select{function|method}1 %0 should return a value">, DefaultError,
-  InGroup;
+  InGroup;
 def ext_return_has_expr : ExtWarn<
   "%select{void function|void method|constructor|destructor}1 %0 "
   "should not return a value">,
-  DefaultError, InGroup;
+  DefaultError, InGroup;
 def ext_return_has_void_expr : Extension<
   "void %select{function|method|block}1 %0 should not return void expression">;
 def err_return_init_list : Error<
diff --git a/clang/test/Analysis/null-deref-ps.c 
b/clang/test/Analysis/null-deref-ps.c
index d80de15c05a3fe..2682ad02ee37f9 100644
--- a/clang/test/Analysis/null-deref-ps.c
+++ b/clang/test/Analysis/null-deref-ps.c
@@ -1,5 +1,5 @@
-// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion 
-Wno-strict-prototypes -Wno-tautological-constant-compare 
-Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core 
-std=gnu99 -analyzer-purge=none -verify %s -Wno-error=return-type
-// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion 
-Wno-strict-prototypes -Wno-tautological-constant-compare 
-Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core 
-std=gnu99 -verify %s -Wno-error=return-type
+// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion 
-Wno-strict-prototypes -Wno-tautological-constant-compare 
-Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core 
-std=gnu99 -analyzer-purge=none -verify %s -Wno-error=return-type 
-Wno-error=return-mismatch
+// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion 
-Wno-strict-prototypes -Wno-tautological-constant-compare 
-Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core 
-std=gnu99 -verify %s -Wno-error=return-type -Wno-error=return-mismatch
 
 typedef unsigned uintptr_t;
 
diff --git a/clang/test/CodeGen/statements.c b/clang/test/CodeGen/statements.c
index 07ae075d6d807e..76f4f254dfd1dc 100644
--- a/clang/test/CodeGen/statements.c
+++ b/clang/test/CodeGen/statements.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -Wno-error=return-type -Wno-error=int-conversion %s 
-emit-llvm-only
+// RUN: %clang_cc1 -Wno-error=return-type -Wno-error=return-mismatch 

  1   2   3   >