Skip to content

[clang-tidy] Ignore user-defined literals in google-runtime-int #78859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 14 additions & 5 deletions clang-tools-extra/clang-tidy/google/IntegerTypesCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ static Token getTokenAtLoc(SourceLocation Loc,
return Tok;
}

namespace {
AST_MATCHER(FunctionDecl, isUserDefineLiteral) {
return Node.getLiteralIdentifier() != nullptr;
}
} // namespace

namespace tidy::google::runtime {

IntegerTypesCheck::IntegerTypesCheck(StringRef Name, ClangTidyContext *Context)
Expand All @@ -56,11 +62,14 @@ void IntegerTypesCheck::registerMatchers(MatchFinder *Finder) {
// http://google.github.io/styleguide/cppguide.html#64-bit_Portability
// "Where possible, avoid passing arguments of types specified by
// bitwidth typedefs to printf-based APIs."
Finder->addMatcher(typeLoc(loc(isInteger()),
unless(hasAncestor(callExpr(
callee(functionDecl(hasAttr(attr::Format)))))))
.bind("tl"),
this);
Finder->addMatcher(
typeLoc(loc(isInteger()),
unless(anyOf(hasAncestor(callExpr(
callee(functionDecl(hasAttr(attr::Format))))),
hasParent(parmVarDecl(hasAncestor(
functionDecl(isUserDefineLiteral())))))))
.bind("tl"),
this);
IdentTable = std::make_unique<IdentifierTable>(getLangOpts());
}

Expand Down
3 changes: 3 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ Changes in existing checks
<clang-tidy/checks/google/readability-casting>` check to ignore constructor
calls disguised as functional casts.

- Improved :doc:`google-runtime-int <clang-tidy/checks/google/runtime-int>`
check to ignore false positives on user defined-literals.

- Improved :doc:`llvm-namespace-comment
<clang-tidy/checks/llvm/namespace-comment>` check to provide fixes for
``inline`` namespaces in the same format as :program:`clang-format`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ void qux() {
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: consider replacing 'short' with 'int16'
}

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

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