From ef002658deb645ea299fa3336caf3d0df432f943 Mon Sep 17 00:00:00 2001 From: firewave Date: Fri, 1 Mar 2024 18:24:41 +0100 Subject: [PATCH 1/4] added environment variable `TEST_CPPCHECK_INJECT_CLANG` to inject `--clang` into the cppcheck invocation of Python tests --- .github/workflows/CI-unixish.yml | 9 +++++++++ .github/workflows/CI-windows.yml | 9 +++++++++ .github/workflows/asan.yml | 9 +++++++++ .github/workflows/tsan.yml | 9 +++++++++ .github/workflows/ubsan.yml | 9 +++++++++ test/cli/testutils.py | 10 ++++++++++ 6 files changed, 55 insertions(+) diff --git a/.github/workflows/CI-unixish.yml b/.github/workflows/CI-unixish.yml index 22e5bddfdcf..f0393093701 100644 --- a/.github/workflows/CI-unixish.yml +++ b/.github/workflows/CI-unixish.yml @@ -405,6 +405,15 @@ jobs: env: TEST_CPPCHECK_INJECT_J: 2 + # do not use pushd in this step since we go below the working directory + - name: Run test/cli (--clang) + if: false + run: | + cd test/cli + python3 -m pytest -Werror --strict-markers -vv + env: + TEST_CPPCHECK_INJECT_CLANG: clang + - name: Run cfg tests if: matrix.os != 'ubuntu-22.04' run: | diff --git a/.github/workflows/CI-windows.yml b/.github/workflows/CI-windows.yml index a22dbf1b22f..53458b87701 100644 --- a/.github/workflows/CI-windows.yml +++ b/.github/workflows/CI-windows.yml @@ -182,6 +182,15 @@ jobs: env: TEST_CPPCHECK_INJECT_J: 2 + # TODO: install clang + - name: Run test/cli (--clang) + if: false # matrix.config == 'release' + run: | + cd test/cli || exit /b !errorlevel! + python -m pytest -Werror --strict-markers -vv || exit /b !errorlevel! + env: + TEST_CPPCHECK_INJECT_CLANG: clang + - name: Test addons if: matrix.config == 'release' run: | diff --git a/.github/workflows/asan.yml b/.github/workflows/asan.yml index a8eade22368..bc0aa9468b0 100644 --- a/.github/workflows/asan.yml +++ b/.github/workflows/asan.yml @@ -108,6 +108,15 @@ jobs: env: TEST_CPPCHECK_INJECT_J: 2 + - name: Run test/cli (--clang) + if: false + run: | + pwd=$(pwd) + cd test/cli + TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv + env: + TEST_CPPCHECK_INJECT_CLANG: clang + - name: Generate dependencies if: false run: | diff --git a/.github/workflows/tsan.yml b/.github/workflows/tsan.yml index 6c510517c56..8ace8797f25 100644 --- a/.github/workflows/tsan.yml +++ b/.github/workflows/tsan.yml @@ -107,6 +107,15 @@ jobs: env: TEST_CPPCHECK_INJECT_J: 2 + - name: Run test/cli (--clang) + if: false + run: | + pwd=$(pwd) + cd test/cli + TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv + env: + TEST_CPPCHECK_INJECT_CLANG: clang + - name: Generate dependencies if: false run: | diff --git a/.github/workflows/ubsan.yml b/.github/workflows/ubsan.yml index 68f73e640b5..a1951adc406 100644 --- a/.github/workflows/ubsan.yml +++ b/.github/workflows/ubsan.yml @@ -107,6 +107,15 @@ jobs: env: TEST_CPPCHECK_INJECT_J: 2 + - name: Run test/cli (--clang) + if: false + run: | + pwd=$(pwd) + cd test/cli + TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv + env: + TEST_CPPCHECK_INJECT_CLANG: clang + - name: Generate dependencies run: | # make sure auto-generated GUI files exist diff --git a/test/cli/testutils.py b/test/cli/testutils.py index 48ac83d0a7c..b076f27d4d2 100644 --- a/test/cli/testutils.py +++ b/test/cli/testutils.py @@ -87,6 +87,16 @@ def cppcheck(args, env=None, remove_checkers_report=True, cwd=None, cppcheck_exe arg_j = '-j' + str(os.environ['TEST_CPPCHECK_INJECT_J']) args.append(arg_j) + if 'TEST_CPPCHECK_INJECT_CLANG' in os.environ: + found_clang = False + for arg in args: + if arg.startswith('--clang'): + found_clang = True + break + if not found_clang: + arg_clang = '--clang=' + str(os.environ['TEST_CPPCHECK_INJECT_CLANG']) + args.append(arg_clang) + logging.info(exe + ' ' + ' '.join(args)) p = subprocess.Popen([exe] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env, cwd=cwd) try: From 0c8dade6fbb5c4be5df777ff2be1402b5cf32fe3 Mon Sep 17 00:00:00 2001 From: firewave Date: Fri, 1 Mar 2024 18:33:49 +0100 Subject: [PATCH 2/4] Cppcheck: improved `--clang` error reporting --- lib/cppcheck.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index e9610531ed3..b9b2814ac34 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -470,9 +470,17 @@ unsigned int CppCheck::checkClang(const std::string &path) } std::string output2; - if (mExecuteCommand(exe,split(args2),redirect2,output2) != EXIT_SUCCESS || output2.find("TranslationUnitDecl") == std::string::npos) { - std::cerr << "Failed to execute '" << exe << " " << args2 << " " << redirect2 << "'" << std::endl; - return 0; + const int exitcode = mExecuteCommand(exe,split(args2),redirect2,output2); + if (exitcode != EXIT_SUCCESS) { + // TODO: report as proper error + std::cerr << "Failed to execute '" << exe << " " << args2 << " " << redirect2 << "' - (exitcode: " << exitcode << " / output: " << output2 << ")" << std::endl; + return 0; // TODO: report as failure? + } + + if (output2.find("TranslationUnitDecl") == std::string::npos) { + // TODO: report as proper error + std::cerr << "Failed to execute '" << exe << " " << args2 << " " << redirect2 << "' - (no TranslationUnitDecl in output)" << std::endl; + return 0; // TODO: report as failure? } // Ensure there are not syntax errors... From 9ec6657b9c27d0f2fe605cc835d2ebf5b779eec2 Mon Sep 17 00:00:00 2001 From: firewave Date: Fri, 1 Mar 2024 18:34:13 +0100 Subject: [PATCH 3/4] made `test/cli/helloworld/main.c` compile and avoid newly reported warning --- test/cli/helloworld/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cli/helloworld/main.c b/test/cli/helloworld/main.c index cfcce06fdfa..c5b94db7aa0 100644 --- a/test/cli/helloworld/main.c +++ b/test/cli/helloworld/main.c @@ -2,7 +2,7 @@ int main(void) { (void)printf("Hello world!\n"); - x = 3 / 0; // ERROR + int x = 3 / 0; (void)x; // ERROR return 0; } From 3cdba5c97a6aa7e0fef5eeb7740ddadad71ace19 Mon Sep 17 00:00:00 2001 From: firewave Date: Fri, 1 Mar 2024 18:36:04 +0100 Subject: [PATCH 4/4] Cppcheck: simplify path before calling `checkClang()` --- lib/cppcheck.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index b9b2814ac34..224c5a3b74e 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -559,7 +559,7 @@ unsigned int CppCheck::checkClang(const std::string &path) unsigned int CppCheck::check(const std::string &path) { if (mSettings.clang) - return checkClang(path); + return checkClang(Path::simplifyPath(path)); return checkFile(Path::simplifyPath(path), emptyString); }