Skip to content

Commit

Permalink
Enable modules analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandro-alvarez-sonarsource committed Oct 24, 2024
1 parent 859ee7b commit eb0fec1
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 22 deletions.
16 changes: 12 additions & 4 deletions CMakeLists.txt
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
)
5 changes: 5 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 40 additions & 0 deletions src/args.cppm
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
32 changes: 14 additions & 18 deletions src/main.cpp
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;
}

0 comments on commit eb0fec1

Please sign in to comment.