/src/CMake/Source/cmDiagnostics.cxx
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file LICENSE.rst or https://cmake.org/licensing for details. */ |
3 | | #include "cmDiagnostics.h" |
4 | | |
5 | | #include <cassert> |
6 | | #include <map> |
7 | | #include <string> |
8 | | #include <utility> |
9 | | |
10 | | #include <cmext/string_view> |
11 | | |
12 | | #include "cmStringAlgorithms.h" |
13 | | |
14 | | namespace { |
15 | | cm::optional<cmDiagnosticCategory> stringToCategory(cm::string_view input) |
16 | 0 | { |
17 | 0 | using Map = std::map<cm::string_view, cmDiagnosticCategory>; |
18 | 0 | static Map const mapping = { |
19 | 0 | #define CATEGORY_MAP(C) { #C ""_s, cmDiagnostics::C }, |
20 | 0 | CM_FOR_EACH_DIAGNOSTIC_CATEGORY(CATEGORY_MAP) |
21 | 0 | #undef CATEGORY_MAP |
22 | 0 | }; |
23 | |
|
24 | 0 | assert(!input.empty()); |
25 | 0 | if (input.size() >= 4 && cmHasLiteralPrefix(input, "CMD_")) { |
26 | 0 | auto const i = mapping.find(input); |
27 | 0 | if (i != mapping.end()) { |
28 | 0 | return i->second; |
29 | 0 | } |
30 | 0 | } |
31 | | |
32 | 0 | return cm::nullopt; |
33 | 0 | } |
34 | | } |
35 | | |
36 | | #if __cplusplus < 201703L |
37 | | // Prior to C++17, the compiler is unhappy if this member doesn't have explicit |
38 | | // storage... and clang-tidy is unhappy if it does. |
39 | | // NOLINTNEXTLINE(*-redundant-declaration) |
40 | | constexpr cmDiagnostics::DiagnosticCategoryInformation |
41 | | cmDiagnostics::CategoryInfo[cmDiagnostics::CategoryCount]; |
42 | | #endif |
43 | | |
44 | | cm::string_view cmDiagnostics::GetActionString(DiagnosticAction action) |
45 | 0 | { |
46 | 0 | switch (action) { |
47 | 0 | case Ignore: |
48 | 0 | return "IGNORE"_s; |
49 | 0 | case Warn: |
50 | 0 | return "WARN"_s; |
51 | 0 | case SendError: |
52 | 0 | return "SEND_ERROR"_s; |
53 | 0 | case FatalError: |
54 | 0 | return "FATAL_ERROR"_s; |
55 | 0 | default: |
56 | 0 | return {}; |
57 | 0 | } |
58 | 0 | } |
59 | | |
60 | | cm::string_view cmDiagnostics::GetCategoryString(DiagnosticCategory category) |
61 | 16 | { |
62 | 16 | static cm::string_view const names[CategoryCount] = { |
63 | 16 | {}, // CMD_NONE |
64 | 192 | #define CATEGORY_NAME(C) #C ""_s, |
65 | 64 | CM_FOR_EACH_DIAGNOSTIC_CATEGORY(CATEGORY_NAME) |
66 | 16 | #undef CATEGORY_MAP |
67 | 16 | }; |
68 | | |
69 | 16 | if (category < CategoryCount) { |
70 | 16 | return names[category]; |
71 | 16 | } |
72 | 0 | return {}; |
73 | 16 | } |
74 | | |
75 | | cm::optional<cmDiagnosticAction> cmDiagnostics::GetDiagnosticAction( |
76 | | cm::string_view name) |
77 | 0 | { |
78 | 0 | if (name == "IGNORE"_s) { |
79 | 0 | return DiagnosticAction::Ignore; |
80 | 0 | } |
81 | 0 | if (name == "WARN"_s) { |
82 | 0 | return DiagnosticAction::Warn; |
83 | 0 | } |
84 | 0 | if (name == "SEND_ERROR"_s) { |
85 | 0 | return DiagnosticAction::SendError; |
86 | 0 | } |
87 | 0 | if (name == "FATAL_ERROR"_s) { |
88 | 0 | return DiagnosticAction::FatalError; |
89 | 0 | } |
90 | | |
91 | 0 | return cm::nullopt; |
92 | 0 | } |
93 | | |
94 | | cm::optional<cmDiagnosticCategory> cmDiagnostics::GetDiagnosticCategory( |
95 | | cm::string_view name) |
96 | 0 | { |
97 | 0 | return stringToCategory(name); |
98 | 0 | } |