[clang-tools-extra] r319021 - [clang-tidy] readability-non-const-parameter fixes should update all declarations

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 27 04:42:04 PST 2017


Author: alexfh
Date: Mon Nov 27 04:42:04 2017
New Revision: 319021

URL: http://llvm.org/viewvc/llvm-project?rev=319021&view=rev
Log:
[clang-tidy] readability-non-const-parameter fixes should update all declarations

Fixes http://llvm.org/PR34410.

Modified:
    clang-tools-extra/trunk/clang-tidy/readability/NonConstParameterCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/readability-non-const-parameter.cpp

Modified: clang-tools-extra/trunk/clang-tidy/readability/NonConstParameterCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/NonConstParameterCheck.cpp?rev=319021&r1=319020&r2=319021&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/NonConstParameterCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/NonConstParameterCheck.cpp Mon Nov 27 04:42:04 2017
@@ -138,9 +138,18 @@ void NonConstParameterCheck::diagnoseNon
     if (!ParamInfo.CanBeConst)
       continue;
 
+    SmallVector<FixItHint, 8> Fixes;
+    auto *Function =
+        dyn_cast_or_null<const FunctionDecl>(Par->getParentFunctionOrMethod());
+    if (!Function)
+      continue;
+    unsigned Index = Par->getFunctionScopeIndex();
+    for (FunctionDecl *FnDecl : Function->redecls())
+      Fixes.push_back(FixItHint::CreateInsertion(
+          FnDecl->getParamDecl(Index)->getLocStart(), "const "));
+
     diag(Par->getLocation(), "pointer parameter '%0' can be pointer to const")
-        << Par->getName()
-        << FixItHint::CreateInsertion(Par->getLocStart(), "const ");
+        << Par->getName() << Fixes;
   }
 }
 

Modified: clang-tools-extra/trunk/test/clang-tidy/readability-non-const-parameter.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-non-const-parameter.cpp?rev=319021&r1=319020&r2=319021&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/readability-non-const-parameter.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/readability-non-const-parameter.cpp Mon Nov 27 04:42:04 2017
@@ -277,3 +277,13 @@ public:
     int x = *p;
   }
 };
+
+extern char foo(char *s); // 1
+// CHECK-FIXES: {{^}}extern char foo(const char *s); // 1{{$}}
+// CHECK-MESSAGES: :[[@LINE+1]]:16: warning: pointer parameter 's' can be
+char foo(char *s) {
+  // CHECK-FIXES: {{^}}char foo(const char *s) {{{$}}
+  return *s;
+}
+char foo(char *s); // 2
+// CHECK-FIXES: {{^}}char foo(const char *s); // 2{{$}}




More information about the cfe-commits mailing list