Skip to content

Commit

Permalink
Merge pull request #68 from Team766/Add-Linter
Browse files Browse the repository at this point in the history
create yml workflow to ensure code is linted and lint it if it isn't. CLOSES PLANE 209
  • Loading branch information
cwosw authored Feb 8, 2025
2 parents 436f81a + 252b4a3 commit 310a3c5
Show file tree
Hide file tree
Showing 21 changed files with 200 additions and 140 deletions.
68 changes: 68 additions & 0 deletions .github/workflows/cpp-linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: C++ Lint and Auto-Fix

on:
push:
branches:
- main
pull_request:
branches:
- main

permissions:
contents: write # Required for pushing auto-fix commits

jobs:
lint:
name: Lint C++ Code
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }} # Checkout the correct branch
fetch-depth: 0 # Ensure full commit history for pushing changes

- name: Install clang-format and cppcheck
run: sudo apt-get install -y clang-format cppcheck

- name: Run clang-format and fix issues
run: |
FILES=$(find . -type f \( -name "*.cpp" -o -name "*.hpp" -o -name "*.h" \) | tr '\n' ' ')
if [ -z "$FILES" ]; then
echo "No C++ source files found. Skipping clang-format."
exit 0
fi
clang-format -i $FILES
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
# Ensure we are on the correct branch
git checkout ${{ github.head_ref }}
git add .
if ! git diff --cached --quiet; then
git commit -m "Auto-format C++ code using clang-format"
git push origin ${{ github.head_ref }}
fi
- name: Run cppcheck (C++ Mode)
run: |
FILES=$(find . -type f \( -name "*.cpp" -o -name "*.hpp" -o -name "*.h" \) | tr '\n' ' ')
if [ -z "$FILES" ]; then
echo "No C++ source files found. Skipping cppcheck."
exit 0
fi
echo "Running cppcheck on: $FILES"
cppcheck --enable=all --error-exitcode=1 --inline-suppr --force --quiet --language=c++ \
--suppress=missingInclude \
--suppress=missingIncludeSystem \
--suppress=unusedStructMember \
--suppress=noExplicitConstructor \
--suppress=passedByValue \
--suppress=useInitializationList \
--suppress=cstyleCast \
--suppress=unusedFunction \
--suppress=unmatchedSuppression $FILES
4 changes: 2 additions & 2 deletions src/BooleanValueSender.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
#include "networktables/NetworkTableInstance.h"

