Skip to content
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
3 changes: 3 additions & 0 deletions .clangd
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ Diagnostics:
FastCheckFilter: Loose
Remove:
- readability-inconsistent-declaration-parameter-name # Unable to be suppressed by NOLINT
CompileFlags:
Add:
- "-Wextra-semi"
75 changes: 49 additions & 26 deletions .scripts/clang-tidy-check
Original file line number Diff line number Diff line change
Expand Up @@ -118,46 +118,69 @@ def parse_args() -> argparse.Namespace:
return parser.parse_args()


def parse_clangd(target: LintTarget) -> Optional[dict]:
if target.clangd is None:
return None
if not target.clangd.is_file():
return None
def collect_clangd_extra_args(project_dir: Path, root: Path) -> List[str]:
resolved_root = root.resolve()
try:
data = yaml.safe_load(target.clangd.read_text())
except (yaml.YAMLError, OSError):
return None
return data if isinstance(data, dict) else None
rel_parts = project_dir.resolve().relative_to(resolved_root).parts
except ValueError:
return []

candidate_dirs = [resolved_root]
current = resolved_root
for part in rel_parts:
current = current / part
candidate_dirs.append(current)

extra_args: List[str] = []
seen_flags: Set[str] = set()
for candidate_dir in candidate_dirs:
clangd_path = candidate_dir / ".clangd"
if not clangd_path.is_file():
continue
try:
data = yaml.safe_load(clangd_path.read_text())
except (yaml.YAMLError, OSError):
continue
if not isinstance(data, dict):
continue
compile_flags = data.get("CompileFlags")
if not isinstance(compile_flags, dict):
continue
add_flags = compile_flags.get("Add")
if not isinstance(add_flags, list):
continue
for flag in add_flags:
flag_str = str(flag)
if flag_str in seen_flags:
continue
seen_flags.add(flag_str)
extra_args.append(flag_str)

def clangd_extra_args(target: LintTarget) -> List[str]:
data = parse_clangd(target)
if data is None:
return []
compile_flags = data.get("CompileFlags")
if not isinstance(compile_flags, dict):
return []
add_flags = compile_flags.get("Add")
if isinstance(add_flags, list):
return [str(flag) for flag in add_flags]
return []
return extra_args


def clang_tidy_args(target: LintTarget, fix: bool) -> List[str]:
args = ["-p", str(target.compile_database.parent)]
def clang_tidy_args(
compile_database: Path, project_dir: Path, root: Path, fix: bool
) -> List[str]:
args = ["-p", str(compile_database.parent)]
if fix:
args.append("--fix")
else:
args.append("--warnings-as-errors=*")
for flag in clangd_extra_args(target):
for flag in collect_clangd_extra_args(project_dir, root):
args.append(f"--extra-arg={flag}")
return args


def run_clang_tidy(
target: LintTarget, files: Sequence[Path], fix: bool
target: LintTarget, files: Sequence[Path], fix: bool, root: Path
) -> int:
args = clang_tidy_args(target, fix=fix)
args = clang_tidy_args(
compile_database=target.compile_database,
project_dir=target.project_dir,
root=root,
fix=fix,
)
result = subprocess.run(["clang-tidy", *args, *[str(p) for p in files]])
return result.returncode

Expand Down Expand Up @@ -234,7 +257,7 @@ def main() -> int:
f"clang-tidy: {name}: checking {len(files)} files...",
flush=True,
)
rc = run_clang_tidy(target, files, fix=args.fix)
rc = run_clang_tidy(target, files, fix=args.fix, root=root)
if rc != 0:
overall_rc = rc

Expand Down
5 changes: 2 additions & 3 deletions .scripts/lint-targets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ exclude:
- build

host:
compile_database: host/build/compile_commands.json
cmake: host/CMakeLists.txt
folders:
- core/src
- core/include
- host/src
- host/include

rmcs_board:
compile_database: firmware/rmcs_board/build/compile_commands.json
clangd: firmware/rmcs_board/.clangd
cmake: firmware/rmcs_board/CMakeLists.txt
folders:
- core/src
- core/include
Expand Down
16 changes: 8 additions & 8 deletions .scripts/lint_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import yaml
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, List, Optional, Sequence, Set, Tuple
from typing import Dict, List, Sequence, Set, Tuple


SOURCE_EXTENSIONS = {".c", ".cpp", ".h", ".hpp"}
Expand All @@ -12,10 +12,13 @@
@dataclass(frozen=True)
class LintTarget:
name: str
compile_database: Path
clangd: Optional[Path]
project_dir: Path
folders: List[Path]

@property
def compile_database(self) -> Path:
return self.project_dir / "build" / "compile_commands.json"


