Revert "[ELF] Refine isExported/isPreemptible condition"
[llvm-project.git] / clang-tools-extra / docs / clang-tidy / checks / bugprone / implicit-widening-of-multiplication-result.rst
blob117310d404f6f49fc0c75dca5e82e96296dbea4d
1 .. title:: clang-tidy - bugprone-implicit-widening-of-multiplication-result
3 bugprone-implicit-widening-of-multiplication-result
4 ===================================================
6 The check diagnoses instances where a result of a multiplication is implicitly
7 widened, and suggests (with fix-it) to either silence the code by making
8 widening explicit, or to perform the multiplication in a wider type,
9 to avoid the widening afterwards.
11 This is mainly useful when operating on very large buffers.
12 For example, consider:
14 .. code-block:: c++
16   void zeroinit(char* base, unsigned width, unsigned height) {
17     for(unsigned row = 0; row != height; ++row) {
18       for(unsigned col = 0; col != width; ++col) {
19         char* ptr = base + row * width + col;
20         *ptr = 0;
21       }
22     }
23   }
25 This is fine in general, but if ``width * height`` overflows,
26 you end up wrapping back to the beginning of ``base``
27 instead of processing the entire requested buffer.
29 Indeed, this only matters for pretty large buffers (4GB+),
30 but that can happen very easily for example in image processing,
31 where for that to happen you "only" need a ~269MPix image.
34 Options
35 -------
37 .. option:: UseCXXStaticCastsInCppSources
39    When suggesting fix-its for C++ code, should C++-style ``static_cast<>()``'s
40    be suggested, or C-style casts. Defaults to ``true``.
42 .. option:: UseCXXHeadersInCppSources
44    When suggesting to include the appropriate header in C++ code,
45    should ``<cstddef>`` header be suggested, or ``<stddef.h>``.
46    Defaults to ``true``.
48 .. option:: IgnoreConstantIntExpr
50    If the multiplication operands are compile-time constants (like literals or
51    are ``constexpr``) and fit within the source expression type, do not emit a
52    diagnostic or suggested fix.  Only considers expressions where the source
53    expression is a signed integer type.  Defaults to ``false``.
55 Examples:
57 .. code-block:: c++
59   long mul(int a, int b) {
60     return a * b; // warning: performing an implicit widening conversion to type 'long' of a multiplication performed in type 'int'
61   }
63   char* ptr_add(char *base, int a, int b) {
64     return base + a * b; // warning: result of multiplication in type 'int' is used as a pointer offset after an implicit widening conversion to type 'ssize_t'
65   }
67   char ptr_subscript(char *base, int a, int b) {
68     return base[a * b]; // warning: result of multiplication in type 'int' is used as a pointer offset after an implicit widening conversion to type 'ssize_t'
69   }