Skip to content

Commit

Permalink
refactor: add linter for uarch sources
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Oct 16, 2024
1 parent 5ea8697 commit 8a19b68
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
16 changes: 13 additions & 3 deletions uarch/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ COMPUTE_UARCH_C_SOURCES=\
UARCH_OBJS = $(patsubst %.c,%.uarch_c.o,$(patsubst %.cpp,%.uarch_cpp.o,$(UARCH_SOURCES)))
EMULATOR_OBJS = $(patsubst %.c,%.emulator_c.o,$(patsubst %.cpp,%.emulator_cpp.o,$(EMULATOR_SOURCES)))

LINTER_SOURCES=$(filter-out $(LINTER_IGNORE_SOURCES),$(strip $(wildcard uarch-*.cpp)))
CLANG_TIDY=clang-tidy
CLANG_TIDY_TARGETS=$(patsubst %.cpp,%.clang-tidy,$(patsubst %.c,%.clang-tidy,$(LINTER_SOURCES)))

.PHONY: all clean

all: uarch-ram.bin uarch-pristine-ram.c uarch-pristine-hash.c
Expand All @@ -104,23 +108,29 @@ uarch-ram-entry.o: uarch-ram-entry.S
$(CC) $(CFLAGS) -c -o $@ $(<F)

%.emulator_cpp.o: $(EMULATOR_SRC_DIR)/%.cpp
$(CXX) $(CXXFLAGS) $(CFLAGS) -c -o $@ $(EMULATOR_SRC_DIR)/$(<F)
$(CXX) $(CFLAGS) $(CXXFLAGS) -c -o $@ $(EMULATOR_SRC_DIR)/$(<F)

%.uarch_c.o: %.c
$(CC) $(CFLAGS) -c -o $@ $(<F)

%.uarch_cpp.o: %.cpp
$(CXX) $(CXXFLAGS) $(CFLAGS) -c -o $@ $(<F)
$(CXX) $(CFLAGS) $(CXXFLAGS) -c -o $@ $(<F)

%.ld: %.ld.in
$(CC) -o $(@F).tmp -x c $(CFLAGS) -E $(^F)
grep -v '^#' $@.tmp > $@

%.clang-tidy: %.cpp
@$(CLANG_TIDY) --header-filter='$(CLANG_TIDY_HEADER_FILTER)' $< -- -target riscv64-linux-gnu $(CFLAGS) $(CXXFLAGS) -Wno-unused-command-line-argument 2>/dev/null
@touch $@

lint: $(CLANG_TIDY_TARGETS)

clean-executables:
@rm -f compute-uarch-pristine-hash

clean-auto-generated:
@rm -f uarch-pristine-hash.c uarch-pristine-ram.c

clean: clean-executables clean-auto-generated
@rm -f *.ld *.elf *.bin *.tmp link.ld *.o
@rm -f *.ld *.elf *.bin *.tmp link.ld *.o *.clang-tidy
3 changes: 3 additions & 0 deletions uarch/uarch-printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
//
///////////////////////////////////////////////////////////////////////////////

#define PRINTF_DISABLE_SUPPORT_FLOAT
#define PRINTF_DISABLE_SUPPORT_EXPONENTIAL

#include <stdbool.h>
#include <stdint.h>

Expand Down
8 changes: 5 additions & 3 deletions uarch/uarch-run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

#include "uarch-runtime.h" // must be included first, because of assert

#include "compiler-defines.h"
#include "interpret.h"
#include "shadow-uarch-state.h"
#include "uarch-constants.h"
#include "uarch-machine-state-access.h"
#include <cinttypes>

#include <cstdint>

using namespace cartesi;

Expand All @@ -33,7 +35,7 @@ static void set_uarch_halt_flag() {
}

// Let the state accessor be on static memory storage to speed up uarch initialization
static uarch_machine_state_access a;
static uarch_machine_state_access a; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)

namespace cartesi {

Expand Down
16 changes: 15 additions & 1 deletion uarch/uarch-runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,39 @@
//

#include "uarch-runtime.h"
#include "compiler-defines.h"
#include "uarch-constants.h"
#include <algorithm>

#include <cstddef>
#include <cstdint>

using namespace cartesi;

// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
extern "C" void __cxa_pure_virtual() {
abort();
}

// NOLINTNEXTLINE(cert-dcl54-cpp,misc-new-delete-overloads)
void operator delete(void * /*ptr*/) {}

// NOLINTNEXTLINE(cert-dcl54-cpp,misc-new-delete-overloads)
void operator delete(void * /*ptr*/, size_t /*size*/) {}

// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
extern "C" void __assert_func(const char * /*file*/, int /*line*/, const char * /*func*/, const char * /*e*/) {}

// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
extern "C" void __assert_fail(const char * /*__assertion*/, const char * /*__file*/, unsigned int /*__line*/,
const char * /*__function*/) {}

extern "C" void *memmove(void *dest, const void *src, size_t n) {
if (!n || src == dest) {
return dest;
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast,cppcoreguidelines-pro-type-reinterpret-cast)
const auto *s = const_cast<char *>(reinterpret_cast<const char *>(src));
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto *d = reinterpret_cast<char *>(dest);
if (d < s) {
for (; n; n--) {
Expand All @@ -51,7 +62,9 @@ extern "C" void *memmove(void *dest, const void *src, size_t n) {
}

extern "C" void *memcpy(void *dest, const void *src, size_t n) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
const auto *s = reinterpret_cast<const unsigned char *>(src);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto *d = reinterpret_cast<unsigned char *>(dest);
while (n--) {
*d++ = *s++;
Expand All @@ -60,6 +73,7 @@ extern "C" void *memcpy(void *dest, const void *src, size_t n) {
}

extern "C" void *memset(void *ptr, int value, size_t num) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
volatile unsigned char *p = reinterpret_cast<unsigned char *>(ptr);
while (num--) {
*p++ = value;
Expand Down

0 comments on commit 8a19b68

Please sign in to comment.