Skip to content
Open
Show file tree
Hide file tree
Changes from 81 commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
3ef4feb
AvoidFundamentalIntegerTypesCheck
jj-marr Jul 3, 2025
48598ef
Test typedefs properly
jj-marr Jul 3, 2025
524fdd8
Document properly
jj-marr Jul 3, 2025
d5fd2e7
Rename files
jj-marr Jul 4, 2025
25425cc
Other renaming for portability change
jj-marr Jul 4, 2025
86787ec
Formatting fix
jj-marr Jul 4, 2025
c66668e
Make matchers more specific
jj-marr Jul 4, 2025
1b314bb
Remove dead code
jj-marr Jul 4, 2025
411d598
Fix doc
jj-marr Jul 4, 2025
b427df7
Fix issue on MSVC
jj-marr Jul 5, 2025
5dd5c72
Merge comment with previous line
jj-marr Jul 5, 2025
7c2031b
Add linter check to release notes
jj-marr Jul 5, 2025
475f1b2
Update documentation
jj-marr Jul 5, 2025
20e43d8
Fix redundant check
jj-marr Jul 5, 2025
20e9b32
In progress work on float check
jj-marr Jul 5, 2025
f49a289
Declare const
jj-marr Jul 5, 2025
143fcad
Synchronize with release notes
jj-marr Jul 5, 2025
3c2ccd5
Attempt to format
jj-marr Jul 5, 2025
40b8be3
Allow for int to not be warned
jj-marr Jul 6, 2025
93c8489
Reduce some duplication
jj-marr Jul 6, 2025
91eb765
vibecoding: not even once
jj-marr Jul 6, 2025
723a76e
For each loop
jj-marr Jul 6, 2025
a30bedf
Format code
jj-marr Jul 6, 2025
76125bb
Warn on chars
jj-marr Jul 6, 2025
8258324
Fix header doc
jj-marr Jul 6, 2025
8dd606b
Forgot to format
jj-marr Jul 6, 2025
90c9887
Fix typo
jj-marr Jul 6, 2025
c487bf3
Fix formatting
jj-marr Jul 6, 2025
09a762d
Declare as const
jj-marr Jul 6, 2025
a53b5bc
Add WarnOnInts documentation
jj-marr Jul 6, 2025
b525a7a
Enable all checks by default
jj-marr Jul 6, 2025
de662e8
Update clang-tools-extra/clang-tidy/portability/AvoidPlatformSpecific…
jj-marr Jul 7, 2025
d0012d3
Fix formatting more
jj-marr Jul 11, 2025
304b7b0
Use or later standards
jj-marr Jul 11, 2025
e66cf7b
Remove header filters
jj-marr Jul 11, 2025
c2f53d9
Replace string matching
jj-marr Jul 11, 2025
32343ae
Remove useless comments
jj-marr Jul 11, 2025
d37015e
Add static helper functions
jj-marr Jul 11, 2025
9d47d7a
Apply suggestions from code review
jj-marr Aug 4, 2025
1fed634
Trim to 80 characters and reword
jj-marr Aug 4, 2025
110bb52
Fix changes from code review
jj-marr Aug 4, 2025
7c0003c
Use existing checker
jj-marr Aug 4, 2025
b3a206f
Update clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pla…
jj-marr Aug 4, 2025
1361674
Update clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pla…
jj-marr Aug 10, 2025
4a1af0c
Fix tests
jj-marr Aug 10, 2025
bdbccb3
link as option
jj-marr Aug 10, 2025
8c05d1f
Split run directives across multiple lines
jj-marr Aug 10, 2025
03f0dfe
Harmonize release notes with header
jj-marr Aug 10, 2025
ed7157d
Make "default is true" the last sentence
jj-marr Aug 10, 2025
d26bb9e
warn on bad config and other stuff
jj-marr Aug 10, 2025
9634a74
Refactor redundant code, works for int and float
jj-marr Aug 10, 2025
89d4489
Fix for char
jj-marr Aug 10, 2025
4669797
Avoid flagging unicode characters
jj-marr Aug 10, 2025
a595421
Fix typo
jj-marr Aug 10, 2025
69e4f38
add std::byte test case
jj-marr Aug 10, 2025
950513d
also add to rst
jj-marr Aug 10, 2025
6693940
remove CV qualifier when reporting issue
jj-marr Aug 10, 2025
49fb728
Merge branch 'main' of github.com:llvm/llvm-project into TypedefsOnly
jj-marr Aug 10, 2025
082a5ad
fix indent
jj-marr Aug 10, 2025
88bbef0
Fix doc
jj-marr Aug 10, 2025
485022d
Fix formatting
jj-marr Aug 10, 2025
9a01bc0
Apply suggestions from code review
jj-marr Aug 24, 2025
2db38a1
Apply suggestions from code review
jj-marr Aug 24, 2025
a5d4073
Apply suggestions from code review
jj-marr Aug 24, 2025
9864a96
Update clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pla…
jj-marr Aug 24, 2025
09158b0
Update clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pla…
jj-marr Aug 24, 2025
6c4803a
Fix char documentation (which isn't even correct)
jj-marr Aug 24, 2025
568990b
remove typedef examples
jj-marr Aug 24, 2025
a71bb2e
Revise documentation
jj-marr Aug 24, 2025
d96e8d3
further documentation edits
jj-marr Sep 17, 2025
87fbe9d
further edits
jj-marr Sep 17, 2025
e4f9a80
Apply suggestions from code review
jj-marr Sep 17, 2025
c868336
another test case
jj-marr Sep 17, 2025
5dc7c47
replace more matcher if statements with assertions
jj-marr Sep 17, 2025
2a6222d
use std::optional instead of empty string
jj-marr Sep 17, 2025
10a3a2e
Use early return
jj-marr Sep 17, 2025
8c115ad
Merge branch 'main' of github.com:llvm/llvm-project into TypedefsOnly
jj-marr Sep 17, 2025
c7c28c9
add more floating point types to the switch statement
jj-marr Sep 17, 2025
2104952
fix use of optional
jj-marr Sep 20, 2025
c725301
fix unit tests to match new warning message
jj-marr Sep 20, 2025
57e3e39
test char type template specialization
jj-marr Sep 21, 2025
466133f
Update clang-tools-extra/clang-tidy/portability/AvoidPlatformSpecific…
jj-marr Sep 27, 2025
8138945
trim contents of char check
jj-marr Oct 5, 2025
b02eccc
vibe coded replacement suggestion
jj-marr Oct 5, 2025
388fe0c
Touch up suggestion code
jj-marr Oct 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "AvoidPlatformSpecificFundamentalTypesCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Type.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Basic/TargetInfo.h"

using namespace clang::ast_matchers;

namespace {

AST_MATCHER(clang::QualType, isBuiltinInt) {
const auto *BT = Node->getAs<clang::BuiltinType>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't particularly like that we need to cast to BuiltinType every time. Can we make AST_MATCHER(clang::BuiltinType) and later write:

builtinType(
  anyOf(isBuiltinInt, isBuiltinFloat, ...)
)

assert(BT);

// BT->isInteger() would detect char and bool
switch (BT->getKind()) {
case clang::BuiltinType::Short:
case clang::BuiltinType::UShort:
case clang::BuiltinType::Int:
case clang::BuiltinType::UInt:
case clang::BuiltinType::Long:
case clang::BuiltinType::ULong:
case clang::BuiltinType::LongLong:
case clang::BuiltinType::ULongLong:
return true;
default:
return false;
}
}

AST_MATCHER(clang::QualType, isBuiltinFloat) {
const auto *BT = Node->getAs<clang::BuiltinType>();
assert(BT);

return BT->isFloatingPoint();
}

} // namespace

namespace clang::tidy::portability {

AvoidPlatformSpecificFundamentalTypesCheck::
AvoidPlatformSpecificFundamentalTypesCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
WarnOnFloats(Options.get("WarnOnFloats", true)),
WarnOnInts(Options.get("WarnOnInts", true)),
WarnOnChars(Options.get("WarnOnChars", true)),
IncludeInserter(Options.getLocalOrGlobal("IncludeStyle",
utils::IncludeSorter::IS_LLVM),
areDiagsSelfContained()) {
if (!WarnOnFloats && !WarnOnInts && !WarnOnChars)
this->configurationDiag(
"The check 'portability-avoid-platform-specific-fundamental-types' "
"will not perform any analysis because 'WarnOnFloats', 'WarnOnInts' "
"and 'WarnOnChars' are all false.");
}

void AvoidPlatformSpecificFundamentalTypesCheck::registerPPCallbacks(
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
IncludeInserter.registerPreprocessor(PP);
}

void AvoidPlatformSpecificFundamentalTypesCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "WarnOnFloats", WarnOnFloats);
Options.store(Opts, "WarnOnInts", WarnOnInts);
Options.store(Opts, "WarnOnChars", WarnOnChars);
Options.store(Opts, "IncludeStyle", IncludeInserter.getStyle());
}

