diff mbox series

build: enable static analyzer

Message ID 20220714072722.879156-1-amitprakashs@marvell.com (mailing list archive)
State New
Delegated to: Thomas Monjalon
Headers show
Series build: enable static analyzer | expand

Checks

Context Check Description
ci/iol-aarch64-compile-testing success Testing PASS
ci/iol-abi-testing success Testing PASS
ci/github-robot: build success github build: passed
ci/iol-x86_64-compile-testing success Testing PASS
ci/iol-x86_64-unit-testing success Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-aarch64-unit-testing success Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/Intel-compilation success Compilation OK
ci/checkpatch success coding style OK

Commit Message

Amit Prakash Shukla July 14, 2022, 7:27 a.m. UTC
This patch adds support to enable compiler static analyzer for
gcc and clang.

Signed-off-by: Amit Prakash Shukla <amitprakashs@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

Comments

Bruce Richardson July 14, 2022, 11:01 a.m. UTC | #1
On Thu, Jul 14, 2022 at 12:57:22PM +0530, Amit Prakash Shukla wrote:
> This patch adds support to enable compiler static analyzer for
> gcc and clang.
> 
> Signed-off-by: Amit Prakash Shukla <amitprakashs@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')

Rather than checking for specific compiler versions, we should instead just
check for particular flags and use those.

> +    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+')
> -- 

Rather than adding support for this through DPDK project itself, this might
be better added to meson directly so that other projects can also benefit
from it. It also would fit in well with other meson options such as the
ASAN support, coverage support, or PGO support.

Regards,
/Bruce
Amit Prakash Shukla July 15, 2022, 12:05 p.m. UTC | #2
Thanks Bruce for the review. Please find my comments in-line.

> -----Original Message-----
> From: Bruce Richardson <bruce.richardson@intel.com>
> Sent: Thursday, July 14, 2022 4:31 PM
> To: Amit Prakash Shukla <amitprakashs@marvell.com>
> Cc: dev@dpdk.org; Jerin Jacob Kollanukkaran <jerinj@marvell.com>;
> thomas@monjalon.net; david.marchand@redhat.com;
> dmitry.kozliuk@gmail.com; navasile@linux.microsoft.com;
> dmitrym@microsoft.com
> Subject: [EXT] Re: [PATCH] build: enable static analyzer
> 
> External Email
> 
> ----------------------------------------------------------------------
> On Thu, Jul 14, 2022 at 12:57:22PM +0530, Amit Prakash Shukla wrote:
> > This patch adds support to enable compiler static analyzer for gcc and
> > clang.
> >
> > Signed-off-by: Amit Prakash Shukla <amitprakashs@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')
> 
> Rather than checking for specific compiler versions, we should instead just
> check for particular flags and use those.

Sure, I will make the change as part of v2 patch.

> 
> > +    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://urldefense.proofpoint.com/v2/url?u=https-
> 3A__gcc.gnu.org_onl
> > +inedocs_gcc-2D10.1.0_gcc_Static-2DAnalyzer-
> 2DOptions.html&d=DwIBAg&c=
> >
> +nKjWec2b6R0mOyPaz7xtfQ&r=ALGdXl3fZgFGR69VnJLdSnADun7zLaXG1p5R
> s7pXihE&
> > +m=41ZGerooHPN2LY2ibMFNcjUVJscn-
> MehdDhTHMaoqddHVciH516PLZOrDEhur1ra&s=
> > +v1pD8igta4HfoQgJlCAmOwZCo3PJ-b2_NXk0ODPEoms&e= >`_
> > +
> > +`Clang static analyzer document
> > +<https://urldefense.proofpoint.com/v2/url?u=https-3A__releases.llvm.o
> >
> +rg_9.0.1_tools_clang_docs_ClangStaticAnalyzer.html&d=DwIBAg&c=nKjWe
> c2
> >
> +b6R0mOyPaz7xtfQ&r=ALGdXl3fZgFGR69VnJLdSnADun7zLaXG1p5Rs7pXihE&
> m=41ZGe
> > +rooHPN2LY2ibMFNcjUVJscn-
> MehdDhTHMaoqddHVciH516PLZOrDEhur1ra&s=J2lNXZy
> > +id-zCxLgclayGzcTbrof1athBH6Rn_MAGPt4&e= >`_
> > +
> > +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+')
> > --
> 
> Rather than adding support for this through DPDK project itself, this might be
> better added to meson directly so that other projects can also benefit from
> it. It also would fit in well with other meson options such as the ASAN
> support, coverage support, or PGO support.

Sure, will remove it from dpdk in v2 patch and add it to meson.

> 
> Regards,
> /Bruce

Thanks,
Amit Shukla
diff mbox series

Patch

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+')