Skip to content

[clang-tidy] add AllowedTypes option to misc-const-correctness #122951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

vbvictor
Copy link
Contributor

@vbvictor vbvictor commented Jan 14, 2025

Add option AllowedTypes which allow users to specify types they want to exclude from const-correctness check.

Small real-world example:

#include <mutex>

int main() {
  std::mutex m;
  std::lock_guard<std::mutex> l(m); // we want to ignore it since std::lock_guard is already immutable.
}

Closes issue #122592

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Jan 14, 2025

@llvm/pr-subscribers-clang-tools-extra

@llvm/pr-subscribers-clang-tidy

Author: Baranov Victor (vbvictor)

Changes

This PR add option AllowedTypes which allow users to specify types they want to exclude from const-correctness check.

Small real-world example:

#include &lt;mutex&gt;

int main() {
  std::mutex m;
  std::lock_guard&lt;std::mutex&gt; l(m); // we want to ignore it since std::lock_guard is already immutable.
}

Closes issue #122592


Full diff: https://github.com/llvm/llvm-project/pull/122951.diff

5 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp (+16-2)
  • (modified) clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h (+2)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+5)
  • (modified) clang-tools-extra/docs/clang-tidy/checks/misc/const-correctness.rst (+10)
  • (added) clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-allowed-types.cpp (+180)
diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
index 71a4cee4bdc6ef..aee4a3b789863c 100644
--- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
@@ -8,6 +8,8 @@
 
 #include "ConstCorrectnessCheck.h"
 #include "../utils/FixItHintUtils.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/ASTMatchers/ASTMatchers.h"
@@ -41,7 +43,9 @@ ConstCorrectnessCheck::ConstCorrectnessCheck(StringRef Name,
       TransformValues(Options.get("TransformValues", true)),
       TransformReferences(Options.get("TransformReferences", true)),
       TransformPointersAsValues(
-          Options.get("TransformPointersAsValues", false)) {
+          Options.get("TransformPointersAsValues", false)),
+      AllowedTypes(
+          utils::options::parseStringList(Options.get("AllowedTypes", ""))) {
   if (AnalyzeValues == false && AnalyzeReferences == false)
     this->configurationDiag(
         "The check 'misc-const-correctness' will not "
@@ -57,6 +61,9 @@ void ConstCorrectnessCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
   Options.store(Opts, "TransformValues", TransformValues);
   Options.store(Opts, "TransformReferences", TransformReferences);
   Options.store(Opts, "TransformPointersAsValues", TransformPointersAsValues);
+
+  Options.store(Opts, "AllowedTypes",
+                utils::options::serializeStringList(AllowedTypes));
 }
 
 void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) {
@@ -73,6 +80,12 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) {
       hasType(referenceType(pointee(hasCanonicalType(templateTypeParmType())))),
       hasType(referenceType(pointee(substTemplateTypeParmType()))));
 
+  const auto AllowedType = hasType(qualType(anyOf(
+      hasDeclaration(namedDecl(matchers::matchesAnyListedName(AllowedTypes))),
+      references(namedDecl(matchers::matchesAnyListedName(AllowedTypes))),
+      pointerType(pointee(hasDeclaration(
+          namedDecl(matchers::matchesAnyListedName(AllowedTypes))))))));
+
   const auto AutoTemplateType = varDecl(
       anyOf(hasType(autoType()), hasType(referenceType(pointee(autoType()))),
             hasType(pointerType(pointee(autoType())))));
@@ -87,7 +100,8 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) {
       unless(anyOf(ConstType, ConstReference, TemplateType,
                    hasInitializer(isInstantiationDependent()), AutoTemplateType,
                    RValueReference, FunctionPointerRef,
-                   hasType(cxxRecordDecl(isLambda())), isImplicit())));
+                   hasType(cxxRecordDecl(isLambda())), isImplicit(),
+                   AllowedType)));
 
   // Match the function scope for which the analysis of all local variables
   // shall be run.
diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h
index bba060e555d001..3b7aba7c4be384 100644
--- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h
+++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h
@@ -45,6 +45,8 @@ class ConstCorrectnessCheck : public ClangTidyCheck {
   const bool TransformValues;
   const bool TransformReferences;
   const bool TransformPointersAsValues;
+
+  const std::vector<StringRef> AllowedTypes;
 };
 
 } // namespace clang::tidy::misc
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index fa3a8e577a33ad..16be70ca944e95 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -260,6 +260,11 @@ Changes in existing checks
   <clang-tidy/checks/cppcoreguidelines/pro-type-union-access>` check to
   report a location even when the member location is not valid.
 
+- Improved :doc:`misc-const-correctness
+  <clang-tidy/checks/misc/const-correctness>` check by adding
+  the option ``AllowedTypes``, that excludes specified types
+  from const-correctness checking.
+
 - Improved :doc:`misc-definitions-in-headers
   <clang-tidy/checks/misc/definitions-in-headers>` check by rewording the
   diagnostic note that suggests adding ``inline``.
diff --git a/clang-tools-extra/docs/clang-tidy/checks/misc/const-correctness.rst b/clang-tools-extra/docs/clang-tidy/checks/misc/const-correctness.rst
index 8ac1ad56bc8cf7..201f6252816f04 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/misc/const-correctness.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/misc/const-correctness.rst
@@ -196,3 +196,13 @@ Options
     // The following pointer may not become a 'int *const'.
     int *changing_pointee = &value;
     changing_pointee = &result;
+
+.. option:: AllowedTypes (default = '')
+
+  A semicolon-separated list of names of types that
+  will be excluded from const-correctness checking.
+  Regular expressions are accepted, e.g. `[Rr]ef(erence)?$` matches every type
+  with suffix `Ref`, `ref`, `Reference` and `reference`.
+  If a name in the list contains the sequence `::` it is matched against
+  the qualified typename (i.e. `namespace::Type`), otherwise it is matched
+  against only the type name (i.e. `Type`).
\ No newline at end of file
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-allowed-types.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-allowed-types.cpp
new file mode 100644
index 00000000000000..a73b4a08d0a711
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-allowed-types.cpp
@@ -0,0 +1,180 @@
+// RUN: %check_clang_tidy %s misc-const-correctness %t -- \
+// RUN:   -config="{CheckOptions: {\
+// RUN:     misc-const-correctness.AllowedTypes: '[Pp]ointer$;[Pp]tr$;[Rr]ef(erence)?$;qualified::Type;::fully::QualifiedType;ConstTemplate', \
+// RUN:     misc-const-correctness.TransformPointersAsValues: true, \
+// RUN:     misc-const-correctness.TransformReferences: true, \
+// RUN:     misc-const-correctness.WarnPointersAsValues: true } \
+// RUN:   }" -- -fno-delayed-template-parsing
+
+struct SmartPointer {
+};
+
+struct smart_pointer {
+};
+
+struct SmartPtr {
+};
+
+struct smart_ptr {
+};
+
+struct SmartReference {
+};
+
+struct smart_reference {
+};
+
+struct SmartRef {
+};
+
+struct smart_ref {
+};
+
+struct OtherType {
+};
+
+template <typename T> struct ConstTemplate {
+};
+
+namespace qualified {
+struct Type {
+};
+} // namespace qualified
+
+namespace fully {
+struct QualifiedType {
+};
+} // namespace fully
+
+void negativeSmartPointer() {
+  SmartPointer p1 = {};
+  SmartPointer* p2 = {};
+  SmartPointer& p3 = p1;
+}
+
+void negative_smart_pointer() {
+  smart_pointer p1 = {};
+  smart_pointer* p2 = {};
+  smart_pointer& p3 = p1;
+}
+
+void negativeSmartPtr() {
+  SmartPtr p1 = {};
+  SmartPtr* p2 = {};
+  SmartPtr& p3 = p1;
+}
+
+void negative_smart_ptr() {
+  smart_ptr p1 = {};
+  smart_ptr* p2 = {};
+  smart_ptr& p3 = p1;
+}
+
+void negativeSmartReference() {
+  SmartReference p1 = {};
+  SmartReference* p2 = {};
+  SmartReference& p3 = p1;
+}
+
+void negative_smart_reference() {
+  smart_reference p1 = {};
+  smart_reference* p2 = {};
+  smart_reference& p3 = p1;
+}
+
+void negativeSmartRef() {
+  SmartRef p1 = {};
+  SmartRef* p2 = {};
+  SmartRef& p3 = p1;
+}
+
+void negative_smart_ref() {
+  smart_ref p1 = {};
+  smart_ref* p2 = {};
+  smart_ref& p3 = p1;
+}
+
+void positiveOtherType() {
+  OtherType t = {};
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 't' of type 'OtherType' can be declared 'const'
+  // CHECK-FIXES: OtherType const t = {};
+}
+
+void negativeSomeComplex() {
+   ConstTemplate<int> t1 = {};
+   ConstTemplate<int>* t2 = {};
+   ConstTemplate<int>& t3 = t1;
+}
+
+void negativeQualified() {
+  qualified::Type t1 = {};
+  qualified::Type* t2 = {};
+  qualified::Type& t3 = t1;
+
+  using qualified::Type;
+  Type t4 = {};
+  Type* t5 = {};
+  Type& t6 = t4;
+}
+
+void negativeFullyQualified() {
+  fully::QualifiedType t1 = {};
+  fully::QualifiedType* t2 = {};
+  fully::QualifiedType& t3 = t1;
+
+  using fully::QualifiedType;
+  QualifiedType t4 = {};
+  QualifiedType* t5 = {};
+  QualifiedType& t6 = t4;
+}
+
+using MySP = SmartPointer;
+using MyTemplate = ConstTemplate<int>;
+template <typename T> using MyTemplate2 = ConstTemplate<T>;
+
+void positiveTypedefs() {
+  MySP p1 = {};
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p1' of type 'MySP' (aka 'SmartPointer') can be declared 'const'
+  // CHECK-FIXES: MySP const p1 = {};
+  
+  MySP* p2 = {};
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p2' of type 'MySP *' (aka 'SmartPointer *') can be declared 'const'
+  // CHECK-FIXES: MySP* const p2 = {};
+
+  MySP& p3 = p1;
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p3' of type 'MySP &' (aka 'SmartPointer &') can be declared 'const'
+  // CHECK-FIXES: MySP const& p3 = p1;
+
+  MyTemplate t1 = {};
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 't1' of type 'MyTemplate' (aka 'ConstTemplate<int>') can be declared 'const'
+  // CHECK-FIXES: MyTemplate const t1 = {};
+
+  MyTemplate* t2 = {};
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 't2' of type 'MyTemplate *' (aka 'ConstTemplate<int> *') can be declared 'const'
+  // CHECK-FIXES: MyTemplate* const t2 = {};
+
+  MyTemplate& t3 = t1;
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 't3' of type 'MyTemplate &' (aka 'ConstTemplate<int> &') can be declared 'const'
+  // CHECK-FIXES: MyTemplate const& t3 = t1;
+
+  MyTemplate2<int> t4 = {};
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 't4' of type 'MyTemplate2<int>' (aka 'ConstTemplate<int>') can be declared 'const'
+  // CHECK-FIXES: MyTemplate2<int> const t4 = {};
+
+  MyTemplate2<int>* t5 = {};
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 't5' of type 'MyTemplate2<int> *' (aka 'ConstTemplate<int> *') can be declared 'const'
+  // CHECK-FIXES: MyTemplate2<int>* const t5 = {};
+
+  MyTemplate2<int>& t6 = t4;
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 't6' of type 'MyTemplate2<int> &' (aka 'ConstTemplate<int> &') can be declared 'const'
+  // CHECK-FIXES: MyTemplate2<int> const& t6 = t4;
+}
+
+template <typename T>
+class Vector {};
+
+void positiveSmartPtrWrapped() {
+  Vector<SmartPtr> vec = {};
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'vec' of type 'Vector<SmartPtr>' can be declared 'const'
+  // CHECK-FIXES: Vector<SmartPtr> const vec = {};
+}

@vbvictor vbvictor changed the title [clang-tidy] add AllowedTypes to misc-const-correctness [clang-tidy] add AllowedTypes option to misc-const-correctness Jan 14, 2025
@vbvictor
Copy link
Contributor Author

Ping

@EugeneZelenko
Copy link
Contributor

Please also fix preceding option defaults.

@vbvictor
Copy link
Contributor Author

Please also fix preceding option defaults.

Fixed preceding option defaults

@vbvictor vbvictor force-pushed the misc-const-correctness-allowed-types-opt branch from 4d8b4f7 to 44abe93 Compare January 31, 2025 16:42
@vbvictor
Copy link
Contributor Author

vbvictor commented Feb 7, 2025

Ping

Copy link
Contributor

@HerrCai0907 HerrCai0907 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@vbvictor vbvictor force-pushed the misc-const-correctness-allowed-types-opt branch from 360df60 to 24d12e6 Compare February 12, 2025 16:56
@vbvictor
Copy link
Contributor Author

@HerrCai0907, I rebased on main, so no more conflicts.
I think this PR can also be merged, could you help please?

@vbvictor
Copy link
Contributor Author

@HerrCai0907 Ping, please address #122951 (comment) when you have time.
Thank you in advance!

Copy link
Contributor

@HerrCai0907 HerrCai0907 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@HerrCai0907 HerrCai0907 merged commit 8363b0a into llvm:main Feb 19, 2025
9 checks passed
Copy link

@vbvictor Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@HerrCai0907 HerrCai0907 linked an issue Mar 4, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-tidy] Add AllowedTypes option to misc-const-correctness
4 participants