Skip to content
clang-parse-all.sh 1.39 KiB
Newer Older
#!/bin/bash

if [ $# -eq 0 ]; then
  echo "No arguments supplied"
  exit 1
fi

arch=$1
basepath=$(pwd)
workdir=$basepath/workdir-$arch
ghidra_sources=$workdir/ghidra-sources
ghidra_headers=$workdir/ghidra-headers
clang_dir=$basepath/scripting/ghidra-extraction-scripts/clang-parser
metrics_dir=$workdir/ghidra-cyclomatic

if [ ! -d $metrics_dir ]; then
  mkdir $metrics_dir;
fi

cd $ghidra_sources

for filename in *
do
  filename=${filename%".c"}
  echo "Parsing with clang input: $filename"

  # Copy the source and the header file in the parsing directory.
  cp $filename.c $clang_dir/
  cp $ghidra_headers/$filename.h $clang_dir/
  cd $clang_dir

  # Remove newlines inhside commands, this will make easier our fixed-point purgin.
  clang-format -style='{ColumnLimit: 5000}' -i $filename.c

  # Invoke the normalizer pass for the Ghidra decompiled output.
  ./ghidra-normalizer-wrapper.sh $filename.c

  # Parse the source with clang
  clang-7 -std=c11 -S -emit-llvm -w $filename.c.normalized.c

  # Invoke the analys passes which collect the metrics we are interested in.
  revng opt -cyclomatic -cyclomatic-output=$metrics_dir/$filename.c.csv -S -o /dev/null $filename.c.normalized.ll

  # Go back to the source directory after cleaning the workdir
  rm $filename.c $filename.h $filename.c.normalized.c $filename.c.normalized.ll

  # Return to the directory with all the decompiled sources.
  cd $ghidra_sources
done;