Skip to content

Commit

Permalink
[cmd] Parse command exits with error in case of duplicated suppress c…
Browse files Browse the repository at this point in the history
…omments
  • Loading branch information
zomen2 committed Apr 8, 2021
1 parent e083e9b commit c784e4a
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 34 deletions.
25 changes: 12 additions & 13 deletions analyzer/tests/functional/suppress/test_suppress.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from libtest import env


def call_cmd(command, cwd, env):
def _call_cmd(command, cwd, env):
try:
print(' '.join(command))
proc = subprocess.Popen(
Expand Down Expand Up @@ -61,15 +61,15 @@ def test_source_suppress_export(self):
"generated.suppress")
skip_file = os.path.join(self._test_directory, "suppress_export.skip")

extract_cmd = ['CodeChecker', 'parse',
extract_cmd = [env.codechecker_cmd(), 'parse',
os.path.join(self._test_workspace, "reports"),
"--suppress", generated_file,
"--export-source-suppress", "--ignore", skip_file,
"--verbose", "debug"]

ret = call_cmd(extract_cmd,
self._test_project_path,
env.test_env(self._test_directory))
ret = _call_cmd(extract_cmd,
self._test_project_path,
env.test_env(self._test_directory))
self.assertEqual(ret, 2, "Failed to generate suppress file.")

with open(generated_file, 'r',
Expand Down Expand Up @@ -101,13 +101,13 @@ def test_doubled_suppress(self):

skip_file = os.path.join(self._test_directory, "doubled_suppress.skip")

extract_cmd = ['CodeChecker', 'parse',
extract_cmd = [env.codechecker_cmd(), 'parse',
os.path.join(self._test_workspace, "reports"),
"--ignore", skip_file, "--verbose", "debug"]

ret = call_cmd(extract_cmd,
self._test_project_path,
env.test_env(self._test_workspace))
ret = _call_cmd(extract_cmd,
self._test_project_path,
env.test_env(self._test_workspace))
self.assertEqual(ret, 1, "Repeated suppress comment not recognized.")

def test_doubled_suppress_by_all(self):
Expand All @@ -118,12 +118,11 @@ def test_doubled_suppress_by_all(self):

skip_file = os.path.join(self._test_directory, "suppress_by_all.skip")

extract_cmd = ['CodeChecker', 'parse',
extract_cmd = [env.codechecker_cmd(), 'parse',
os.path.join(self._test_workspace, "reports"),
"--ignore", skip_file, "--verbose", "debug"]

ret = call_cmd(extract_cmd,
self._test_project_path,
env.test_env(self._test_workspace))
ret = _call_cmd(extract_cmd, self._test_project_path,
env.test_env(self._test_workspace))
self.assertEqual(ret, 1, "Already covered suppress comment not "
"recognized.")
7 changes: 5 additions & 2 deletions web/tests/functional/store/test_proj/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
# for the store update tests.

build:
$(CXX) divide_zero.cpp -o divide_zero
$(CXX) -c divide_zero.cpp -o divide_zero.o
$(CXX) -c double_suppress.cpp -o double_suppress.o

clean:
rm -rf divide_zero
rm -rf divide_zero.o double_suppress.o

# Using relative path to the source files
# it is easier to prefix them with the temporary
# directory during test preparation.
analyze:
clang --analyze -I. divide_zero.cpp --analyzer-output plist-multi-file \
-o divide_zero.plist
clang --analyze double_suppress.cpp --analyzer-output plist-multi-file \
-o double_suppress.plist
6 changes: 6 additions & 0 deletions web/tests/functional/store/test_proj/double_suppress.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
void foo()
{
// codechecker_suppress [deadcode.DeadStores] Same suppress comment twice.
// codechecker_suppress [deadcode.DeadStores] Same suppress comment twice.
int *p = new int;
}
190 changes: 190 additions & 0 deletions web/tests/functional/store/test_proj/double_suppress.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>clang_version</key>
<string>Ubuntu clang version 11.0.0-2~ubuntu20.04.1</string>
<key>diagnostics</key>
<array>
<dict>
<key>path</key>
<array>
<dict>
<key>kind</key><string>event</string>
<key>location</key>
<dict>
<key>line</key><integer>5</integer>
<key>col</key><integer>10</integer>
<key>file</key><integer>0</integer>
</dict>
<key>ranges</key>
<array>
<array>
<dict>
<key>line</key><integer>5</integer>
<key>col</key><integer>10</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
<key>line</key><integer>5</integer>
<key>col</key><integer>10</integer>
<key>file</key><integer>0</integer>
</dict>
</array>
<array>
<dict>
<key>line</key><integer>5</integer>
<key>col</key><integer>14</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
<key>line</key><integer>5</integer>
<key>col</key><integer>20</integer>
<key>file</key><integer>0</integer>
</dict>
</array>
</array>
<key>depth</key><integer>0</integer>
<key>extended_message</key>
<string>Value stored to &apos;p&apos; during its initialization is never read</string>
<key>message</key>
<string>Value stored to &apos;p&apos; during its initialization is never read</string>
</dict>
</array>
<key>description</key><string>Value stored to &apos;p&apos; during its initialization is never read</string>
<key>category</key><string>Dead store</string>
<key>type</key><string>Dead initialization</string>
<key>check_name</key><string>deadcode.DeadStores</string>
<!-- This hash is experimental and going to change! -->
<key>issue_hash_content_of_line_in_context</key><string>5ccff44aa49130936add404dab9e1a6c</string>
<key>issue_context_kind</key><string>function</string>
<key>issue_context</key><string>foo</string>
<key>issue_hash_function_offset</key><string>3</string>
<key>location</key>
<dict>
<key>line</key><integer>5</integer>
<key>col</key><integer>10</integer>
<key>file</key><integer>0</integer>
</dict>
<key>ExecutedLines</key>
<dict>
<key>0</key>
<array>
<integer>5</integer>
</array>
</dict>
</dict>
<dict>
<key>path</key>
<array>
<dict>
<key>kind</key><string>event</string>
<key>location</key>
<dict>
<key>line</key><integer>5</integer>
<key>col</key><integer>14</integer>
<key>file</key><integer>0</integer>
</dict>
<key>ranges</key>
<array>
<array>
<dict>
<key>line</key><integer>5</integer>
<key>col</key><integer>14</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
<key>line</key><integer>5</integer>
<key>col</key><integer>20</integer>
<key>file</key><integer>0</integer>
</dict>
</array>
</array>
<key>depth</key><integer>0</integer>
<key>extended_message</key>
<string>Memory is allocated</string>
<key>message</key>
<string>Memory is allocated</string>
</dict>
<dict>
<key>kind</key><string>control</string>
<key>edges</key>
<array>
<dict>
<key>start</key>
<array>
<dict>
<key>line</key><integer>5</integer>
<key>col</key><integer>5</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
<key>line</key><integer>5</integer>
<key>col</key><integer>7</integer>
<key>file</key><integer>0</integer>
</dict>
</array>
<key>end</key>
<array>
<dict>
<key>line</key><integer>6</integer>
<key>col</key><integer>1</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
<key>line</key><integer>6</integer>
<key>col</key><integer>1</integer>
<key>file</key><integer>0</integer>
</dict>
</array>
</dict>
</array>
</dict>
<dict>
<key>kind</key><string>event</string>
<key>location</key>
<dict>
<key>line</key><integer>6</integer>
<key>col</key><integer>1</integer>
<key>file</key><integer>0</integer>
</dict>
<key>depth</key><integer>0</integer>
<key>extended_message</key>
<string>Potential leak of memory pointed to by &apos;p&apos;</string>
<key>message</key>
<string>Potential leak of memory pointed to by &apos;p&apos;</string>
</dict>
</array>
<key>description</key><string>Potential leak of memory pointed to by &apos;p&apos;</string>
<key>category</key><string>Memory error</string>
<key>type</key><string>Memory leak</string>
<key>check_name</key><string>cplusplus.NewDeleteLeaks</string>
<!-- This hash is experimental and going to change! -->
<key>issue_hash_content_of_line_in_context</key><string>1a6ea464fbd26b31573aab0d8a784d6a</string>
<key>issue_context_kind</key><string>function</string>
<key>issue_context</key><string>foo</string>
<key>issue_hash_function_offset</key><string>3</string>
<key>location</key>
<dict>
<key>line</key><integer>6</integer>
<key>col</key><integer>1</integer>
<key>file</key><integer>0</integer>
</dict>
<key>ExecutedLines</key>
<dict>
<key>0</key>
<array>
<integer>1</integer>
<integer>2</integer>
<integer>5</integer>
<integer>6</integer>
</array>
</dict>
</dict>
</array>
<key>files</key>
<array>
<string>double_suppress.cpp</string>
</array>
</dict>
</plist>
66 changes: 47 additions & 19 deletions web/tests/functional/store/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import json
import os
import shlex
import inspect
import plistlib
import shutil
import subprocess
Expand All @@ -24,6 +26,22 @@
from codechecker_common import util


def _call_cmd(command, cwd=None, env=None):
try:
print(' '.join(command))
proc = subprocess.Popen(
shlex.split(' '.join(command)),
cwd=cwd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env, encoding="utf-8", errors="ignore")
out, err = proc.communicate()
return proc.returncode, out, err
except subprocess.CalledProcessError as cerr:
print("Failed to call:\n" + ' '.join(cerr.cmd))
return cerr.returncode, None, None


class TestStore(unittest.TestCase):
"""Test storage reports"""

Expand All @@ -36,6 +54,8 @@ def setUp(self):
print("Running " + test_class + " tests in " + self._test_workspace)

self._test_cfg = env.import_test_cfg(self._test_workspace)
self._test_directory = os.path.dirname(os.path.abspath(inspect.getfile(
inspect.currentframe())))

def test_tim_path_prefix_store(self):
"""Trim the path prefix from the sored reports.
Expand All @@ -45,19 +65,16 @@ def test_tim_path_prefix_store(self):
test directory from the source file path during the storage.
"""

test_dir = os.path.dirname(os.path.realpath(__file__))

report_dir = os.path.join(test_dir, "test_proj")
source_dir = os.path.join(self._test_directory, "test_proj")

codechecker_cfg = self._test_cfg["codechecker_cfg"]

# Copy report files to a temporary directory not to modify the
# files in the repository.
# Report files will be overwritten during the tests.
temp_workspace = os.path.join(
codechecker_cfg["workspace"], "test_proj"
)
shutil.copytree(report_dir, temp_workspace)
temp_workspace = os.path.join(codechecker_cfg["workspace"],
"test_proj")
shutil.copytree(source_dir, temp_workspace)

report_file = os.path.join(temp_workspace, "divide_zero.plist")

Expand Down Expand Up @@ -88,15 +105,8 @@ def test_tim_path_prefix_store(self):
"debug",
]

try:
out = subprocess.check_output(
store_cmd, encoding="utf-8", errors="ignore"
)
print(out)
except subprocess.CalledProcessError as cerr:
print(cerr.stdout)
print(cerr.stderr)
raise
ret, _, _ = _call_cmd(store_cmd)
self.assertEqual(ret, 0, "Plist file could not store.")

query_cmd = [
env.codechecker_cmd(),
Expand All @@ -110,13 +120,31 @@ def test_tim_path_prefix_store(self):
"json",
]

out = subprocess.check_output(
query_cmd, encoding="utf-8", errors="ignore"
)
ret, out, _ = _call_cmd(query_cmd)
self.assertEqual(ret, 0, "Could not read from server.")
reports = json.loads(out)

print(json.dumps(reports, indent=2))

self.assertEqual(len(reports), 4)
for report in reports:
self.assertIn(report["checkedFile"], trimmed_paths)

def test_duplicated_suppress(self):
"""
Test server if recognise duplicated suppress comments in the stored
source code.
"""

codechecker_cfg = self._test_cfg["codechecker_cfg"]
project_dir = os.path.join(self._test_directory, "test_proj")
test_plist_file = "double_suppress.plist"

store_cmd = [env.codechecker_cmd(), "store", "--url",
env.parts_to_url(codechecker_cfg), "--name", "never",
"--input-format", "plist",
"--verbose", "debug", test_plist_file]

ret, _, _ = _call_cmd(store_cmd, project_dir)
self.assertEqual(ret, 1, "Duplicated suppress comments was not "
"recognised.")

0 comments on commit c784e4a

Please sign in to comment.