Skip to content
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

Adding clang-format configuration and script #458

Merged
merged 10 commits into from
Nov 13, 2023
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ p
/*.txt
.idea/*
*.ipynb_checkpoint*
tools/.format/*
2 changes: 1 addition & 1 deletion src/Factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Factory {
ret = (*it)->Produce(input);
if (ret) return ret;
}
return NULL;
return nullptr;
};
// Template class for registration of the worker functions
template <Product* (*T)(const Input&)>
Expand Down
1 change: 1 addition & 0 deletions src/Geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define GEOMETRY_H

#include "unit.h"
#include "pugixml.hpp"
#include <map>
/// STL triangle structure
#ifdef _WIN32
Expand Down
1 change: 1 addition & 0 deletions src/Region.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#ifndef REGION_H
#include <stdio.h>
#include "cross.h"
class lbRegion {
public:
int dx,dy,dz;
Expand Down
1 change: 1 addition & 0 deletions src/SolidGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <vector>
#include <set>
#include <math.h>
#include "types.h"

typedef int gr_addr_t;

Expand Down
5 changes: 1 addition & 4 deletions src/SolidTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
#include <vector>
#include <set>
#include <math.h>

typedef char tr_flag_t;
typedef int tr_addr_t;
typedef double tr_real_t;
#include "types.h"

struct tr_elem {
tr_flag_t flag;
Expand Down
1 change: 1 addition & 0 deletions src/def.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#include "pugixml.hpp"
extern pugi::xml_document xml_def;
int xml_def_init();
5 changes: 5 additions & 0 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
#define CUT_MAX 65000
#define CUT_LEN(x__) (0.005f * (x__))

typedef char tr_flag_t;
typedef int tr_addr_t;
typedef double tr_real_t;


/*
struct vector_t {
real_t x,y,z;
Expand Down
12 changes: 12 additions & 0 deletions tools/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
BasedOnStyle: Google
ColumnLimit: 160
IndentWidth: 4
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: InlineOnly
AllowShortIfStatementsOnASingleLine: Always
DerivePointerAlignment: false
PointerAlignment: Left
BinPackArguments: false
BinPackParameters: false
ReflowComments: false
144 changes: 144 additions & 0 deletions tools/format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#!/bin/bash

set -o pipefail

PP=$(dirname $0)
FORMATFILE="$PP/.clang-format"
PRINTSKIP=true

function format {
clang-format --style=file:$FORMATFILE \
| sed -E 's/for[[:blank:]]*[(]([[:alpha:]_]*[[:blank:]]|)[[:blank:]]*([[:alnum:]_]+)[[:blank:]]*=[[:blank:]]*([[:alnum:]_]+)[[:blank:]]*;[[:blank:]]*([[:alnum:]_]+)[[:blank:]]*([<=]*)[[:blank:]]*([[:alnum:]_]+)[[:blank:]]*;[[:blank:]]*([[:alnum:]_]+)[+][+][[:blank:]]*\)/for (\1\2=\3; \4\5\6; \7++)/g' \
| sed -E 's| ([*/]) |\1|g'
}

function formatRT {
R -s -e 'rtemplate::RTtokenize()' | format | R -s -e 'rtemplate::RTtokenize(inv=TRUE)'
}

function format_sel {
if [[ -z "$1" || "$1" =~ \.[rR][tT]$ ]]
then
echo formatRT
else
echo format
fi
}

function format_to {
F="$(format_sel "$1")"
if test -z "$1" || test -z "$2"
then
echo "Something went wrong in format_to"
exit 5
fi
mkdir -p $PP/.format
NAMESUM=$(echo "$1 $2" | sha256sum | cut -c 1-30)
SUMFILE="$PP/.format/$NAMESUM"
if test -f "$SUMFILE"
then
if sha256sum --status --check "$SUMFILE"
then
$PRINTSKIP && echo "Skipping $1"
return 0
fi
fi
if [[ "$1" == "$2" ]]
then
echo "Running $F on $1"
else
echo "Running $F on $1 -> $2"
fi
cat "$1" | $F >tmp
mv tmp "$2"
sha256sum "$1" "$2" "$FORMATFILE" >"$SUMFILE"
}

OUTFILE=""
OUTSAME=false
while test -n "$1"
do
case "$1" in
--help)
echo ""
echo "$0 [--all] [-o output] [-x] [files]"
echo ""
echo " -o (--output) OUTPUT : put the formated output in OUTPUT"
echo " -x (--overwrite) : put the formated output in the same file"
echo " --all [FROM_DIR] [TO_DIR] : format all source files in DIR"
echo ""
exit 0;
;;
-o|--output)
shift
OUTFILE="$1"
;;
-x|--overwrite)
OUTSAME=true
;;
-a|--all)
shift
FROM_DIR="src"
if ! test -z "$1"
then
if ! test -d "$1"
then
echo "$1: not a directory"
exit 3
fi
FROM_DIR="$1"
shift
fi
TO_DIR="$FROM_DIR"
if ! test -z "$1"
then
if ! test -d "$1"
then
echo "$1: not a directory"
exit 3
fi
TO_DIR="$1"
shift
fi
if test "$FROM_DIR" == "$TO_DIR" && ! $OUTSAME
then
echo "Trying to format all file into the same directory. If you want to overwrite say '-x'"
exit 4
fi
PRINTSKIP=false
find "$FROM_DIR" | grep -E '[.](cpp|h|hpp)([.]Rt|)$' | while read i
do
if test "$FROM_DIR" == "$TO_DIR"
then
j="$i"
else
j="$TO_DIR/${i#$FROM_DIR}"
fi
format_to "$i" "$j"
done
;;
-p|--pipe)
formatRT
;;
-*)
echo "Uknown option $1"
exit 1;
;;
*)
INFILE="$1"
if $OUTSAME && test -z "$OUTFILE"
then
OUTFILE="$INFILE"
fi
if test -z $OUTFILE
then
cat "$INFILE" | $(format_sel "$INFILE")
else
format_to "$INFILE" "$OUTFILE"
OUTFILE=""
fi
esac
shift
done

exit 0