njames93 updated this revision to Diff 241082.
njames93 added a comment.

- Always add const to `auto` typed variable.

This update adds const to variables just typed with `auto`, but wont enforce 
checking on `auto *` or `auto &` unless `AddConstToQualified` is set.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D73548/new/

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 'const auto *NakedConstPtr'
+  // CHECK-FIXES: {{^}}  const 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,23 +3,16 @@
 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
+`LLVM Coding Standards <https://llvm.org/docs/CodingStandards.html#beware-unnecessary-copies-with-auto>`_
+advises to 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);
   }
@@ -31,12 +24,6 @@
 
 .. code-block:: c++
 
-  for (auto &Data : MutatableContainer) {
-    change(Data);
-  }
-  for (const auto &Data : ConstantContainer) {
-    observe(Data);
-  }
   for (auto *Data : MutatablePtrContainer) {
     change(*Data);
   }
@@ -48,17 +35,47 @@
 
 .. 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);
+   const auto *const Bar = cast<const int *>(Baz2);
+   auto *volatile FooBar = cast<int *>(Baz3);
+
+Options
+-------
+
+.. option:: AddConstToQualified
+   
+   When set to `1` the check will add const qualifiers variables defined as
+   ``auto *`` or ``auto &`` when applicable.
+   Default value is '1'.
+
+.. code-block:: c++
+
+   auto Foo1 = cast<const int *>(Bar1);
+   auto *Foo2 = cast<const int *>(Bar2);
+   auto &Foo3 = cast<const int &>(Bar3);
+
+   If AddConstToQualified is set to `0`,  it will be transformed into:
+
+.. code-block:: c++
+
+   const auto *Foo1 = cast<const int *>(Bar1);
+   auto *Foo2 = cast<const int *>(Bar2);
+   auto &Foo3 = cast<const int &>(Bar3);
+
+   Otherwise it will be transformed into:
+
+.. code-block:: c++
+
+   const auto *Foo1 = cast<const int *>(Bar1);
+   const auto *Foo2 = cast<const int *>(Bar2);
+   const auto &Foo3 = cast<const int &>(Bar3);
 
-This check helps to enforce this `LLVM Coding Standards recommendation
-<https://llvm.org/docs/CodingStandards.html#beware-unnecessary-copies-with-auto>`_.
+   Note in the LLVM alias, the default value is `0`.
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 
+  `AddConstToQualified` to enable adding ``const`` qualifiers to variables
+  typed with ``auto *`` and ``auto &``.
+
 - 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),
+        AddConstToQualified(Options.get("AddConstToQualified", 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 AddConstToQualified;
 };
 
 } // 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, "AddConstToQualified", AddConstToQualified);
+}
+
 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 (!AddConstToQualified)
+    return;
   Finder->addMatcher(ExplicitSingleVarDecl(
                          hasType(pointerType(pointee(autoType()))), "auto_ptr"),
                      this);
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.AddConstToQualified"] = "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