Skip to content

Use ClangFormat as code formatter for examples #58

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

Merged
merged 8 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 190 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
# Source: https://github.com/arduino/tooling-project-assets/tree/main/other/clang-format-configuration
---
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignArrayOfStructures: None
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields: None
AlignConsecutiveDeclarations: None
AlignConsecutiveMacros: None
AlignEscapedNewlines: DontAlign
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: true
AllowShortEnumsOnASingleLine: true
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AllowShortLambdasOnASingleLine: Empty
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: No
AttributeMacros:
- __capability
BasedOnStyle: LLVM
BinPackArguments: true
BinPackParameters: true
BitFieldColonSpacing: Both
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakAfterJavaFieldAnnotations: false
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Attach
BreakBeforeConceptDeclarations: false
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakConstructorInitializersBeforeComma: false
BreakInheritanceList: BeforeColon
BreakStringLiterals: false
ColumnLimit: 0
CommentPragmas: ''
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 2
Cpp11BracedListStyle: false
DeriveLineEnding: true
DerivePointerAlignment: true
DisableFormat: false
EmptyLineAfterAccessModifier: Leave
EmptyLineBeforeAccessModifier: Leave
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IfMacros:
- KJ_IF_MAYBE
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
SortPriority: 0
CaseSensitive: false
- Regex: '.*'
Priority: 1
SortPriority: 0
CaseSensitive: false
IncludeIsMainRegex: ''
IncludeIsMainSourceRegex: ''
IndentAccessModifiers: false
IndentCaseBlocks: true
IndentCaseLabels: true
IndentExternBlock: Indent
IndentGotoLabels: false
IndentPPDirectives: None
IndentRequires: true
IndentWidth: 2
IndentWrappedFunctionNames: false
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
LambdaBodyIndentation: Signature
Language: Cpp
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 100000
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PPIndentWidth: -1
PackConstructorInitializers: BinPack
PenaltyBreakAssignment: 1
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 1
PenaltyBreakFirstLessLess: 1
PenaltyBreakOpenParenthesis: 1
PenaltyBreakString: 1
PenaltyBreakTemplateDeclaration: 1
PenaltyExcessCharacter: 1
PenaltyIndentedWhitespace: 1
PenaltyReturnTypeOnItsOwnLine: 1
PointerAlignment: Right
QualifierAlignment: Leave
ReferenceAlignment: Pointer
ReflowComments: false
RemoveBracesLLVM: false
SeparateDefinitionBlocks: Leave
ShortNamespaceLines: 0
SortIncludes: Never
SortJavaStaticImport: Before
SortUsingDeclarations: false
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceAroundPointerQualifiers: Default
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeParensOptions:
AfterControlStatements: true
AfterForeachMacros: true
AfterFunctionDefinitionName: false
AfterFunctionDeclarationName: false
AfterIfMacros: true
AfterOverloadedOperator: false
BeforeNonEmptyParentheses: false
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: Leave
SpacesInCStyleCastParentheses: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: false
SpacesInLineCommentPrefix:
Minimum: 0
Maximum: -1
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Auto
StatementAttributeLikeMacros:
- Q_EMIT
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 2
UseCRLF: false
UseTab: Never
WhitespaceSensitiveMacros:
- STRINGIZE
- PP_STRINGIZE
- BOOST_PP_STRINGIZE
- NS_SWIFT_NAME
- CF_SWIFT_NAME
64 changes: 31 additions & 33 deletions .github/workflows/code-formatting-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,58 @@ on:
pull_request:
paths:
- ".github/workflows/code-formatting-check.yml"
- ".clang-format"
- "examples_formatter.sh"
- "examples/**"
push:
paths:
- ".github/workflows/code-formatting-check.yml"
- ".clang-format"
- "examples_formatter.sh"
- "examples/**"

jobs:
check-formatting:
runs-on: ubuntu-latest

env:
# See: https://sourceforge.net/projects/astyle/files/astyle/
ASTYLE_VERSION: 3.1
# See: https://github.com/arduino/arduino-ide/blob/main/arduino-ide-extension/package.json
CLANG_FORMAT_VERSION: 14.0.0

steps:
- name: Set environment variables
run: |
# See: https://docs.github.com/actions/using-workflows/workflow-commands-for-github-actions#setting-an-environment-variable
echo "CLANG_FORMAT_INSTALL_PATH=${{ runner.temp }}/clang-format" >> "$GITHUB_ENV"

- name: Checkout
uses: actions/checkout@v3

