Skip to content

Commit aa580c2

Browse files
authored
[clang-tidy] Add EnableQtSupport option to modernize-use-integer-sign-comprison (#122127)
- add an option `EnableQtSupport`, that makes C++17 `q20::cmp_*` alternative available for Qt-based applications.
1 parent c938436 commit aa580c2

File tree

5 files changed

+149
-4
lines changed

5 files changed

+149
-4
lines changed

clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ UseIntegerSignComparisonCheck::UseIntegerSignComparisonCheck(
8080
: ClangTidyCheck(Name, Context),
8181
IncludeInserter(Options.getLocalOrGlobal("IncludeStyle",
8282
utils::IncludeSorter::IS_LLVM),
83-
areDiagsSelfContained()) {}
83+
areDiagsSelfContained()),
84+
EnableQtSupport(Options.get("EnableQtSupport", false)) {}
8485

8586
void UseIntegerSignComparisonCheck::storeOptions(
8687
ClangTidyOptions::OptionMap &Opts) {
8788
Options.store(Opts, "IncludeStyle", IncludeInserter.getStyle());
89+
Options.store(Opts, "EnableQtSupport", EnableQtSupport);
8890
}
8991

9092
void UseIntegerSignComparisonCheck::registerMatchers(MatchFinder *Finder) {
@@ -154,8 +156,17 @@ void UseIntegerSignComparisonCheck::check(
154156
DiagnosticBuilder Diag =
155157
diag(BinaryOp->getBeginLoc(),
156158
"comparison between 'signed' and 'unsigned' integers");
157-
const std::string CmpNamespace = ("std::" + parseOpCode(OpCode)).str();
158-
const std::string CmpHeader = "<utility>";
159+
std::string CmpNamespace;
160+
llvm::StringRef CmpHeader;
161+
162+
if (getLangOpts().CPlusPlus20) {
163+
CmpHeader = "<utility>";
164+
CmpNamespace = llvm::Twine("std::" + parseOpCode(OpCode)).str();
165+
} else if (getLangOpts().CPlusPlus17 && EnableQtSupport) {
166+
CmpHeader = "<QtCore/q20utility.h>";
167+
CmpNamespace = llvm::Twine("q20::" + parseOpCode(OpCode)).str();
168+
}
169+
159170
// Prefer modernize-use-integer-sign-comparison when C++20 is available!
160171
Diag << FixItHint::CreateReplacement(
161172
CharSourceRange(R1, SubExprLHS != nullptr),

clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ class UseIntegerSignComparisonCheck : public ClangTidyCheck {
3030
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
3131
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
3232
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
33-
return LangOpts.CPlusPlus20;
33+
return LangOpts.CPlusPlus20 || (LangOpts.CPlusPlus17 && EnableQtSupport);
3434
}
3535

3636
private:
3737
utils::IncludeInserter IncludeInserter;
38+
const bool EnableQtSupport;
3839
};
3940

4041
} // namespace clang::tidy::modernize

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@ Changes in existing checks
329329
<clang-tidy/checks/modernize/use-designated-initializers>` check to fix a
330330
crash when a class is declared but not defined.
331331

332+
- Improved :doc:`modernize-use-integer-sign-comparison
333+
<clang-tidy/checks/modernize/use-integer-sign-comparison>` check to
334+
add an option ``EnableQtSupport``, that makes C++17 ``q20::cmp_*`` alternative
335+
available for Qt-based applications.
336+
332337
- Improved :doc:`modernize-use-nullptr
333338
<clang-tidy/checks/modernize/use-nullptr>` check to also recognize
334339
``NULL``/``__null`` (but not ``0``) when used with a templated type.

clang-tools-extra/docs/clang-tidy/checks/modernize/use-integer-sign-comparison.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ Options
3434

3535
A string specifying which include-style is used, `llvm` or `google`.
3636
Default is `llvm`.
37+
38+
.. option:: EnableQtSupport
39+
40+
Makes C++17 ``q20::cmp_*`` alternative available for Qt-based
41+
applications. Default is `false`.
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// CHECK-FIXES: #include <QtCore/q20utility.h>
2+
// RUN: %check_clang_tidy -std=c++17 %s modernize-use-integer-sign-comparison %t -- \
3+
// RUN: -config="{CheckOptions: {modernize-use-integer-sign-comparison.EnableQtSupport: true}}"
4+
5+
// The code that triggers the check
6+
#define MAX_MACRO(a, b) (a < b) ? b : a
7+
8+
unsigned int FuncParameters(int bla) {
9+
unsigned int result = 0;
10+
if (result == bla)
11+
return 0;
12+
// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
13+
// CHECK-FIXES: if (q20::cmp_equal(result , bla))
14+
15+
return 1;
16+
}
17+
18+
template <typename T>
19+
void TemplateFuncParameter(T val) {
20+
unsigned long uL = 0;
21+
if (val >= uL)
22+
return;
23+
// CHECK-MESSAGES-NOT: warning:
24+
}
25+
26+
template <typename T1, typename T2>
27+
int TemplateFuncParameters(T1 val1, T2 val2) {
28+
if (val1 >= val2)
29+
return 0;
30+
// CHECK-MESSAGES-NOT: warning:
31+
return 1;
32+
}
33+
34+
int AllComparisons() {
35+
unsigned int uVar = 42;
36+
unsigned short uArray[7] = {0, 1, 2, 3, 9, 7, 9};
37+
38+
int sVar = -42;
39+
short sArray[7] = {-1, -2, -8, -94, -5, -4, -6};
40+
41+
enum INT_TEST {
42+
VAL1 = 0,
43+
VAL2 = -1
44+
};
45+
46+
char ch = 'a';
47+
unsigned char uCh = 'a';
48+
signed char sCh = 'a';
49+
bool bln = false;
50+
51+
if (bln == sVar)
52+
return 0;
53+
// CHECK-MESSAGES-NOT: warning:
54+
55+
if (ch > uCh)
56+
return 0;
57+
// CHECK-MESSAGES-NOT: warning:
58+
59+
if (sVar <= INT_TEST::VAL2)
60+
return 0;
61+
// CHECK-MESSAGES-NOT: warning:
62+
63+
if (uCh < sCh)
64+
return -1;
65+
// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
66+
// CHECK-FIXES: if (q20::cmp_less(uCh , sCh))
67+
68+
if ((int)uVar < sVar)
69+
return 0;
70+
// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
71+
// CHECK-FIXES: if (q20::cmp_less(uVar, sVar))
72+
73+
(uVar != sVar) ? uVar = sVar
74+
: sVar = uVar;
75+
// CHECK-MESSAGES: :[[@LINE-2]]:6: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
76+
// CHECK-FIXES: (q20::cmp_not_equal(uVar , sVar)) ? uVar = sVar
77+
78+
while (uArray[0] <= sArray[0])
79+
return 0;
80+
// CHECK-MESSAGES: :[[@LINE-2]]:12: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
81+
// CHECK-FIXES: while (q20::cmp_less_equal(uArray[0] , sArray[0]))
82+
83+
if (uArray[1] > sArray[1])
84+
return 0;
85+
// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
86+
// CHECK-FIXES: if (q20::cmp_greater(uArray[1] , sArray[1]))
87+
88+
MAX_MACRO(uVar, sArray[0]);
89+
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
90+
91+
if (static_cast<unsigned int>(uArray[2]) < static_cast<int>(sArray[2]))
92+
return 0;
93+
// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
94+
// CHECK-FIXES: if (q20::cmp_less(uArray[2],sArray[2]))
95+
96+
if ((unsigned int)uArray[3] < (int)sArray[3])
97+
return 0;
98+
// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
99+
// CHECK-FIXES: if (q20::cmp_less(uArray[3],sArray[3]))
100+
101+
if ((unsigned int)(uArray[4]) < (int)(sArray[4]))
102+
return 0;
103+
// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
104+
// CHECK-FIXES: if (q20::cmp_less((uArray[4]),(sArray[4])))
105+
106+
if (uArray[5] > sArray[5])
107+
return 0;
108+
// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
109+
// CHECK-FIXES: if (q20::cmp_greater(uArray[5] , sArray[5]))
110+
111+
#define VALUE sArray[6]
112+
if (uArray[6] > VALUE)
113+
return 0;
114+
// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
115+
// CHECK-FIXES: if (q20::cmp_greater(uArray[6] , VALUE))
116+
117+
118+
FuncParameters(uVar);
119+
TemplateFuncParameter(sVar);
120+
TemplateFuncParameters(uVar, sVar);
121+
122+
return 0;
123+
}

0 commit comments

Comments
 (0)