homebrew-core/Formula/cppcheck.rb

113 lines
3.9 KiB
Ruby

class Cppcheck < Formula
desc "Static analysis of C and C++ code"
homepage "https://sourceforge.net/projects/cppcheck/"
url "https://github.com/danmar/cppcheck/archive/2.2.tar.gz"
sha256 "ad96290a1842c5934bf9c4268cb270953f3078d4ce33a9a53922d6cb6daf61aa"
license "GPL-3.0-or-later"
revision 1
head "https://github.com/danmar/cppcheck.git"
bottle do
sha256 "b126ab11f1a38e53d94f4f58a5f5f48d0e0cb7cc2a24fd104e0d8a4526f8455f" => :catalina
sha256 "e2f6f74f00041c30404a337c78c6b15e7a189a94398cfec1bcae3ccc48c4ba98" => :mojave
sha256 "a43ed45890d3253137ca3b47d7c2562951abd5791a404a29c121461fda28813d" => :high_sierra
end
depends_on "python@3.9" => :test
depends_on "pcre"
def install
ENV.cxx11
system "make", "HAVE_RULES=yes", "FILESDIR=#{prefix}/cfg"
# FILESDIR is relative to the prefix for install, don't add #{prefix}.
system "make", "DESTDIR=#{prefix}", "BIN=#{bin}", "FILESDIR=/cfg", "install"
# Move the python addons to the cppcheck pkgshare folder
(pkgshare/"addons").install Dir.glob("addons/*.py")
end
test do
# Execution test with an input .cpp file
test_cpp_file = testpath/"test.cpp"
test_cpp_file.write <<~EOS
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
class Example
{
public:
int GetNumber() const;
explicit Example(int initialNumber);
private:
int number;
};
Example::Example(int initialNumber)
{
number = initialNumber;
}
EOS
system "#{bin}/cppcheck", test_cpp_file
# Test the "out of bounds" check
test_cpp_file_check = testpath/"testcheck.cpp"
test_cpp_file_check.write <<~EOS
int main()
{
char a[10];
a[10] = 0;
return 0;
}
EOS
output = shell_output("#{bin}/cppcheck #{test_cpp_file_check} 2>&1")
assert_match "out of bounds", output
# Test the addon functionality: sampleaddon.py imports the cppcheckdata python
# module and uses it to parse a cppcheck dump into an OOP structure. We then
# check the correct number of detected tokens and function names.
addons_dir = pkgshare/"addons"
cppcheck_module = "#{name}data"
expect_token_count = 55
expect_function_names = "main,GetNumber,Example"
assert_parse_message = "Error: sampleaddon.py: failed: can't parse the #{name} dump."
sample_addon_file = testpath/"sampleaddon.py"
sample_addon_file.write <<~EOS
#!/usr/bin/env #{Formula["python@3.9"].opt_bin}/python3
"""A simple test addon for #{name}, prints function names and token count"""
import sys
from importlib import machinery, util
# Manually import the '#{cppcheck_module}' module
spec = machinery.PathFinder().find_spec("#{cppcheck_module}", ["#{addons_dir}"])
cpp_check_data = util.module_from_spec(spec)
spec.loader.exec_module(cpp_check_data)
for arg in sys.argv[1:]:
# Parse the dump file generated by #{name}
configKlass = cpp_check_data.parsedump(arg)
if len(configKlass.configurations) == 0:
sys.exit("#{assert_parse_message}") # Parse failure
fConfig = configKlass.configurations[0]
# Pick and join the function names in a string, separated by ','
detected_functions = ','.join(fn.name for fn in fConfig.functions)
detected_token_count = len(fConfig.tokenlist)
# Print the function names on the first line and the token count on the second
print("%s\\n%s" %(detected_functions, detected_token_count))
EOS
system "#{bin}/cppcheck", "--dump", test_cpp_file
test_cpp_file_dump = "#{test_cpp_file}.dump"
assert_predicate testpath/test_cpp_file_dump, :exist?
output = shell_output(Formula["python@3.9"].opt_bin/"python3 #{sample_addon_file} #{test_cpp_file_dump}")
assert_match "#{expect_function_names}\n#{expect_token_count}", output
end
end