# See: http://astyle.sourceforge.net/
- name: Download Artistic Style
uses: carlosperate/download-file-action@v1
- name: Download ClangFormat
id: download
uses: MrOctopus/download-asset-action@1.0
with:
file-url: https://iweb.dl.sourceforge.net/project/astyle/astyle/astyle%20${{ env.ASTYLE_VERSION }}/astyle_${{ env.ASTYLE_VERSION }}_linux.tar.gz
location: ${{ runner.temp }}/astyle
repository: arduino/clang-static-binaries
tag: ${{ env.CLANG_FORMAT_VERSION }}
asset: clang-format_${{ env.CLANG_FORMAT_VERSION }}_Linux_64bit.tar.bz2
target: ${{ env.CLANG_FORMAT_INSTALL_PATH }}

# See: http://astyle.sourceforge.net/install.html#_GCC_Makefile
- name: Build Artistic Style
- name: Install ClangFormat
run: |
# Build Artistic Style
cd "${{ runner.temp }}/astyle"
tar --extract --file="astyle_${{ env.ASTYLE_VERSION }}_linux.tar.gz"
cd "astyle/build/gcc"
make

# GITHUB_WORKSPACE
- name: Check code formatting
cd "${{ env.CLANG_FORMAT_INSTALL_PATH }}"
tar --extract --file="${{ steps.download.outputs.name }}"
# Add installation to PATH:
# See: https://docs.github.com/actions/using-workflows/workflow-commands-for-github-actions#adding-a-system-path
echo "${{ env.CLANG_FORMAT_INSTALL_PATH }}/clang_Linux_64bit" >> "$GITHUB_PATH"

- name: Format examples
run: ./examples_formatter.sh

- name: Check formatting
run: |
# Check code formatting of example sketches
# Don't exit on first formatting check fail
set +e
# Set default exit status
EXIT_STATUS=0
while read -r filePath; do
# Check if it's a file (find matches on pruned folders)
if [[ -f "$filePath" ]]; then
if ! diff --strip-trailing-cr "$filePath" <("${{ runner.temp }}/astyle/astyle/build/gcc/bin/astyle" --options="${GITHUB_WORKSPACE}/examples_formatter.conf" --dry-run <"$filePath"); then
echo "ERROR: Non-compliant code formatting in $filePath"
EXIT_STATUS=1
fi
fi
done <<<"$(find "${GITHUB_WORKSPACE}/examples" -regextype posix-extended \( -regex '.*\.((ino)|(h)|(cpp)|(c))$' -and -type f \))"
if [[ "$EXIT_STATUS" != "0" ]]; then
if ! git diff --color --exit-code; then
echo "Please do an Auto Format on the sketches:"
echo "Arduino IDE: Tools > Auto Format"
echo "Arduino Web Editor: Ctrl + B"
exit 1
fi
exit "$EXIT_STATUS"
2 changes: 1 addition & 1 deletion examples/01.Basics/AnalogReadSerial/AnalogReadSerial.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ void loop() {
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
delay(1); // delay in between reads for stability
}
2 changes: 0 additions & 2 deletions examples/01.Basics/BareMinimum/BareMinimum.ino
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
void setup() {
// put your setup code here, to run once:

}

void loop() {
// put your main code here, to run repeatedly:

}
8 changes: 4 additions & 4 deletions examples/01.Basics/Blink/Blink.ino
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ void setup() {

// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
2 changes: 1 addition & 1 deletion examples/01.Basics/DigitalReadSerial/DigitalReadSerial.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ void loop() {
int buttonState = digitalRead(pushButton);
// print out the state of the button:
Serial.println(buttonState);
delay(1); // delay in between reads for stability
delay(1); // delay in between reads for stability
}
6 changes: 3 additions & 3 deletions examples/01.Basics/Fade/Fade.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Fade
*/

int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
Expand Down
8 changes: 4 additions & 4 deletions examples/02.Digital/BlinkWithoutDelay/BlinkWithoutDelay.ino
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@
*/

// constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN;// the number of the LED pin
const int ledPin = LED_BUILTIN; // the number of the LED pin

// Variables will change:
int ledState = LOW; // ledState used to set the LED
int ledState = LOW; // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long previousMillis = 0; // will store last time LED was updated

// constants won't change:
const long interval = 1000; // interval at which to blink (milliseconds)
const long interval = 1000; // interval at which to blink (milliseconds)

void setup() {
// set the digital pin as output:
Expand Down
6 changes: 3 additions & 3 deletions examples/02.Digital/Button/Button.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
Expand Down
10 changes: 5 additions & 5 deletions examples/02.Digital/Debounce/Debounce.ino
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ void setup() {
//configure pin 2 as an input and enable the internal pull-up resistor
pinMode(2, INPUT_PULLUP);
pinMode(13, OUTPUT);

}

void loop() {
Expand Down
Loading