NewLang Project
Yet another programm language
Loading...
Searching...
No Matches
diag.h
Go to the documentation of this file.
1#ifndef NEWLANG_DIAG_H_
2#define NEWLANG_DIAG_H_
3
4#include "types.h"
5
6#include "warning_push.h"
7#include "parser.yy.h"
8#include "warning_pop.h"
9
10namespace newlang {
11
12 /*
13 -Werror Turn warnings into errors.
14 -Werror=foo Turn warning “foo” into an error.
15 -Wno-error=foo Turn warning “foo” into a warning even if -Werror is specified.
16 -Wfoo Enable warning “foo”. See the diagnostics reference for a complete list of the warning flags that can be specified in this way.
17 -Wno-foo Disable warning “foo”.
18 -w Disable all diagnostics.
19
20 * For "-Wfoo"
21 * -Werror=foo
22 * -Wno-error=foo
23 * -Wno-foo
24 *
25 * -Wclang-error
26 * For "-Wclang-*"
27 * -Werror=clang-*
28 * -Wno-error=clang-*
29 * -Wno-clang-*
30
31 */
32 class Diag {
33 public:
34
35
36#define DIAG_STATE(_) \
37 _(none) /* not exist */ \
38 _(flag) /* set flag only (-w, -Weverything, -pedantic, -fdiagnostics-format=clang/msvc/vi ...) */ \
39 _(option) /* enable or disable diagnostic (-f[no-]caret-diagnostics, -f[no-]show-source-location ...) */ \
40 _(ignored) /* mutable diagnostic status -Wfoo, -Wno-foo, -Werror=foo, -Wno-error=foo */ \
41 _(warning) /* mutable diagnostic status -Wfoo, -Wno-foo, -Werror=foo, -Wno-error=foo */ \
42 _(error) /* mutable diagnostic status -Wfoo, -Wno-foo, -Werror=foo, -Wno-error=foo */
43
44 enum class State : uint8_t {
45#define DEFINE_ENUM(name) name,
47#undef DEFINE_ENUM
48 };
49
50 inline static const char * toString(State state) {
51 switch (state) {
52
53#define DEFINE_CASE(name) \
54 case State::name: \
55 return #name;
57#undef DEFINE_CASE
58
59 default:
60 return nullptr;
61 }
62 }
63
64 struct DataDiag {
65 std::string Name;
67 const char *Desc;
68 };
69
70 typedef std::vector<DataDiag> DiagListType;
71 typedef std::vector<DiagListType> DiagStackType;
72
73
74 inline static const char * DIAG_MACRO_NOT_FOUND = "-Wmacro-not-found";
75 inline static const char * DIAG_MACRO_STORAGE_NOT_EXIST = "-Wmacro-storage-not-exist";
76 inline static const char * DIAG_ERROR_LIMIT = "-ferror-limit="; //-ferror-limit=20
77 inline static const char * DIAG_EXTRA_TOKENS = "-Wextra-tokens";
78 inline static const char * DIAG_FILL_REMAINDER = "-Wfill-remainder";
79
80 inline static bool IsClang(const std::string name) {
81 return name.find("-clang") == 0;
82 }
83
84 inline static bool IsTurnError(const std::string name) {
85 return name.find("-Werror=") == 0;
86 }
87
88 inline static bool IsTurnNoError(const std::string name) {
89 return name.find("-Wno-error=") == 0;
90 }
91
92 inline static bool IsDisable(const std::string name) {
93 return name.find("-Wno-") == 0;
94 }
95
96 inline static bool IsEnable(const std::string name) {
97 return name.find("-W") == 0 && !IsDisable(name)&& !IsTurnError(name)&& !IsTurnNoError(name);
98 }
99
100 inline static std::string RemoveDiagPrefix(const std::string name) {
101
102 std::string result(IsClang(name) ? name.substr(6) : name); // Remove prefix "-clang"
103
104 if (IsEnable(result)) {
105 return result.substr(2); // Remove prefix "-W"
106 }
107
111
112 return result; // Return any options ???
113 }
114
115 static std::string ChangeState(const std::string name, State from_state, State to_state);
116
117 //-Werror: Turn warnings into errors.
118 //-Werror=foo: Turn warning "foo" into an error.
119 //-Wno-error=foo: Turn warning "foo" into an warning even if -Werror is specified.
120
121 //-Wno-foo: Disable warning foo
122 //-Wfoo: Enable warning foo
123 //-w: Disable all warnings.
124
125 //-Wno-error
126
127 static DiagPtr Init(int argc = 0, const char** argv = nullptr) {
128 DiagPtr diag = std::make_shared<Diag>();
129 if (!diag->ParseArgs(argc, argv)) {
130 LOG_RUNTIME("Fail parse args!");
131 }
132 return diag;
133 }
134
135 void Push(const TermPtr term = nullptr);
136 void Pop(const TermPtr term = nullptr);
137 bool Apply(const char *name, State state, const TermPtr term = nullptr);
138
139 Diag::State Test(const char *name);
140 bool TestIgnore(const char *name);
141 bool Emit(const char *name, const TermPtr term = nullptr);
142
143 bool Register(const char *name, State state, const char *desc = nullptr);
144
145 virtual ~Diag() {
146 }
147
148 Diag();
149
150 // SCOPE(private) :
151
152 bool ParseArgs(int argc, const char** argv) {
153
154 for (int i = 0; i < argc; i++) {
155
156 // m_args->push_back(Obj::CreateString(argv[i]));
157 //
158 // if (strstr(argv[i], "--nlc-") == argv[i]) {
159 // if (strstr(argv[i], "--nlc-search=") == argv[i]) {
160 // std::string list(argv[i]);
161 // list = list.substr(strlen("--nlc-search="));
162 // m_search_dir = Context::SplitString(list.c_str(), ";");
163 // } else if (strstr(argv[i], "--nlc-search=") == argv[i]) {
164 // } else {
165 // LOG_RUNTIME("System arg '%s' not found!", argv[i]);
166 // }
167 // }
168
169 }
170
171
172 // llvm::SmallString<1024> path;
173 // auto error = llvm::sys::fs::current_path(path);
174 // if (error) {
175 // LOG_RUNTIME("%s", error.message().c_str());
176 // }
177 // m_work_dir = path.c_str();
178 // // LOG_DEBUG("work_dir: %s", m_work_dir.c_str());
179 //
180 // path = llvm::sys::fs::getMainExecutable(nullptr, nullptr);
181 // // LOG_DEBUG("%s", path.c_str());
182 //
183 // llvm::sys::path::remove_filename(path);
184 // m_exec_dir = path.c_str();
185 // // LOG_DEBUG("exec_dir: %s", m_exec_dir.c_str());
186 //
187 // m_search_dir.push_back(m_work_dir);
188 // m_search_dir.push_back(m_exec_dir);
189
190 return true;
191 }
192
194
198
199
200 // Diag();
201 Diag(const Diag&) = delete;
202 const Diag& operator=(const Diag&) = delete;
203
204 };
205
206
207} // namespace example
208
209#endif // NEWLANG_DIAG_H_
static std::string RemoveDiagPrefix(const std::string name)
Definition diag.h:100
static const char * toString(State state)
Definition diag.h:50
static bool IsClang(const std::string name)
Definition diag.h:80
static bool IsEnable(const std::string name)
Definition diag.h:96
bool TestIgnore(const char *name)
Definition diag.cpp:170
void Push(const TermPtr term=nullptr)
Definition diag.cpp:133
Diag(const Diag &)=delete
bool Apply(const char *name, State state, const TermPtr term=nullptr)
Definition diag.cpp:149
void Pop(const TermPtr term=nullptr)
Definition diag.cpp:138
static const char * DIAG_MACRO_NOT_FOUND
Definition diag.h:74
Diag::State Test(const char *name)
Definition diag.cpp:158
std::vector< DataDiag > DiagListType
Definition diag.h:70
bool Emit(const char *name, const TermPtr term=nullptr)
Definition diag.cpp:175
bool Register(const char *name, State state, const char *desc=nullptr)
Definition diag.cpp:95
static const char * DIAG_MACRO_STORAGE_NOT_EXIST
Definition diag.h:75
static bool IsDisable(const std::string name)
Definition diag.h:92
static const char * DIAG_EXTRA_TOKENS
Definition diag.h:77
const Diag & operator=(const Diag &)=delete
bool m_fill_remainder
Definition diag.h:197
static bool IsTurnNoError(const std::string name)
Definition diag.h:88
static const char * DIAG_FILL_REMAINDER
Definition diag.h:78
virtual ~Diag()
Definition diag.h:145
bool ParseArgs(int argc, const char **argv)
Definition diag.h:152
int m_error_limit
Definition diag.h:195
static std::string ChangeState(const std::string name, State from_state, State to_state)
Definition diag.cpp:106
DiagStackType m_diag_stack
Definition diag.h:193
std::vector< DiagListType > DiagStackType
Definition diag.h:71
int m_error_count
Definition diag.h:196
static DiagPtr Init(int argc=0, const char **argv=nullptr)
Definition diag.h:127
static const char * DIAG_ERROR_LIMIT
Definition diag.h:76
static bool IsTurnError(const std::string name)
Definition diag.h:84
#define DIAG_STATE(_)
Definition diag.h:36
#define DEFINE_CASE(name)
int result
Definition lexer.l:367
#define LOG_RUNTIME(format,...)
Definition logger.h:26
#define ASSERT(condition)
Definition logger.h:60
Definition nlc.h:59
std::shared_ptr< Term > TermPtr
Definition variable.h:33
std::shared_ptr< Diag > DiagPtr
Definition types.h:243
Diag::State State
Definition diag.h:66
const char * Desc
Definition diag.h:67
std::string Name
Definition diag.h:65