Skip to content

Commit

Permalink
Add clang-format CI
Browse files Browse the repository at this point in the history
Add `.clang-format-ignore`

Use LLVM in format

Populate `.clang-format-ignore`

add `Samples/Common/Sources/CrashTriggers` to trigggers

Add subdirectories in ignore

Run only for trigger paths
  • Loading branch information
GLinnik21 committed Jun 28, 2024
1 parent 183243f commit a3d61b5
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 60 deletions.
37 changes: 5 additions & 32 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,36 +1,9 @@
BasedOnStyle: Google
IndentWidth: 4
TabWidth: 4
UseTab: Never
ColumnLimit: 120
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
AllowShortIfStatementsOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
ObjCSpaceAfterProperty: true
ObjCBlockIndentWidth: 4
ObjCSpaceBeforeProtocolList: true
SpaceAfterCStyleCast: false
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: false
SpacesInAngles: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Cpp11BracedListStyle: false
ColumnLimit: 120
BreakBeforeBraces: Linux
PointerAlignment: Right
DerivePointerAlignment: false
PointerAlignment: Left
IndentCaseLabels: true
SpaceBeforeCpp11BracedList: true
Cpp11BracedListStyle: false
5 changes: 5 additions & 0 deletions .clang-format-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Sources/KSCrashRecordingCore/KSObjCApple.h
Sources/KSCrashRecordingCore/llvm/*
Sources/KSCrashRecordingCore/llvm/*/*
Sources/KSCrashRecordingCore/swift/*
Sources/KSCrashRecordingCore/swift/*/*
61 changes: 61 additions & 0 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Clang Format Check
on:
pull_request:
paths:
- 'Sources/**'
- 'Tests/**'
- 'Samples/Common/Sources/CrashTriggers/**'
- '.github/workflows/clang-format.yml'
- '.clang-format-ignore'

jobs:
formatting-check:
name: Formatting Check
runs-on: ubuntu-latest
env:
DIRS: 'Sources Tests Samples/Common/Sources/CrashTriggers'
steps:
- uses: actions/checkout@v4

- name: Install clang-format-18
run: |
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh 18
sudo apt-get install -y clang-format-18
- name: Check formatting
id: check_format
run: |
find $DIRS -name '*.c' -o -name '*.cpp' -o -name '*.h' -o -name '*.m' -o -name '*.mm' | \
xargs -r clang-format-18 -style=file -n -Werror 2> clang_format_errors.log
- name: Suggest formatting fixes
if: failure()
run: |
echo "##[error]Formatting issues found. Please run clang-format-18 on your code."
- name: Create summary and annotations
if: failure()
run: |
echo "### Formatting issues found" >> $GITHUB_STEP_SUMMARY
echo "Please run clang-format-18 on your code." >> $GITHUB_STEP_SUMMARY
echo "Note: Some files may be excluded from formatting based on .clang-format-ignore file." >> $GITHUB_STEP_SUMMARY
echo "::group::Formatting issues"
awk '
/^.*error:/ {
split($0, parts, ":")
file=parts[1]
line=parts[2]
col=parts[3]
message=$0
getline code_line
getline caret_line
sub(/:.*$/, "", file)
sub(/[^0-9]/, "", line)
sub(/[^0-9]/, "", col)
# Remove the "error: " prefix from the message
sub(/^[^:]+: [^:]+: [^:]+: /, "", message)
print "::error file="file",line="line",col="col"::"message
}' clang_format_errors.log
echo "::endgroup::"
4 changes: 2 additions & 2 deletions Sources/KSCrashRecording/KSCrashReportStore.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ static void pruneReports(void)
}
}
}

// clang-format off
static void initializeIDs(void)
{
time_t rawTime;
Expand All @@ -170,7 +170,7 @@ static void initializeIDs(void)
g_nextUniqueIDHigh = baseID & ~(int64_t)0xffffffff;
g_nextUniqueIDLow = (uint32_t)(baseID & 0xffffffff);
}

// clang-format on

// Public API

