[clang] [analyzer] Add documentation for std::variant checker (PR #76501)

Gábor Spaits via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 28 08:11:52 PST 2023


https://github.com/spaits updated https://github.com/llvm/llvm-project/pull/76501

>From b271420a6e4ed23abf2749d767f986aa16d9fe9d Mon Sep 17 00:00:00 2001
From: Gabor Spaits <gaborspaits1 at gmail.com>
Date: Thu, 28 Dec 2023 14:17:55 +0100
Subject: [PATCH 1/3] [analyzer] Add documentation for std::variant checker

---
 clang/docs/ReleaseNotes.rst      |  1 +
 clang/docs/analyzer/checkers.rst | 15 +++++++++++++++
 2 files changed, 16 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e21ec78a1e8a77..bf63cc981eb32d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -1072,6 +1072,7 @@ New features
 
 - Added a new experimental checker ``alpha.core.StdVariant`` to detect variant
   accesses via wrong alternatives.
+  `Documentation <https://clang.llvm.org/docs/analyzer/checkers.html#alpha-core-stdvariantchecker-c>`__.
   (`#66481 <https://github.com/llvm/llvm-project/pull/66481>`_)
 
 - Added a new experimental checker ``alpha.cplusplus.ArrayDelete`` to detect
diff --git a/clang/docs/analyzer/checkers.rst b/clang/docs/analyzer/checkers.rst
index 64a4752695f11f..94c712f63d5ba3 100644
--- a/clang/docs/analyzer/checkers.rst
+++ b/clang/docs/analyzer/checkers.rst
@@ -2095,6 +2095,21 @@ This checker is a part of ``core.StackAddressEscape``, but is temporarily disabl
                  //       returned block
  }
 
+.. _alpha-core-StdVariant:
+
+alpha.core.StdVariant (C++)
+"""""""""""""""""""""""""""
+Check if a value of active type is retrieved from an ``std::variant`` instance with ``std::get``.
+In case of bad variant type access(the accessed type differs from the active type)
+a warning is emitted. Currently this checker does not take exception handling into account.
+
+.. code-block:: cpp
+
+ void test() {
+   std::variant <int, char> v = 25;
+   char c = stg::get<char>(v); // warn
+ }
+
 .. _alpha-core-TestAfterDivZero:
 
 alpha.core.TestAfterDivZero (C)

>From 450fd58fd64e64b2a0df6cb90b6c232b2d84ece6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20Spaits?=
 <48805437+spaits at users.noreply.github.com>
Date: Thu, 28 Dec 2023 17:11:39 +0100
Subject: [PATCH 2/3] Update clang/docs/analyzer/checkers.rst

Co-authored-by: Balazs Benics <benicsbalazs at gmail.com>
---
 clang/docs/analyzer/checkers.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/docs/analyzer/checkers.rst b/clang/docs/analyzer/checkers.rst
index 94c712f63d5ba3..7903ad75bcb831 100644
--- a/clang/docs/analyzer/checkers.rst
+++ b/clang/docs/analyzer/checkers.rst
@@ -2101,7 +2101,7 @@ alpha.core.StdVariant (C++)
 """""""""""""""""""""""""""
 Check if a value of active type is retrieved from an ``std::variant`` instance with ``std::get``.
 In case of bad variant type access(the accessed type differs from the active type)
-a warning is emitted. Currently this checker does not take exception handling into account.
+a warning is emitted. Currently, this checker does not take exception handling into account.
 
 .. code-block:: cpp
 

>From 6ba5052007ef724e90f6665d6df350ee6d9aa1f9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20Spaits?=
 <48805437+spaits at users.noreply.github.com>
Date: Thu, 28 Dec 2023 17:11:46 +0100
Subject: [PATCH 3/3] Update clang/docs/analyzer/checkers.rst

Co-authored-by: Balazs Benics <benicsbalazs at gmail.com>
---
 clang/docs/analyzer/checkers.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/docs/analyzer/checkers.rst b/clang/docs/analyzer/checkers.rst
index 7903ad75bcb831..eb01fcf8020802 100644
--- a/clang/docs/analyzer/checkers.rst
+++ b/clang/docs/analyzer/checkers.rst
@@ -2107,7 +2107,7 @@ a warning is emitted. Currently, this checker does not take exception handling i
 
  void test() {
    std::variant <int, char> v = 25;
-   char c = stg::get<char>(v); // warn
+   char c = stg::get<char>(v); // warn: "int" is the active alternative
  }
 
 .. _alpha-core-TestAfterDivZero:



More information about the cfe-commits mailing list