From 5c2b64174d9bb212d8f3a0fcf8367ca987a9637a Mon Sep 17 00:00:00 2001 From: Pascal Menuet Date: Fri, 27 Oct 2023 21:24:34 +0200 Subject: [PATCH 1/4] Try fixing exceptions stuff accordingly to comments in #30 and #36 --- .gitignore | 1 + include/ifc/abstract-sgraph.hxx | 13 ++++++++++++- src/ifc-printer/main.cxx | 10 +++++++--- src/ifc-reader/reader.cxx | 4 ++-- src/sgraph.cxx | 2 +- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index d7d6930..df04aab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/.vs/ /build/ /prefix/ /linux-build/ diff --git a/include/ifc/abstract-sgraph.hxx b/include/ifc/abstract-sgraph.hxx index 5a1e15b..19f0801 100644 --- a/include/ifc/abstract-sgraph.hxx +++ b/include/ifc/abstract-sgraph.hxx @@ -3544,7 +3544,18 @@ namespace ifc { // -- exception type in case of an invalid partition name struct InvalidPartitionName { - std::string_view name; + static constexpr unsigned NAME_BUFFER_SIZE = 64; + + char name[NAME_BUFFER_SIZE] = ""; + + static InvalidPartitionName make(std::string_view name) noexcept + { + InvalidPartitionName e{}; + name = name.substr(0, NAME_BUFFER_SIZE - 1); + strncpy_s(e.name, name.data(), name.length()); + e.name[name.length()] = 0; + return e; + } }; // Retrieve a partition summary based on the partition's name. diff --git a/src/ifc-printer/main.cxx b/src/ifc-printer/main.cxx index 270ebdb..714c21f 100644 --- a/src/ifc-printer/main.cxx +++ b/src/ifc-printer/main.cxx @@ -18,14 +18,18 @@ void translate_exception() { std::cerr << "ifc architecture mismatch\n"; } - catch(ifc::error_condition::UnexpectedVisitor& e) + catch (const ifc::InvalidPartitionName& e) + { + std::cerr << "invalid partition name: " << e.name << '\n'; + } + catch (ifc::error_condition::UnexpectedVisitor& e) { std::cerr << "visit unexpected " << e.category << ": " << e.sort << '\n'; } - catch (const char* message) + catch (const std::exception& e) { - std::cerr << "caught: " << message; + std::cerr << "caught std exception: " << e.what(); } catch (...) { diff --git a/src/ifc-reader/reader.cxx b/src/ifc-reader/reader.cxx index 96e7bfb..2d15cb1 100644 --- a/src/ifc-reader/reader.cxx +++ b/src/ifc-reader/reader.cxx @@ -3,14 +3,14 @@ #include "ifc/util.hxx" #include "ifc/reader.hxx" +#include namespace ifc { constexpr std::string_view analysis_partition_prefix = ".msvc.code-analysis."; Reader::Reader(const ifc::InputIfc& ifc_) : ifc(ifc_) { - if (not ifc.header()) - throw "file not found"; + assert(ifc.header()); read_table_of_contents(); } diff --git a/src/sgraph.cxx b/src/sgraph.cxx index 4030e30..f811bcf 100644 --- a/src/sgraph.cxx +++ b/src/sgraph.cxx @@ -546,7 +546,7 @@ namespace ifc { { auto p = table.find(name); if (p == table.end()) - throw InvalidPartitionName{name}; + throw InvalidPartitionName::make(name); return p->sort; } From 970bf466c20ac110a70dabf18d647045010e6650 Mon Sep 17 00:00:00 2001 From: Pascal Menuet Date: Sun, 29 Oct 2023 16:14:13 +0100 Subject: [PATCH 2/4] Use std::array, std::copy and a ctor (+ add an accessor and make the buffer private to enforce the invariant of always ending with 0) --- include/ifc/abstract-sgraph.hxx | 22 +++++++++++++--------- src/ifc-printer/main.cxx | 2 +- src/sgraph.cxx | 2 +- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/include/ifc/abstract-sgraph.hxx b/include/ifc/abstract-sgraph.hxx index 19f0801..4d80402 100644 --- a/include/ifc/abstract-sgraph.hxx +++ b/include/ifc/abstract-sgraph.hxx @@ -18,6 +18,8 @@ #include #include #include +#include +#include #include #include @@ -3544,18 +3546,20 @@ namespace ifc { // -- exception type in case of an invalid partition name struct InvalidPartitionName { - static constexpr unsigned NAME_BUFFER_SIZE = 64; - - char name[NAME_BUFFER_SIZE] = ""; + InvalidPartitionName(std::string_view partition_name) + { + partition_name = partition_name.substr(0, partition_name_buffer.size() - 1); + std::copy(partition_name.begin(), partition_name.end(), partition_name_buffer.begin()); + partition_name_buffer[partition_name.length()] = 0; + } - static InvalidPartitionName make(std::string_view name) noexcept + constexpr const char* partition_name() const noexcept { - InvalidPartitionName e{}; - name = name.substr(0, NAME_BUFFER_SIZE - 1); - strncpy_s(e.name, name.data(), name.length()); - e.name[name.length()] = 0; - return e; + return partition_name_buffer.data(); } + + private: + std::array partition_name_buffer; }; // Retrieve a partition summary based on the partition's name. diff --git a/src/ifc-printer/main.cxx b/src/ifc-printer/main.cxx index 714c21f..f8a644c 100644 --- a/src/ifc-printer/main.cxx +++ b/src/ifc-printer/main.cxx @@ -20,7 +20,7 @@ void translate_exception() } catch (const ifc::InvalidPartitionName& e) { - std::cerr << "invalid partition name: " << e.name << '\n'; + std::cerr << "invalid partition name: " << e.partition_name() << '\n'; } catch (ifc::error_condition::UnexpectedVisitor& e) { diff --git a/src/sgraph.cxx b/src/sgraph.cxx index f811bcf..4030e30 100644 --- a/src/sgraph.cxx +++ b/src/sgraph.cxx @@ -546,7 +546,7 @@ namespace ifc { { auto p = table.find(name); if (p == table.end()) - throw InvalidPartitionName::make(name); + throw InvalidPartitionName{name}; return p->sort; } From efcc9cb2d5d74a5a3eb5d4ba64ec1a6c851684ce Mon Sep 17 00:00:00 2001 From: Pascal Menuet Date: Sun, 29 Oct 2023 16:16:19 +0100 Subject: [PATCH 3/4] Use IFCASSERT instead of assert to be more consistent with the remaining file --- src/ifc-reader/reader.cxx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ifc-reader/reader.cxx b/src/ifc-reader/reader.cxx index 2d15cb1..ff936af 100644 --- a/src/ifc-reader/reader.cxx +++ b/src/ifc-reader/reader.cxx @@ -3,14 +3,13 @@ #include "ifc/util.hxx" #include "ifc/reader.hxx" -#include namespace ifc { constexpr std::string_view analysis_partition_prefix = ".msvc.code-analysis."; Reader::Reader(const ifc::InputIfc& ifc_) : ifc(ifc_) { - assert(ifc.header()); + IFCASSERT(ifc.header()); read_table_of_contents(); } From a5bc0bd16356ce82e5d2edb5938037bd2db15eac Mon Sep 17 00:00:00 2001 From: Pascal Menuet Date: Mon, 30 Oct 2023 09:07:51 +0100 Subject: [PATCH 4/4] Mark one-arg constructor as explicit --- include/ifc/abstract-sgraph.hxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/ifc/abstract-sgraph.hxx b/include/ifc/abstract-sgraph.hxx index 4d80402..52296b6 100644 --- a/include/ifc/abstract-sgraph.hxx +++ b/include/ifc/abstract-sgraph.hxx @@ -3546,14 +3546,14 @@ namespace ifc { // -- exception type in case of an invalid partition name struct InvalidPartitionName { - InvalidPartitionName(std::string_view partition_name) + explicit InvalidPartitionName(std::string_view partition_name) { partition_name = partition_name.substr(0, partition_name_buffer.size() - 1); std::copy(partition_name.begin(), partition_name.end(), partition_name_buffer.begin()); partition_name_buffer[partition_name.length()] = 0; } - constexpr const char* partition_name() const noexcept + const char* partition_name() const noexcept { return partition_name_buffer.data(); }