class BooleanValueSender {
private:
private:
nt::NetworkTableInstance inst_;
nt::BooleanPublisher publisher_;

public:
public:
// Constructor declaration
BooleanValueSender(std::string key);

Expand Down
4 changes: 2 additions & 2 deletions src/DoubleArraySender.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
#include "networktables/NetworkTableInstance.h"

class DoubleArraySender {
private:
private:
nt::NetworkTableInstance inst_;
nt::DoubleArrayPublisher publisher_;

public:
public:
// Constructor declaration
DoubleArraySender(std::string key);

Expand Down
4 changes: 2 additions & 2 deletions src/DoubleValueSender.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
#include "networktables/NetworkTableInstance.h"

class DoubleValueSender {
private:
private:
nt::NetworkTableInstance inst_;
nt::DoublePublisher publisher_;

public:
public:
// Constructor declaration
DoubleValueSender(std::string key);

Expand Down
4 changes: 2 additions & 2 deletions src/IntegerArraySender.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
#include "networktables/NetworkTableInstance.h"

class IntegerArraySender {
private:
private:
nt::NetworkTableInstance inst_;
nt::IntegerArrayPublisher publisher_;

public:
public:
// Constructor declaration
IntegerArraySender(std::string key);

Expand Down
4 changes: 2 additions & 2 deletions src/IntegerValueSender.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
#include "networktables/NetworkTableInstance.h"

class IntegerValueSender {
private:
private:
nt::NetworkTableInstance inst_;
nt::IntegerPublisher publisher_;

public:
public:
// Constructor declaration
IntegerValueSender(std::string key);

Expand Down
4 changes: 2 additions & 2 deletions src/NetworkTablesConfig.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef NETWORK_TABLES_CONFIG_H_
#define NETWORK_TABLES_CONFIG_H_

inline const char* TABLE_ADDRESS = "10.7.66.2";
inline const char* TABLE_NAME = "/SmartDashboard";
inline const char *TABLE_ADDRESS = "10.7.66.2";
inline const char *TABLE_NAME = "/SmartDashboard";

#endif
10 changes: 4 additions & 6 deletions src/NetworkTablesUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
#include "NetworkTablesConfig.h"

NetworkTablesUtil::NetworkTablesUtil() {
// inst_ = nt::NetworkTableInstance::GetDefault();
// inst_.SetServer(TABLE_ADDRESS);
// inst_.StartClient4(TABLE_ADDRESS);
// inst_ = nt::NetworkTableInstance::GetDefault();
// inst_.SetServer(TABLE_ADDRESS);
// inst_.StartClient4(TABLE_ADDRESS);
}

double NetworkTablesUtil::getTime() {
return wpi::GetSystemTime();
}
double NetworkTablesUtil::getTime() { return wpi::GetSystemTime(); }
11 changes: 5 additions & 6 deletions src/NetworkTablesUtil.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#ifndef NETWORKTABLESUTIL_H
#define NETWORKTABLESUTIL_H


//#include "networktables/NetworkTable.h"
//#include "networktables/NetworkTableInstance.h"
// #include "networktables/NetworkTable.h"
// #include "networktables/NetworkTableInstance.h"
#include "wpi/timestamp.h"

class NetworkTablesUtil {
//private:
//nt::NetworkTableInstance inst_;
// private:
// nt::NetworkTableInstance inst_;

public:
public:
// Constructor declaration
NetworkTablesUtil();

Expand Down
19 changes: 9 additions & 10 deletions src/apriltag_gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace frc971::apriltag {

// Class to find the blob index of a point in a point vector.
class BlobExtentsIndexFinder {
public:
public:
BlobExtentsIndexFinder(const MinMaxExtents *extents_device,
size_t num_extents)
: extents_device_(extents_device), num_extents_(num_extents) {}
Expand Down Expand Up @@ -45,7 +45,7 @@ class BlobExtentsIndexFinder {
return extents_device_[index];
}

private:
private:
const MinMaxExtents *extents_device_;
size_t num_extents_;

Expand Down Expand Up @@ -75,7 +75,7 @@ struct DistCoeffs {

// GPU based april tag detector.
class GpuDetector {
public:
public:
// The number of blobs we will consider when counting april tags.
static constexpr size_t kMaxBlobs = IndexPoint::kMaxBlobs;

Expand Down Expand Up @@ -138,8 +138,8 @@ class GpuDetector {
return extents_device_.Copy(NumQuads());
}

std::vector<cub::KeyValuePair<long, MinMaxExtents>> CopySelectedExtents()
const {
std::vector<cub::KeyValuePair<long, MinMaxExtents>>
CopySelectedExtents() const {
return selected_extents_device_.Copy(NumQuads());
}

Expand Down Expand Up @@ -199,7 +199,7 @@ class GpuDetector {
static bool UnDistort(double *u, double *v, const CameraMatrix *camera_matrix,
const DistCoeffs *distortion_coefficients);

private:
private:
void UpdateFitQuads();

void AdjustPixelCenters();
Expand All @@ -209,8 +209,7 @@ class GpuDetector {
static void QuadDecodeTask(void *_u);

// Creates a GPU image wrapped around the provided memory.
template <typename T>
GpuImage<T> ToGpuImage(GpuMemory<T> &memory) {
template <typename T> GpuImage<T> ToGpuImage(GpuMemory<T> &memory) {
if (memory.size() == width_ * height_) {
return GpuImage<T>{
.data = memory.get(),
Expand Down Expand Up @@ -359,6 +358,6 @@ class GpuDetector {
zarray_t *detections_ = nullptr;
};

} // namespace frc971::apriltag
} // namespace frc971::apriltag

#endif // FRC971_ORIN_APRILTAG_H_
#endif // FRC971_ORIN_APRILTAG_H_
4 changes: 2 additions & 2 deletions src/cameraexception.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#define CAMERAEXCEPTION_H_

class CameraException : public std::exception {
public:
const char* what() const noexcept override {
public:
const char *what() const noexcept override {
return "Error: No camera detected.";
}
};
Expand Down
32 changes: 15 additions & 17 deletions src/cuda_frc971.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
// CHECKs that a cuda method returned success.
// TODO(austin): This will not handle if and else statements quite right, fix if
// we care.
#define CHECK_CUDA(condition) \
if (auto c = condition) \
LOG(FATAL) << "Check failed: " #condition " (" << cudaGetErrorString(c) \
#define CHECK_CUDA(condition) \
if (auto c = condition) \
LOG(FATAL) << "Check failed: " #condition " (" << cudaGetErrorString(c) \
<< ") "

namespace frc971::apriltag {

// Class to manage the lifetime of a Cuda stream. This is used to provide
// relative ordering between kernels on the same stream.
class CudaStream {
public:
public:
CudaStream() { CHECK_CUDA(cudaStreamCreate(&stream_)); }

CudaStream(const CudaStream &) = delete;
Expand All @@ -32,14 +32,14 @@ class CudaStream {
// Returns the stream.
cudaStream_t get() { return stream_; }

private:
private:
cudaStream_t stream_;
};

// Class to manage the lifetime of a Cuda Event. Cuda events are used for
// timing events on a stream.
class CudaEvent {
public:
public:
CudaEvent() { CHECK_CUDA(cudaEventCreate(&event_)); }

CudaEvent(const CudaEvent &) = delete;
Expand All @@ -64,15 +64,14 @@ class CudaEvent {
// Waits until the event has been triggered.
void Synchronize() { CHECK_CUDA(cudaEventSynchronize(event_)); }

private:
private:
cudaEvent_t event_;
};

// Class to manage the lifetime of page locked host memory for fast copies back
// to host memory.
template <typename T>
class HostMemory {
public:
template <typename T> class HostMemory {
public:
// Allocates a block of memory for holding up to size objects of type T.
HostMemory(size_t size) {
T *memory;
Expand Down Expand Up @@ -100,14 +99,13 @@ class HostMemory {
memcpy(other, span_.data(), sizeof(T) * size());
}

private:
private:
std::span<T> span_;
};

// Class to manage the lifetime of device memory.
template <typename T>
class GpuMemory {
public:
template <typename T> class GpuMemory {
public:
// Allocates a block of memory for holding up to size objects of type T in
// device memory.
GpuMemory(size_t size) : size_(size) {
Expand Down Expand Up @@ -190,7 +188,7 @@ class GpuMemory {
// it.
std::vector<T> Copy() const { return Copy(size_); }

private:
private:
T *memory_;
const size_t size_;
};
Expand All @@ -203,6 +201,6 @@ void CheckAndSynchronize(std::string_view message = "");
void MaybeCheckAndSynchronize();
void MaybeCheckAndSynchronize(std::string_view message);

} // namespace frc971::apriltag
} // namespace frc971::apriltag

#endif // FRC971_ORIN_CUDA_H_
#endif // FRC971_ORIN_CUDA_H_
5 changes: 2 additions & 3 deletions src/gpu_image.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#ifndef FRC971_ORIN_GPU_IMAGE_H_
#define FRC971_ORIN_GPU_IMAGE_H_

template <typename T>
struct GpuImage {
template <typename T> struct GpuImage {
typedef T type;
T *data;
size_t rows;
Expand All @@ -11,4 +10,4 @@ struct GpuImage {
size_t step;
};

#endif // FRC971_ORIN_GPU_IMAGE_H_
#endif // FRC971_ORIN_GPU_IMAGE_H_
5 changes: 2 additions & 3 deletions src/json_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main(void) {
std::ifstream f(
"/home/nvidia/code/OrinVisionSystem/cameracalibration/"
"calibrationmatrix.json");
std::ifstream f("/home/nvidia/code/OrinVisionSystem/cameracalibration/"
"calibrationmatrix.json");
json data = json::parse(f);
std::cout << "Matrix: " << std::endl;
for (int i = 0; i < 3; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion src/labeling_allegretti_2019_BKE.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ void LabelImage(const GpuImage<uint8_t> input, GpuImage<uint32_t> output,
GpuImage<uint32_t> union_markers_size_device,
cudaStream_t stream);

#endif // FRC971_ORIN_LABELING_ALLEGRETTI_2019_BKE_H_
#endif // FRC971_ORIN_LABELING_ALLEGRETTI_2019_BKE_H_
Loading

0 comments on commit 310a3c5

Please sign in to comment.