Skip to content

Commit 49d11dc

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 7856f2e commit 49d11dc

File tree

5 files changed

+52
-26
lines changed

5 files changed

+52
-26
lines changed

cmake/packaging.cmake

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ set(CPACK_PACKAGE_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
2424
# Automatically find dependencies for shared libraries
2525
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS YES)
2626

27-
# In addition, we depend on gcc for preprocessing
28-
set(CPACK_DEBIAN_PACKAGE_DEPENDS gcc)
27+
# In addition, we depend on gcc for preprocessing and bash-completion to make
28+
# CBMC's bash completion work
29+
set(CPACK_DEBIAN_PACKAGE_DEPENDS "gcc, bash-completion")
2930

3031
# Enable debug output so that we can see the dependencies being generated in the
3132
# logs

scripts/bash-autocomplete/Readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Follow 1. 2. and 3. as above.
2323

2424
4. Put the following at the end of your `~/.zshrc`, with the directories adapted to your directory structure:
2525
```bash
26+
autoload -Uz compinit
27+
compinit
2628
autoload bashcompinit
2729
bashcompinit
2830
cbmcautocomplete=~/diffblue/cbmc/scripts/bash-autocomplete/cbmc.sh

scripts/bash-autocomplete/cbmc.sh.template

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,30 @@ _cbmc_autocomplete()
1919
COMPREPLY=( $( compgen -W "sc tso pso" -- $cur ) )
2020
return 0
2121
;;
22-
--arch) #for architecture we list the options explicitly
23-
COMPREPLY=( $( compgen -W "i386 x86_64" -- $cur ) )
22+
--arch) #for architecture we list the options explicitly; this list is populated using
23+
# `grep -w 'arch[[:space:]]*==' src/util/config.cpp | cut # -f2 -d'"' | sort`
24+
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 ) )
2425
return 0
2526
;;
26-
-I|--classpath|-cp|--outfile|--existing-coverage|--graphml-cex)
27+
--os) #for architecture we list the options explicitly
28+
COMPREPLY=( $( compgen -W "freebsd linux macos solaris windows" -- $cur ) )
29+
return 0
30+
;;
31+
--timestamp) #for timestamp we list the options explicitly
32+
COMPREPLY=( $( compgen -W "monotonic wall" -- $cur ) )
33+
return 0
34+
;;
35+
--paths) #for paths we list the options explicitly
36+
COMPREPLY=( $( compgen -W "fifo lifo" -- $cur ) )
37+
return 0
38+
;;
39+
-I|--classpath|-cp) # a directory
40+
_filedir -d
41+
return 0
42+
;;
43+
--external-sat-solver|--incremental-smt2-solver)
2744
#a switch that takes a file parameter of which we don't know an extension
28-
#TODO probably we can do more for -I, --classpath, -cp
29-
COMPREPLY=( $(compgen -f -- $cur) )
45+
_filedir -f
3046
return 0
3147
;;
3248
esac
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,19 @@ 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+
DEPENDS $<TARGET_FILE:cbmc>
66+
)
67+
add_custom_target(cbmc.sh ALL
68+
DEPENDS "${CMAKE_SOURCE_DIR}/scripts/bash-autocomplete/cbmc.sh"
69+
)
70+
install(
71+
FILES ${CMAKE_SOURCE_DIR}/scripts/bash-autocomplete/cbmc.sh
72+
DESTINATION etc/bash_completion.d
73+
RENAME cbmc
74+
)
75+
endif()

0 commit comments

Comments
 (0)