[clang-tools-extra] dcde8fd - [clang-tidy] bugprone-infinite-loop: Fix false positives with volatile addresses.
Artem Dergachev via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 7 15:10:57 PDT 2021
Author: Artem Dergachev
Date: 2021-09-07T15:10:46-07:00
New Revision: dcde8fdeeb3ebda6fe6a23d933fbe5caee01c088
URL: https://github.com/llvm/llvm-project/commit/dcde8fdeeb3ebda6fe6a23d933fbe5caee01c088
DIFF: https://github.com/llvm/llvm-project/commit/dcde8fdeeb3ebda6fe6a23d933fbe5caee01c088.diff
LOG: [clang-tidy] bugprone-infinite-loop: Fix false positives with volatile addresses.
Low-level code may occasionally deal with direct access by concrete addresses
such as 0x1234. Values at these addresses act like globals: they can change
at any time. They typically wear volatile qualifiers.
Suppress all warnings on loops with conditions that involve casting anything to
a pointer-to-...-pointer-to-volatile type.
The closely related bugprone-redundant-branch-condition check
doesn't seem to be affected. Add a test just in case.
Differential Revision: https://reviews.llvm.org/D108808
Added:
Modified:
clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp
clang-tools-extra/test/clang-tidy/checkers/bugprone-redundant-branch-condition.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
index 8ac7f8eb28aee..99775b2400458 100644
--- a/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
@@ -65,6 +65,17 @@ static bool isVarThatIsPossiblyChanged(const Decl *Func, const Stmt *LoopStmt,
ObjCIvarRefExpr, ObjCPropertyRefExpr, ObjCMessageExpr>(Cond)) {
// FIXME: Handle MemberExpr.
return true;
+ } else if (const auto *CE = dyn_cast<CastExpr>(Cond)) {
+ QualType T = CE->getType();
+ while (true) {
+ if (T.isVolatileQualified())
+ return true;
+
+ if (!T->isAnyPointerType() && !T->isReferenceType())
+ break;
+
+ T = T->getPointeeType();
+ }
}
return false;
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp
index 6afde606481c1..ed50b5c7d74ea 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp
@@ -591,3 +591,34 @@ void test_structured_bindings_bad() {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (x) are updated in the loop body [bugprone-infinite-loop]
}
}
+
+void test_volatile_cast() {
+ // This is a no-op cast. Clang ignores the qualifier, we should too.
+ for (int i = 0; (volatile int)i < 10;) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (i) are updated in the loop body [bugprone-infinite-loop]
+ }
+}
+
+void test_volatile_concrete_address(int i, int size) {
+ // No warning. The value behind the volatile concrete address
+ // is beyond our control. It may change at any time.
+ for (; *((volatile int *)0x1234) < size;) {
+ }
+
+ for (; *((volatile int *)(0x1234 + i)) < size;) {
+ }
+
+ for (; **((volatile int **)0x1234) < size;) {
+ }
+
+ volatile int *x = (volatile int *)0x1234;
+ for (; *x < 10;) {
+ }
+
+ // FIXME: This one should probably also be suppressed.
+ // Whatever the developer is doing here, they can do that again anywhere else
+ // which basically makes it a global.
+ for (; *(int *)0x1234 < size;) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (size) are updated in the loop body [bugprone-infinite-loop]
+ }
+}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone-redundant-branch-condition.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone-redundant-branch-condition.cpp
index 9b84406e70e03..40994b0ff884e 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone-redundant-branch-condition.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone-redundant-branch-condition.cpp
@@ -1387,3 +1387,13 @@ void relational_operator_reversed() {
}
}
}
+
+void volatile_concrete_address() {
+ // No warning. The value behind the volatile concrete address
+ // is beyond our control. It may change at any time.
+ if (*(volatile int *)0x1234) {
+ if (*(volatile int *)0x1234) {
+ doSomething();
+ }
+ }
+}
More information about the cfe-commits
mailing list