forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLLVMModuleTest.cpp
321 lines (289 loc) · 12.1 KB
/
LLVMModuleTest.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "ClangTidyOptions.h"
#include "ClangTidyTest.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/HeaderGuardCheck.h"
#include "llvm/IncludeOrderCheck.h"
#include "gtest/gtest.h"
#include <optional>
using namespace clang::tidy::llvm_check;
namespace clang {
namespace tidy {
namespace test {
template <typename T>
static std::string runCheck(StringRef Code, const Twine &Filename,
std::optional<StringRef> ExpectedWarning,
std::map<StringRef, StringRef> PathsToContent =
std::map<StringRef, StringRef>()) {
std::vector<ClangTidyError> Errors;
std::string Result = test::runCheckOnCode<T>(
Code, &Errors, Filename, std::string("-xc++-header"), ClangTidyOptions{},
std::move(PathsToContent));
if (Errors.size() != (size_t)ExpectedWarning.has_value())
return "invalid error count";
if (ExpectedWarning && *ExpectedWarning != Errors.back().Message.Message)
return "expected: '" + ExpectedWarning->str() + "', saw: '" +
Errors.back().Message.Message + "'";
return Result;
}
static std::string
runHeaderGuardCheck(StringRef Code, const Twine &Filename,
std::optional<StringRef> ExpectedWarning) {
return runCheck<LLVMHeaderGuardCheck>(Code, Filename,
std::move(ExpectedWarning));
}
static std::string
runIncludeOrderCheck(StringRef Code, const Twine &Filename,
std::optional<StringRef> ExpectedWarning,
llvm::ArrayRef<llvm::StringLiteral> Includes) {
std::map<StringRef, StringRef> PathsToContent;
for (auto Include : Includes)
PathsToContent.emplace(Include, "");
return runCheck<IncludeOrderCheck>(Code, Filename, std::move(ExpectedWarning),
PathsToContent);
}
namespace {
struct WithEndifComment : public LLVMHeaderGuardCheck {
WithEndifComment(StringRef Name, ClangTidyContext *Context)
: LLVMHeaderGuardCheck(Name, Context) {}
bool shouldSuggestEndifComment(StringRef Filename) override { return true; }
};
static std::string
runHeaderGuardCheckWithEndif(StringRef Code, const Twine &Filename,
std::optional<StringRef> ExpectedWarning) {
return runCheck<WithEndifComment>(Code, Filename, std::move(ExpectedWarning));
}
} // namespace
TEST(LLVMHeaderGuardCheckTest, FixHeaderGuards) {
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif\n",
runHeaderGuardCheck(
"#ifndef FOO\n"
"#define FOO\n"
"#endif\n",
"include/llvm/ADT/foo.h",
StringRef("header guard does not follow preferred style")));
// Allow trailing underscores.
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H_\n"
"#define LLVM_ADT_FOO_H_\n"
"#endif\n",
runHeaderGuardCheck("#ifndef LLVM_ADT_FOO_H_\n"
"#define LLVM_ADT_FOO_H_\n"
"#endif\n",
"include/llvm/ADT/foo.h", std::nullopt));
EXPECT_EQ("#ifndef LLVM_CLANG_C_BAR_H\n"
"#define LLVM_CLANG_C_BAR_H\n"
"\n"
"\n"
"#endif\n",
runHeaderGuardCheck("", "./include/clang-c/bar.h",
StringRef("header is missing header guard")));
EXPECT_EQ("#ifndef LLVM_CLANG_LIB_CODEGEN_C_H\n"
"#define LLVM_CLANG_LIB_CODEGEN_C_H\n"
"\n"
"\n"
"#endif\n",
runHeaderGuardCheck("", "tools/clang/lib/CodeGen/c.h",
StringRef("header is missing header guard")));
EXPECT_EQ("#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_X_H\n"
"#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_X_H\n"
"\n"
"\n"
"#endif\n",
runHeaderGuardCheck("", "tools/clang/tools/extra/clang-tidy/x.h",
StringRef("header is missing header guard")));
EXPECT_EQ(
"int foo;\n"
"#ifndef LLVM_CLANG_BAR_H\n"
"#define LLVM_CLANG_BAR_H\n"
"#endif\n",
runHeaderGuardCheck("int foo;\n"
"#ifndef LLVM_CLANG_BAR_H\n"
"#define LLVM_CLANG_BAR_H\n"
"#endif\n",
"include/clang/bar.h",
StringRef("code/includes outside of area guarded by "
"header guard; consider moving it")));
EXPECT_EQ(
"#ifndef LLVM_CLANG_BAR_H\n"
"#define LLVM_CLANG_BAR_H\n"
"#endif\n"
"int foo;\n",
runHeaderGuardCheck("#ifndef LLVM_CLANG_BAR_H\n"
"#define LLVM_CLANG_BAR_H\n"
"#endif\n"
"int foo;\n",
"include/clang/bar.h",
StringRef("code/includes outside of area guarded by "
"header guard; consider moving it")));
EXPECT_EQ("#ifndef LLVM_CLANG_BAR_H\n"
"#define LLVM_CLANG_BAR_H\n"
"\n"
"int foo;\n"
"#ifndef FOOLOLO\n"
"#define FOOLOLO\n"
"#endif\n"
"\n"
"#endif\n",
runHeaderGuardCheck("int foo;\n"
"#ifndef FOOLOLO\n"
"#define FOOLOLO\n"
"#endif\n",
"include/clang/bar.h",
StringRef("header is missing header guard")));
// Fix incorrect #endif comments even if we shouldn't add new ones.
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif // LLVM_ADT_FOO_H\n",
runHeaderGuardCheck(
"#ifndef FOO\n"
"#define FOO\n"
"#endif // FOO\n",
"include/llvm/ADT/foo.h",
StringRef("header guard does not follow preferred style")));
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif // LLVM_ADT_FOO_H\n",
runHeaderGuardCheckWithEndif(
"#ifndef FOO\n"
"#define FOO\n"
"#endif\n",
"include/llvm/ADT/foo.h",
StringRef("header guard does not follow preferred style")));
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif // LLVM_ADT_FOO_H\n",
runHeaderGuardCheckWithEndif(
"#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif // LLVM_H\n",
"include/llvm/ADT/foo.h",
StringRef("#endif for a header guard should reference the "
"guard macro in a comment")));
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif /* LLVM_ADT_FOO_H */\n",
runHeaderGuardCheckWithEndif("#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif /* LLVM_ADT_FOO_H */\n",
"include/llvm/ADT/foo.h",
std::nullopt));
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H_\n"
"#define LLVM_ADT_FOO_H_\n"
"#endif // LLVM_ADT_FOO_H_\n",
runHeaderGuardCheckWithEndif("#ifndef LLVM_ADT_FOO_H_\n"
"#define LLVM_ADT_FOO_H_\n"
"#endif // LLVM_ADT_FOO_H_\n",
"include/llvm/ADT/foo.h",
std::nullopt));
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif // LLVM_ADT_FOO_H\n",
runHeaderGuardCheckWithEndif(
"#ifndef LLVM_ADT_FOO_H_\n"
"#define LLVM_ADT_FOO_H_\n"
"#endif // LLVM\n",
"include/llvm/ADT/foo.h",
StringRef("header guard does not follow preferred style")));
// An extra space inside the comment is OK.
llvm::StringRef WithExtraSpace = "#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif // LLVM_ADT_FOO_H\n";
EXPECT_EQ(WithExtraSpace,
runHeaderGuardCheckWithEndif(
WithExtraSpace, "include/llvm/ADT/foo.h", std::nullopt));
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif \\ \n"
"// LLVM_ADT_FOO_H\n",
runHeaderGuardCheckWithEndif(
"#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif \\ \n"
"// LLVM_ADT_FOO_H\n",
"include/llvm/ADT/foo.h",
StringRef("backslash and newline separated by space")));
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif /* LLVM_ADT_FOO_H\\ \n"
" FOO */",
runHeaderGuardCheckWithEndif("#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif /* LLVM_ADT_FOO_H\\ \n"
" FOO */",
"include/llvm/ADT/foo.h",
std::nullopt));
EXPECT_EQ("#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_FOO_H\n"
"#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_FOO_H\n"
"\n"
"\n"
"#endif\n",
runHeaderGuardCheck(
"", "/llvm-project/clang-tools-extra/clangd/foo.h",
StringRef("header is missing header guard")));
// Substitution of characters should not result in a header guard starting
// with "_".
EXPECT_EQ("#ifndef BAR_H\n"
"#define BAR_H\n"
"\n"
"\n"
"#endif\n",
runHeaderGuardCheck("", "include/--bar.h",
StringRef("header is missing header guard")));
#ifdef WIN32
// Check interaction with Windows-style path separators (\).
EXPECT_EQ(
"#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_FOO_H\n"
"#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_FOO_H\n"
"\n"
"\n"
"#endif\n",
runHeaderGuardCheck("", "llvm-project\\clang-tools-extra\\clangd\\foo.h",
StringRef("header is missing header guard")));
EXPECT_EQ("#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_FOO_H\n"
"#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_FOO_H\n"
"\n"
"\n"
"#endif\n",
runHeaderGuardCheck(
"", "C:\\llvm-project\\clang-tools-extra\\clangd\\foo.h",
StringRef("header is missing header guard")));
EXPECT_EQ("#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_FOO_H\n"
"#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_FOO_H\n"
"\n"
"\n"
"#endif\n",
runHeaderGuardCheck(
"",
"\\\\SMBShare\\llvm-project\\clang-tools-extra\\clangd\\foo.h",
StringRef("header is missing header guard")));
EXPECT_EQ("#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_FOO_H\n"
"#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_FOO_H\n"
"\n"
"\n"
"#endif\n",
runHeaderGuardCheck(
"", "\\\\?\\C:\\llvm-project\\clang-tools-extra\\clangd\\foo.h",
StringRef("header is missing header guard")));
#endif
}
TEST(IncludeOrderCheck, GTestHeaders) {
EXPECT_EQ(
R"cpp(
#include "foo.h"
#include "llvm/foo.h"
#include "gtest/foo.h"
#include <algorithm>)cpp",
runIncludeOrderCheck(
R"cpp(
#include "foo.h"
#include "llvm/foo.h"
#include <algorithm>
#include "gtest/foo.h")cpp",
"foo.cc", StringRef("#includes are not sorted properly"),
{"foo.h", "algorithm", "gtest/foo.h", "llvm/foo.h"}));
}
} // namespace test
} // namespace tidy
} // namespace clang