static std::optional<std::string> getFloatReplacement(const BuiltinType *BT,
ASTContext &Context) {
const TargetInfo &Target = Context.getTargetInfo();

auto GetReplacementType = [](unsigned Width) -> std::optional<std::string> {
switch (Width) {
// This is ambiguous by default since it could be bfloat16 or float16
case 16U:
return std::nullopt;
case 32U:
return "float32_t";
case 64U:
return "float64_t";
case 128U:
return "float128_t";
default:
return std::nullopt;
}
};

switch (BT->getKind()) {
// Not an ambiguous type
case BuiltinType::BFloat16:
return "bfloat16_t";
case BuiltinType::Half:
return GetReplacementType(Target.getHalfWidth());
case BuiltinType::Float:
return GetReplacementType(Target.getFloatWidth());
case BuiltinType::Double:
return GetReplacementType(Target.getDoubleWidth());
case BuiltinType::LongDouble:
return GetReplacementType(Target.getLongDoubleWidth());
case BuiltinType::Float128:
return "float128_t";
default:
return std::nullopt;
}
}

void AvoidPlatformSpecificFundamentalTypesCheck::registerMatchers(
MatchFinder *Finder) {
auto PlatformSpecificFundamentalType = qualType(allOf(
builtinType(), anyOf(WarnOnInts ? isBuiltinInt() : unless(anything()),
WarnOnFloats ? isBuiltinFloat() : unless(anything()),
WarnOnChars ? isChar() : unless(anything()),
WarnOnChars ? isWideChar() : unless(anything()))));

if (!WarnOnInts && !WarnOnFloats && !WarnOnChars)
return;

Finder->addMatcher(typeLoc(loc(PlatformSpecificFundamentalType)).bind("type"),
this);
}

