[llvm-branch-commits] [clang-tools-extra] c6348e8 - cppcoreguidelines Narrowing Conversions Check: detect narrowing conversions involving typedefs

Aaron Ballman via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Dec 8 10:15:33 PST 2020


Author: Eric Seidel
Date: 2020-12-08T13:10:41-05:00
New Revision: c6348e8c95ee1eaa9dd2322b278def7a4127ff26

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

LOG: cppcoreguidelines Narrowing Conversions Check: detect narrowing conversions involving typedefs

The check 'cppcoreguidelines-narrowing-conversions' does not detect conversions
involving typedef. This notably includes the standard fixed-width integer types
like int32_t, uint64_t, etc. Now look through the typedefs at the desugared type.

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp
    clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-narrowing-conversions.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp
index 1837ccb6002f..3ab300dc499f 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp
@@ -48,8 +48,10 @@ void NarrowingConversionsCheck::registerMatchers(MatchFinder *Finder) {
   Finder->addMatcher(
       traverse(
           ast_type_traits::TK_AsIs,
-          implicitCastExpr(hasImplicitDestinationType(builtinType()),
-                           hasSourceExpression(hasType(builtinType())),
+          implicitCastExpr(hasImplicitDestinationType(
+                               hasUnqualifiedDesugaredType(builtinType())),
+                           hasSourceExpression(hasType(
+                               hasUnqualifiedDesugaredType(builtinType()))),
                            unless(hasSourceExpression(IsCeilFloorCallExpr)),
                            unless(hasParent(castExpr())),
                            unless(isInTemplateInstantiation()))
@@ -58,16 +60,18 @@ void NarrowingConversionsCheck::registerMatchers(MatchFinder *Finder) {
 
   // Binary operators:
   //   i += 0.5;
-  Finder->addMatcher(binaryOperator(isAssignmentOperator(),
-                                    hasLHS(expr(hasType(builtinType()))),
-                                    hasRHS(expr(hasType(builtinType()))),
-                                    unless(hasRHS(IsCeilFloorCallExpr)),
-                                    unless(isInTemplateInstantiation()),
-                                    // The `=` case generates an implicit cast
-                                    // which is covered by the previous matcher.
-                                    unless(hasOperatorName("=")))
-                         .bind("binary_op"),
-                     this);
+  Finder->addMatcher(
+      binaryOperator(
+          isAssignmentOperator(),
+          hasLHS(expr(hasType(hasUnqualifiedDesugaredType(builtinType())))),
+          hasRHS(expr(hasType(hasUnqualifiedDesugaredType(builtinType())))),
+          unless(hasRHS(IsCeilFloorCallExpr)),
+          unless(isInTemplateInstantiation()),
+          // The `=` case generates an implicit cast
+          // which is covered by the previous matcher.
+          unless(hasOperatorName("=")))
+          .bind("binary_op"),
+      this);
 }
 
 static const BuiltinType *getBuiltinType(const Expr &E) {

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-narrowing-conversions.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-narrowing-conversions.cpp
index cc817a021fde..493a447913bd 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-narrowing-conversions.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-narrowing-conversions.cpp
@@ -343,4 +343,17 @@ void macro_context() {
   DERP(i, .5l);
 }
 
+// We understand typedefs.
+void typedef_context() {
+  typedef long long myint64_t;
+  int i;
+  myint64_t i64;
+
+  i64 = i64; // Okay, no conversion.
+  i64 = i;   // Okay, no narrowing.
+
+  i = i64;
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: narrowing conversion from 'myint64_t' (aka 'long long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
+}
+
 } // namespace floats


        


More information about the llvm-branch-commits mailing list