Skip to content

Add script to text-extract all contracts #323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions scripts/find-contracts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

# Script to find files with specific annotations and print contracts
# containing those annotations up to a line with "fn"
PATTERN_STRING='^\s*#\[((safety::)?(requires|ensures)\(|cfg_attr\((kani|not\(kani\)))'

# Find all files in the git repository that contain any of the patterns
FILES=$(git grep -l -P "$PATTERN_STRING" library/)

if [ -z "$FILES" ]; then
echo "No files found with the specified patterns."
exit 1
fi

# Process each file
for FILE in $FILES; do
echo "=== $FILE ==="

# Use perl to find contracts starting with any pattern and ending at "fn"
cat $FILE | perl -e "
\$in_contract = 0;
\$ws = 0;
while(<>) {
\$line = \$_;
if (!\$in_contract && /$PATTERN_STRING/ && !/^\s*#\[cfg_attr\(kani, derive\(/) {
\$in_contract = 1;
/^(\s*)/;
\$ws = length(\$1);
}
if (\$in_contract) {
\$line =~ s/^ {\$ws}//;
print \$line;
if (\$line =~ /(^| )fn /) {
\$in_contract = 0;
print \"\n\";
}
}
}
"

echo
done
Loading