From 8c21ee3c8c4202d59bab9b24d42e62bd2f733764 Mon Sep 17 00:00:00 2001
From: Giovanni Bussi <giovanni.bussi@gmail.com>
Date: Wed, 19 Apr 2017 18:15:38 +0200
Subject: [PATCH] Plumedcheck checks for astyle

Travis-ci job doing cppcheck also checks whether astyle
has been enforced.

See #215
---
 .travis.yml               |  2 ++
 src/maketools/codecheck   |  4 ++--
 src/maketools/plumedcheck | 23 +++++++++++++++++++++++
 3 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index 9f6fc47e2..f4f1a4cd5 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -90,6 +90,8 @@ install:
   - if test "$CPPCHECK" == yes ; then  ./.travis/install.cppcheck $CPPCHECK_VERSION ; fi
 # for plumedcheck I need latest gawk
   - if test "$CPPCHECK" == yes ; then  ./.travis/install.gawk 4.1.4 ; fi
+# for plumedcheck I need astyle
+  - if test "$CPPCHECK" == yes ; then make -j 4 -C astyle ; fi
 # installation of these packages takes a lot of time
 # we do it only when needed
   - if test "$PLUMED_CXX" == "mpic++" -a "$TRAVIS_OS_NAME" == "linux" ; then sudo apt-get install -y libopenmpi-dev openmpi-bin ; fi
diff --git a/src/maketools/codecheck b/src/maketools/codecheck
index 15b6bd2f2..7f6a67e8a 100755
--- a/src/maketools/codecheck
+++ b/src/maketools/codecheck
@@ -22,9 +22,9 @@ done
 
 if [ $do_plumedcheck == true ] ; then
   if [ $do_local == true ] ; then
-    ../maketools/plumedcheck *.{h,cpp} ../../configure.ac
+    ../maketools/plumedcheck *.{h,cpp} ../../configure.ac --astyle=../../astyle/astyle --astyle-options=../../.astyle.options
   else
-    maketools/plumedcheck --global-check */*.{h,cpp} ../configure.ac
+    maketools/plumedcheck --global-check */*.{h,cpp} ../configure.ac --astyle=../astyle/astyle --astyle-options=../.astyle.options
   fi
 fi
 
diff --git a/src/maketools/plumedcheck b/src/maketools/plumedcheck
index 751a07db3..120808dec 100755
--- a/src/maketools/plumedcheck
+++ b/src/maketools/plumedcheck
@@ -66,6 +66,7 @@ BEGIN{
   newargv[0]=ARGV[0]
   pre=""
   opt_global_check=0
+  astyle="astyle"
   for(i=1;i<ARGC;i++){
     opt=pre ARGV[i]
     pre=""
@@ -75,6 +76,14 @@ BEGIN{
         usage(); break;
       case "--global-check":
         opt_global_check=1; break;
+      case /--astyle=.*/:
+        sub("^--astyle=","",opt);
+        astyle=opt;
+      case /--astyle-options=.*/:
+        sub("^--astyle-options=","",opt);
+        astyle_options=opt;
+        break;
+        break;
       case /-.*/:
         print "Unknown option " opt; exit(1)
       default:
@@ -129,6 +138,9 @@ BEGIN{
   core_modules["core"]=1
   core_modules["tools"]=1
   core_modules["reference"]=1
+
+# create tmp dir for future usage
+  "mktemp -d" | getline tmpdir
 }
 
 # for each input file
@@ -182,6 +194,14 @@ BEGINFILE{
 # checks that are done only on some modules will be skipped if the module name is not known
   if((filetype=="source" || filetype=="header") && !module) information("missing_module_name","module name is not know, some check will be skipped");
 
+  if(filetype=="source" || filetype=="header"){
+    tempfile = tmpdir "/astyle"
+    system(astyle " --options=" astyle_options " < " FILENAME " > " tempfile)
+    s=system("diff -q " FILENAME " " tempfile ">/dev/null 2>/dev/null")
+    if(s!=0) error("astyle","astyle not satisfied")
+    system("rm " tempfile)
+  }
+
 }
 
 # line by line analysis
@@ -526,6 +546,9 @@ END{
       error("unused_has","has " has " at " plumed_definehas[has] " is not used")
     }
   }
+
+# remove temporary directory
+  system("rmdir " tmpdir);
 }
 
 EOF
-- 
GitLab