void AvoidPlatformSpecificFundamentalTypesCheck::check(
const MatchFinder::MatchResult &Result) {
const auto *TL = Result.Nodes.getNodeAs<TypeLoc>("type");
assert(TL);

const SourceLocation Loc = TL->getBeginLoc();
const QualType QT = TL->getType();
const SourceRange TypeRange = TL->getSourceRange();

// Skip implicit type locations, such as literals
if (!Loc.isValid() || !TypeRange.isValid())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC there is .isInvalid() method

return;

const std::string TypeName = QT.getUnqualifiedType().getAsString();

const auto *BT = QT->getAs<BuiltinType>();

assert(BT);
if (BT->isFloatingPoint()) {
const auto Replacement = getFloatReplacement(BT, *Result.Context);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const auto Replacement = getFloatReplacement(BT, *Result.Context);
const std::optional<std::string> Replacement = getFloatReplacement(BT, *Result.Context);


if (!Replacement.has_value()) {
diag(Loc, "avoid using platform-dependent floating point type '%0'; "
"consider using a type alias or fixed-width type instead")
<< TypeName;
return;
}

auto Diag =
diag(Loc, "avoid using platform-dependent floating point type '%0'; "
"consider using '%1' instead")
<< TypeName << Replacement.value();

if (TypeRange.isValid())
Diag << FixItHint::CreateReplacement(TypeRange, Replacement.value());

if (auto IncludeFixit = IncludeInserter.createIncludeInsertion(
Result.SourceManager->getFileID(Loc), "<stdfloat>")) {
Diag << *IncludeFixit;
}
} else if (QT->isCharType() || QT->isWideCharType()) {
diag(Loc, "avoid using platform-dependent character type '%0'; "
"consider using 'char8_t' for text or 'std::byte' for bytes")
<< TypeName;
} else {
diag(Loc, "avoid using platform-dependent fundamental integer type '%0'; "
"consider using a type alias or fixed-width type instead")
<< TypeName;
}
}

} // namespace clang::tidy::portability
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//==------------------------------------------------------------------------==//

