Skip to content

Commit 589feee

Browse files
committed
citnames: add back tool unit tests
1 parent 21c6076 commit 589feee

File tree

7 files changed

+391
-358
lines changed

7 files changed

+391
-358
lines changed

source/citnames/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ target_link_libraries(citnames_a PUBLIC report_a)
3333
target_link_libraries(citnames_a PUBLIC sys_a)
3434
target_link_libraries(citnames_a PUBLIC fmt::fmt)
3535
target_link_libraries(citnames_a PUBLIC spdlog::spdlog)
36+
target_compile_options(citnames_a PRIVATE -frtti)
3637

3738
# Create an executable from the sub projects.
3839
add_executable(citnames
@@ -69,8 +70,8 @@ install(FILES man/citnames.1
6970
if (ENABLE_UNIT_TESTS)
7071
add_executable(citnames_unit_test
7172
test/OutputTest.cc
72-
test/SemanticTest.cc
7373
test/ToolTest.cc
74+
test/ToolGccTest.cc
7475
)
7576

7677
target_link_libraries(citnames_unit_test citnames_a)

source/citnames/source/semantic/Semantic.cc

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
#include "semantic/Semantic.h"
2121

22+
#include <fmt/format.h>
23+
2224
namespace {
2325

2426
inline
@@ -51,41 +53,6 @@ namespace cs::semantic {
5153
, flags(std::move(_flags))
5254
{ }
5355

54-
void QueryCompiler::extend_flags(const std::list<std::string> &) {
55-
}
56-
57-
void Preprocess::extend_flags(const std::list<std::string> &_flags) {
58-
std::copy(_flags.begin(), _flags.end(), std::back_inserter(flags));
59-
}
60-
61-
void Compile::extend_flags(const std::list<std::string> &_flags) {
62-
std::copy(_flags.begin(), _flags.end(), std::back_inserter(flags));
63-
}
64-
65-
void Link::extend_flags(const std::list<std::string> &_flags) {
66-
std::copy(_flags.begin(), _flags.end(), std::back_inserter(flags));
67-
}
68-
69-
std::ostream &QueryCompiler::operator<<(std::ostream &os) const {
70-
os << "Query";
71-
return os;
72-
}
73-
74-
std::ostream &Preprocess::operator<<(std::ostream &os) const {
75-
os << "Preprocess";
76-
return os;
77-
}
78-
79-
std::ostream &Compile::operator<<(std::ostream &os) const {
80-
os << "Compile";
81-
return os;
82-
}
83-
84-
std::ostream &Link::operator<<(std::ostream &os) const {
85-
os << "Link";
86-
return os;
87-
}
88-
8956
std::optional<cs::Entry> QueryCompiler::into_entry() const {
9057
return std::optional<cs::Entry>();
9158
}
@@ -105,8 +72,60 @@ namespace cs::semantic {
10572
return std::make_optional(std::move(entry));
10673
}
10774

108-
std::optional<cs::Entry> Link::into_entry() const {
109-
// TODO
110-
return std::optional<cs::Entry>();
75+
bool QueryCompiler::operator==(const Semantic &rhs) const {
76+
if (this == &rhs)
77+
return true;
78+
79+
if (const auto* ptr = dynamic_cast<QueryCompiler const*>(&rhs); ptr != nullptr) {
80+
return (command == ptr->command);
81+
}
82+
return false;
83+
}
84+
85+
bool Preprocess::operator==(const Semantic &rhs) const {
86+
if (this == &rhs)
87+
return true;
88+
89+
if (const auto* ptr = dynamic_cast<Preprocess const*>(&rhs); ptr != nullptr) {
90+
return (command == ptr->command) &&
91+
(source == ptr->source) &&
92+
(output == ptr->output) &&
93+
(flags == ptr->flags);
94+
}
95+
return false;
96+
}
97+
98+
bool Compile::operator==(const Semantic &rhs) const {
99+
if (this == &rhs)
100+
return true;
101+
102+
if (const auto* ptr = dynamic_cast<Compile const*>(&rhs); ptr != nullptr) {
103+
return (command == ptr->command) &&
104+
(source == ptr->source) &&
105+
(output == ptr->output) &&
106+
(flags == ptr->flags);
107+
}
108+
return false;
109+
}
110+
111+
std::ostream &QueryCompiler::operator<<(std::ostream &os) const {
112+
os << "Query";
113+
return os;
114+
}
115+
116+
std::ostream &Preprocess::operator<<(std::ostream &os) const {
117+
os << "Preprocess { source: " << source
118+
<< ", output: " << output
119+
<< ", flags: " << fmt::format("[{}]", fmt::join(flags.begin(), flags.end(), ", "))
120+
<< " }";
121+
return os;
122+
}
123+
124+
std::ostream &Compile::operator<<(std::ostream &os) const {
125+
os << "Compile { source: " << source
126+
<< ", output: " << output
127+
<< ", flags: " << fmt::format("[{}]", fmt::join(flags.begin(), flags.end(), ", "))
128+
<< " }";
129+
return os;
111130
}
112131
}

source/citnames/source/semantic/Semantic.h

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ namespace cs::semantic {
3838
explicit Semantic(report::Command) noexcept;
3939
virtual ~Semantic() noexcept = default;
4040

41-
virtual void extend_flags(std::list<std::string> const&) = 0;
41+
[[nodiscard]] virtual std::optional<cs::Entry> into_entry() const = 0;
4242

43+
virtual bool operator==(Semantic const&) const = 0;
4344
virtual std::ostream& operator<<(std::ostream&) const = 0;
44-
[[nodiscard]] virtual std::optional<cs::Entry> into_entry() const = 0;
4545

4646
report::Command command;
4747
};
@@ -54,20 +54,20 @@ namespace cs::semantic {
5454
struct QueryCompiler : public Semantic {
5555
explicit QueryCompiler(report::Command) noexcept;
5656

57-
void extend_flags(std::list<std::string> const&) override;
57+
[[nodiscard]] std::optional<cs::Entry> into_entry() const override;
5858

59+
bool operator==(Semantic const&) const override;
5960
std::ostream& operator<<(std::ostream&) const override;
60-
[[nodiscard]] std::optional<cs::Entry> into_entry() const override;
6161
};
6262

6363
// Represents a compiler call, which runs the preprocessor pass.
6464
struct Preprocess : public Semantic {
6565
Preprocess(report::Command, fs::path source, fs::path output, std::list<std::string>) noexcept;
6666

67-
void extend_flags(std::list<std::string> const&) override;
67+
[[nodiscard]] std::optional<cs::Entry> into_entry() const override;
6868

69+
bool operator==(Semantic const&) const override;
6970
std::ostream& operator<<(std::ostream&) const override;
70-
[[nodiscard]] std::optional<cs::Entry> into_entry() const override;
7171

7272
fs::path source;
7373
fs::path output;
@@ -78,35 +78,14 @@ namespace cs::semantic {
7878
struct Compile : public Semantic {
7979
Compile(report::Command, fs::path source, fs::path output, std::list<std::string>) noexcept;
8080

81-
void extend_flags(std::list<std::string> const&) override;
82-
83-
std::ostream& operator<<(std::ostream&) const override;
8481
[[nodiscard]] std::optional<cs::Entry> into_entry() const override;
8582

86-
fs::path source;
87-
fs::path output;
88-
std::list<std::string> flags;
89-
};
90-
91-
// Represents a compiler call, which runs the linking pass.
92-
struct Link : public Semantic {
93-
94-
void extend_flags(std::list<std::string> const&) override;
95-
83+
bool operator==(Semantic const&) const override;
9684
std::ostream& operator<<(std::ostream&) const override;
97-
[[nodiscard]] std::optional<cs::Entry> into_entry() const override;
98-
99-
enum Type {
100-
EXECUTABLE,
101-
LIBRARY
102-
};
10385

104-
std::list<fs::path> inputs;
105-
std::list<fs::path> libraries;
86+
fs::path source;
10687
fs::path output;
107-
Type type;
10888
std::list<std::string> flags;
109-
11089
};
11190

11291
inline
@@ -125,4 +104,22 @@ namespace cs::semantic {
125104
}
126105
return os;
127106
}
107+
108+
inline
109+
bool operator==(Semantic const &lhs, Semantic const &rhs) {
110+
return lhs.operator==(rhs);
111+
}
112+
113+
inline
114+
bool operator==(SemanticPtrs const &lhs, SemanticPtrs const &rhs) {
115+
auto lhs_it = lhs.begin();
116+
auto rhs_it = rhs.begin();
117+
const auto lhs_end = lhs.end();
118+
const auto rhs_end = rhs.end();
119+
while (lhs_it != lhs_end && rhs_it != rhs_end && (*lhs_it)->operator==(**rhs_it)) {
120+
++lhs_it;
121+
++rhs_it;
122+
}
123+
return lhs_it == lhs_end && rhs_it == rhs_end;
124+
}
128125
}

source/citnames/source/semantic/ToolExtendingWrapper.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,16 @@ namespace cs {
3838
return ToolGcc().compilations(command)
3939
.map<cs::semantic::SemanticPtrs>([this](auto semantics) {
4040
for (auto& semantic : semantics) {
41-
semantic->extend_flags(compilers_to_recognize_.additional_flags);
41+
if (auto* ptr = dynamic_cast<Preprocess*>(semantic.get()); ptr != nullptr) {
42+
std::copy(compilers_to_recognize_.additional_flags.begin(),
43+
compilers_to_recognize_.additional_flags.end(),
44+
std::back_inserter(ptr->flags));
45+
}
46+
if (auto* ptr = dynamic_cast<Compile*>(semantic.get()); ptr != nullptr) {
47+
std::copy(compilers_to_recognize_.additional_flags.begin(),
48+
compilers_to_recognize_.additional_flags.end(),
49+
std::back_inserter(ptr->flags));
50+
}
4251
}
4352
return semantics;
4453
});

0 commit comments

Comments
 (0)