Skip to content

Commit ee0b4d9

Browse files
authored
[clang-tidy] Ignore user-defined literals in google-runtime-int (#78859)
User-defined literals do not accept u?intXX(_t)? variables. So the check should not emit a warning. Fixes #54546 #25214
1 parent d37d1c8 commit ee0b4d9

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

clang-tools-extra/clang-tidy/google/IntegerTypesCheck.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ static Token getTokenAtLoc(SourceLocation Loc,
3636
return Tok;
3737
}
3838

39+
namespace {
40+
AST_MATCHER(FunctionDecl, isUserDefineLiteral) {
41+
return Node.getLiteralIdentifier() != nullptr;
42+
}
43+
} // namespace
44+
3945
namespace tidy::google::runtime {
4046

4147
IntegerTypesCheck::IntegerTypesCheck(StringRef Name, ClangTidyContext *Context)
@@ -56,11 +62,14 @@ void IntegerTypesCheck::registerMatchers(MatchFinder *Finder) {
5662
// http://google.github.io/styleguide/cppguide.html#64-bit_Portability
5763
// "Where possible, avoid passing arguments of types specified by
5864
// bitwidth typedefs to printf-based APIs."
59-
Finder->addMatcher(typeLoc(loc(isInteger()),
60-
unless(hasAncestor(callExpr(
61-
callee(functionDecl(hasAttr(attr::Format)))))))
62-
.bind("tl"),
63-
this);
65+
Finder->addMatcher(
66+
typeLoc(loc(isInteger()),
67+
unless(anyOf(hasAncestor(callExpr(
68+
callee(functionDecl(hasAttr(attr::Format))))),
69+
hasParent(parmVarDecl(hasAncestor(
70+
functionDecl(isUserDefineLiteral())))))))
71+
.bind("tl"),
72+
this);
6473
IdentTable = std::make_unique<IdentifierTable>(getLangOpts());
6574
}
6675

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ Changes in existing checks
381381
<clang-tidy/checks/google/readability-casting>` check to ignore constructor
382382
calls disguised as functional casts.
383383

384+
- Improved :doc:`google-runtime-int <clang-tidy/checks/google/runtime-int>`
385+
check to ignore false positives on user defined-literals.
386+
384387
- Improved :doc:`llvm-namespace-comment
385388
<clang-tidy/checks/llvm/namespace-comment>` check to provide fixes for
386389
``inline`` namespaces in the same format as :program:`clang-format`.

clang-tools-extra/test/clang-tidy/checkers/google/runtime-int.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ void qux() {
5959
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: consider replacing 'short' with 'int16'
6060
}
6161

62-
// FIXME: This shouldn't warn, as UD-literal operators require one of a handful
63-
// of types as an argument.
6462
struct some_value {};
65-
constexpr some_value operator"" _some_literal(unsigned long long int i);
66-
// CHECK-MESSAGES: [[@LINE-1]]:47: warning: consider replacing 'unsigned long long'
63+
constexpr some_value operator"" _some_literal(unsigned long long int i)
64+
{
65+
short j;
66+
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: consider replacing 'short' with 'int16'
67+
return some_value();
68+
}
6769

6870
struct A { A& operator=(const A&); };
6971
class B { A a[0]; };

0 commit comments

Comments
 (0)