[clang] [clang-tools-extra] [clang-tidy] Add portability-avoid-platform-specific-fundamental-types (PR #146970)
JJ Marr via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 24 14:10:37 PDT 2025
================
@@ -0,0 +1,147 @@
+.. title:: clang-tidy - portability-avoid-platform-specific-fundamental-types
+
+portability-avoid-platform-specific-fundamental-types
+=====================================================
+
+Detects fundamental types (``int``, ``short``, ``long``, ``long long``, ``char``
+, ``float``, etc) and warns against their use due to platform-dependent
+behavior.
+
+This check detects fundamental types (``int``, ``short``, ``long``, ``float``,
+``char`` and their ``unsigned`` or ``signed`` variants) and warns against their
+use due to non-standard platform-dependent behavior. For example, ``long`` is
+64 bits on Linux but 32 bits on Windows. There is no standard rationale or
+intent for the sizes of these types.
+
+Instead of fundamental types, use fixed-width types such as ``int32_t`` or
+implementation-defined types with standard semantics, e.g. ``int_fast32_t`` for
+the fastest integer type greater than or equal to 32 bits.
+
+Examples
+--------
+
+.. code-block:: c++
+
+ // Bad: platform-dependent fundamental types
+ int global_int = 42;
+ short global_short = 10;
+ // unsigned long is 32 bits on Windows and 64 bits on Mac/Linux, so this will
+ // overflow depending on platform.
+ unsigned long global_unsigned_long = 1 << 36;
+ // On many systems, loading into a register must be done at the processor's
+ // word size. On a 64-bit system with 32-bit integers, loading an element from
+ // slowVec could take multiple instructions. The first will load two elements,
+ // and additional instructions will delete the unneeded element.
----------------
jj-marr wrote:
not sure if true on x86_64, but on embedded systems this can cause problems.
https://github.com/llvm/llvm-project/pull/146970
More information about the cfe-commits
mailing list