From: Alberto Faria <afaria@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Stefano Garzarella" <sgarzare@redhat.com>,
"Hannes Reinecke" <hare@suse.com>,
"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
"Vladimir Sementsov-Ogievskiy" <vsementsov@yandex-team.ru>,
"Maciej S. Szmigiero" <maciej.szmigiero@oracle.com>,
"Peter Lieven" <pl@kamp.de>,
kvm@vger.kernel.org, "Xie Yongji" <xieyongji@bytedance.com>,
"Eric Auger" <eric.auger@redhat.com>,
"Hanna Reitz" <hreitz@redhat.com>,
"Jeff Cody" <codyprime@gmail.com>,
"Eric Blake" <eblake@redhat.com>,
"Denis V. Lunev" <den@openvz.org>,
"Daniel P. Berrangé" <berrange@redhat.com>,
"Philippe Mathieu-Daudé" <f4bug@amsat.org>,
"Christian Schoenebeck" <qemu_oss@crudebyte.com>,
"Stefan Weil" <sw@weilnetz.de>,
"Klaus Jensen" <its@irrelevant.dk>,
"Laurent Vivier" <lvivier@redhat.com>,
"Alberto Garcia" <berto@igalia.com>,
"Michael Roth" <michael.roth@amd.com>,
"Juan Quintela" <quintela@redhat.com>,
"David Hildenbrand" <david@redhat.com>,
qemu-block@nongnu.org, "Konstantin Kostiuk" <kkostiuk@redhat.com>,
"Kevin Wolf" <kwolf@redhat.com>,
"Gerd Hoffmann" <kraxel@redhat.com>,
"Stefan Hajnoczi" <stefanha@redhat.com>,
"Marcelo Tosatti" <mtosatti@redhat.com>,
"Greg Kurz" <groug@kaod.org>,
"Michael S. Tsirkin" <mst@redhat.com>,
"Amit Shah" <amit@kernel.org>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Alex Williamson" <alex.williamson@redhat.com>,
"Peter Xu" <peterx@redhat.com>,
"Raphael Norwitz" <raphael.norwitz@nutanix.com>,
"Ronnie Sahlberg" <ronniesahlberg@gmail.com>,
"Jason Wang" <jasowang@redhat.com>,
"Emanuele Giuseppe Esposito" <eesposit@redhat.com>,
"Richard Henderson" <richard.henderson@linaro.org>,
"Marcel Apfelbaum" <marcel.apfelbaum@gmail.com>,
"Dmitry Fleytman" <dmitry.fleytman@gmail.com>,
"Eduardo Habkost" <eduardo@habkost.net>,
"Fam Zheng" <fam@euphon.net>, "Thomas Huth" <thuth@redhat.com>,
"Keith Busch" <kbusch@kernel.org>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Richard W.M. Jones" <rjones@redhat.com>,
"John Snow" <jsnow@redhat.com>,
"Markus Armbruster" <armbru@redhat.com>,
"Alberto Faria" <afaria@redhat.com>
Subject: [RFC v2 03/10] static-analyzer: Support adding tests to checks
Date: Fri, 29 Jul 2022 14:00:32 +0100 [thread overview]
Message-ID: <20220729130040.1428779-4-afaria@redhat.com> (raw)
In-Reply-To: <20220729130040.1428779-1-afaria@redhat.com>
Introduce an add_check_tests() method to add output-comparison tests to
checks, and add some tests to the "return-value-never-used" check.
Signed-off-by: Alberto Faria <afaria@redhat.com>
---
static-analyzer.py | 133 ++++++++++++++++++++-
static_analyzer/__init__.py | 39 +++++-
static_analyzer/return_value_never_used.py | 103 ++++++++++++++++
3 files changed, 267 insertions(+), 8 deletions(-)
diff --git a/static-analyzer.py b/static-analyzer.py
index 3ade422dbf..16e331000d 100755
--- a/static-analyzer.py
+++ b/static-analyzer.py
@@ -11,17 +11,26 @@
import subprocess
import sys
import re
-from argparse import ArgumentParser, Namespace, RawDescriptionHelpFormatter
+from argparse import (
+ Action,
+ ArgumentParser,
+ Namespace,
+ RawDescriptionHelpFormatter,
+)
from multiprocessing import Pool
from pathlib import Path
+from tempfile import TemporaryDirectory
import textwrap
import time
+from traceback import print_exc
from typing import (
+ Callable,
Iterable,
Iterator,
List,
Mapping,
NoReturn,
+ Optional,
Sequence,
Union,
)
@@ -37,7 +46,7 @@
def parse_args() -> Namespace:
available_checks = "\n".join(
- f" {name} {' '.join((CHECKS[name].__doc__ or '').split())}"
+ f" {name} {' '.join((CHECKS[name].checker.__doc__ or '').split())}"
for name in sorted(CHECKS)
)
@@ -134,14 +143,37 @@ def parse_args() -> Namespace:
help="Do everything except actually running the checks.",
)
+ dev_options.add_argument(
+ "--test",
+ action=TestAction,
+ nargs=0,
+ help="Run tests for all checks and exit.",
+ )
+
# parse arguments
- args = parser.parse_args()
+ try:
+ args = parser.parse_args()
+ except TestSentinelError:
+ # --test was specified
+ if len(sys.argv) > 2:
+ parser.error("--test must be given alone")
+ return Namespace(test=True)
+
args.check_names = sorted(set(args.check_names or CHECKS))
return args
+class TestAction(Action):
+ def __call__(self, parser, namespace, values, option_string=None):
+ raise TestSentinelError
+
+
+class TestSentinelError(Exception):
+ pass
+
+
# ---------------------------------------------------------------------------- #
# Main
@@ -150,7 +182,11 @@ def main() -> int:
args = parse_args()
- if args.profile:
+ if args.test:
+
+ return test()
+
+ elif args.profile:
import cProfile
import pstats
@@ -170,6 +206,18 @@ def main() -> int:
return analyze(args)
+def test() -> int:
+
+ for (check_name, check) in CHECKS.items():
+ for group in check.test_groups:
+ for input in group.inputs:
+ run_test(
+ check_name, group.location, input, group.expected_output
+ )
+
+ return 0
+
+
def analyze(args: Namespace) -> int:
tr_units = get_translation_units(args)
@@ -247,6 +295,7 @@ class TranslationUnit:
build_command: str
system_include_paths: Sequence[str]
check_names: Sequence[str]
+ custom_printer: Optional[Callable[[str], None]]
def get_translation_units(args: Namespace) -> Sequence["TranslationUnit"]:
@@ -264,6 +313,7 @@ def get_translation_units(args: Namespace) -> Sequence["TranslationUnit"]:
build_command=cmd["command"],
system_include_paths=system_include_paths,
check_names=args.check_names,
+ custom_printer=None,
)
for cmd in compile_commands
)
@@ -380,7 +430,7 @@ def analyze_translation_unit(tr_unit: TranslationUnit) -> bool:
try:
for name in tr_unit.check_names:
- CHECKS[name](check_context)
+ CHECKS[name].checker(check_context)
except Exception as e:
raise RuntimeError(f"Error analyzing {check_context._rel_path}") from e
@@ -428,7 +478,9 @@ def get_check_context(tr_unit: TranslationUnit) -> CheckContext:
except clang.cindex.TranslationUnitLoadError as e:
raise RuntimeError(f"Failed to load {rel_path}") from e
- if sys.stdout.isatty():
+ if tr_unit.custom_printer is not None:
+ printer = tr_unit.custom_printer
+ elif sys.stdout.isatty():
# add padding to fully overwrite progress message
printer = lambda s: print(s.ljust(50))
else:
@@ -457,6 +509,75 @@ def get_check_context(tr_unit: TranslationUnit) -> CheckContext:
return check_context
+# ---------------------------------------------------------------------------- #
+# Tests
+
+
+def run_test(
+ check_name: str, location: str, input: str, expected_output: str
+) -> None:
+
+ with TemporaryDirectory() as temp_dir:
+
+ os.chdir(temp_dir)
+
+ input_path = Path(temp_dir) / "file.c"
+ input_path.write_text(input)
+
+ actual_output_list = []
+
+ tu_context = TranslationUnit(
+ absolute_path=str(input_path),
+ build_working_dir=str(input_path.parent),
+ build_command=f"cc {shlex.quote(str(input_path))}",
+ system_include_paths=[],
+ check_names=[check_name],
+ custom_printer=lambda s: actual_output_list.append(s + "\n"),
+ )
+
+ check_context = get_check_context(tu_context)
+
+ # analyze translation unit
+
+ try:
+
+ for name in tu_context.check_names:
+ CHECKS[name].checker(check_context)
+
+ except Exception:
+
+ print(
+ f"\033[0;31mTest defined at {location} raised an"
+ f" exception.\033[0m"
+ )
+ print(f"\033[0;31mInput:\033[0m")
+ print(input, end="")
+ print(f"\033[0;31mExpected output:\033[0m")
+ print(expected_output, end="")
+ print(f"\033[0;31mException:\033[0m")
+ print_exc(file=sys.stdout)
+ print(f"\033[0;31mAST:\033[0m")
+ check_context.print_tree(check_context.translation_unit.cursor)
+
+ sys.exit(1)
+
+ actual_output = "".join(actual_output_list)
+
+ if actual_output != expected_output:
+
+ print(f"\033[0;33mTest defined at {location} failed.\033[0m")
+ print(f"\033[0;33mInput:\033[0m")
+ print(input, end="")
+ print(f"\033[0;33mExpected output:\033[0m")
+ print(expected_output, end="")
+ print(f"\033[0;33mActual output:\033[0m")
+ print(actual_output, end="")
+ print(f"\033[0;33mAST:\033[0m")
+ check_context.print_tree(check_context.translation_unit.cursor)
+
+ sys.exit(3)
+
+
# ---------------------------------------------------------------------------- #
# Utilities
diff --git a/static_analyzer/__init__.py b/static_analyzer/__init__.py
index e6ca48d714..36028724b1 100644
--- a/static_analyzer/__init__.py
+++ b/static_analyzer/__init__.py
@@ -3,16 +3,20 @@
from ctypes import CFUNCTYPE, c_int, py_object
from dataclasses import dataclass
from enum import Enum
+import inspect
import os
import os.path
from pathlib import Path
from importlib import import_module
+import textwrap
from typing import (
Any,
Callable,
Dict,
+ Iterable,
List,
Optional,
+ Sequence,
Union,
)
@@ -218,18 +222,49 @@ def print_tree(
Checker = Callable[[CheckContext], None]
-CHECKS: Dict[str, Checker] = {}
+
+class CheckTestGroup:
+
+ inputs: Sequence[str]
+ expected_output: str
+
+ location: str
+
+ def __init__(self, inputs: Iterable[str], expected_output: str) -> None:
+ def reformat_string(s: str) -> str:
+ return "".join(
+ line + "\n" for line in textwrap.dedent(s).strip().splitlines()
+ )
+
+ self.inputs = [reformat_string(input) for input in inputs]
+ self.expected_output = reformat_string(expected_output)
+
+ caller = inspect.stack()[1]
+ self.location = f"{os.path.relpath(caller.filename)}:{caller.lineno}"
+
+
+@dataclass
+class Check:
+ checker: Checker
+ test_groups: List[CheckTestGroup]
+
+
+CHECKS: Dict[str, Check] = {}
def check(name: str) -> Callable[[Checker], Checker]:
def decorator(checker: Checker) -> Checker:
assert name not in CHECKS
- CHECKS[name] = checker
+ CHECKS[name] = Check(checker=checker, test_groups=[])
return checker
return decorator
+def add_check_tests(check_name: str, *groups: CheckTestGroup) -> None:
+ CHECKS[check_name].test_groups.extend(groups)
+
+
# ---------------------------------------------------------------------------- #
# Import all checks
diff --git a/static_analyzer/return_value_never_used.py b/static_analyzer/return_value_never_used.py
index 05c06cd4c2..de1a6542d1 100644
--- a/static_analyzer/return_value_never_used.py
+++ b/static_analyzer/return_value_never_used.py
@@ -11,7 +11,9 @@
from static_analyzer import (
CheckContext,
+ CheckTestGroup,
VisitorResult,
+ add_check_tests,
check,
might_have_attribute,
visit,
@@ -115,3 +117,104 @@ def get_parent_if_unexposed_expr(node: Cursor) -> Cursor:
# ---------------------------------------------------------------------------- #
+
+add_check_tests(
+ "return-value-never-used",
+ CheckTestGroup(
+ inputs=[
+ R"""
+ static void f(void) { }
+ """,
+ R"""
+ static __attribute__((unused)) int f(void) { }
+ """,
+ R"""
+ #define ATTR __attribute__((unused))
+ static __attribute__((unused)) int f(void) { }
+ """,
+ R"""
+ #define FUNC static __attribute__((unused)) int f(void) { }
+ FUNC
+ """,
+ R"""
+ static __attribute__((unused, constructor)) int f(void) { }
+ """,
+ R"""
+ static __attribute__((constructor, unused)) int f(void) { }
+ """,
+ R"""
+ static int f(void) { return 42; }
+ static void g(void) {
+ int x = f();
+ }
+ """,
+ R"""
+ static int f(void) { return 42; }
+ static void g(void) {
+ for (0; f(); 0) { }
+ }
+ """,
+ R"""
+ static int f(void) { return 42; }
+ static void g(void) {
+ for (f(); ; ) { }
+ }
+ """,
+ R"""
+ static int f(void) { return 42; }
+ static void g(void) {
+ for ( ; f(); ) { }
+ }
+ """,
+ R"""
+ static int f(void) { return 42; }
+ static void g(void) {
+ for ( ; ; f()) { }
+ }
+ """,
+ R"""
+ static int f(void) { return 42; }
+ static void g(void) {
+ int (*ptr)(void) = 0;
+ __atomic_store_n(&ptr, f, __ATOMIC_RELAXED);
+ }
+ """,
+ ],
+ expected_output=R"""
+ """,
+ ),
+ CheckTestGroup(
+ inputs=[
+ R"""
+ static int f(void) { return 42; }
+ """,
+ R"""
+ static int f(void) { return 42; }
+ static void g(void) {
+ f();
+ }
+ """,
+ R"""
+ static int f(void) { return 42; }
+ static void g(void) {
+ for (f(); 0; f()) { }
+ }
+ """,
+ ],
+ expected_output=R"""
+ file.c:1:12: f() return value is never used
+ """,
+ ),
+ CheckTestGroup(
+ inputs=[
+ R"""
+ static __attribute__((constructor)) int f(void) { }
+ """,
+ ],
+ expected_output=R"""
+ file.c:1:41: f() return value is never used
+ """,
+ ),
+)
+
+# ---------------------------------------------------------------------------- #
--
2.37.1
next prev parent reply other threads:[~2022-07-29 13:09 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-29 13:00 [RFC v2 00/10] Introduce an extensible static analyzer Alberto Faria
2022-07-29 13:00 ` [RFC v2 01/10] Add " Alberto Faria
2022-07-29 13:00 ` [RFC v2 02/10] Drop unused static function return values Alberto Faria
2022-08-03 10:46 ` Dr. David Alan Gilbert
2022-08-03 11:07 ` Alberto Faria
2022-08-03 11:15 ` Richard W.M. Jones
2022-08-03 11:37 ` Daniel P. Berrangé
2022-08-03 12:25 ` Peter Maydell
2022-08-03 12:47 ` Richard W.M. Jones
2022-08-12 16:29 ` Alberto Faria
2022-08-03 11:12 ` Daniel P. Berrangé
2022-08-03 12:30 ` Peter Maydell
2022-08-12 16:01 ` Alberto Faria
2022-07-29 13:00 ` Alberto Faria [this message]
2022-07-29 13:00 ` [RFC v2 04/10] static-analyzer: Avoid reanalyzing unmodified translation units Alberto Faria
2022-07-29 13:00 ` [RFC v2 05/10] static-analyzer: Enforce coroutine_fn restrictions for direct calls Alberto Faria
2022-07-29 13:00 ` [RFC v2 06/10] Fix some direct calls from non-coroutine_fn to coroutine_fn Alberto Faria
2022-07-29 13:00 ` [RFC v2 07/10] static-analyzer: Enforce coroutine_fn restrictions on function pointers Alberto Faria
2022-07-29 13:00 ` [RFC v2 08/10] Fix some bad coroutine_fn indirect calls and pointer assignments Alberto Faria
2022-07-29 13:00 ` [RFC v2 09/10] block: Add no_coroutine_fn marker Alberto Faria
2022-07-29 13:00 ` [RFC v2 10/10] Fix some calls from coroutine_fn to no_coroutine_fn Alberto Faria
2022-08-04 11:44 ` [RFC v2 00/10] Introduce an extensible static analyzer Marc-André Lureau
2022-08-12 15:48 ` Alberto Faria
2022-08-16 7:17 ` Marc-André Lureau
2022-10-13 22:00 ` Paolo Bonzini
2022-10-15 13:14 ` Christian Schoenebeck
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220729130040.1428779-4-afaria@redhat.com \
--to=afaria@redhat.com \
--cc=alex.bennee@linaro.org \
--cc=alex.williamson@redhat.com \
--cc=amit@kernel.org \
--cc=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=berto@igalia.com \
--cc=codyprime@gmail.com \
--cc=david@redhat.com \
--cc=den@openvz.org \
--cc=dgilbert@redhat.com \
--cc=dmitry.fleytman@gmail.com \
--cc=eblake@redhat.com \
--cc=eduardo@habkost.net \
--cc=eesposit@redhat.com \
--cc=eric.auger@redhat.com \
--cc=f4bug@amsat.org \
--cc=fam@euphon.net \
--cc=groug@kaod.org \
--cc=hare@suse.com \
--cc=hreitz@redhat.com \
--cc=its@irrelevant.dk \
--cc=jasowang@redhat.com \
--cc=jsnow@redhat.com \
--cc=kbusch@kernel.org \
--cc=kkostiuk@redhat.com \
--cc=kraxel@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=kwolf@redhat.com \
--cc=lvivier@redhat.com \
--cc=maciej.szmigiero@oracle.com \
--cc=marcandre.lureau@redhat.com \
--cc=marcel.apfelbaum@gmail.com \
--cc=michael.roth@amd.com \
--cc=mst@redhat.com \
--cc=mtosatti@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.com \
--cc=pl@kamp.de \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu_oss@crudebyte.com \
--cc=quintela@redhat.com \
--cc=raphael.norwitz@nutanix.com \
--cc=richard.henderson@linaro.org \
--cc=rjones@redhat.com \
--cc=ronniesahlberg@gmail.com \
--cc=sgarzare@redhat.com \
--cc=stefanha@redhat.com \
--cc=sw@weilnetz.de \
--cc=thuth@redhat.com \
--cc=vsementsov@yandex-team.ru \
--cc=xieyongji@bytedance.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).