[PATCH] D36672: readability-non-const-parameter: fixit on all function declarations

Anders Rönnholm via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 14 05:48:02 PDT 2017


AndersRonnholm created this revision.

Fixes issue reported in https://bugs.llvm.org/show_bug.cgi?id=33219


Repository:
  rL LLVM

https://reviews.llvm.org/D36672

Files:
  clang-tidy/readability/NonConstParameterCheck.cpp
  test/clang-tidy/readability-non-const-parameter.cpp


Index: test/clang-tidy/readability-non-const-parameter.cpp
===================================================================
--- test/clang-tidy/readability-non-const-parameter.cpp
+++ test/clang-tidy/readability-non-const-parameter.cpp
@@ -277,3 +277,11 @@
     int x = *p;
   }
 };
+
+int declarationFixit(int *i);
+// CHECK-FIXES: {{^}}int declarationFixit(const int *i);{{$}}
+// CHECK-MESSAGES: :[[@LINE+1]]:27: warning: pointer parameter 'i' can be
+int declarationFixit(int *i) {
+  // CHECK-FIXES: {{^}}int declarationFixit(const int *i) {{{$}}
+  return *i;
+}
Index: clang-tidy/readability/NonConstParameterCheck.cpp
===================================================================
--- clang-tidy/readability/NonConstParameterCheck.cpp
+++ clang-tidy/readability/NonConstParameterCheck.cpp
@@ -138,9 +138,20 @@
     if (!ParamInfo.CanBeConst)
       continue;
 
-    diag(Par->getLocation(), "pointer parameter '%0' can be pointer to const")
-        << Par->getName()
-        << FixItHint::CreateInsertion(Par->getLocStart(), "const ");
+    auto D = diag(Par->getLocation(),
+                  "pointer parameter '%0' can be pointer to const")
+             << Par->getName()
+             << FixItHint::CreateInsertion(Par->getLocStart(), "const ");
+
+    if (const auto *Parent = Par->getParentFunctionOrMethod()) {
+      if (const auto *F = dyn_cast<FunctionDecl>(Parent)) {
+        const auto ParDecl =
+            F->getFirstDecl()->getParamDecl(Par->getFunctionScopeIndex());
+        if (Par != ParDecl)
+          D << ParDecl->getName()
+            << FixItHint::CreateInsertion(ParDecl->getLocStart(), "const ");
+      }
+    }
   }
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36672.110944.patch
Type: text/x-patch
Size: 1672 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170814/db59ed28/attachment.bin>


More information about the cfe-commits mailing list