njames93 created this revision.
Herald added subscribers: cfe-commits, xazax.hun.
Herald added a project: clang.

Adds an option called `AddConstQualifier` to readability-qualified-auto to 
toggle adding const to the auto typed pointers and references. By default its 
enabled but in the LLVM module its disabled.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D73548

Files:
  clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp
  clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.cpp
  clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.h
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/docs/clang-tidy/checks/readability-qualified-auto.rst
  clang-tools-extra/test/clang-tidy/checkers/llvm-qualified-auto.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/llvm-qualified-auto.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/llvm-qualified-auto.cpp
@@ -0,0 +1,21 @@
+// RUN: %check_clang_tidy %s llvm-qualified-auto %t
+
+// This check just ensures by default the llvm alias doesn't add const
+// qualifiers to decls, so no need to copy the entire test file from
+// readability-qualified-auto.
+
+int *getIntPtr();
+const int *getCIntPtr();
+
+void foo() {
+  auto NakedPtr = getIntPtr();
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'auto NakedPtr' can be declared as 'auto *NakedPtr'
+  // CHECK-FIXES: {{^}}  auto *NakedPtr = getIntPtr();
+  auto NakedConstPtr = getCIntPtr();
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'auto NakedConstPtr' can be declared as 'auto *NakedConstPtr'
+  // CHECK-FIXES: {{^}}  auto *NakedConstPtr = getCIntPtr();
+  auto *Ptr = getIntPtr();
+  auto *ConstPtr = getCIntPtr();
+  auto &NakedRef = *getIntPtr();
+  auto &NakedConstRef = *getCIntPtr();
+}
Index: clang-tools-extra/docs/clang-tidy/checks/readability-qualified-auto.rst
===================================================================
--- clang-tools-extra/docs/clang-tidy/checks/readability-qualified-auto.rst
+++ clang-tools-extra/docs/clang-tidy/checks/readability-qualified-auto.rst
@@ -3,62 +3,72 @@
 readability-qualified-auto
 ==========================
 
-Adds pointer and ``const`` qualifications to ``auto``-typed variables that are deduced
-to pointers and ``const`` pointers.
+Adds pointer qualifications to ``auto``-typed variables that are deduced to 
+pointers.
 
 `LLVM Coding Standards <https://llvm.org/docs/CodingStandards.html>`_ advises to
-make it obvious if a ``auto`` typed variable is a pointer, constant pointer or 
-constant reference. This check will transform ``auto`` to ``auto *`` when the 
-type is deduced to be a pointer, as well as adding ``const`` when applicable to
-``auto`` pointers or references
+make it obvious if a ``auto`` typed variable is a pointer. This check will
+transform ``auto`` to ``auto *`` when the type is deduced to be a pointer.
 
 .. code-block:: c++
 
-  for (auto &Data : MutatableContainer) {
-    change(Data);
-  }
-  for (auto &Data : ConstantContainer) {
-    observe(Data);
-  }
   for (auto Data : MutatablePtrContainer) {
     change(*Data);
   }
-  for (auto Data : ConstantPtrContainer) {
-    observe(*Data);
-  }
 
 Would be transformed into:
 
 .. code-block:: c++
 
-  for (auto &Data : MutatableContainer) {
-    change(Data);
-  }
-  for (const auto &Data : ConstantContainer) {
-    observe(Data);
-  }
   for (auto *Data : MutatablePtrContainer) {
     change(*Data);
   }
-  for (const auto *Data : ConstantPtrContainer) {
-    observe(*Data);
-  }
 
 Note const volatile qualified types will retain their const and volatile qualifiers.
 
 .. code-block:: c++
 
-  const auto Foo = cast<int *>(Baz1);
-  const auto Bar = cast<const int *>(Baz2);
-  volatile auto FooBar = cast<int*>(Baz3);
+   const auto Foo = cast<int *>(Baz1);
+   const auto Bar = cast<const int *>(Baz2);
+   volatile auto FooBar = cast<int *>(Baz3);
 
 Would be transformed into:
 
 .. code-block:: c++
 