#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPLATFORMSPECIFICFUNDAMENTALTYPESCHECK_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPLATFORMSPECIFICFUNDAMENTALTYPESCHECK_H

#include "../ClangTidyCheck.h"
#include "../utils/IncludeInserter.h"

namespace clang::tidy::portability {

/// Detects fundamental types (int, short, long, long long, char, float, etc)
/// and warns against their use due to platform-dependent behavior.
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/portability/avoid-platform-specific-fundamental-types.html
class AvoidPlatformSpecificFundamentalTypesCheck : public ClangTidyCheck {
public:
AvoidPlatformSpecificFundamentalTypesCheck(StringRef Name,
ClangTidyContext *Context);
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
Preprocessor *ModuleExpanderPP) override;
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
return LangOpts.CPlusPlus11;
}
std::optional<TraversalKind> getCheckTraversalKind() const override {
return TK_IgnoreUnlessSpelledInSource;
}

private:
const bool WarnOnFloats;
const bool WarnOnInts;
const bool WarnOnChars;
utils::IncludeInserter IncludeInserter;
};

} // namespace clang::tidy::portability

#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPLATFORMSPECIFICFUNDAMENTALTYPESCHECK_H
1 change: 1 addition & 0 deletions clang-tools-extra/clang-tidy/portability/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
)

add_clang_library(clangTidyPortabilityModule STATIC
AvoidPlatformSpecificFundamentalTypesCheck.cpp
AvoidPragmaOnceCheck.cpp
PortabilityTidyModule.cpp
RestrictSystemIncludesCheck.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "../ClangTidy.h"
#include "../ClangTidyModule.h"
#include "../ClangTidyModuleRegistry.h"
#include "AvoidPlatformSpecificFundamentalTypesCheck.h"
#include "AvoidPragmaOnceCheck.h"
#include "RestrictSystemIncludesCheck.h"
#include "SIMDIntrinsicsCheck.h"
Expand All @@ -21,6 +22,8 @@ namespace portability {
class PortabilityModule : public ClangTidyModule {
public:
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
CheckFactories.registerCheck<AvoidPlatformSpecificFundamentalTypesCheck>(
"portability-avoid-platform-specific-fundamental-types");
CheckFactories.registerCheck<AvoidPragmaOnceCheck>(
"portability-avoid-pragma-once");
CheckFactories.registerCheck<RestrictSystemIncludesCheck>(
Expand Down
8 changes: 8 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ New checks
Finds virtual function overrides with different visibility than the function
in the base class.

- New :doc:`portability-avoid-platform-specific-fundamental-types
<clang-tidy/checks/portability/avoid-platform-specific-fundamental-types>`
check.

Detects fundamental types (``int``, ``short``, ``long``, ``long long``,
``char``, ``float``, etc) and warns against their use due to
platform-dependent behavior.

New check aliases
^^^^^^^^^^^^^^^^^

Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/docs/clang-tidy/checks/list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ Clang-Tidy Checks
:doc:`performance-type-promotion-in-math-fn <performance/type-promotion-in-math-fn>`, "Yes"
:doc:`performance-unnecessary-copy-initialization <performance/unnecessary-copy-initialization>`, "Yes"
:doc:`performance-unnecessary-value-param <performance/unnecessary-value-param>`, "Yes"
:doc:`portability-avoid-platform-specific-fundamental-types <portability/avoid-platform-specific-fundamental-types>`,
:doc:`portability-avoid-pragma-once <portability/avoid-pragma-once>`,
:doc:`portability-restrict-system-includes <portability/restrict-system-includes>`, "Yes"
:doc:`portability-simd-intrinsics <portability/simd-intrinsics>`,
Expand Down
Loading
Loading