mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-06-01 02:55:07 +08:00
Auto Formatting with clang-format-6.0 (#727)
* Add clang-format * Auto-format AlphaFilter and RingBuffer * Update to tab=8spaces * Allow for 120 width
This commit is contained in:
+117
@@ -0,0 +1,117 @@
|
|||||||
|
---
|
||||||
|
Language: Cpp
|
||||||
|
# BasedOnStyle: Google
|
||||||
|
AccessModifierOffset: -8 # Modified
|
||||||
|
AlignAfterOpenBracket: Align
|
||||||
|
AlignConsecutiveAssignments: false
|
||||||
|
AlignConsecutiveDeclarations: false
|
||||||
|
AlignEscapedNewlines: Left
|
||||||
|
AlignOperands: true
|
||||||
|
AlignTrailingComments: true
|
||||||
|
AllowAllParametersOfDeclarationOnNextLine: true
|
||||||
|
AllowShortBlocksOnASingleLine: false
|
||||||
|
AllowShortCaseLabelsOnASingleLine: false
|
||||||
|
AllowShortFunctionsOnASingleLine: All
|
||||||
|
AllowShortIfStatementsOnASingleLine: true
|
||||||
|
AllowShortLoopsOnASingleLine: true
|
||||||
|
AlwaysBreakAfterDefinitionReturnType: None
|
||||||
|
AlwaysBreakAfterReturnType: None
|
||||||
|
AlwaysBreakBeforeMultilineStrings: true
|
||||||
|
AlwaysBreakTemplateDeclarations: true
|
||||||
|
BinPackArguments: true
|
||||||
|
BinPackParameters: true
|
||||||
|
BraceWrapping:
|
||||||
|
AfterClass: false
|
||||||
|
AfterControlStatement: false
|
||||||
|
AfterEnum: false
|
||||||
|
AfterFunction: false
|
||||||
|
AfterNamespace: false
|
||||||
|
AfterObjCDeclaration: false
|
||||||
|
AfterStruct: false
|
||||||
|
AfterUnion: false
|
||||||
|
AfterExternBlock: false
|
||||||
|
BeforeCatch: false
|
||||||
|
BeforeElse: false
|
||||||
|
IndentBraces: false
|
||||||
|
SplitEmptyFunction: true
|
||||||
|
SplitEmptyRecord: true
|
||||||
|
SplitEmptyNamespace: true
|
||||||
|
BreakBeforeBinaryOperators: None
|
||||||
|
BreakBeforeBraces: Attach
|
||||||
|
BreakBeforeInheritanceComma: false
|
||||||
|
BreakBeforeTernaryOperators: true
|
||||||
|
BreakConstructorInitializersBeforeComma: false
|
||||||
|
BreakConstructorInitializers: BeforeColon
|
||||||
|
BreakAfterJavaFieldAnnotations: false
|
||||||
|
BreakStringLiterals: true
|
||||||
|
ColumnLimit: 120
|
||||||
|
CommentPragmas: '^ IWYU pragma:'
|
||||||
|
CompactNamespaces: false
|
||||||
|
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||||
|
ConstructorInitializerIndentWidth: 4
|
||||||
|
ContinuationIndentWidth: 4
|
||||||
|
Cpp11BracedListStyle: true
|
||||||
|
DerivePointerAlignment: true
|
||||||
|
DisableFormat: false
|
||||||
|
ExperimentalAutoDetectBinPacking: false
|
||||||
|
FixNamespaceComments: true
|
||||||
|
ForEachMacros:
|
||||||
|
- foreach
|
||||||
|
- Q_FOREACH
|
||||||
|
- BOOST_FOREACH
|
||||||
|
IncludeBlocks: Preserve
|
||||||
|
IncludeCategories:
|
||||||
|
- Regex: '^<ext/.*\.h>'
|
||||||
|
Priority: 2
|
||||||
|
- Regex: '^<.*\.h>'
|
||||||
|
Priority: 1
|
||||||
|
- Regex: '^<.*'
|
||||||
|
Priority: 2
|
||||||
|
- Regex: '.*'
|
||||||
|
Priority: 3
|
||||||
|
IncludeIsMainRegex: '([-_](test|unittest))?$'
|
||||||
|
IndentCaseLabels: true
|
||||||
|
IndentPPDirectives: None
|
||||||
|
IndentWidth: 8 # Modified
|
||||||
|
IndentWrappedFunctionNames: false
|
||||||
|
JavaScriptQuotes: Leave
|
||||||
|
JavaScriptWrapImports: true
|
||||||
|
KeepEmptyLinesAtTheStartOfBlocks: false
|
||||||
|
MacroBlockBegin: ''
|
||||||
|
MacroBlockEnd: ''
|
||||||
|
MaxEmptyLinesToKeep: 1
|
||||||
|
NamespaceIndentation: None
|
||||||
|
ObjCBlockIndentWidth: 2
|
||||||
|
ObjCSpaceAfterProperty: false
|
||||||
|
ObjCSpaceBeforeProtocolList: false
|
||||||
|
PenaltyBreakAssignment: 2
|
||||||
|
PenaltyBreakBeforeFirstCallParameter: 1
|
||||||
|
PenaltyBreakComment: 300
|
||||||
|
PenaltyBreakFirstLessLess: 120
|
||||||
|
PenaltyBreakString: 1000
|
||||||
|
PenaltyExcessCharacter: 1000000
|
||||||
|
PenaltyReturnTypeOnItsOwnLine: 200
|
||||||
|
PointerAlignment: Left
|
||||||
|
RawStringFormats:
|
||||||
|
- Delimiter: pb
|
||||||
|
Language: TextProto
|
||||||
|
BasedOnStyle: google
|
||||||
|
ReflowComments: true
|
||||||
|
SortIncludes: true
|
||||||
|
SortUsingDeclarations: true
|
||||||
|
SpaceAfterCStyleCast: false
|
||||||
|
SpaceAfterTemplateKeyword: true
|
||||||
|
SpaceBeforeAssignmentOperators: true
|
||||||
|
SpaceBeforeParens: ControlStatements
|
||||||
|
SpaceInEmptyParentheses: false
|
||||||
|
SpacesBeforeTrailingComments: 2
|
||||||
|
SpacesInAngles: false
|
||||||
|
SpacesInContainerLiterals: true
|
||||||
|
SpacesInCStyleCastParentheses: false
|
||||||
|
SpacesInParentheses: false
|
||||||
|
SpacesInSquareBrackets: false
|
||||||
|
Standard: Auto
|
||||||
|
TabWidth: 8 # Modified
|
||||||
|
UseTab: Always # Modified
|
||||||
|
...
|
||||||
|
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
name: Format Checks
|
||||||
|
|
||||||
|
on: [push, pull_request]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
format_checks:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container: px4io/px4-dev-clang:2019-10-24
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v1
|
||||||
|
- name: install clang-format-6.0
|
||||||
|
run: apt install -y clang-format-6.0
|
||||||
|
- name: check_format
|
||||||
|
run: ./tools/format.sh 0
|
||||||
|
|
||||||
+8
-16
@@ -38,34 +38,26 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
template<typename T>
|
template <typename T>
|
||||||
class AlphaFilter final
|
class AlphaFilter final {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
AlphaFilter() = default;
|
AlphaFilter() = default;
|
||||||
~AlphaFilter() = default;
|
~AlphaFilter() = default;
|
||||||
|
|
||||||
void reset(const T &val) { _x = val; }
|
void reset(const T &val) { _x = val; }
|
||||||
|
|
||||||
void update(const T &input, float tau, float dt)
|
void update(const T &input, float tau, float dt) {
|
||||||
{
|
const float alpha = dt / tau;
|
||||||
const float alpha = dt / tau;
|
|
||||||
update(input, alpha);
|
update(input, alpha);
|
||||||
}
|
}
|
||||||
|
|
||||||
void update(const T &input, float alpha)
|
void update(const T &input, float alpha) { _x = (1.f - alpha) * _x + alpha * input; }
|
||||||
{
|
|
||||||
_x = (1.f - alpha) * _x + alpha * input;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Typical 0.9/0.1 lowpass filter
|
// Typical 0.9/0.1 lowpass filter
|
||||||
void update(const T &input)
|
void update(const T &input) { update(input, 0.1f); }
|
||||||
{
|
|
||||||
update(input, 0.1f);
|
|
||||||
}
|
|
||||||
|
|
||||||
const T& getState() const { return _x; }
|
const T &getState() const { return _x; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T _x{}; ///< current state of the filter
|
T _x{}; ///< current state of the filter
|
||||||
};
|
};
|
||||||
|
|||||||
+13
-17
@@ -42,11 +42,9 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
template <typename data_type>
|
template <typename data_type>
|
||||||
class RingBuffer
|
class RingBuffer {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
RingBuffer()
|
RingBuffer() {
|
||||||
{
|
|
||||||
if (allocate(1)) {
|
if (allocate(1)) {
|
||||||
// initialize with one empty sample
|
// initialize with one empty sample
|
||||||
data_type d = {};
|
data_type d = {};
|
||||||
@@ -61,8 +59,7 @@ public:
|
|||||||
RingBuffer(RingBuffer &&) = delete;
|
RingBuffer(RingBuffer &&) = delete;
|
||||||
RingBuffer &operator=(RingBuffer &&) = delete;
|
RingBuffer &operator=(RingBuffer &&) = delete;
|
||||||
|
|
||||||
bool allocate(uint8_t size)
|
bool allocate(uint8_t size) {
|
||||||
{
|
|
||||||
if (_buffer != nullptr) {
|
if (_buffer != nullptr) {
|
||||||
delete[] _buffer;
|
delete[] _buffer;
|
||||||
}
|
}
|
||||||
@@ -78,7 +75,8 @@ public:
|
|||||||
_head = 0;
|
_head = 0;
|
||||||
_tail = 0;
|
_tail = 0;
|
||||||
|
|
||||||
// set the time elements to zero so that bad data is not retrieved from the buffers
|
// set the time elements to zero so that bad data is not
|
||||||
|
// retrieved from the buffers
|
||||||
for (uint8_t index = 0; index < _size; index++) {
|
for (uint8_t index = 0; index < _size; index++) {
|
||||||
_buffer[index] = {};
|
_buffer[index] = {};
|
||||||
}
|
}
|
||||||
@@ -88,14 +86,12 @@ public:
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void unallocate()
|
void unallocate() {
|
||||||
{
|
|
||||||
delete[] _buffer;
|
delete[] _buffer;
|
||||||
_buffer = nullptr;
|
_buffer = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void push(const data_type &sample)
|
void push(const data_type &sample) {
|
||||||
{
|
|
||||||
uint8_t head_new = _head;
|
uint8_t head_new = _head;
|
||||||
|
|
||||||
if (!_first_write) {
|
if (!_first_write) {
|
||||||
@@ -123,19 +119,18 @@ public:
|
|||||||
|
|
||||||
uint8_t get_oldest_index() const { return _tail; }
|
uint8_t get_oldest_index() const { return _tail; }
|
||||||
|
|
||||||
bool pop_first_older_than(const uint64_t ×tamp, data_type *sample)
|
bool pop_first_older_than(const uint64_t ×tamp, data_type *sample) {
|
||||||
{
|
|
||||||
// start looking from newest observation data
|
// start looking from newest observation data
|
||||||
for (uint8_t i = 0; i < _size; i++) {
|
for (uint8_t i = 0; i < _size; i++) {
|
||||||
int index = (_head - i);
|
int index = (_head - i);
|
||||||
index = index < 0 ? _size + index : index;
|
index = index < 0 ? _size + index : index;
|
||||||
|
|
||||||
if (timestamp >= _buffer[index].time_us && timestamp - _buffer[index].time_us < (uint64_t)1e5) {
|
if (timestamp >= _buffer[index].time_us && timestamp - _buffer[index].time_us < (uint64_t)1e5) {
|
||||||
|
|
||||||
*sample = _buffer[index];
|
*sample = _buffer[index];
|
||||||
|
|
||||||
// Now we can set the tail to the item which comes after the one we removed
|
// Now we can set the tail to the item which
|
||||||
// since we don't want to have any older data in the buffer
|
// comes after the one we removed since we don't
|
||||||
|
// want to have any older data in the buffer
|
||||||
if (index == _head) {
|
if (index == _head) {
|
||||||
_tail = _head;
|
_tail = _head;
|
||||||
_first_write = true;
|
_first_write = true;
|
||||||
@@ -150,7 +145,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (index == _tail) {
|
if (index == _tail) {
|
||||||
// we have reached the tail and haven't got a match
|
// we have reached the tail and haven't got a
|
||||||
|
// match
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,8 @@
|
|||||||
FIRST_ARG := $(firstword $(MAKECMDGOALS))
|
FIRST_ARG := $(firstword $(MAKECMDGOALS))
|
||||||
ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
|
ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
|
||||||
j ?= 4
|
j ?= 4
|
||||||
|
BLUE='\033[1;36m'
|
||||||
|
NC='\033[0m' # No Color
|
||||||
|
|
||||||
NINJA_BIN := ninja
|
NINJA_BIN := ninja
|
||||||
ifndef NO_NINJA_BUILD
|
ifndef NO_NINJA_BUILD
|
||||||
@@ -95,7 +97,7 @@ doxygen:
|
|||||||
# Testing
|
# Testing
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
|
|
||||||
.PHONY: test_build test test_EKF
|
.PHONY: test_build test
|
||||||
|
|
||||||
test_build:
|
test_build:
|
||||||
@$(call cmake-build,$@,$(SRC_DIR), "-DBUILD_TESTING=ON")
|
@$(call cmake-build,$@,$(SRC_DIR), "-DBUILD_TESTING=ON")
|
||||||
@@ -124,6 +126,22 @@ coverage_html: coverage_build
|
|||||||
coverage_html_view: coverage_build
|
coverage_html_view: coverage_build
|
||||||
@cmake --build $(SRC_DIR)/build/coverage_build --target coverage_html_view
|
@cmake --build $(SRC_DIR)/build/coverage_build --target coverage_html_view
|
||||||
|
|
||||||
|
# Code formatting
|
||||||
|
# --------------------------------------------------------------------
|
||||||
|
.PHONY: check_format format clang-format
|
||||||
|
|
||||||
|
clang-format:
|
||||||
|
@echo -e ${BLUE}Check clang-format-6.0 installation${NC}
|
||||||
|
@if ! hash clang-format-6.0; then sudo apt install clang-format-6.0 -y; fi
|
||||||
|
|
||||||
|
check_format: clang-format
|
||||||
|
@echo -e ${BLUE}Checking formatting with clang-format${NC}
|
||||||
|
@$(SRC_DIR)/tools/format.sh 0
|
||||||
|
|
||||||
|
format: clang-format
|
||||||
|
@echo -e ${BLUE}Formatting with clang-format${NC}
|
||||||
|
@$(SRC_DIR)/tools/format.sh 1
|
||||||
|
|
||||||
# Cleanup
|
# Cleanup
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
.PHONY: clean distclean
|
.PHONY: clean distclean
|
||||||
|
|||||||
Executable
+34
@@ -0,0 +1,34 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
do_format=$1
|
||||||
|
files_to_format="""
|
||||||
|
EKF/AlphaFilter.hpp
|
||||||
|
EKF/RingBuffer.h
|
||||||
|
"""
|
||||||
|
RED='\033[0;31m'
|
||||||
|
GREEN='\033[0;32m'
|
||||||
|
YELLOW='\033[1;33m'
|
||||||
|
NC='\033[0m' # No Color
|
||||||
|
|
||||||
|
if ! hash clang-format-6.0
|
||||||
|
then
|
||||||
|
echo -e ${RED}Error: No clang-format-6.0 installed${NC}
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ $do_format -eq 1 ]]
|
||||||
|
then
|
||||||
|
# formatting
|
||||||
|
clang-format-6.0 -i -style=file ${files_to_format}
|
||||||
|
echo -e ${GREEN}Formatting finished${NC}
|
||||||
|
else
|
||||||
|
# checking format...
|
||||||
|
clang-format-6.0 -style=file -output-replacements-xml ${files_to_format} | grep -c "<replacement " > /dev/null
|
||||||
|
if [[ $? -eq 0 ]]
|
||||||
|
then
|
||||||
|
echo -e ${RED}Error: need to format${NC}
|
||||||
|
echo -e ${YELLOW}From cmake build directory run: 'make format'${NC}
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo -e ${GREEN}no formatting needed${NC}
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
Reference in New Issue
Block a user