commit 5d3e2d4e20f1373ab8fec5e86206d35ec3b989ad Author: Matteo Settenvini Date: Wed Nov 29 21:54:14 2023 +0100 Initial skeleton diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..e69de29 diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..4fc556b --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,28 @@ +--- +Checks: 'clang-diagnostic-*,clang-analyzer-*' +WarningsAsErrors: '' +HeaderFilterRegex: '' +AnalyzeTemporaryDtors: false +FormatStyle: none +User: matteo +CheckOptions: + llvm-else-after-return.WarnOnConditionVariables: 'false' + modernize-loop-convert.MinConfidence: reasonable + modernize-replace-auto-ptr.IncludeStyle: llvm + cert-str34-c.DiagnoseSignedUnsignedCharComparisons: 'false' + google-readability-namespace-comments.ShortNamespaceLines: '10' + cert-err33-c.CheckedFunctions: '::aligned_alloc;::asctime_s;::at_quick_exit;::atexit;::bsearch;::bsearch_s;::btowc;::c16rtomb;::c32rtomb;::calloc;::clock;::cnd_broadcast;::cnd_init;::cnd_signal;::cnd_timedwait;::cnd_wait;::ctime_s;::fclose;::fflush;::fgetc;::fgetpos;::fgets;::fgetwc;::fopen;::fopen_s;::fprintf;::fprintf_s;::fputc;::fputs;::fputwc;::fputws;::fread;::freopen;::freopen_s;::fscanf;::fscanf_s;::fseek;::fsetpos;::ftell;::fwprintf;::fwprintf_s;::fwrite;::fwscanf;::fwscanf_s;::getc;::getchar;::getenv;::getenv_s;::gets_s;::getwc;::getwchar;::gmtime;::gmtime_s;::localtime;::localtime_s;::malloc;::mbrtoc16;::mbrtoc32;::mbsrtowcs;::mbsrtowcs_s;::mbstowcs;::mbstowcs_s;::memchr;::mktime;::mtx_init;::mtx_lock;::mtx_timedlock;::mtx_trylock;::mtx_unlock;::printf_s;::putc;::putwc;::raise;::realloc;::remove;::rename;::scanf;::scanf_s;::setlocale;::setvbuf;::signal;::snprintf;::snprintf_s;::sprintf;::sprintf_s;::sscanf;::sscanf_s;::strchr;::strerror_s;::strftime;::strpbrk;::strrchr;::strstr;::strtod;::strtof;::strtoimax;::strtok;::strtok_s;::strtol;::strtold;::strtoll;::strtoul;::strtoull;::strtoumax;::strxfrm;::swprintf;::swprintf_s;::swscanf;::swscanf_s;::thrd_create;::thrd_detach;::thrd_join;::thrd_sleep;::time;::timespec_get;::tmpfile;::tmpfile_s;::tmpnam;::tmpnam_s;::tss_create;::tss_get;::tss_set;::ungetc;::ungetwc;::vfprintf;::vfprintf_s;::vfscanf;::vfscanf_s;::vfwprintf;::vfwprintf_s;::vfwscanf;::vfwscanf_s;::vprintf_s;::vscanf;::vscanf_s;::vsnprintf;::vsnprintf_s;::vsprintf;::vsprintf_s;::vsscanf;::vsscanf_s;::vswprintf;::vswprintf_s;::vswscanf;::vswscanf_s;::vwprintf_s;::vwscanf;::vwscanf_s;::wcrtomb;::wcschr;::wcsftime;::wcspbrk;::wcsrchr;::wcsrtombs;::wcsrtombs_s;::wcsstr;::wcstod;::wcstof;::wcstoimax;::wcstok;::wcstok_s;::wcstol;::wcstold;::wcstoll;::wcstombs;::wcstombs_s;::wcstoul;::wcstoull;::wcstoumax;::wcsxfrm;::wctob;::wctrans;::wctype;::wmemchr;::wprintf_s;::wscanf;::wscanf_s;' + cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField: 'false' + cert-dcl16-c.NewSuffixes: 'L;LL;LU;LLU' + google-readability-braces-around-statements.ShortStatementLines: '1' + cppcoreguidelines-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic: 'true' + google-readability-namespace-comments.SpacesBeforeComments: '2' + modernize-loop-convert.MaxCopySize: '16' + modernize-pass-by-value.IncludeStyle: llvm + modernize-use-nullptr.NullMacros: 'NULL' + llvm-qualified-auto.AddConstToQualified: 'false' + modernize-loop-convert.NamingStyle: CamelCase + llvm-else-after-return.WarnOnUnfixable: 'false' + google-readability-function-size.StatementThreshold: '800' +... + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..59ca910 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/build +*.orig +*.rej +*~ diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..17a8058 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,16 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "cmake", + "label": "CMake: build", + "command": "build", + "targets": [ + "format" + ], + "group": "build", + "problemMatcher": [], + "detail": "CMake template build task" + } + ] +} diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..417bb79 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 3.22) + +project(format-demo) + +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +add_custom_command(OUTPUT main-formatted.cc + DEPENDS main-unformatted.cc .clang-format .clang-tidy + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/main-unformatted.cc main-formatted.cc + COMMAND clang-format-16 -i main-formatted.cc + COMMAND clang-tidy-16 --fix --fix-notes -fix-errors + -fix-errors -p ${CMAKE_CURRENT_BINARY_DIR} main-formatted.cc + VERBATIM) +add_custom_target(format + DEPENDS main-formatted.cc + COMMENT "Formatting and linting sources" + SOURCES main-unformatted.cc ${CMAKE_CURRENT_BINARY_DIR}/main-formatted.cc + .clang-format .clang-tidy + VERBATIM) diff --git a/main-unformatted.cc b/main-unformatted.cc new file mode 100644 index 0000000..4fee5c9 --- /dev/null +++ b/main-unformatted.cc @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include + +#include +#include +#include + +#define BIT_MASK 0xDEADBEAF + +#define MULTILINE_DEF(a,b) if ((a)>2) { \ +auto temp = (b)/2; \ +(b)+=10; \ +someFunctionCall((a),(b));\ +} + +namespace LevelOneNamespace { + +extern auto someFunctionCall(int16_t, float *) -> int; +namespace LevelTwoNamespace { + +template bool is_sorted(T(&array)[size]) { + return std::adjacent_find(array, array + size, std::greater()) == + array + size; +} + +std::vector returnVector( uint32_t* LongNameForParameter1, + double* LongNameForParameter2, + const float& LongNameForParameter3, + std::map& LongNameForParameter4) { + + //TODO: This is a long comment that allows you to understand how long comments will be trimmed. Here should be deep thought but it's just not right time for this + + for (auto& i: LongNameForParameter4) { + auto b = someFunctionCall(static_cast(*LongNameForParameter2),reinterpret_cast(LongNameForParameter2)); + i.second++; + } + + do { + auto a = 100; + if (a) + a--; + else + a++; + } while (false); + + return {}; +} + +} +} + +int main() { + std::srand(std::time(0)); + + int list[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + + +using LevelOneNamespace::LevelTwoNamespace::is_sorted; + do { + std::random_shuffle(list, list + 9); + } while (is_sorted(list)); + + int score = 0; + + do { + std::cout << "Current list: "; + std::copy(list, list + 9, std::ostream_iterator(std::cout, " ")); + + int rev; + while (true) { + std::cout << "\nDigits to reverse? "; + std::cin >> rev; + if (rev > 1 && rev < 10) + break; + std::cout << "Please enter a value between 2 and 9."; + } + + ++score; + std::reverse(list, list + rev); + } while (!is_sorted(list)); + + std::cout << "Congratulations, you sorted the list.\n" + << "You needed " << score << " reversals." << std::endl; + return 0; +}