[clang-tools-extra] [clang-tidy] Add portability-avoid-platform-specific-fundamental-types (PR #146970)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Jul 6 06:44:52 PDT 2025
================
@@ -0,0 +1,190 @@
+.. title:: clang-tidy - portability-avoid-platform-specific-fundamental-types
+
+portability-avoid-platform-specific-fundamental-types
+=====================================================
+
+Finds fundamental types (e.g. `int`, `float`) and recommends using typedefs
+or fixed-width types instead to improve portability across different platforms.
+
+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;
+ long global_long = 100L;
+ unsigned long global_unsigned_long = 100UL;
+
+ void function_with_int_param(int param) {
+ // ...
+ }
+
+ int function_returning_int() {
+ return 42;
+ }
+
+ struct MyStruct {
+ int member_int;
+ long member_long;
+ };
+
+.. code-block:: c++
+
+ // Good: use fixed-width types or typedefs
+ #include <cstdint>
+
+ int32_t global_int32 = 42;
+ int16_t global_int16 = 10;
+ int64_t global_int64 = 100L;
+ uint64_t global_uint64 = 100UL;
+
+ void function_with_int32_param(int32_t param) {
+ // ...
+ }
+
+ int32_t function_returning_int32() {
+ return 42;
+ }
+
+ struct MyStruct {
+ int32_t member_int32;
+ int64_t member_int64;
+ };
+
+The check will also warn about typedef declarations that use fundamental types
+as their underlying type:
+
+.. code-block:: c++
+
+ // Bad: typedef using fundamental type
+ typedef long long MyLongType;
+ using MyIntType = int;
+
+.. code-block:: c++
+
+ // Good: use descriptive names or fixed-width types
+ typedef int64_t TimestampType;
+ using CounterType = uint32_t;
+
+Rationale
+---------
+
+Fundamental types have platform-dependent sizes and behavior:
+
+- ``int`` is typically 32 bits on modern platforms but is only guaranteed to be
+ 16 bits by the spec
+- ``long int`` is 32 bits on Windows but 64 bits on most Unix systems
+- ``double`` is typically 64-bit IEEE754, but on some microcontrollers without
+ a 64-bit FPU (e.g. certain Arduinos) it can be 32 bits
+- ``char`` is signed on ARM and unsigned on x86
+
+The C++ specification does not define these types beyond their minimum sizes.
+That means they can communicate intent in non-standard ways and are often
+needlessly incompatible. For example, ``int``was traditionally the word size of
----------------
EugeneZelenko wrote:
```suggestion
needlessly incompatible. For example, ``int`` was traditionally the word size of
```
https://github.com/llvm/llvm-project/pull/146970
More information about the cfe-commits
mailing list