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

Anders Rönnholm via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 8 05:44:21 PDT 2017


AndersRonnholm updated this revision to Diff 114346.
AndersRonnholm added a comment.
Herald added subscribers: xazax.hun, JDevlieghere.

Fixed comments


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,26 @@
     int x = *p;
   }
 };
+
+int declarationFixit(int *i);
+// CHECK-FIXES: {{^}}int declarationFixit(const int *i);{{$}}
+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;
+}
+
+
+class D {
+private:
+  int declarationFixit(int *i);
+  // CHECK-FIXES: {{^}}  int declarationFixit(const int *i);{{$}}
+};
+
+// CHECK-MESSAGES: :[[@LINE+1]]:30: warning: pointer parameter 'i' can be
+int D::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 ");
+
+    const DeclContext *Parent = Par->getParentFunctionOrMethod();
+    const auto *FD = dyn_cast<FunctionDecl>(Parent);
+    while (FD) {
+      const auto ParDecl = FD->getParamDecl(Par->getFunctionScopeIndex());
+      if (Par != ParDecl)
+        D << ParDecl->getName()
+          << FixItHint::CreateInsertion(ParDecl->getLocStart(), "const ");
+      FD = FD->getPreviousDecl();
+    }
   }
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36672.114346.patch
Type: text/x-patch
Size: 2092 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170908/83ed2604/attachment.bin>


More information about the cfe-commits mailing list