-  auto *const Foo = cast<int *>(Baz1);
-  const auto *const Bar = cast<const int *>(Baz2);
-  auto *volatile FooBar = cast<int*>(Baz3);
+   auto *const Foo = cast<int *>(Baz1);
+   auto *const Bar = cast<const int *>(Baz2);
+   auto *volatile FooBar = cast<int *>(Baz3);
+
+Options
+-------
+
+.. option:: AddConstQualifier
+   
+   When set to `1` the check will add const qualifiers to auto typed pointers
+   or references.
+   Default value is '1'.
+
+.. code-block:: c++
+
+   auto Foo1 = cast<const int *>(Bar1);
+   auto &Foo2 = cast<const int &>(Bar2);
+
+   If AddConstQualifier is set to `0`, Will be transformed into:
+
+.. code-block:: c++
+
+   auto *Foo1 = cast<const int *>(Bar1);
+   auto &Foo2 = cast<const int &>(Bar2);
+
+   Otherwise it will get be transformed into:
+
+.. code-block:: c++
+
+   const auto *Foo1 = cast<const int *>(Bar1);
+   const auto &Foo2 = cast<const int &>(Bar2);
+
+   Note in the LLVM alias, the default value is `0`.
 
 This check helps to enforce this `LLVM Coding Standards recommendation
 <https://llvm.org/docs/CodingStandards.html#beware-unnecessary-copies-with-auto>`_.
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -104,6 +104,11 @@
 Changes in existing checks
 ^^^^^^^^^^^^^^^^^^^^^^^^^^
 
+- Improved :doc:`readability-qualified-auto
+  <clang-tidy/checks/readability-qualified-about>` check now supports a 
+  `AddConstQualifier` to enable adding ``const`` qualifiers to auto typed
+  pointers and references.
+
 - Improved :doc:`readability-redundant-string-init
   <clang-tidy/checks/readability-redundant-string-init>` check now supports a
   `StringNames` option enabling its application to custom string classes. The 
Index: clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.h
===================================================================
--- clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.h
+++ clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.h
@@ -24,9 +24,14 @@
 class QualifiedAutoCheck : public ClangTidyCheck {
 public:
   QualifiedAutoCheck(StringRef Name, ClangTidyContext *Context)
-      : ClangTidyCheck(Name, Context) {}
+      : ClangTidyCheck(Name, Context),
+        AddConstQualifier(Options.get("AddConstQualifier", true)) {}
+  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+
+private:
+  const bool AddConstQualifier;
 };
 
 } // namespace readability
Index: clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.cpp
@@ -102,6 +102,10 @@
 
 } // namespace
 
+void QualifiedAutoCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+  Options.store(Opts, "AddConstQualifier", AddConstQualifier);
+}
+
 void QualifiedAutoCheck::registerMatchers(MatchFinder *Finder) {
   if (!getLangOpts().CPlusPlus11)
     return; // Auto deduction not used in 'C or C++03 and earlier', so don't
@@ -142,6 +146,8 @@
                           hasAnyTemplateArgument(IsBoundToType))))),
           "auto"),
       this);
+  if (!AddConstQualifier)
+    return;
   Finder->addMatcher(ExplicitSingleVarDecl(
                          hasType(pointerType(pointee(autoType()))), "auto_ptr"),
                      this);
@@ -199,7 +205,8 @@
     }
 
     std::string ReplStr = [&] {
-      llvm::StringRef PtrConst = isPointerConst(Var->getType()) ? "const " : "";
+      llvm::StringRef PtrConst =
+          (AddConstQualifier && isPointerConst(Var->getType())) ? "const " : "";
       llvm::StringRef LocalConst = IsLocalConst ? "const " : "";
       llvm::StringRef LocalVol = IsLocalVolatile ? "volatile " : "";
       llvm::StringRef LocalRestrict = IsLocalRestrict ? "__restrict " : "";
Index: clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp
===================================================================
--- clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp
+++ clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp
@@ -36,6 +36,12 @@
         "llvm-qualified-auto");
     CheckFactories.registerCheck<TwineLocalCheck>("llvm-twine-local");
   }
+
+  ClangTidyOptions getModuleOptions() override {
+    ClangTidyOptions Options;
+    Options.CheckOptions["llvm-qualified-auto.AddConstQualifier"] = "0";
+    return Options;
+  }
 };
 
 // Register the LLVMTidyModule using this statically initialized variable.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to