[PATCH] D159436: [clang-tidy] Add support for optional parameters in config.

FĂ©lix-Antoine Constantin via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 4 19:01:49 PDT 2023


felix642 created this revision.
Herald added subscribers: PiotrZSL, xazax.hun.
Herald added a reviewer: njames93.
Herald added a project: All.
felix642 requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

If a parameter value is either 'none', 'null', 'false', '-1' or '', we will in that case use the default value


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D159436

Files:
  clang-tools-extra/clang-tidy/ClangTidyCheck.h
  clang-tools-extra/test/clang-tidy/infrastructure/optional-parameter.cpp


Index: clang-tools-extra/test/clang-tidy/infrastructure/optional-parameter.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/infrastructure/optional-parameter.cpp
@@ -0,0 +1,24 @@
+// RUN: %check_clang_tidy %s bugprone-easily-swappable-parameters %t \
+// RUN:   -config='{CheckOptions: { \
+// RUN:     bugprone-easily-swappable-parameters.MinimumLength: "", \
+// RUN:  }}' --
+
+// RUN: %check_clang_tidy %s bugprone-easily-swappable-parameters %t \
+// RUN:   -config='{CheckOptions: { \
+// RUN:     bugprone-easily-swappable-parameters.MinimumLength: "none", \
+// RUN:  }}' --
+
+// RUN: %check_clang_tidy %s bugprone-easily-swappable-parameters %t \
+// RUN:   -config='{CheckOptions: { \
+// RUN:     bugprone-easily-swappable-parameters.MinimumLength: "null", \
+// RUN:  }}' --
+
+// RUN: %check_clang_tidy %s bugprone-easily-swappable-parameters %t \
+// RUN:   -config='{CheckOptions: { \
+// RUN:     bugprone-easily-swappable-parameters.MinimumLength: "false", \
+// RUN:  }}' --
+
+void a(int b, int c) {}
+// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: 2 adjacent parameters of 'a' of similar type ('int') are easily swapped by mistake [bugprone-easily-swappable-parameters]
+// CHECK-MESSAGES: :[[@LINE-2]]:12: note: the first parameter in the range is 'b'
+// CHECK-MESSAGES: :[[@LINE-3]]:19: note: the last parameter in the range is 'c'
Index: clang-tools-extra/clang-tidy/ClangTidyCheck.h
===================================================================
--- clang-tools-extra/clang-tidy/ClangTidyCheck.h
+++ clang-tools-extra/clang-tidy/ClangTidyCheck.h
@@ -184,8 +184,8 @@
     /// integral type ``T``.
     ///
     /// Reads the option with the check-local name \p LocalName from the
-    /// ``CheckOptions``. If the corresponding key is not present, return
-    /// ``std::nullopt``.
+    /// ``CheckOptions``. If the corresponding key is not present or empty,
+    ///  return ``std::nullopt``.
     ///
     /// If the corresponding key can't be parsed as a ``T``, emit a
     /// diagnostic and return ``std::nullopt``.
@@ -193,6 +193,9 @@
     std::enable_if_t<std::is_integral_v<T>, std::optional<T>>
     get(StringRef LocalName) const {
       if (std::optional<StringRef> Value = get(LocalName)) {
+        if (Value == "" || Value == "none" || Value == "null" ||
+          Value == "false" || (std::is_unsigned_v<T> && Value == "-1"))
+              return std::nullopt;
         T Result{};
         if (!StringRef(*Value).getAsInteger(10, Result))
           return Result;
@@ -286,8 +289,8 @@
     /// enum type ``T``.
     ///
     /// Reads the option with the check-local name \p LocalName from the
-    /// ``CheckOptions``. If the corresponding key is not present, return
-    /// \p Default.
+    /// ``CheckOptions``. If the corresponding key is not present or empty,
+    /// return \p Default.
     ///
     /// If the corresponding key can't be parsed as a ``T``, emit a
     /// diagnostic and return \p Default.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D159436.555787.patch
Type: text/x-patch
Size: 3037 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230905/ff6a48ea/attachment.bin>


More information about the cfe-commits mailing list