Commit 789094c8 authored by Pádraig Ó Conbhuí's avatar Pádraig Ó Conbhuí
Browse files

Ran clang-tidy. Added some new exceptions due to array-to-pointer decay in assert on macOS 10.14.

parent 080c80a5
---
Checks: '*,-llvm-header-guard'
Checks: '*,
-llvm-header-guard,
-fuchsia-default-arguments,
-cppcoreguidelines-pro-bounds-constant-array-index,
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
-hicpp-no-array-decay'
# -cppcoreguidelines-pro-bounds-array-to-pointer-decay
# and -hicpp-no-array-decay due to array-to-pointer decay in assert on some
# platforms (e.g. macOS 10.14).
WarningsAsErrors: ''
HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false
......
......@@ -7,6 +7,7 @@ project(
LANGUAGES CXX
)
add_library(
libmegadep
src/megadep/filesystem.cpp
......@@ -17,13 +18,20 @@ set_target_properties(
OUTPUT_NAME "megadep"
)
target_compile_features(libmegadep PUBLIC cxx_std_17)
target_include_directories(libmegadep PRIVATE src)
target_include_directories(libmegadep PUBLIC src)
add_executable(
megadep
src/megadep.main.cpp
)
target_link_libraries(megadep PRIVATE libmegadep)
option(MEGADEP_ENABLE_TESTING "Enable testing for Megadep" ON)
if(MEGADEP_ENABLE_TESTING)
enable_testing()
add_subdirectory(external/catch2)
add_subdirectory(tests)
add_executable(
......
......@@ -3,6 +3,7 @@
#include <megadep/filesystem.hpp>
#include <array>
#include <cassert>
#include <cerrno>
#include <cstring>
#include <vector>
......@@ -62,10 +63,14 @@ public:
FILE *file_ptr() { return m_file_ptr; }
explicit raii_fopen(std::string_view file_name) {
assert(file_name.size() + 1 < filename_max);
std::array<char, filename_max> file_name_buf{};
file_name.copy(file_name_buf.data(), file_name.size());
file_name_buf[file_name.size()] = '\0';
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
m_file_ptr = fopen(file_name_buf.data(), "re");
}
......@@ -145,24 +150,29 @@ resolve_include(std::string_view include, std::string_view source_file,
std::array<char, filename_max> filename_buffer{};
assert(include.size() + 1 < filename_buffer.size());
// The include name without <, >, ", or '.
std::string_view stripped_include = include.substr(1, include.size() - 2);
// Build the test file name and return a pointer to it.
// This uses filename_buffer as a buffer.
const auto build_file_name = [&](std::string_view search_directory) {
assert(search_directory.size() + 1 + stripped_include.size() + 1<
filename_buffer.size());
// Add search directory
search_directory.copy(filename_buffer.data(), search_directory.size());
search_directory.copy(filename_buffer.begin(), search_directory.size());
// Add directory separator
filename_buffer[search_directory.size()] = '/';
// Add include name
stripped_include.copy(filename_buffer.data() + search_directory.size() + 1,
stripped_include.copy(filename_buffer.begin() + search_directory.size() + 1,
stripped_include.size());
// Make sure it's null terminated
filename_buffer[search_directory.size() + stripped_include.size() + 1] =
filename_buffer[search_directory.size() + 1 + stripped_include.size()] =
'\0';
return filename_buffer.data();
......@@ -170,9 +180,10 @@ resolve_include(std::string_view include, std::string_view source_file,
// Check the file name exists on the disk / is accessible.
const auto file_exists = [&](const char *file_name) {
return access(filename_buffer.data(), F_OK) == 0;
return access(file_name, F_OK) == 0;
};
// For #include "filename.hpp", look in the source file directory first.
switch (include[0]) {
case '"': // fallthrough
......@@ -182,6 +193,7 @@ resolve_include(std::string_view include, std::string_view source_file,
}
}
// For everything else, look in the supplied include directories
for (const auto &search_directory : search_directories) {
if (file_exists(build_file_name(search_directory))) {
......@@ -189,9 +201,11 @@ resolve_include(std::string_view include, std::string_view source_file,
}
}
// If nothing is found, return the original include as <filename.hpp>
stripped_include.copy(filename_buffer.begin() + 1, stripped_include.size());
filename_buffer[0] = '<';
stripped_include.copy(filename_buffer.data() + 1, stripped_include.size());
filename_buffer[stripped_include.size() + 1] = '>';
filename_buffer[stripped_include.size() + 2] = '\0';
......
......@@ -108,25 +108,29 @@ TEST_CASE("megadep::resolve_include resolves the #include statements",
// #include "filesystem.hpp"
// #include "megadep/filesystem.hpp"
// #include "cstdio"
includes.push_back("\"filesystem.hpp\"");
includes.push_back("\"megadep/filesystem.hpp\"");
includes.push_back("\"cstdio\"");
includes.emplace_back("\"filesystem.hpp\"");
includes.emplace_back("\"megadep/filesystem.hpp\"");
includes.emplace_back("\"cstdio\"");
std::string megadep_include = "src";
std::string catch2_include = "external/catch2/single_include";
std::vector<std::string> search_directories = {megadep_include,
catch2_include};
const std::string megadep_include = "src";
const std::string catch2_include = "external/catch2/single_include";
const std::vector<std::string> search_directories = {megadep_include,
catch2_include};
for (auto &include : includes) {
auto resolved_include =
megadep::resolve_include(include, this_file, search_directories);
// remove <, >, ", '.
std::string stripped_include{include.data() + 1, include.size() - 2};
const std::string stripped_include = include.substr(1, include.size() - 2);
std::string_view megadep_prefix = "megadep/";
std::string_view catch2_prefix = "catch2/";
// expect <megadep/...> and "megadep/..." to resolve to the src/megadep
// directory.
if (stripped_include.compare(0, 8, "megadep/") == 0) {
if (stripped_include.substr(0, megadep_prefix.size()) == megadep_prefix) {
// NOLINTNEXTLINE(performance-inefficient-string-concatenation)
REQUIRE(resolved_include == megadep_include + "/" + stripped_include);
}
// expect "fileystem.hpp" to resolve to the src/megadep directory.
......@@ -135,7 +139,9 @@ TEST_CASE("megadep::resolve_include resolves the #include statements",
std::string{this_directory} + "/" + stripped_include);
}
// expect <catch2/...> to resolve to the external/catch2/... directory.
else if (stripped_include.compare(0, 7, "catch2/") == 0) {
else if (stripped_include.substr(0, catch2_prefix.size()) ==
catch2_prefix) {
// NOLINTNEXTLINE(performance-inefficient-string-concatenation)
REQUIRE(resolved_include == catch2_include + "/" + stripped_include);
}
// expect everything else to be unresolved.
......
add_subdirectory(../external/catch2 catch2)
add_library(
catch2_runner
catch2_runner.main.cpp
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment