File tree Expand file tree Collapse file tree 3 files changed +54
-26
lines changed
Expand file tree Collapse file tree 3 files changed +54
-26
lines changed Original file line number Diff line number Diff line change 11cmake_minimum_required (VERSION 3.30)
22
3- # `import std;` is not supported yet by libstdc++
4- set (CMAKE_CXX_COMPILER clang++)
5- set (CMAKE_C_COMPILER clang)
6-
73project (sonar_scanner_example LANGUAGES CXX)
84
95set (CMAKE_CXX_STANDARD 20)
106set (CMAKE_CXX_STANDARD_REQUIRED ON )
117set (CMAKE_CXX_EXTENSIONS OFF )
12- set (CMAKE_CXX_MODULE_STD 1)
13- set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror -Wpedantic" )
8+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall" )
149
1510set (CMAKE_EXPORT_COMPILE_COMMANDS ON )
1611
1712add_executable (sonar_scanner_example src/main.cpp)
18- target_compile_features (sonar_scanner_example
19- INTERFACE cxx_std_20)
2013target_sources (sonar_scanner_example
2114 PRIVATE
2215 FILE_SET CXX_MODULES FILES src/args.cppm
Original file line number Diff line number Diff line change 1+ module ;
2+ #include < iostream>
3+ #include < string_view>
4+ #include < variant>
5+
16export module args;
7+
8+ export namespace args {
9+
10+ enum class Error {
11+ Ok,
12+ TooLong,
13+ TooManyArgs,
14+ NullPtr,
15+ };
16+
17+ std::variant<std::string_view, Error> process_args (int argc, char *argv[]) {
18+ int num = argc - 1 ;
19+
20+ if (num == 0 ) {
21+ std::cout << " No arguments provided\n " ;
22+ } else if (num == 0 ) { // intentional mistake
23+ std::cout << " 1 argument provided\n " ;
24+ } else if (num == 2 ) {
25+ std::cout << " 2 arguments provided\n " ;
26+ } else {
27+ std::cout << num << " arguments provided\n " ;
28+ }
29+ if (argv != 0 ) {
30+ std::cout << " argv not null\n " ;
31+ ; // intentional extra-semicolon
32+ }
33+
34+ if (argv == nullptr ) {
35+ return std::string_view (*argv); // intentional nullptr dereference
36+ }
37+
38+ return std::string_view (argv[0 ]);
39+ }
40+ } // namespace args
Original file line number Diff line number Diff line change 11#include < iostream>
2+ #include < variant>
23
3- using namespace std ;
4+ import args ;
45
5- int main (int argc, char * argv[]) {
6- int num = argc - 1 ;
6+ using namespace std ;
77
8- if (num == 0 ) {
9- cout << " No arguments provided\n " ;
10- } else if (num == 0 ) { // intentional mistake
11- cout << " 1 argument provided\n " ;
12- } else if (num == 2 ) {
13- cout << " 2 arguments provided\n " ;
14- } else {
15- cout << num << " arguments provided\n " ;
16- }
17- if (argv != 0 ) {
18- cout << " argv not null\n " ;; // intentional extra-semicolon
19- }
20- if (argv == nullptr ) {
21- return **argv; // intentional nullptr dereference
8+ int main (int argc, char *argv[]) {
9+ auto get_proc_name = args::process_args (argc, argv);
10+ if (std::holds_alternative<args::Error>(get_proc_name)) {
11+ switch (std::get<args::Error>(get_proc_name)) {
12+ case args::Error::TooLong:
13+ std::cout << " Proc name too long\n " ;
14+ return 1 ;
15+ }
16+ return 0 ;
2217 }
2318
19+ auto &&value = std::get<std::string_view>(get_proc_name);
20+ std::cout << value << ' \n ' ;
2421 return 0 ;
2522}
26-
You can’t perform that action at this time.
0 commit comments