Skip to content

Commit fd06155

Browse files
authored
[clang-tidy] Improved cppcoreguidelines-narrowing-conversions.IgnoreConversionFromTypes (#69242)
Extended IgnoreConversionFromTypes option to include types without a declaration, such as built-in types.
1 parent af07d7b commit fd06155

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "clang/ASTMatchers/ASTMatchFinder.h"
1515
#include "clang/ASTMatchers/ASTMatchers.h"
1616
#include "llvm/ADT/APSInt.h"
17+
#include "llvm/ADT/STLExtras.h"
1718
#include "llvm/ADT/SmallString.h"
1819
#include "llvm/ADT/SmallVector.h"
1920

@@ -23,6 +24,26 @@ using namespace clang::ast_matchers;
2324

2425
namespace clang::tidy::cppcoreguidelines {
2526

27+
namespace {
28+
29+
AST_MATCHER_P(QualType, hasAnyType, std::vector<StringRef>, Names) {
30+
if (Names.empty())
31+
return false;
32+
33+
std::string Name = Node.getLocalUnqualifiedType().getAsString();
34+
return llvm::any_of(Names, [&Name](StringRef Ref) { return Ref == Name; });
35+
}
36+
37+
AST_MATCHER(FieldDecl, hasIntBitwidth) {
38+
assert(Node.isBitField());
39+
const ASTContext &Ctx = Node.getASTContext();
40+
unsigned IntBitWidth = Ctx.getIntWidth(Ctx.IntTy);
41+
unsigned CurrentBitWidth = Node.getBitWidthValue(Ctx);
42+
return IntBitWidth == CurrentBitWidth;
43+
}
44+
45+
} // namespace
46+
2647
NarrowingConversionsCheck::NarrowingConversionsCheck(StringRef Name,
2748
ClangTidyContext *Context)
2849
: ClangTidyCheck(Name, Context),
@@ -53,25 +74,22 @@ void NarrowingConversionsCheck::storeOptions(
5374
Options.store(Opts, "PedanticMode", PedanticMode);
5475
}
5576

56-
AST_MATCHER(FieldDecl, hasIntBitwidth) {
57-
assert(Node.isBitField());
58-
const ASTContext &Ctx = Node.getASTContext();
59-
unsigned IntBitWidth = Ctx.getIntWidth(Ctx.IntTy);
60-
unsigned CurrentBitWidth = Node.getBitWidthValue(Ctx);
61-
return IntBitWidth == CurrentBitWidth;
62-
}
63-
6477
void NarrowingConversionsCheck::registerMatchers(MatchFinder *Finder) {
6578
// ceil() and floor() are guaranteed to return integers, even though the type
6679
// is not integral.
6780
const auto IsCeilFloorCallExpr = expr(callExpr(callee(functionDecl(
6881
hasAnyName("::ceil", "::std::ceil", "::floor", "::std::floor")))));
6982

83+
std::vector<StringRef> IgnoreConversionFromTypesVec =
84+
utils::options::parseStringList(IgnoreConversionFromTypes);
85+
7086
// We may want to exclude other types from the checks, such as `size_type`
7187
// and `difference_type`. These are often used to count elements, represented
7288
// in 64 bits and assigned to `int`. Rarely are people counting >2B elements.
73-
const auto IsConversionFromIgnoredType = hasType(namedDecl(
74-
hasAnyName(utils::options::parseStringList(IgnoreConversionFromTypes))));
89+
const auto IsConversionFromIgnoredType =
90+
anyOf(hasType(namedDecl(hasAnyName(IgnoreConversionFromTypesVec))),
91+
allOf(unless(hasType(namedDecl())),
92+
hasType(qualType(hasAnyType(IgnoreConversionFromTypesVec)))));
7593

7694
// `IsConversionFromIgnoredType` will ignore narrowing calls from those types,
7795
// but not expressions that are promoted to an ignored type as a result of a

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ Changes in existing checks
243243
coroutine functions and increase issue detection for cases involving type
244244
aliases with references.
245245

246+
- Improved :doc:`cppcoreguidelines-narrowing-conversions
247+
<clang-tidy/checks/cppcoreguidelines/narrowing-conversions>` check by
248+
extending the `IgnoreConversionFromTypes` option to include types without a
249+
declaration, such as built-in types.
250+
246251
- Improved :doc:`cppcoreguidelines-prefer-member-initializer
247252
<clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>` check to
248253
ignore delegate constructors.

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/narrowing-conversions-ignoreconversionfromtypes-option.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// RUN: %check_clang_tidy -check-suffix=IGNORED %s \
55
// RUN: cppcoreguidelines-narrowing-conversions %t -- \
66
// RUN: -config='{CheckOptions: { \
7-
// RUN: cppcoreguidelines-narrowing-conversions.IgnoreConversionFromTypes: "global_size_t;nested_size_type" \
7+
// RUN: cppcoreguidelines-narrowing-conversions.IgnoreConversionFromTypes: "global_size_t;nested_size_type;long" \
88
// RUN: }}'
99

1010
// We use global_size_t instead of 'size_t' because windows predefines size_t.
@@ -72,3 +72,10 @@ void most_narrowing_is_not_ok() {
7272
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'long long' to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
7373
// CHECK-MESSAGES-IGNORED: :[[@LINE-2]]:7: warning: narrowing conversion from 'long long' to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
7474
}
75+
76+
void test_ignore_builtin_type_pr58809() {
77+
long x = 123;
78+
short y = x;
79+
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:13: warning: narrowing conversion from 'long' to signed type 'short' is implementation-defined [cppcoreguidelines-narrowing-conversions]
80+
// CHECK-MESSAGES-NOT-IGNORED: :[[@LINE-2]]:13: warning: narrowing conversion from 'long' to signed type 'short' is implementation-defined [cppcoreguidelines-narrowing-conversions]
81+
}

0 commit comments

Comments
 (0)