-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclang-util-test.cc
428 lines (349 loc) · 10.1 KB
/
clang-util-test.cc
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// clang-util-test.cc
// Tests for `clang-util` module.
#include "clang-util.h" // module under test
// this dir
#include "clang-ast.h" // ClangASTUtil, ClangASTUtilTempFile
#include "clang-test-visitor.h" // ClangTestVisitor
#include "symbolic-line-mapper.h" // SymbolicLineMapper
// smbase
#include "smbase/gdvalue.h" // gdv::GDValue
#include "smbase/sm-macros.h" // OPEN_ANONYMOUS_NAMESPACE
#include "smbase/sm-test.h" // EXPECT_EQ
#include "smbase/sm-trace.h" // INIT_TRACE, etc.
// clang
#include "clang/AST/Decl.h" // clang::NamedDecl
// libc++
#include <sstream> // std::ostringstream
using namespace gdv;
using namespace smbase;
using clang::dyn_cast;
INIT_TRACE("clang-util-test");
OPEN_ANONYMOUS_NAMESPACE
// Visitor that calls `getInstFromDeclOpt` on every `Decl` node and
// accumulates the results.
class GIFDVisitor : public ClangTestVisitor {
public: // types
using Base = ClangTestVisitor;
public: // methods
GIFDVisitor(clang::ASTContext &astContext)
: ClangTestVisitor(astContext)
{}
// ClangTestVisitor methods
virtual void visitDecl(
VisitDeclContext context,
clang::Decl const *decl) override;
};
void GIFDVisitor::visitDecl(
VisitDeclContext context,
clang::Decl const *decl)
{
TRACE2_SCOPED("GIFDVisitor::visitDecl: " << declKindAtLocStr(decl));
if (auto namedDecl = dyn_cast<clang::NamedDecl>(decl)) {
// For some reason I have two functions that do almost the same
// thing...
TRACE3("compactId: " << namedDeclCompactIdentifier(namedDecl));
TRACE3("namedDeclStr: " << namedDeclStr(namedDecl));
if (auto instFrom = getInstFromDeclOpt(namedDecl)) {
m_actual.setInsert(GDVTuple({
// Name and definition-ness of the instantiation.
namedDeclStr(namedDecl),
GDValue::makeBool(isThisDeclarationADefinition(namedDecl)),
// Name and definition-ness of the template.
namedDeclStr(instFrom),
GDValue::makeBool(isThisDeclarationADefinition(instFrom)),
}));
}
else {
// Do not add anything if the call returns `nullptr`.
}
}
Base::visitDecl(context, decl);
}
void testOneGetInstFromDeclOpt(char const *source, GDValue const &expect)
{
ClangASTUtilTempFile ast(source);
GIFDVisitor(ast.getASTContext()).scanTUExpect(expect);
}
void testGetInstFromDeclOpt()
{
GDValue const T = GDValue::makeBool(true);
GDValue const F = GDValue::makeBool(false);
// Simple example of a function template.
testOneGetInstFromDeclOpt(
R"(
template <typename T>
void f() {}
void g()
{
f<int>();
f<float>();
}
)",
GDValue(GDVSet{
GDVTuple{"f<int>()", T, "f<T>()", T},
GDVTuple{"f<float>()", T, "f<T>()", T},
})
);
// Simple function template that is not defined.
testOneGetInstFromDeclOpt(
R"(
template <typename T>
void f();
void g()
{
f<int>();
f<float>();
}
)",
GDValue(GDVSet{
GDVTuple{"f<int>()", F, "f<T>()", F},
GDVTuple{"f<float>()", F, "f<T>()", F},
})
);
// Simple example of a class template.
testOneGetInstFromDeclOpt(
R"(
template <typename T>
struct S {};
void g()
{
S<int> s1;
S<float> s2;
}
)",
GDValue(GDVSet{
GDVTuple{"S<int>", T, "S<T>", T},
GDVTuple{"S<float>", T, "S<T>", T},
})
);
// Class template contains members that get instantiated.
testOneGetInstFromDeclOpt(
R"(
template <typename T>
struct S {
void m1();
void m2() {} // Body instantiated for `int`.
void m3() {} // Body instantiated for `float`.
};
void g()
{
S<int> s1;
s1.m2();
S<float> s2;
s2.m3();
}
)",
GDValue(GDVSet{
// The "S" in "S::" does not have the template parameters because
// it results from calling `NamedDecl::getQualifiedNameAsString`,
// whereas the other "<T>" instances come from my own code.
//
// Note how the template definition-ness depends only on whether
// a definition is syntactically present, while the instantiation
// definition-ness depends on which parts get instantiated.
GDVTuple{"S<int>", T, "S<T>", T},
GDVTuple{"S<int>::m1()", F, "S::m1()", F},
GDVTuple{"S<int>::m2()", T, "S::m2()", T},
GDVTuple{"S<int>::m3()", F, "S::m3()", T},
GDVTuple{"S<float>", T, "S<T>", T},
GDVTuple{"S<float>::m1()", F, "S::m1()", F},
GDVTuple{"S<float>::m2()", F, "S::m2()", T},
GDVTuple{"S<float>::m3()", T, "S::m3()", T},
})
);
// Class template contains function template.
testOneGetInstFromDeclOpt(
R"(
template <typename T>
struct S {
template <typename U>
void m1() {}
};
void g()
{
S<int> s1;
s1.m1<float>();
}
)",
GDValue(GDVSet{
GDVTuple{"S<int>", T, "S<T>", T},
GDVTuple{"S<int>::m1<float>()", T, "S::m1<U>()", T},
})
);
// Class template contains ordinary class.
testOneGetInstFromDeclOpt(
R"(
template <typename T>
struct S {
struct Inner {
void m() {}
};
};
void g()
{
S<int>::Inner i;
i.m();
}
)",
GDValue(GDVSet{
GDVTuple{"S<int>", T, "S<T>", T},
GDVTuple{"S<int>::Inner", T, "S::Inner", T},
GDVTuple{"S<int>::Inner::m()", T, "S::Inner::m()", T},
})
);
// Class template contains class template.
testOneGetInstFromDeclOpt(
R"(
template <typename T>
struct S {
template <typename U>
struct Inner {
void m1() {}
void m2() {}
};
};
void g()
{
S<int>::Inner<float> i;
i.m1();
}
)",
GDValue(GDVSet{
GDVTuple{"S<int>", T, "S<T>", T},
GDVTuple{"S<int>::Inner<float>", T, "S::Inner<U>", T},
GDVTuple{"S<int>::Inner<float>::m1()", T, "S::Inner::m1()", T},
GDVTuple{"S<int>::Inner<float>::m2()", F, "S::Inner::m2()", T},
})
);
// Variable template.
//
// One thing this tests is that we do not regard `v<N>` as being
// instantiated from itself, which is what happens if we naively call
// `VarDecl::getTemplateInstantiationPattern`.
//
testOneGetInstFromDeclOpt(
R"(
template <int N>
int v = N;
int f()
{
return v<3>;
}
)",
GDValue(GDVSet{
GDVTuple{"v<3>", T, "v<N>", T},
})
);
}
// Visitor that calls `getDeclLoc` on every `Decl` node and accumulates
// the results.
class DeclLocVisitor : public ClangTestVisitor {
public: // types
using Base = ClangTestVisitor;
public: // data
// Enable symbolic line numbers.
SymbolicLineMapper m_symLineMapper;
public: // methods
DeclLocVisitor(clang::ASTContext &astContext)
: ClangTestVisitor(astContext),
m_symLineMapper(astContext)
{}
// ClangTestVisitor methods
virtual void visitDecl(
VisitDeclContext context,
clang::Decl const *decl) override;
};
void DeclLocVisitor::visitDecl(
VisitDeclContext context,
clang::Decl const *decl)
{
TRACE2("DeclLocVisitor::visitDecl: " << declKindAtLocStr(decl));
clang::SourceLocation loc = declLoc(decl);
// Get the basename of all referenced files.
m_actual.setInsert(basenameOfLocFile(loc));
// Get extra information about CTSDs.
if (auto ctsd = dyn_cast<clang::ClassTemplateSpecializationDecl>(decl)) {
clang::NamedDecl const * NULLABLE instFromDecl =
getInstFromDeclOpt(ctsd);
clang::NamedDecl const * NULLABLE instFromDefn =
getDefnForDeclOpt(instFromDecl);
// This serves as a crude test of `getDefnOrSelfForDecl`.
if (instFromDecl) {
xassert(getDefnOrSelfForDecl(instFromDecl) ==
(instFromDefn? instFromDefn : instFromDecl));
}
m_actual.setInsert(GDVTuple({
namedDeclStr(ctsd),
m_symLineMapper.symLineColStr(loc),
m_symLineMapper.symLineColStr(declLocOpt(instFromDecl)),
m_symLineMapper.symLineColStr(declLocOpt(instFromDefn)),
}));
}
Base::visitDecl(context, decl);
}
void testOneDeclLoc(char const *fname, GDValue const &expect)
{
ClangASTUtil ast({fname});
DeclLocVisitor(ast.getASTContext()).scanTUExpect(expect);
}
void testDeclLoc()
{
testOneDeclLoc("in/src/use-template-via-typedef.cc", GDValue(GDVSet{
"<none>",
"use-template-via-typedef.cc",
GDVTuple{"S<int>", "S_defn:1", "S_decl:1", "S_defn:1"},
}));
testOneDeclLoc("in/src/macro-expansion.cc", GDValue(GDVSet{
"<none>",
"macro-definition.h",
"macro-expansion.cc",
}));
}
// Test `getLoc`, `getMainFileLoc`, `locLine`, and `locCol`.
//
// Also test `locAfterToken`.
void testGetLoc()
{
// Note that lines 2 and 3 start in column 5.
ClangASTUtilTempFile ast(R"(// line 1
int x;
// line 3
)");
clang::SourceLocation loc = ast.getMainFileLoc(2,3);
xassert(ast.locLine(loc) == 2);
xassert(ast.locCol(loc) == 3);
// `int`.
EXPECT_EQ(ast.locLineColStr(ast.locAfterToken(ast.getMainFileLoc(2,5))),
"2:8");
// `x`.
EXPECT_EQ(ast.locLineColStr(ast.locAfterToken(ast.getMainFileLoc(2,9))),
"2:10");
// `;`.
EXPECT_EQ(ast.locLineColStr(ast.locAfterToken(ast.getMainFileLoc(2,10))),
"2:11");
}
// Test `tuSyntaxStr`, which calls `declSyntaxStr`.
void testTUSyntaxStr()
{
// This input was chosen simply because it is the same input I am
// using in `header-analysis/name-anon-tparams-test.cc`.
ClangASTUtilTempFile ast(R"(
template <typename A, bool>
struct S {};
)");
// I have not made any effort to tweak how the output is printed. The
// string here is just how it prints at the moment.
EXPECT_EQ(ast.tuSyntaxStr(), R"(template <typename A, bool> struct S {
};
)");
}
CLOSE_ANONYMOUS_NAMESPACE
// Called from pca-unit-tests.cc.
void clang_util_unit_tests()
{
testGetInstFromDeclOpt();
testDeclLoc();
testGetLoc();
testTUSyntaxStr();
}
// EOF