Expand Down
2 changes: 2 additions & 0 deletions Sources/KSCrashRecording/include/KSCrashAppTransitionState.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ extern "C" {
#define NS_SWIFT_NAME(_name)
#endif

// clang-format off
/** States of transition for the application */
#ifdef __OBJC__
typedef NS_ENUM(uint8_t, KSCrashAppTransitionState)
Expand All @@ -60,6 +61,7 @@ enum
#ifndef __OBJC__
typedef uint8_t KSCrashAppTransitionState;
#endif
// clang-format on

/**
* Returns true if the transition state is user perceptible.
Expand Down
4 changes: 4 additions & 0 deletions Sources/KSCrashRecording/include/KSCrashMonitorType.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ extern "C" {
#define NS_SWIFT_NAME(_name)
#endif

// clang-format off

/** Various aspects of the system that can be monitored:
* - Mach kernel exception
* - Fatal signal
Expand Down Expand Up @@ -147,6 +149,8 @@ KSCrashMonitorType
#endif
;

// clang-format on

#ifdef __cplusplus
}
#endif
Expand Down
53 changes: 27 additions & 26 deletions Sources/KSCrashRecordingCore/KSObjC.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,39 +115,40 @@ static int taggedDateDescription(const void* object, char* buffer, int bufferLen
static int taggedNumberDescription(const void* object, char* buffer, int bufferLength);
static int taggedStringDescription(const void* object, char* buffer, int bufferLength);


// clang-format off
static ClassData g_classData[] =
{
{"__NSCFString", KSObjCClassTypeString, ClassSubtypeNone, true, stringIsValid, stringDescription},
{"NSCFString", KSObjCClassTypeString, ClassSubtypeNone, true, stringIsValid, stringDescription},
{"__NSCFConstantString", KSObjCClassTypeString, ClassSubtypeNone, true, stringIsValid, stringDescription},
{"NSCFConstantString", KSObjCClassTypeString, ClassSubtypeNone, true, stringIsValid, stringDescription},
{"__NSArray0", KSObjCClassTypeArray, ClassSubtypeNSArrayImmutable, false, arrayIsValid, arrayDescription},
{"__NSArrayI", KSObjCClassTypeArray, ClassSubtypeNSArrayImmutable, false, arrayIsValid, arrayDescription},
{"__NSArrayM", KSObjCClassTypeArray, ClassSubtypeNSArrayMutable, true, arrayIsValid, arrayDescription},
{"__NSCFArray", KSObjCClassTypeArray, ClassSubtypeCFArray, false, arrayIsValid, arrayDescription},
{"__NSSingleObjectArrayI", KSObjCClassTypeArray, ClassSubtypeNSArrayImmutable, false, arrayIsValid, arrayDescription},
{"NSCFArray", KSObjCClassTypeArray, ClassSubtypeCFArray, false, arrayIsValid, arrayDescription},
{"__NSDate", KSObjCClassTypeDate, ClassSubtypeNone, false, dateIsValid, dateDescription},
{"NSDate", KSObjCClassTypeDate, ClassSubtypeNone, false, dateIsValid, dateDescription},
{"__NSCFNumber", KSObjCClassTypeNumber, ClassSubtypeNone, false, numberIsValid, numberDescription},
{"NSCFNumber", KSObjCClassTypeNumber, ClassSubtypeNone, false, numberIsValid, numberDescription},
{"NSNumber", KSObjCClassTypeNumber, ClassSubtypeNone, false, numberIsValid, numberDescription},
{"NSURL", KSObjCClassTypeURL, ClassSubtypeNone, false, urlIsValid, urlDescription},
{NULL, KSObjCClassTypeUnknown, ClassSubtypeNone, false, objectIsValid, objectDescription},
{"__NSCFString", KSObjCClassTypeString, ClassSubtypeNone, true, stringIsValid, stringDescription},
{"NSCFString", KSObjCClassTypeString, ClassSubtypeNone, true, stringIsValid, stringDescription},
{"__NSCFConstantString", KSObjCClassTypeString, ClassSubtypeNone, true, stringIsValid, stringDescription},
{"NSCFConstantString", KSObjCClassTypeString, ClassSubtypeNone, true, stringIsValid, stringDescription},
{"__NSArray0", KSObjCClassTypeArray, ClassSubtypeNSArrayImmutable, false, arrayIsValid, arrayDescription},
{"__NSArrayI", KSObjCClassTypeArray, ClassSubtypeNSArrayImmutable, false, arrayIsValid, arrayDescription},
{"__NSArrayM", KSObjCClassTypeArray, ClassSubtypeNSArrayMutable, true, arrayIsValid, arrayDescription},
{"__NSCFArray", KSObjCClassTypeArray, ClassSubtypeCFArray, false, arrayIsValid, arrayDescription},
{"__NSSingleObjectArrayI", KSObjCClassTypeArray, ClassSubtypeNSArrayImmutable, false, arrayIsValid, arrayDescription},
{"NSCFArray", KSObjCClassTypeArray, ClassSubtypeCFArray, false, arrayIsValid, arrayDescription},
{"__NSDate", KSObjCClassTypeDate, ClassSubtypeNone, false, dateIsValid, dateDescription},
{"NSDate", KSObjCClassTypeDate, ClassSubtypeNone, false, dateIsValid, dateDescription},
{"__NSCFNumber", KSObjCClassTypeNumber, ClassSubtypeNone, false, numberIsValid, numberDescription},
{"NSCFNumber", KSObjCClassTypeNumber, ClassSubtypeNone, false, numberIsValid, numberDescription},
{"NSNumber", KSObjCClassTypeNumber, ClassSubtypeNone, false, numberIsValid, numberDescription},
{"NSURL", KSObjCClassTypeURL, ClassSubtypeNone, false, urlIsValid, urlDescription},
{NULL, KSObjCClassTypeUnknown, ClassSubtypeNone, false, objectIsValid, objectDescription},
};

static ClassData g_taggedClassData[] =
{
{"NSAtom", KSObjCClassTypeUnknown, ClassSubtypeNone, false, taggedObjectIsValid, taggedObjectDescription},
{NULL, KSObjCClassTypeUnknown, ClassSubtypeNone, false, taggedObjectIsValid, taggedObjectDescription},
{"NSString", KSObjCClassTypeString, ClassSubtypeNone, false, taggedStringIsValid, taggedStringDescription},
{"NSNumber", KSObjCClassTypeNumber, ClassSubtypeNone, false, taggedNumberIsValid, taggedNumberDescription},
{"NSIndexPath", KSObjCClassTypeUnknown, ClassSubtypeNone, false, taggedObjectIsValid, taggedObjectDescription},
{"NSManagedObjectID", KSObjCClassTypeUnknown, ClassSubtypeNone, false, taggedObjectIsValid, taggedObjectDescription},
{"NSDate", KSObjCClassTypeDate, ClassSubtypeNone, false, taggedDateIsValid, taggedDateDescription},
{NULL, KSObjCClassTypeUnknown, ClassSubtypeNone, false, taggedObjectIsValid, taggedObjectDescription},
{"NSAtom", KSObjCClassTypeUnknown, ClassSubtypeNone, false, taggedObjectIsValid, taggedObjectDescription},
{NULL, KSObjCClassTypeUnknown, ClassSubtypeNone, false, taggedObjectIsValid, taggedObjectDescription},
{"NSString", KSObjCClassTypeString, ClassSubtypeNone, false, taggedStringIsValid, taggedStringDescription},
{"NSNumber", KSObjCClassTypeNumber, ClassSubtypeNone, false, taggedNumberIsValid, taggedNumberDescription},
{"NSIndexPath", KSObjCClassTypeUnknown, ClassSubtypeNone, false, taggedObjectIsValid, taggedObjectDescription},
{"NSManagedObjectID", KSObjCClassTypeUnknown, ClassSubtypeNone, false, taggedObjectIsValid, taggedObjectDescription},
{"NSDate", KSObjCClassTypeDate, ClassSubtypeNone, false, taggedDateIsValid, taggedDateDescription},
{NULL, KSObjCClassTypeUnknown, ClassSubtypeNone, false, taggedObjectIsValid, taggedObjectDescription},
};
// clang-format on
static int g_taggedClassDataCount = sizeof(g_taggedClassData) / sizeof(*g_taggedClassData);

static const char* g_blockBaseClassName = "NSBlock";
Expand Down
2 changes: 2 additions & 0 deletions Sources/KSCrashRecordingCore/KSString.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#define likely_if(x) if(__builtin_expect(x,1))
#define unlikely_if(x) if(__builtin_expect(x,0))

// clang-format off
static const int g_printableControlChars[0x20] =
{
// Only tab, CR, and LF are considered printable
Expand All @@ -58,6 +59,7 @@ static const int g_continuationByteCount[0x40] =
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 0, 0,
};
// clang-format on

bool ksstring_isNullTerminatedUTF8String(const void* memory,
int minLength,
Expand Down

0 comments on commit a3d61b5

Please sign in to comment.