def repo_root() -> Path:
return Path(__file__).resolve().parent.parent
Expand Down Expand Up @@ -57,14 +60,11 @@ def load_targets(root: Path) -> Tuple[List[str], Dict[str, LintTarget]]:
exclude_dirs: List[str] = data.pop("exclude", [])
targets: Dict[str, LintTarget] = {}
for name, section in data.items():
compile_database = root / section["compile_database"]
clangd_rel = section.get("clangd")
clangd = root / clangd_rel if clangd_rel else None
project_dir = root / Path(section["cmake"]).parent
folders = [root / f for f in section["folders"]]
targets[name] = LintTarget(
name=name,
compile_database=compile_database,
clangd=clangd,
project_dir=project_dir,
folders=folders,
)
return exclude_dirs, targets
2 changes: 1 addition & 1 deletion core/src/protocol/deserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Deserializer : private coroutine::InlineLifoContext<1024> {
public:
constexpr explicit Deserializer(DeserializeCallback& callback)
: callback_(callback)
, main_task_(process_stream()) {};
, main_task_(process_stream()) {}

~Deserializer() { finish_transfer(); }
Deserializer(const Deserializer&) = delete;
Expand Down
2 changes: 1 addition & 1 deletion firmware/rmcs_board/src/uart/rx_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class RxBuffer {
RxBuffer(UART_Type* uart_base, uint32_t dmamux_src)
: uart_base_(uart_base) {
init_dma(dmamux_src);
};
}

void dma_tc_half_tc_callback() { try_dequeue(false); }

Expand Down
10 changes: 5 additions & 5 deletions firmware/rmcs_board/src/usb/vendor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class Vendor
case data::DataId::kCan3: can::can3->handle_downlink(data); break;
default: core::utility::assert_failed_always();
}
};
}

void uart_deserialized_callback(
core::protocol::FieldId id, const data::UartDataView& data) override {
Expand All @@ -100,17 +100,17 @@ class Vendor
// TODO: Handle other UART events
default: core::utility::assert_failed_always();
}
};
}

void accelerometer_deserialized_callback(const data::AccelerometerDataView& data) override {
(void)data;
};
}

void gyroscope_deserialized_callback(const data::GyroscopeDataView& data) override {
(void)data;
};
}

void error_callback() override { core::utility::assert_failed_always(); };
void error_callback() override { core::utility::assert_failed_always(); }

static bool device_ready() { return tud_ready() && tud_vendor_n_write_available(0); }

Expand Down
2 changes: 1 addition & 1 deletion firmware/rmcs_board/src/utility/lazy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Lazy {
: init_status_(InitStatus::kUninitialized)
, construction_arguments_{std::move(args)...} {}

constexpr ~Lazy() {}; // No need to deconstruct
constexpr ~Lazy() {} // No need to deconstruct
Lazy(const Lazy&) = delete;
Lazy& operator=(const Lazy&) = delete;
Lazy(Lazy&&) = delete;
Expand Down
6 changes: 3 additions & 3 deletions host/src/protocol/stream_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class StreamBuffer : public core::protocol::SerializeBuffer {
* Must outlive this StreamBuffer instance.
*/
explicit StreamBuffer(transport::Transport& transport) noexcept
: transport_(transport) {};
: transport_(transport) {}

StreamBuffer(StreamBuffer&& other) noexcept
: transport_(other.transport_)
Expand All @@ -76,7 +76,7 @@ class StreamBuffer : public core::protocol::SerializeBuffer {
, end_(other.end_) {
other.current_ = nullptr;
other.end_ = nullptr;
};
}
StreamBuffer& operator=(StreamBuffer&&) = delete;
StreamBuffer(const StreamBuffer&) = delete;
StreamBuffer& operator=(const StreamBuffer&) = delete;
Expand All @@ -90,7 +90,7 @@ class StreamBuffer : public core::protocol::SerializeBuffer {
~StreamBuffer() override {
if (buffer_)
finalize_buffer();
};
}

/**
* @brief Allocates a contiguous memory region of exactly the specified size.
Expand Down
4 changes: 2 additions & 2 deletions host/src/transport/usb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Usb : public Transport {
return nullptr;

return std::unique_ptr<TransportBuffer>{transfer};
};
}

void transmit(std::unique_ptr<TransportBuffer> buffer, size_t size) override {
core::utility::assert_debug(static_cast<bool>(buffer));
Expand Down Expand Up @@ -119,7 +119,7 @@ class Usb : public Transport {

receive_callback_ = std::move(callback);
init_receive_transfers();
};
}

private:
class TransferWrapper : public TransportBuffer {
Expand Down
2 changes: 1 addition & 1 deletion host/src/utility/final_action.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ requires requires(Functor& action) {
}
}

void disable() { enabled_ = false; };
void disable() { enabled_ = false; }

private:
bool enabled_{true};
Expand Down