×

INDI Library v2.0.6 is Released (02 Feb 2024)

Bi-monthly release with minor bug fixes and improvements

INDI Code Style

  • Posts: 111
  • Thank you received: 40
Hi there,

is there an official INDI code style (e.g. clang-format) file?
In the core INDI repo exists only the style file
./drivers/spectrograph/shelyak/.clang-format
whereas in the indi-3rdparty repo two additional clang-format files exist:
./.clang-format
./indi-libcamera/libcamera-apps/.clang-format

The .clang-format files differ and are not ensuring a conform code style.

In the main INDI repo
CMakeLists.txt
already contains code for formatting and checking:
# Clang Format support
if(UNIX OR APPLE)
    set(FORMAT_CODE OFF CACHE BOOL "Enable Clang Format")
...

How about unifying the code style with one .clang-format file (for core and 3rdparty)?

Cheers
Thomas
1 year 2 months ago #89648

Please Log in or Create an account to join the conversation.

Replied by Jasem Mutlaq on topic INDI Code Style

There is astyle format used and documented in the INDI Github. But I don't mind using a universal clang-format as well.
1 year 2 months ago #89649

Please Log in or Create an account to join the conversation.

  • Posts: 111
  • Thank you received: 40

Replied by Thomas Stibor on topic INDI Code Style

Sticking with astyle is probably a simpler approch and it is already documented in README.md as you mentioned.
With astyle one could also hook into github's CI
steps:
    - script: sudo apt-get install -y astyle
      displayName: 'Install extra package'
    - bash: for f in `git diff --name-only origin/master`; do check_cstyle.sh "${f}"; done
      displayName: 'C style check'

and use e.g. the following script check_cstyle.sh to show the ill-formatted lines:
#!/bin/bash
 
FILE_TO_CHECK=${1}
RC=0
 
# Make sure file exists and has proper suffix.
if [[ ! -f "${FILE_TO_CHECK}" ]] || [[ ! "${FILE_TO_CHECK##*\.}" =~ ^[cpp,cxx,c,h,C,H]$ ]]; then
    exit ${RC}
fi
 
TMP_FILE_DIFF=$(mktemp /tmp/indi.diff.XXXXXXXXX)
TMP_FILE_ASTYLE=$(mktemp /tmp/indi.astyle.XXXXXXXXX)
TMP_FILE_WARNINGS=$(mktemp /tmp/indi.warning.XXXXXXXXX)
ASTYLE_OPTIONS="--style=allman
                --align-reference=name
                --indent-switches
                --indent-modifiers
                --indent-classes
                --pad-oper
                --indent-col1-comments
                --lineend=linux
                --max-code-length=124"
 
git diff --function-context --unified=1 HEAD ${FILE_TO_CHECK} | sed -e '/diff --git/,+3d;/^@@/d;/^-/d;s/^+/ /' | cut -d' ' -f2- > ${TMP_FILE_DIFF}
astyle ${ASTYLE_OPTIONS} -n < ${TMP_FILE_DIFF} > ${TMP_FILE_ASTYLE}
diff -u ${TMP_FILE_DIFF} ${TMP_FILE_ASTYLE} | tail -n +3 | sed -e '/^-/d;/^@@/d;s/^+/[STYLE WARNING] /' > ${TMP_FILE_WARNINGS}
 
if [[ -s ${TMP_FILE_WARNINGS} ]]; then
    cat ${TMP_FILE_WARNINGS}
    RC=1
fi
 
if [[ ${RC} -eq 1 ]]; then
    echo -e "File ${FILE_TO_CHECK} has style warning(s), see" \
         "https://github.com/indilib/indi/blob/master/README.md"
fi
 
rm -f ${TMP_FILE_DIFF} ${TMP_FILE_ASTYLE} ${TMP_FILE_WARNINGS}
 
exit ${RC}
The following user(s) said Thank You: Jasem Mutlaq
1 year 2 months ago #89653

Please Log in or Create an account to join the conversation.

Replied by Jasem Mutlaq on topic INDI Code Style

That's awesome! So you think you can submit a PR to include astyle as part of the CI process??
1 year 2 months ago #89670

Please Log in or Create an account to join the conversation.

  • Posts: 111
  • Thank you received: 40

Replied by Thomas Stibor on topic INDI Code Style

Hi Jasem,

I think it should work and one could integrate it into the github CI process. It actually works as follows:
For demonstration I ill-formatted:
diff --git a/examples/tutorial_one/simpledevice.cpp b/examples/tutorial_one/simpledevice.cpp
index cbb68f65e..82e5d76eb 100644
--- a/examples/tutorial_one/simpledevice.cpp
+++ b/examples/tutorial_one/simpledevice.cpp
@@ -30,15 +30,14 @@ std::unique_ptr<SimpleDevice> simpleDevice(new SimpleDevice());
 ***************************************************************************************/
 bool SimpleDevice::Connect()
 {
-    IDMessage(getDeviceName(), "Simple device connected successfully!");
-    return true;
+IDMessage(getDeviceName(), "Simple device connected successfully!");
+return true;
 }
 
 /**************************************************************************************
 ** Client is asking us to terminate connection to the device
 ***************************************************************************************/
-bool SimpleDevice::Disconnect()
-{
+bool SimpleDevice::Disconnect() {
     IDMessage(getDeviceName(), "Simple device disconnected successfully!");
     return true;
 }
diff --git a/examples/tutorial_one/simpledevice.h b/examples/tutorial_one/simpledevice.h
index 0b0dbf709..b02ac03c0 100644
--- a/examples/tutorial_one/simpledevice.h
+++ b/examples/tutorial_one/simpledevice.h
@@ -25,7 +25,7 @@
 
 class SimpleDevice : public INDI::DefaultDevice
 {
-    public:
+public:
         SimpleDevice() = default;
 
     protected:

Once the the PR is fetched on github one could integrate into the CI the bash command and script:
$ for f in `git diff --name-only origin/master`; do .circleci/check-codestyle.sh "${f}"; done
***************************************************************************************/
 bool SimpleDevice::Connect()
 {
[STYLE WARNING]     IDMessage(getDeviceName(), "Simple device connected successfully!");
[STYLE WARNING]     return true;
 }
 
 /**************************************************************************************
 ** Client is asking us to terminate connection to the device
 ***************************************************************************************/
[STYLE WARNING] bool SimpleDevice::Disconnect()
[STYLE WARNING] {
     IDMessage(getDeviceName(), "Simple device disconnected successfully!");
     return true;
 }
File examples/tutorial_one/simpledevice.cpp has style warning(s), see https://github.com/indilib/indi/blob/master/README.md
 class SimpleDevice : public INDI::DefaultDevice
 {
[STYLE WARNING]     public:
         SimpleDevice() = default;
 
     protected:
File examples/tutorial_one/simpledevice.h has style warning(s), see https://github.com/indilib/indi/blob/master/README.md

I will try that out and send a PR when it works

Cheers
Thomas
1 year 2 months ago #89677

Please Log in or Create an account to join the conversation.

Time to create page: 0.307 seconds