[clang-tools-extra] b2a2c38 - Fix bug in readability-uppercase-literal-suffix

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 27 11:04:02 PDT 2021


Author: Carlos Galvez
Date: 2021-09-27T14:03:53-04:00
New Revision: b2a2c38349a18b89b04d342632d5ea02f86dfdd6

URL: https://github.com/llvm/llvm-project/commit/b2a2c38349a18b89b04d342632d5ea02f86dfdd6
DIFF: https://github.com/llvm/llvm-project/commit/b2a2c38349a18b89b04d342632d5ea02f86dfdd6.diff

LOG: Fix bug in readability-uppercase-literal-suffix

Fixes https://bugs.llvm.org/show_bug.cgi?id=51790. The check triggers
incorrectly with non-type template parameters.

A bisect determined that the bug was introduced here:
https://github.com/llvm/llvm-project/commit/ea2225a10be986d226e041d20d36dff17e78daed

Unfortunately that patch can no longer be reverted on top of the main
branch, so add a fix instead. Add a unit test to avoid regression in
the future.

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/readability/UppercaseLiteralSuffixCheck.cpp
    clang-tools-extra/test/clang-tidy/checkers/readability-uppercase-literal-suffix-integer.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/readability/UppercaseLiteralSuffixCheck.cpp b/clang-tools-extra/clang-tidy/readability/UppercaseLiteralSuffixCheck.cpp
index 6abb5c3b877d6..dc2882418d358 100644
--- a/clang-tools-extra/clang-tidy/readability/UppercaseLiteralSuffixCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/UppercaseLiteralSuffixCheck.cpp
@@ -134,6 +134,11 @@ shouldReplaceLiteralSuffix(const Expr &Literal,
       CharSourceRange::getTokenRange(*Range), SM, LO, &Invalid);
   assert(!Invalid && "Failed to retrieve the source text.");
 
+  // Make sure the first character is actually a digit, instead of
+  // something else, like a non-type template parameter.
+  if (!std::isdigit(static_cast<unsigned char>(LiteralSourceText.front())))
+    return llvm::None;
+
   size_t Skip = 0;
 
   // Do we need to ignore something before actually looking for the suffix?

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/readability-uppercase-literal-suffix-integer.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-uppercase-literal-suffix-integer.cpp
index a6f38a8e7f261..847501f098f4a 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability-uppercase-literal-suffix-integer.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability-uppercase-literal-suffix-integer.cpp
@@ -270,3 +270,29 @@ void c() { l &a(); }
 void d();
 void d() { c<b>(); }
 } // namespace
+
+// Check that non-type template parameters do not cause any diags.
+// https://bugs.llvm.org/show_bug.cgi?id=51790
+template <int capacity>
+struct Vector {
+  static constexpr int kCapacity = capacity;
+};
+
+template <int capacity>
+constexpr int Vector<capacity>::kCapacity;
+// CHECK-MESSAGES-NOT: :[[@LINE-1]]:22: warning: integer literal has suffix 'ity', which is not uppercase
+
+template <int foo1u>
+struct Foo {
+  static constexpr int kFoo = foo1u;
+};
+
+template <int foo1u>
+constexpr int Foo<foo1u>::kFoo;
+// CHECK-MESSAGES-NOT: :[[@LINE-1]]:19: warning: integer literal has suffix 'u', which is not uppercase
+
+// The template needs to be instantiated for diagnostics to show up
+void test_non_type_template_parameter() {
+  int x = Vector<10>::kCapacity;
+  int f = Foo<10>::kFoo;
+}


        


More information about the cfe-commits mailing list