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 1
1
cmake_minimum_required (VERSION 3.30 )
2
2
3
- # `import std;` is not supported yet by libstdc++
4
- set (CMAKE_CXX_COMPILER clang++ )
5
- set (CMAKE_C_COMPILER clang )
6
-
7
3
project (sonar_scanner_example LANGUAGES CXX )
8
4
9
5
set (CMAKE_CXX_STANDARD 20 )
10
6
set (CMAKE_CXX_STANDARD_REQUIRED ON )
11
7
set (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" )
14
9
15
10
set (CMAKE_EXPORT_COMPILE_COMMANDS ON )
16
11
17
12
add_executable (sonar_scanner_example src/main.cpp )
18
- target_compile_features (sonar_scanner_example
19
- INTERFACE cxx_std_20 )
20
13
target_sources (sonar_scanner_example
21
14
PRIVATE
22
15
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
+
1
6
export 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 1
1
#include < iostream>
2
+ #include < variant>
2
3
3
- using namespace std ;
4
+ import args ;
4
5
5
- int main (int argc, char * argv[]) {
6
- int num = argc - 1 ;
6
+ using namespace std ;
7
7
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 ;
22
17
}
23
18
19
+ auto &&value = std::get<std::string_view>(get_proc_name);
20
+ std::cout << value << ' \n ' ;
24
21
return 0 ;
25
22
}
26
-
You can’t perform that action at this time.
0 commit comments