-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
859ee7b
commit eb0fec1
Showing
4 changed files
with
71 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module; | ||
#include <iostream> | ||
#include <string_view> | ||
#include <variant> | ||
|
||
export module args; | ||
|
||
export namespace args { | ||
|
||
enum class Error { | ||
Ok, | ||
TooLong, | ||
TooManyArgs, | ||
NullPtr, | ||
}; | ||
|
||
std::variant<std::string_view, Error> 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,22 @@ | ||
#include <iostream> | ||
#include <variant> | ||
|
||
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<args::Error>(get_proc_name)) { | ||
switch (std::get<args::Error>(get_proc_name)) { | ||
case args::Error::TooLong: | ||
std::cout << "Proc name too long\n"; | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
auto &&value = std::get<std::string_view>(get_proc_name); | ||
std::cout << value << '\n'; | ||
return 0; | ||
} | ||
|