This patch adds support to enable compiler static analyzer for
gcc and clang.

Signed-off-by: Amit Prakash Shukla <amitpraka...@marvell.com>
---
 config/meson.build                        | 14 ++++++++++++++
 doc/guides/prog_guide/index.rst           |  1 +
 doc/guides/prog_guide/static_analyzer.rst | 21 +++++++++++++++++++++
 meson_options.txt                         |  2 ++
 4 files changed, 38 insertions(+)
 create mode 100644 doc/guides/prog_guide/static_analyzer.rst

diff --git a/config/meson.build b/config/meson.build
index 7f7b6c92fd..1154396326 100644
--- a/config/meson.build
+++ b/config/meson.build
@@ -440,6 +440,20 @@ if get_option('b_sanitize') == 'address' or 
get_option('b_sanitize') == 'address
     endif
 endif
 
+if get_option('static_analyzer')
+    if cc.get_id() == 'gcc' and cc.version().version_compare('>=10.1.0')
+        add_project_arguments('-fanalyzer', language: 'c')
+    elif cc.get_id() == 'clang' and cc.version().version_compare('>=9.0.1')
+        add_project_arguments('--analyze', language: 'c')
+        add_project_arguments('-Xanalyzer', language: 'c')
+        add_project_arguments('-analyzer-output=text', language: 'c')
+    elif cc.get_id() == 'gcc' or cc.get_id() == 'clang'
+        error('Static analysis not supported for current gcc/clang version.')
+    else
+        error('Compiler does not support static analysis')
+    endif
+endif
+
 if get_option('default_library') == 'both'
     error( '''
  Unsupported value "both" for "default_library" option.
diff --git a/doc/guides/prog_guide/index.rst b/doc/guides/prog_guide/index.rst
index 8564883018..9f6d98904a 100644
--- a/doc/guides/prog_guide/index.rst
+++ b/doc/guides/prog_guide/index.rst
@@ -76,4 +76,5 @@ Programmer's Guide
     lto
     profile_app
     asan
+    static_analyzer
     glossary
diff --git a/doc/guides/prog_guide/static_analyzer.rst 
b/doc/guides/prog_guide/static_analyzer.rst
new file mode 100644
index 0000000000..11260eebe7
--- /dev/null
+++ b/doc/guides/prog_guide/static_analyzer.rst
@@ -0,0 +1,21 @@
+.. SPDX-License-Identifier: BSD-3-Clause
+   Copyright(c) 2022 Marvell
+
+Running Static Analyzer
+========================
+Static analyzer is a compiler feature which when enabled scans through the 
source
+code to try and find various problems at compile-time, rather than at runtime.
+
+Static analyzer is a part of clang (9.0.1+) and GCC (10.1.0+).
+
+`GCC Static analyzer document
+<https://gcc.gnu.org/onlinedocs/gcc-10.1.0/gcc/Static-Analyzer-Options.html>`_
+
+`Clang static analyzer document
+<https://releases.llvm.org/9.0.1/tools/clang/docs/ClangStaticAnalyzer.html>`_
+
+Enabling 'Static analyzer' is done by passing the -Dstatic_analyzer=true 
option to
+the meson build system. By-default static analyzer is disabled.
+
+Example::
+  - meson setup -Dstatic_analyzer=true <build_dir>
diff --git a/meson_options.txt b/meson_options.txt
index 7c220ad68d..fde9a579a0 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -48,3 +48,5 @@ option('tests', type: 'boolean', value: true, description:
        'build unit tests')
 option('use_hpet', type: 'boolean', value: false, description:
        'use HPET timer in EAL')
+option('static_analyzer', type: 'boolean', value: false, description:
+       'Compile time static analyses for gcc-10.1.0+ and clang-9.0.1+')
-- 
2.25.1

Reply via email to