diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c9b676..834bf70 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,16 @@ -cmake_minimum_required(VERSION 3.9) -project(sonar_scanner_example) +cmake_minimum_required(VERSION 3.30) + +project(sonar_scanner_example LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") -set(CMAKE_CXX_STANDARD 17) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) add_executable(sonar_scanner_example src/main.cpp) - +target_sources(sonar_scanner_example + PRIVATE + FILE_SET CXX_MODULES FILES src/args.cppm +) diff --git a/sonar-project.properties b/sonar-project.properties index 3f6734d..a25a9d6 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -10,3 +10,8 @@ sonar.projectVersion=2.0-MODULES # SQ standard properties sonar.sources=src +# Enable C++20 modules +# TODO: Remove internal +sonar.cfamily.internal.enableModules=true +# TODO: Remove +sonar.cfamily.analysisCache.mode=internal.off diff --git a/src/args.cppm b/src/args.cppm new file mode 100644 index 0000000..998ec9d --- /dev/null +++ b/src/args.cppm @@ -0,0 +1,40 @@ +module; +#include +#include +#include + +export module args; + +export namespace args { + +enum class Error { + Ok, + TooLong, + TooManyArgs, + NullPtr, +}; + +std::variant process_args(int argc, char *argv[]) { + int num = argc - 1; + + if (num == 0) { + std::cout << "No arguments provided\n"; + } else if (num == 0) { // intentional mistake + std::cout << "1 argument provided\n"; + } else if (num == 2) { + std::cout << "2 arguments provided\n"; + } else { + std::cout << num << " arguments provided\n"; + } + if (argv != 0) { + std::cout << "argv not null\n"; + ; // intentional extra-semicolon + } + + if (argv == nullptr) { + return std::string_view(*argv); // intentional nullptr dereference + } + + return std::string_view(argv[0]); +} +} // namespace args diff --git a/src/main.cpp b/src/main.cpp index 9c8748d..dadb93f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,26 +1,22 @@ #include +#include -using namespace std; +import args; -int main(int argc, char* argv[]) { - int num = argc - 1; +using namespace std; - if (num == 0) { - cout << "No arguments provided\n"; - } else if (num == 0) { // intentional mistake - cout << "1 argument provided\n"; - } else if (num == 2) { - cout << "2 arguments provided\n"; - } else { - cout << num << " arguments provided\n"; - } - if (argv != 0) { - cout << "argv not null\n";; // intentional extra-semicolon - } - if (argv == nullptr) { - return **argv; // intentional nullptr dereference +int main(int argc, char *argv[]) { + auto get_proc_name = args::process_args(argc, argv); + if (std::holds_alternative(get_proc_name)) { + switch (std::get(get_proc_name)) { + case args::Error::TooLong: + std::cout << "Proc name too long\n"; + return 1; + } + return 0; } + auto &&value = std::get(get_proc_name); + std::cout << value << '\n'; return 0; } -