Skip to content

Commit 2341316

Browse files
authored
[clang][analyzer] Improve documentation of checker 'cplusplus.Move' (NFC) (#96295)
1 parent 76864e6 commit 2341316

File tree

3 files changed

+45
-41
lines changed

3 files changed

+45
-41
lines changed

clang/docs/analyzer/checkers.rst

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,21 +420,56 @@ around, such as ``std::string_view``.
420420
421421
cplusplus.Move (C++)
422422
""""""""""""""""""""
423-
Method calls on a moved-from object and copying a moved-from object will be reported.
424-
423+
Find use-after-move bugs in C++. This includes method calls on moved-from
424+
objects, assignment of a moved-from object, and repeated move of a moved-from
425+
object.
425426
426427
.. code-block:: cpp
427428
428-
struct A {
429+
struct A {
429430
void foo() {}
430431
};
431432
432-
void f() {
433+
void f1() {
433434
A a;
434435
A b = std::move(a); // note: 'a' became 'moved-from' here
435436
a.foo(); // warn: method call on a 'moved-from' object 'a'
436437
}
437438
439+
void f2() {
440+
A a;
441+
A b = std::move(a);
442+
A c(std::move(a)); // warn: move of an already moved-from object
443+
}
444+
445+
void f3() {
446+
A a;
447+
A b = std::move(a);
448+
b = a; // warn: copy of moved-from object
449+
}
450+
451+
The checker option ``WarnOn`` controls on what objects the use-after-move is
452+
checked:
453+
454+
* The most strict value is ``KnownsOnly``, in this mode only objects are
455+
checked whose type is known to be move-unsafe. These include most STL objects
456+
(but excluding move-safe ones) and smart pointers.
457+
* With option value ``KnownsAndLocals`` local variables (of any type) are
458+
additionally checked. The idea behind this is that local variables are
459+
usually not tempting to be re-used so an use after move is more likely a bug
460+
than with member variables.
461+
* With option value ``All`` any use-after move condition is checked on all
462+
kinds of variables, excluding global variables and known move-safe cases.
463+
464+
Default value is ``KnownsAndLocals``.
465+
466+
Calls of methods named ``empty()`` or ``isEmpty()`` are allowed on moved-from
467+
objects because these methods are considered as move-safe. Functions called
468+
``reset()``, ``destroy()``, ``clear()``, ``assign``, ``resize``, ``shrink`` are
469+
treated as state-reset functions and are allowed on moved-from objects, these
470+
make the object valid again. This applies to any type of object (not only STL
471+
ones).
472+
438473
.. _cplusplus-NewDelete:
439474
440475
cplusplus.NewDelete (C++)

clang/include/clang/StaticAnalyzer/Checkers/Checkers.td

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -686,22 +686,11 @@ def MoveChecker: Checker<"Move">,
686686
CheckerOptions<[
687687
CmdLineOption<String,
688688
"WarnOn",
689-
"In non-aggressive mode, only warn on use-after-move of "
690-
"local variables (or local rvalue references) and of STL "
691-
"objects. The former is possible because local variables (or "
692-
"local rvalue references) are not tempting their user to "
693-
"re-use the storage. The latter is possible because STL "
694-
"objects are known to end up in a valid but unspecified "
695-
"state after the move and their state-reset methods are also "
696-
"known, which allows us to predict precisely when "
697-
"use-after-move is invalid. Some STL objects are known to "
698-
"conform to additional contracts after move, so they are not "
699-
"tracked. However, smart pointers specifically are tracked "
700-
"because we can perform extra checking over them. In "
701-
"aggressive mode, warn on any use-after-move because the "
702-
"user has intentionally asked us to completely eliminate "
703-
"use-after-move in his code. Values: \"KnownsOnly\", "
704-
"\"KnownsAndLocals\", \"All\".",
689+
"With setting \"KnownsOnly\" warn only on objects with known "
690+
"move semantics like smart pointers and other STL objects. "
691+
"With setting \"KnownsAndLocals\" warn additionally on local "
692+
"variables (or rvalue references). With setting \"All\" warn "
693+
"on all variables (excluding global variables).",
705694
"KnownsAndLocals",
706695
Released>
707696
]>,

clang/test/Analysis/analyzer-checker-option-help.c

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,7 @@
3434
// CHECK-STABLE: OPTIONS:
3535
//
3636
// CHECK-STABLE: cplusplus.Move:WarnOn
37-
// CHECK-STABLE-SAME: (string) In non-aggressive mode, only warn
38-
// CHECK-STABLLE: on use-after-move of local variables (or
39-
// CHECK-STABLLE: local rvalue references) and of STL objects.
40-
// CHECK-STABLLE: The former is possible because local variables
41-
// CHECK-STABLLE: (or local rvalue references) are not tempting
42-
// CHECK-STABLLE: their user to re-use the storage. The latter
43-
// CHECK-STABLLE: is possible because STL objects are known
44-
// CHECK-STABLLE: to end up in a valid but unspecified state
45-
// CHECK-STABLLE: after the move and their state-reset methods
46-
// CHECK-STABLLE: are also known, which allows us to predict
47-
// CHECK-STABLLE: precisely when use-after-move is invalid.
48-
// CHECK-STABLLE: Some STL objects are known to conform to
49-
// CHECK-STABLLE: additional contracts after move, so they
50-
// CHECK-STABLLE: are not tracked. However, smart pointers
51-
// CHECK-STABLLE: specifically are tracked because we can
52-
// CHECK-STABLLE: perform extra checking over them. In aggressive
53-
// CHECK-STABLLE: mode, warn on any use-after-move because
54-
// CHECK-STABLLE: the user has intentionally asked us to completely
55-
// CHECK-STABLLE: eliminate use-after-move in his code. Values:
56-
// CHECK-STABLLE: "KnownsOnly", "KnownsAndLocals", "All".
57-
// CHECK-STABLLE: (default: KnownsAndLocals)
37+
// CHECK-STABLE-SAME: (string) With setting "KnownsOnly" warn
5838

5939
// CHECK-STABLE-NOT: debug.AnalysisOrder:*
6040
// CHECK-DEVELOPER: debug.AnalysisOrder:*

0 commit comments

Comments
 (0)