Skip to content

Refactor duplicated code across kernel subsystems - #140

Merged
iamantoinev merged 1 commit into
mainfrom
copilot/refactor-duplicated-code
Oct 29, 2025
Merged

iamantoinev merged 1 commit into
mainfrom
copilot/refactor-duplicated-code

Conversation

Copilot AI commented Oct 29, 2025 •

Copy link
Copy Markdown
Contributor

Eliminates ~45 lines of duplicated logic across command handling, memory management, and I/O subsystems.

Changes

Command dispatch - Replaced if-else strcmp chains with table-driven lookup:

// Before: 6 repetitive if-else blocks
if (strcmp(command, "ping") == 0) cmd_ping();
else if (strcmp(command, "crash") == 0) cmd_crash();
// ...

// After: Extensible command table
static const command_t commands[] = {
    {"ping", cmd_ping}, {"crash", cmd_crash}, ...
};

Printf base conversion - putnbr_base() now delegates to putnbr_base_unsigned() after sign handling, eliminating 10 lines of recursive duplication

Page table access - Extracted get_page_table() to remove repeated recursive_active() checks across map_page(), unmap_page(), and get_phys_addr()

PIC IRQ masking - Unified pic_enable_irq() and pic_disable_irq() via pic_set_irq_mask(bool enable) helper

Bug fix - Corrected get_level() in printk.c: c >= '0' || c <= '7' → c >= '0' && c <= '7'

Stats

  • 7 files changed: +52, -58 lines
  • Duplication eliminated: ~45 lines
  • Command system now table-driven for easier extensibility
Original prompt

Find and refactor duplicated code


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Refactor duplicated code for improved maintainability Refactor duplicated code across kernel subsystems Oct 29, 2025
Copilot AI requested a review from iamantoinev October 29, 2025 09:24
@iamantoinev
iamantoinev marked this pull request as ready for review October 29, 2025 09:34
@iamantoinev
iamantoinev requested a review from Neffi42 as a code owner October 29, 2025 09:34
Copilot AI review requested due to automatic review settings October 29, 2025 09:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR contains several refactoring improvements and bug fixes across the kernel codebase, focusing on code deduplication, fixing logical errors, and improving maintainability.

  • Fixed a critical logical operator bug in get_level() that prevented proper level parsing
  • Refactored duplicated code in PIC IRQ mask operations and paging table lookups into helper functions
  • Improved command handling architecture by replacing if-else chains with a table-driven approach

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
kernel/src/tty/vprintf.c Simplified putnbr_base() to delegate to putnbr_base_unsigned() after handling negative sign
kernel/src/tty/printk.c Fixed logical OR to AND in get_level() condition
kernel/src/pic.c Refactored pic_enable_irq() and pic_disable_irq() to share common logic via pic_set_irq_mask()
kernel/src/memory/paging.c Extracted duplicated page table lookup logic into get_page_table() helper function
kernel/src/command/handle.c Refactored command dispatch from if-else chain to table-driven approach
kernel/src/command/halt.c Extracted halt command implementation into dedicated file
kernel/include/command.h Added cmd_halt() function declaration

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread kernel/src/tty/vprintf.c
Refactor duplicated code in command handler and printf functions

Co-authored-by: antoineverin <24255551+antoineverin@users.noreply.github.com>

Refactor page table access duplication in paging.c

Co-authored-by: antoineverin <24255551+antoineverin@users.noreply.github.com>

Refactor PIC IRQ mask functions to eliminate duplication

Co-authored-by: antoineverin <24255551+antoineverin@users.noreply.github.com>

Final review - all refactoring complete

Co-authored-by: antoineverin <24255551+antoineverin@users.noreply.github.com>

Delete _codeql_detected_source_root

Signed-off-by: Antoine Verin <24255551+antoineverin@users.noreply.github.com>

apply ql sugestion
@iamantoinev
iamantoinev force-pushed the copilot/refactor-duplicated-code branch from 6b37d0b to b574970 Compare October 29, 2025 13:37
@sonarqubecloud

Copy link
Copy Markdown

@iamantoinev
iamantoinev merged commit 390bc6e into main Oct 29, 2025
4 checks passed
@iamantoinev
iamantoinev deleted the copilot/refactor-duplicated-code branch October 29, 2025 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants