-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSourceAnalyzer.h
More file actions
80 lines (65 loc) · 2.15 KB
/
SourceAnalyzer.h
File metadata and controls
80 lines (65 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#pragma once
#include <array>
#include <map>
#include <string>
#include <string_view>
#include <vector>
struct Linker {
int priority = -1;
std::string command;
bool operator>(const Linker& that) const {
return priority > that.priority;
}
explicit operator bool() const {
return priority >= 0 && !command.empty();
}
static auto ForAsm(const std::string& command) -> Linker {
const auto priority = command.empty() ? -1 : 1;
return {priority, command};
}
static auto ForC(const std::string& command) -> Linker {
const auto priority = command.empty() ? -1 : 2;
return {priority, command};
}
static auto ForCpp(const std::string& command) -> Linker {
const auto priority = command.empty() ? -1 : 3;
return {priority, command};
}
static auto ForLd(const std::string& command) -> Linker {
const auto priority = command.empty() ? -1 : 4;
return {priority, command};
}
};
struct SourceFile {
std::string source;
std::string output;
std::vector<std::string> dependencies;
std::string command;
Linker linker;
explicit operator bool() const {
return !source.empty() || !output.empty();
}
};
class SourceAnalyzer {
using Handler = SourceFile (SourceAnalyzer::*)(const std::string&) const;
public:
explicit SourceAnalyzer(const std::map<std::string, std::string>& args)
: args_(args) {
auto install = [this](Handler handler, auto extensions) {
for (auto extension : extensions) {
handlers_.emplace(extension, handler);
}
};
install(&SourceAnalyzer::ProcessC, std::array{".c"});
install(&SourceAnalyzer::ProcessCpp, std::array{".cc", ".cpp", ".cxx", ".c++"});
install(&SourceAnalyzer::ProcessAsm, std::array{".s", ".asm", ".nas"});
}
[[nodiscard]] auto Process(const std::string& path) const -> SourceFile;
private:
[[nodiscard]] auto ProcessC(const std::string& path) const -> SourceFile;
[[nodiscard]] auto ProcessCpp(const std::string& path) const -> SourceFile;
[[nodiscard]] auto ProcessAsm(const std::string& path) const -> SourceFile;
private:
const std::map<std::string, std::string>& args_;
std::map<std::string_view, Handler> handlers_;
};