aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/test/clang-tidy/checkers/readability
diff options
context:
space:
mode:
authorBhuminjay Soni <Soni5Happy@gmail.com>2024-04-25 22:49:59 +0530
committerGitHub <noreply@github.com>2024-04-25 19:19:59 +0200
commit8dc7db7a24633f55ef28f2ab4b379386a34505f8 (patch)
tree6e24646a040237f09585686c15cc76190be7ccda /clang-tools-extra/test/clang-tidy/checkers/readability
parentd94aeb507d71d72f4153b4c87c77fcb5187b3e9a (diff)
downloadllvm-8dc7db7a24633f55ef28f2ab4b379386a34505f8.zip
llvm-8dc7db7a24633f55ef28f2ab4b379386a34505f8.tar.gz
llvm-8dc7db7a24633f55ef28f2ab4b379386a34505f8.tar.bz2
[clang-tidy] Add clang-tidy check readability-math-missing-parentheses (#84481)
This commit closes #80850 where author suggests adding a readability check to detect missing parentheses around mathematical expressions when operators of different priorities are used. Signed-off-by: 11happy <soni5happy@gmail.com>
Diffstat (limited to 'clang-tools-extra/test/clang-tidy/checkers/readability')
-rw-r--r--clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp120
1 files changed, 120 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp
new file mode 100644
index 0000000..edbe2e1
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp
@@ -0,0 +1,120 @@
+// RUN: %check_clang_tidy %s readability-math-missing-parentheses %t
+
+#define MACRO_AND &
+#define MACRO_ADD +
+#define MACRO_OR |
+#define MACRO_MULTIPLY *
+#define MACRO_XOR ^
+#define MACRO_SUBTRACT -
+#define MACRO_DIVIDE /
+
+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 explicitly specify the order of operations [readability-math-missing-parentheses]
+ //CHECK-FIXES: int a = 1 + (2 * 3);
+ int a = 1 + 2 * 3;
+
+ int a_negative = 1 + (2 * 3); // No warning
+
+ 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 explicitly specify the order of operations [readability-math-missing-parentheses]
+ //CHECK-MESSAGES: :[[@LINE+2]]:25: warning: '/' has higher precedence than '-'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
+ //CHECK-FIXES: int d = 1 + (2 * 3) - (4 / 5);
+ int d = 1 + 2 * 3 - 4 / 5;
+
+ int d_negative = 1 + (2 * 3) - (4 / 5); // No warning
+
+ //CHECK-MESSAGES: :[[@LINE+4]]:13: warning: '&' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
+ //CHECK-MESSAGES: :[[@LINE+3]]:17: warning: '+' has higher precedence than '&'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
+ //CHECK-MESSAGES: :[[@LINE+2]]:25: warning: '*' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
+ //CHECK-FIXES: int e = (1 & (2 + 3)) | (4 * 5);
+ int e = 1 & 2 + 3 | 4 * 5;
+
+ int e_negative = (1 & (2 + 3)) | (4 * 5); // No warning
+
+ //CHECK-MESSAGES: :[[@LINE+2]]:13: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
+ //CHECK-FIXES: int f = (1 * -2) + 4;
+ int f = 1 * -2 + 4;
+
+ int f_negative = (1 * -2) + 4; // No warning
+
+ //CHECK-MESSAGES: :[[@LINE+2]]:13: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
+ //CHECK-FIXES: int g = (1 * 2 * 3) + 4 + 5;
+ int g = 1 * 2 * 3 + 4 + 5;
+
+ int g_negative = (1 * 2 * 3) + 4 + 5; // No warning
+
+ //CHECK-MESSAGES: :[[@LINE+4]]:13: warning: '&' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
+ //CHECK-MESSAGES: :[[@LINE+3]]:19: warning: '+' has higher precedence than '&'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
+ //CHECK-MESSAGES: :[[@LINE+2]]:27: warning: '*' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
+ //CHECK-FIXES: int h = (120 & (2 + 3)) | (22 * 5);
+ int h = 120 & 2 + 3 | 22 * 5;
+
+ int h_negative = (120 & (2 + 3)) | (22 * 5); // No warning
+
+ 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: '+' has higher precedence than '^'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
+ //CHECK-FIXES: int l = (1 + 2) ^ 3;
+ int l = 1 + 2 ^ 3;
+
+ int l_negative = (1 + 2) ^ 3; // No warning
+
+ //CHECK-MESSAGES: :[[@LINE+2]]:13: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
+ //CHECK-FIXES: int m = (2 * foo()) + bar();
+ int m = 2 * foo() + bar();
+
+ int m_negative = (2 * foo()) + bar(); // No warning
+
+ //CHECK-MESSAGES: :[[@LINE+2]]:13: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
+ //CHECK-FIXES: int n = (1.05 * foo()) + double(bar());
+ int n = 1.05 * foo() + double(bar());
+
+ int n_negative = (1.05 * foo()) + double(bar()); // No warning
+
+ //CHECK-MESSAGES: :[[@LINE+3]]:17: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order 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;
+
+ int o_negative = 1 + (obj.A * 3) + obj.B; // No warning
+
+ //CHECK-MESSAGES: :[[@LINE+2]]:18: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
+ //CHECK-FIXES: int p = 1U + (2 * 3);
+ int p = 1U + 2 * 3;
+
+ int p_negative = 1U + (2 * 3); // No warning
+
+ //CHECK-MESSAGES: :[[@LINE+7]]:13: warning: '+' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
+ //CHECK-MESSAGES: :[[@LINE+6]]:25: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
+ //CHECK-MESSAGES: :[[@LINE+5]]:53: warning: '&' has higher precedence than '^'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
+ //CHECK-MESSAGES: :[[@LINE+4]]:53: warning: '^' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
+ //CHECK-MESSAGES: :[[@LINE+3]]:77: warning: '-' has higher precedence than '^'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
+ //CHECK-MESSAGES: :[[@LINE+2]]:94: warning: '/' has higher precedence than '-'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
+ //CHECK-FIXES: int q = (1 MACRO_ADD (2 MACRO_MULTIPLY 3)) MACRO_OR ((4 MACRO_AND 5) MACRO_XOR (6 MACRO_SUBTRACT (7 MACRO_DIVIDE 8)));
+ int q = 1 MACRO_ADD 2 MACRO_MULTIPLY 3 MACRO_OR 4 MACRO_AND 5 MACRO_XOR 6 MACRO_SUBTRACT 7 MACRO_DIVIDE 8; // No warning
+}