File tree 5 files changed +84
-24
lines changed
5 files changed +84
-24
lines changed Original file line number Diff line number Diff line change 5
5
- main
6
6
pull_request :
7
7
types : [opened, synchronize, reopened]
8
+ env :
9
+ CLANG_VERSION : 18
8
10
jobs :
9
11
build :
10
12
name : Build
@@ -15,10 +17,22 @@ jobs:
15
17
fetch-depth : 0 # Shallow clones should be disabled for a better relevancy of analysis
16
18
- name : Install sonar-scanner
17
19
uses : sonarsource/sonarcloud-github-c-cpp@v3
18
- - name : Generate compilation database
20
+ - name : Install Ninja
21
+ run : |
22
+ wget https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-linux.zip
23
+ sudo unzip ninja-linux.zip -d /usr/local/bin
24
+ - name : Install clang
25
+ run : |
26
+ sudo bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)" ${{env.CLANG_VERSION}}
27
+ sudo apt install -y clang-tools-${{env.CLANG_VERSION}} libc++-${{env.CLANG_VERSION}}-dev
28
+ - name : Generate compilation database and build
19
29
run : |
20
30
mkdir build
21
- cmake -S . -B build
31
+ cmake -S . -B build -G Ninja \
32
+ -DCMAKE_CXX_COMPILER=clang++-${{env.CLANG_VERSION}} \
33
+ -DCMAKE_CXX_COMPILER_CLANG_SCAN_DEPS=clang-scan-deps-${{env.CLANG_VERSION}}
34
+ # The project needs to be built so the `.modmap` files are generated
35
+ cmake --build build --target all
22
36
- name : Run sonar-scanner
23
37
env :
24
38
GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
Original file line number Diff line number Diff line change 1
- cmake_minimum_required (VERSION 3.9)
2
- project (sonar_scanner_example)
1
+ cmake_minimum_required (VERSION 3.30)
2
+
3
+ project (sonar_scanner_example LANGUAGES CXX)
4
+
5
+ set (CMAKE_CXX_STANDARD 20)
6
+ set (CMAKE_CXX_STANDARD_REQUIRED ON )
7
+ set (CMAKE_CXX_EXTENSIONS OFF )
8
+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall" )
3
9
4
- set (CMAKE_CXX_STANDARD 17)
5
10
set (CMAKE_EXPORT_COMPILE_COMMANDS ON )
6
11
7
12
add_executable (sonar_scanner_example src/main.cpp)
8
-
13
+ target_sources (sonar_scanner_example
14
+ PRIVATE
15
+ FILE_SET CXX_MODULES FILES src/args.cppm
16
+ )
Original file line number Diff line number Diff line change @@ -10,3 +10,5 @@ sonar.projectVersion=1.0-SNAPSHOT
10
10
11
11
# SQ standard properties
12
12
sonar.sources =src
13
+ # Enable C++20 modules
14
+ sonar.cfamily.enableModules =true
Original file line number Diff line number Diff line change
1
+ module;
2
+ #include < iostream>
3
+ #include < string_view>
4
+ #include < variant>
5
+
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