Skip to content

Commit 54b78f3

Browse files
author
Adam Balogh
committed
[clang-tidy] New bugprone-infinite-loop check for detecting obvious infinite loops
Finding infinite loops is well-known to be impossible (halting problem). However, it is possible to detect some obvious infinite loops, for example, if the loop condition is not changed. Detecting such loops is beneficial since the tests will hang on programs containing infinite loops so testing-time detection may be costly in large systems. Obvious cases are where the programmer forgets to increment/decrement the counter or increments/decrements the wrong variable. Differential Revision: https://reviews.llvm.org/D64736 llvm-svn: 372693
1 parent ef06dd4 commit 54b78f3

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "ForwardingReferenceOverloadCheck.h"
2424
#include "InaccurateEraseCheck.h"
2525
#include "IncorrectRoundingsCheck.h"
26+
#include "InfiniteLoopCheck.h"
2627
#include "IntegerDivisionCheck.h"
2728
#include "LambdaFunctionNameCheck.h"
2829
#include "MacroParenthesesCheck.h"
@@ -88,6 +89,8 @@ class BugproneModule : public ClangTidyModule {
8889
"bugprone-inaccurate-erase");
8990
CheckFactories.registerCheck<IncorrectRoundingsCheck>(
9091
"bugprone-incorrect-roundings");
92+
CheckFactories.registerCheck<InfiniteLoopCheck>(
93+
"bugprone-infinite-loop");
9194
CheckFactories.registerCheck<IntegerDivisionCheck>(
9295
"bugprone-integer-division");
9396
CheckFactories.registerCheck<LambdaFunctionNameCheck>(

clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ add_clang_library(clangTidyBugproneModule
1515
ForwardingReferenceOverloadCheck.cpp
1616
InaccurateEraseCheck.cpp
1717
IncorrectRoundingsCheck.cpp
18+
InfiniteLoopCheck.cpp
1819
IntegerDivisionCheck.cpp
1920
LambdaFunctionNameCheck.cpp
2021
MacroParenthesesCheck.cpp

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,86 @@ Improvements to clang-tidy
7070
- New :doc:`bugprone-dynamic-static-initializers
7171
<clang-tidy/checks/bugprone-dynamic-static-initializers>` check.
7272

73+
- New OpenMP module.
74+
75+
For checks specific to `OpenMP <https://www.openmp.org/>`_ API.
76+
77+
- New :doc:`abseil-duration-addition
78+
<clang-tidy/checks/abseil-duration-addition>` check.
79+
80+
Checks for cases where addition should be performed in the ``absl::Time``
81+
domain.
82+
83+
- New :doc:`abseil-duration-conversion-cast
84+
<clang-tidy/checks/abseil-duration-conversion-cast>` check.
85+
86+
Checks for casts of ``absl::Duration`` conversion functions, and recommends
87+
the right conversion function instead.
88+
89+
- New :doc:`abseil-duration-unnecessary-conversion
90+
<clang-tidy/checks/abseil-duration-unnecessary-conversion>` check.
91+
92+
Finds and fixes cases where ``absl::Duration`` values are being converted to
93+
numeric types and back again.
94+
95+
- New :doc:`abseil-time-comparison
96+
<clang-tidy/checks/abseil-time-comparison>` check.
97+
98+
Prefer comparisons in the ``absl::Time`` domain instead of the integer
99+
domain.
100+
101+
- New :doc:`abseil-time-subtraction
102+
<clang-tidy/checks/abseil-time-subtraction>` check.
103+
104+
Finds and fixes ``absl::Time`` subtraction expressions to do subtraction
105+
in the Time domain instead of the numeric domain.
106+
107+
- New :doc:`android-cloexec-pipe
108+
<clang-tidy/checks/android-cloexec-pipe>` check.
109+
110+
This check detects usage of ``pipe()``.
111+
112+
- New :doc:`android-cloexec-pipe2
113+
<clang-tidy/checks/android-cloexec-pipe2>` check.
114+
115+
This checks ensures that ``pipe2()`` is called with the O_CLOEXEC flag.
116+
117+
- New :doc:`bugprone-infinite-loop
118+
<clang-tidy/checks/bugprone-infinite-loop>` check.
119+
120+
Finds obvious infinite loops (loops where the condition variable is not
121+
changed at all).
122+
123+
- New :doc:`bugprone-unhandled-self-assignment
124+
<clang-tidy/checks/bugprone-unhandled-self-assignment>` check.
125+
126+
Finds user-defined copy assignment operators which do not protect the code
127+
against self-assignment either by checking self-assignment explicitly or
128+
using the copy-and-swap or the copy-and-move method.
129+
130+
- New :doc:`bugprone-branch-clone
131+
<clang-tidy/checks/bugprone-branch-clone>` check.
132+
133+
Checks for repeated branches in ``if/else if/else`` chains, consecutive
134+
repeated branches in ``switch`` statements and indentical true and false
135+
branches in conditional operators.
136+
137+
- New :doc:`bugprone-posix-return
138+
<clang-tidy/checks/bugprone-posix-return>` check.
139+
140+
Checks if any calls to POSIX functions (except ``posix_openpt``) expect negative
141+
return values.
142+
143+
- New :doc:`fuchsia-default-arguments-calls
144+
<clang-tidy/checks/fuchsia-default-arguments-calls>` check.
145+
146+
Warns if a function or method is called with default arguments.
147+
This was previously done by `fuchsia-default-arguments check`, which has been
148+
removed.
149+
150+
- New :doc:`fuchsia-default-arguments-calls
151+
<clang-tidy/checks/fuchsia-default-arguments-calls>` check.
152+
73153
Finds instances where variables with static storage are initialized
74154
dynamically in header files.
75155

@@ -103,6 +183,10 @@ Improvements to clang-tidy
103183
Now also checks if any calls to ``pthread_*`` functions expect negative return
104184
values.
105185

186+
- New :doc:`bugprone-infinite-loop <clang-tidy/checks/bugprone-infinite-loop>`
187+
check to detect obvious infinite loops (loops where the condition variable is
188+
not changed at all).
189+
106190
Improvements to include-fixer
107191
-----------------------------
108192

clang-tools-extra/docs/clang-tidy/checks/list.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Clang-Tidy Checks
5151
bugprone-forwarding-reference-overload
5252
bugprone-inaccurate-erase
5353
bugprone-incorrect-roundings
54+
bugprone-infinite-loop
5455
bugprone-integer-division
5556
bugprone-lambda-function-name
5657
bugprone-macro-parentheses

0 commit comments

Comments
 (0)