[clang-tools-extra] [clang-tidy] Ignore user-defined literals in google-runtime-int (PR #78859)

FĂ©lix-Antoine Constantin via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 21 07:36:27 PST 2024


https://github.com/felix642 updated https://github.com/llvm/llvm-project/pull/78859

>From 57cbce14d5836e57f677fa1fef7214dd2663262a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?=
 <felix-antoine.constantin at polymtl.ca>
Date: Sat, 20 Jan 2024 15:29:06 -0500
Subject: [PATCH 1/2] [clang-tidy] Ignore user-defined literals in
 google-runtime-int

User-defined literals do not accept u?intXX(_t)? variables.
So the check should not emit a warning.

Fixes #54546 #25214
---
 .../clang-tidy/google/IntegerTypesCheck.cpp         | 13 ++++++++-----
 clang-tools-extra/docs/ReleaseNotes.rst             |  3 +++
 .../test/clang-tidy/checkers/google/runtime-int.cpp | 10 ++++++----
 3 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/google/IntegerTypesCheck.cpp b/clang-tools-extra/clang-tidy/google/IntegerTypesCheck.cpp
index bb4e1de8cc4b15..b60f2d0ed63112 100644
--- a/clang-tools-extra/clang-tidy/google/IntegerTypesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/google/IntegerTypesCheck.cpp
@@ -56,11 +56,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(
+                               matchesName("operator\"\".*$"))))))))
+          .bind("tl"),
+      this);
   IdentTable = std::make_unique<IdentifierTable>(getLangOpts());
 }
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 2822b17b435143..0706a1f8d152d7 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -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` 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`.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/google/runtime-int.cpp b/clang-tools-extra/test/clang-tidy/checkers/google/runtime-int.cpp
index 4bb739876b7f4c..88f0b519e9cbee 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/google/runtime-int.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/google/runtime-int.cpp
@@ -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]; };

>From 5495d864bc351e2c888a32cb10f16f935799470f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?=
 <felix-antoine.constantin at polymtl.ca>
Date: Sat, 20 Jan 2024 21:02:42 -0500
Subject: [PATCH 2/2] fixup! [clang-tidy] Ignore user-defined literals in
 google-runtime-int

Fixed Documentation
Improved ast matcher to use getLiteralIdentifier
---
 .../clang-tidy/google/IntegerTypesCheck.cpp            | 10 ++++++++--
 clang-tools-extra/docs/ReleaseNotes.rst                |  4 ++--
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/google/IntegerTypesCheck.cpp b/clang-tools-extra/clang-tidy/google/IntegerTypesCheck.cpp
index b60f2d0ed63112..ef511e9108f2ee 100644
--- a/clang-tools-extra/clang-tidy/google/IntegerTypesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/google/IntegerTypesCheck.cpp
@@ -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)
@@ -60,8 +66,8 @@ void IntegerTypesCheck::registerMatchers(MatchFinder *Finder) {
       typeLoc(loc(isInteger()),
               unless(anyOf(hasAncestor(callExpr(
                                callee(functionDecl(hasAttr(attr::Format))))),
-                           hasParent(parmVarDecl(hasAncestor(functionDecl(
-                               matchesName("operator\"\".*$"))))))))
+                           hasParent(parmVarDecl(hasAncestor(
+                               functionDecl(isUserDefineLiteral())))))))
           .bind("tl"),
       this);
   IdentTable = std::make_unique<IdentifierTable>(getLangOpts());
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 0706a1f8d152d7..b985c3fd61a40d 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -374,8 +374,8 @@ Changes in existing checks
   <clang-tidy/checks/google/readability-casting>` check to ignore constructor
   calls disguised as functional casts.
 
-- Improved :doc:`google-runtime-int` check to ignore
-  false positives on user defined-literals.
+- 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



More information about the cfe-commits mailing list