homebrew-core/Formula/cppcheck.rb

123 lines
4.4 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.8.2.tar.gz"
sha256 "30ba99ab54089c44b83f02e2453da046a7edff5237950d4a0eb1eba4afcb4f45"
license "GPL-3.0-or-later"
head "https://github.com/danmar/cppcheck.git", branch: "main"
bottle do
sha256 arm64_monterey: "232a2df3cd21dff233eacbaca1c084d613eb34b4fee2f1da97b8815108768817"
sha256 arm64_big_sur: "de5858a976b99cc60619925dbf28ce428bc7b3be738e5dc897d864aa867624c6"
sha256 monterey: "3905285f4148db5a09c8ddb7dfef0dd83309703e51c3f2fe5afa02091612b96d"
sha256 big_sur: "bfb855e7230067ea02ec3f7bd4fe1eadf2ab631a0e84b4417faa9df393ad581e"
sha256 catalina: "23209a340b51069e507c33ff08c09b6fb27f93c289f38cc8494100ae54b36968"
sha256 x86_64_linux: "6938798a5827a9137df2ecc2bf590bea908bbcc17b4d09fb87b8f855c7b796f7"
end
depends_on "cmake" => :build
depends_on "python@3.10" => [:build, :test]
depends_on "pcre"
depends_on "tinyxml2"
uses_from_macos "libxml2"
def install
args = std_cmake_args + %W[
-DHAVE_RULES=ON
-DUSE_MATCHCOMPILER=ON
-DUSE_BUNDLED_TINYXML2=OFF
-DENABLE_OSS_FUZZ=OFF
-DPYTHON_EXECUTABLE=#{Formula["python@3.10"].opt_bin}/python3
]
system "cmake", "-S", ".", "-B", "build", *args
system "cmake", "--build", "build"
system "cmake", "--install", "build"
# 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.10"].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.10"].opt_bin/"python3 #{sample_addon_file} #{test_cpp_file_dump}")
assert_match "#{expect_function_names}\n#{expect_token_count}", output
end
end