Skip to content

Commit 862525d

Browse files
committed
Fix and install bash completion for cbmc
1) Use a pragma-free (and working) approach to extract CBMC_OPTIONS. 2) Add completion of all completable arguments to command-line options. 3) Make CMake's install target install the resulting bash completion script in etc/bash_completion.d 4) Make the .deb package depend on bash-completion for the now-included completion snippet to be useful.
1 parent 2bf999e commit 862525d

File tree

4 files changed

+35
-22
lines changed

4 files changed

+35
-22
lines changed

cmake/packaging.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ set(CPACK_PACKAGE_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
2525
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS YES)
2626

2727
# In addition, we depend on gcc for preprocessing
28-
set(CPACK_DEBIAN_PACKAGE_DEPENDS gcc)
28+
set(CPACK_DEBIAN_PACKAGE_DEPENDS "gcc, bash-completion")
2929

3030
# Enable debug output so that we can see the dependencies being generated in the
3131
# logs

scripts/bash-autocomplete/cbmc.sh.template

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,22 @@ _cbmc_autocomplete()
2020
return 0
2121
;;
2222
--arch) #for architecture we list the options explicitly
23-
COMPREPLY=( $( compgen -W "i386 x86_64" -- $cur ) )
23+
COMPREPLY=( $( compgen -W "alpha arm arm64 armel armhf hppa i386 ia64 mips mips64 mips64el mipsel mipsn32 mipsn32el none powerpc ppc64 ppc64le riscv64 s390 s390x sh4 sparc sparc64 v850 x32 x86_64" -- $cur ) )
2424
return 0
2525
;;
26-
-I|--classpath|-cp|--outfile|--existing-coverage|--graphml-cex)
26+
--os) #for architecture we list the options explicitly
27+
COMPREPLY=( $( compgen -W "freebsd linux macos solaris windows" -- $cur ) )
28+
return 0
29+
;;
30+
--timestamp) #for timestamp we list the options explicitly
31+
COMPREPLY=( $( compgen -W "monotonic wall" -- $cur ) )
32+
return 0
33+
;;
34+
--paths) #for paths we list the options explicitly
35+
COMPREPLY=( $( compgen -W "fifo lifo" -- $cur ) )
36+
return 0
37+
;;
38+
-I|--classpath|-cp|--external-sat-solver|--incremental-smt2-solver)
2739
#a switch that takes a file parameter of which we don't know an extension
2840
#TODO probably we can do more for -I, --classpath, -cp
2941
COMPREPLY=( $(compgen -f -- $cur) )
Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
#!/bin/bash
2-
echo "Compiling the helper file to extract the raw list of parameters from cbmc"
3-
g++ -c -MMD -MP -std=c++11 -Wall -I ../../src/ -ftrack-macro-expansion=0 -fno-diagnostics-show-caret switch_extractor_helper.c -o tmp.o 2> pragma.txt
42

5-
retval=$?
3+
set -e
64

7-
#clean up compiled files, we don't need them.
8-
rm tmp.o 2> /dev/null
9-
rm tmp.d 2> /dev/null
5+
# make sure we execute the remainder in the directory containing this script
6+
cd `dirname $0`
107

11-
#check if compilation went fine
12-
if [ $retval -ne 0 ]; then
13-
echo "Problem compiling the helper file, parameter list not extracted."
14-
exit 1;
15-
fi
8+
echo "Compiling the helper file to extract the raw list of parameters from cbmc"
9+
g++ -E -dM -std=c++11 -I../../src ../../src/cbmc/cbmc_parse_options.cpp -o macros.c
10+
echo CBMC_OPTIONS >> macros.c
1611

1712
echo "Converting the raw parameter list to the format required by autocomplete scripts"
18-
rawstring=`sed "s/^.*pragma message: \(.*\)/\1/" pragma.txt`
19-
#delete pragma file, we won't need it
20-
rm pragma.txt 2> /dev/null
13+
rawstring="`gcc -E -P -w macros.c` \"?h(help)\""
14+
rm macros.c
2115

2216
#now the main bit, convert from raw format to a proper list of switches
2317
cleanstring=`(
@@ -26,7 +20,7 @@ cleanstring=`(
2620
(echo $rawstring | grep -o "([^)]*)" | sed "s/^.\(.*\).$/--\1/") ;
2721
#extract 1-hyphen switches, such as -F
2822
#use sed to remove all (foo) expressions, then you're left with switches and ':', so grep the colons out and then use sed to include the '-'
29-
(echo $rawstring | sed "s/([^)]*)//g" | grep -o "[a-zA-Z0-9]" | sed "s/\(.*\)/-\1/")
23+
(echo $rawstring | sed "s/([^)]*)//g" | grep -o "[a-zA-Z0-9?]" | sed "s/\(.*\)/-\1/")
3024
) | tr '\n' ' '`
3125

3226
#sanity check that there is only one line of output
@@ -36,13 +30,10 @@ if [ `echo $cleanstring | wc -l | awk '{print $1}'` -ne 1 ]; then
3630
fi
3731

3832
#sanity check that there are no dangerous characters
39-
echo $cleanstring | grep -q "[^a-zA-Z0-9 -]"
40-
if [ $? -eq 0 ]; then
33+
if echo $cleanstring | grep -q "[^a-zA-Z0-9? -]"; then
4134
echo "Problem converting the parameter list to the correct format, illegal characters detected. This is likely to be an error in this conversion script."
4235
exit 1;
4336
fi
4437

4538
echo "Injecting the parameter list to the autocomplete file."
4639
sed "5 s/.*/ local switches=\"$cleanstring\"/" cbmc.sh.template > cbmc.sh
47-
48-
rm pragma.txt 2> /dev/null

src/cbmc/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,13 @@ if(NOT WIN32)
5757
PATTERN "c*"
5858
PATTERN "g*")
5959
endif()
60+
61+
# bash completion
62+
if(NOT WIN32)
63+
add_custom_command(OUTPUT "${CMAKE_SOURCE_DIR}/scripts/bash-autocomplete/cbmc.sh"
64+
COMMAND "${CMAKE_SOURCE_DIR}/scripts/bash-autocomplete/extract_switches.sh")
65+
install(
66+
FILES ${CMAKE_SOURCE_DIR}/scripts/bash-autocomplete/cbmc.sh
67+
DESTINATION etc/bash_completion.d
68+
RENAME cbmc)
69+
endif()

0 commit comments

Comments
 (0)