Coverage Report

Created: 2018-11-16 02:38

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/Analysis/Analyses/ThreadSafetyTraverse.h
Line
Count
Source (jump to first uncovered line)
1
//===- ThreadSafetyTraverse.h -----------------------------------*- C++ -*-===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This file defines a framework for doing generic traversals and rewriting
11
// operations over the Thread Safety TIL.
12
//
13
// UNDER CONSTRUCTION.  USE AT YOUR OWN RISK.
14
//
15
//===----------------------------------------------------------------------===//
16
17
#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETYTRAVERSE_H
18
#define LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETYTRAVERSE_H
19
20
#include "clang/AST/Decl.h"
21
#include "clang/Analysis/Analyses/ThreadSafetyTIL.h"
22
#include "clang/Analysis/Analyses/ThreadSafetyUtil.h"
23
#include "clang/Basic/LLVM.h"
24
#include "llvm/ADT/StringRef.h"
25
#include "llvm/Support/Casting.h"
26
#include <cstdint>
27
#include <ostream>
28
29
namespace clang {
30
namespace threadSafety {
31
namespace til {
32
33
// Defines an interface used to traverse SExprs.  Traversals have been made as
34
// generic as possible, and are intended to handle any kind of pass over the
35
// AST, e.g. visitors, copying, non-destructive rewriting, destructive
36
// (in-place) rewriting, hashing, typing, etc.
37
//
38
// Traversals implement the functional notion of a "fold" operation on SExprs.
39
// Each SExpr class provides a traverse method, which does the following:
40
//   * e->traverse(v):
41
//       // compute a result r_i for each subexpression e_i
42
//       for (i = 1..n)  r_i = v.traverse(e_i);
43
//       // combine results into a result for e,  where X is the class of e
44
//       return v.reduceX(*e, r_1, .. r_n).
45
//
46
// A visitor can control the traversal by overriding the following methods:
47
//   * v.traverse(e):
48
//       return v.traverseByCase(e), which returns v.traverseX(e)
49
//   * v.traverseX(e):   (X is the class of e)
50
//       return e->traverse(v).
51
//   * v.reduceX(*e, r_1, .. r_n):
52
//       compute a result for a node of type X
53
//
54
// The reduceX methods control the kind of traversal (visitor, copy, etc.).
55
// They are defined in derived classes.
56
//
57
// Class R defines the basic interface types (R_SExpr).
58
template <class Self, class R>
59
class Traversal {
60
public:
61
  Self *self() { return static_cast<Self *>(this); }
62
63
  // Traverse an expression -- returning a result of type R_SExpr.
64
  // Override this method to do something for every expression, regardless
65
  // of which kind it is.
66
  // E is a reference, so this can be use for in-place updates.
67
  // The type T must be a subclass of SExpr.
68
  template <class T>
69
  typename R::R_SExpr traverse(T* &E, typename R::R_Ctx Ctx) {
70
    return traverseSExpr(E, Ctx);
71
  }
72
73
  // Override this method to do something for every expression.
74
  // Does not allow in-place updates.
75
  typename R::R_SExpr traverseSExpr(SExpr *E, typename R::R_Ctx Ctx) {
76
    return traverseByCase(E, Ctx);
77
  }
78
79
  // Helper method to call traverseX(e) on the appropriate type.
80
  typename R::R_SExpr traverseByCase(SExpr *E, typename R::R_Ctx Ctx) {
81
    switch (E->opcode()) {
82
#define TIL_OPCODE_DEF(X)                                                   \
83
    case COP_##X:                                                           \
84
      return self()->traverse##X(cast<X>(E), Ctx);
85
#include "ThreadSafetyOps.def"
86
#undef TIL_OPCODE_DEF
87
    }
88
    return self()->reduceNull();
89
  }
90
91
// Traverse e, by static dispatch on the type "X" of e.
92
// Override these methods to do something for a particular kind of term.
93
#define TIL_OPCODE_DEF(X)                                                   \
94
  typename R::R_SExpr traverse##X(X *e, typename R::R_Ctx Ctx) {            \
95
    return e->traverse(*self(), Ctx);                                       \
96
  }
97
#include "ThreadSafetyOps.def"
98
#undef TIL_OPCODE_DEF
99
};
100
101
// Base class for simple reducers that don't much care about the context.
102
class SimpleReducerBase {
103
public:
104
  enum TraversalKind {
105
    // Ordinary subexpressions.
106
    TRV_Normal,
107
108
    // Declarations (e.g. function bodies).
109
    TRV_Decl,
110
111
    // Expressions that require lazy evaluation.
112
    TRV_Lazy,
113
114
    // Type expressions.
115
    TRV_Type
116
  };
117
118
  // R_Ctx defines a "context" for the traversal, which encodes information
119
  // about where a term appears.  This can be used to encoding the
120
  // "current continuation" for CPS transforms, or other information.
121
  using R_Ctx = TraversalKind;
122
123
  // Create context for an ordinary subexpression.
124
  R_Ctx subExprCtx(R_Ctx Ctx) { return TRV_Normal; }
125
126
  // Create context for a subexpression that occurs in a declaration position
127
  // (e.g. function body).
128
  R_Ctx declCtx(R_Ctx Ctx) { return TRV_Decl; }
129
130
  // Create context for a subexpression that occurs in a position that
131
  // should be reduced lazily.  (e.g. code body).
132
  R_Ctx lazyCtx(R_Ctx Ctx) { return TRV_Lazy; }
133
134
  // Create context for a subexpression that occurs in a type position.
135
  R_Ctx typeCtx(R_Ctx Ctx) { return TRV_Type; }
136
};
137
138
// Base class for traversals that rewrite an SExpr to another SExpr.
139
class CopyReducerBase : public SimpleReducerBase {
140
public:
141
  // R_SExpr is the result type for a traversal.
142
  // A copy or non-destructive rewrite returns a newly allocated term.
143
  using R_SExpr = SExpr *;
144
  using R_BasicBlock = BasicBlock *;
145
146
  // Container is a minimal interface used to store results when traversing
147
  // SExprs of variable arity, such as Phi, Goto, and SCFG.
148
  template <class T> class Container {
149
  public:
150
    // Allocate a new container with a capacity for n elements.
151
    Container(CopyReducerBase &S, unsigned N) : Elems(S.Arena, N) {}
152
153
    // Push a new element onto the container.
154
    void push_back(T E) { Elems.push_back(E); }
155
156
    SimpleArray<T> Elems;
157
  };
158
159
  CopyReducerBase(MemRegionRef A) : Arena(A) {}
160
161
protected:
162
  MemRegionRef Arena;
163
};
164
165
// Base class for visit traversals.
166
class VisitReducerBase : public SimpleReducerBase {
167
public:
168
  // A visitor returns a bool, representing success or failure.
169
  using R_SExpr = bool;
170
  using R_BasicBlock = bool;
171
172
  // A visitor "container" is a single bool, which accumulates success.
173
  template <class T> class Container {
174
  public:
175
    bool Success = true;
176
177
    Container(VisitReducerBase &S, unsigned N) {}
178
179
    void push_back(bool E) { Success = Success && E; }
180
  };
181
};
182
183
// Implements a traversal that visits each subexpression, and returns either
184
// true or false.
185
template <class Self>
186
class VisitReducer : public Traversal<Self, VisitReducerBase>,
187
                     public VisitReducerBase {
188
public:
189
  VisitReducer() = default;
190
191
public:
192
  R_SExpr reduceNull() { return true; }
193
  R_SExpr reduceUndefined(Undefined &Orig) { return true; }
194
  R_SExpr reduceWildcard(Wildcard &Orig) { return true; }
195
196
  R_SExpr reduceLiteral(Literal &Orig) { return true; }
197
  template<class T>
198
  R_SExpr reduceLiteralT(LiteralT<T> &Orig) { return true; }
199
  R_SExpr reduceLiteralPtr(Literal &Orig) { return true; }
200
201
  R_SExpr reduceFunction(Function &Orig, Variable *Nvd, R_SExpr E0) {
202
    return Nvd && E0;
203
  }
204
205
  R_SExpr reduceSFunction(SFunction &Orig, Variable *Nvd, R_SExpr E0) {
206
    return Nvd && E0;
207
  }
208
209
  R_SExpr reduceCode(Code &Orig, R_SExpr E0, R_SExpr E1) {
210
    return E0 && E1;
211
  }
212
213
  R_SExpr reduceField(Field &Orig, R_SExpr E0, R_SExpr E1) {
214
    return E0 && E1;
215
  }
216
217
  R_SExpr reduceApply(Apply &Orig, R_SExpr E0, R_SExpr E1) {
218
    return E0 && E1;
219
  }
220
221
  R_SExpr reduceSApply(SApply &Orig, R_SExpr E0, R_SExpr E1) {
222
    return E0 && E1;
223
  }
224
225
  R_SExpr reduceProject(Project &Orig, R_SExpr E0) { return E0; }
226
  R_SExpr reduceCall(Call &Orig, R_SExpr E0) { return E0; }
227
  R_SExpr reduceAlloc(Alloc &Orig, R_SExpr E0) { return E0; }
228
  R_SExpr reduceLoad(Load &Orig, R_SExpr E0) { return E0; }
229
  R_SExpr reduceStore(Store &Orig, R_SExpr E0, R_SExpr E1) { return E0 && E1; }
230
231
  R_SExpr reduceArrayIndex(Store &Orig, R_SExpr E0, R_SExpr E1) {
232
    return E0 && E1;
233
  }
234
235
  R_SExpr reduceArrayAdd(Store &Orig, R_SExpr E0, R_SExpr E1) {
236
    return E0 && E1;
237
  }
238
239
  R_SExpr reduceUnaryOp(UnaryOp &Orig, R_SExpr E0) { return E0; }
240
241
  R_SExpr reduceBinaryOp(BinaryOp &Orig, R_SExpr E0, R_SExpr E1) {
242
    return E0 && E1;
243
  }
244
245
  R_SExpr reduceCast(Cast &Orig, R_SExpr E0) { return E0; }
246
247
  R_SExpr reduceSCFG(SCFG &Orig, Container<BasicBlock *> Bbs) {
248
    return Bbs.Success;
249
  }
250
251
  R_BasicBlock reduceBasicBlock(BasicBlock &Orig, Container<R_SExpr> &As,
252
                                Container<R_SExpr> &Is, R_SExpr T) {
253
    return (As.Success && Is.Success && T);
254
  }
255
256
  R_SExpr reducePhi(Phi &Orig, Container<R_SExpr> &As) {
257
    return As.Success;
258
  }
259
260
  R_SExpr reduceGoto(Goto &Orig, BasicBlock *B) {
261
    return true;
262
  }
263
264
  R_SExpr reduceBranch(Branch &O, R_SExpr C, BasicBlock *B0, BasicBlock *B1) {
265
    return C;
266
  }
267
268
  R_SExpr reduceReturn(Return &O, R_SExpr E) {
269
    return E;
270
  }
271
272
  R_SExpr reduceIdentifier(Identifier &Orig) {
273
    return true;
274
  }
275
276
  R_SExpr reduceIfThenElse(IfThenElse &Orig, R_SExpr C, R_SExpr T, R_SExpr E) {
277
    return C && T && E;
278
  }
279
280
  R_SExpr reduceLet(Let &Orig, Variable *Nvd, R_SExpr B) {
281
    return Nvd && B;
282
  }
283
284
  Variable *enterScope(Variable &Orig, R_SExpr E0) { return &Orig; }
285
  void exitScope(const Variable &Orig) {}
286
  void enterCFG(SCFG &Cfg) {}
287
  void exitCFG(SCFG &Cfg) {}
288
  void enterBasicBlock(BasicBlock &BB) {}
289
  void exitBasicBlock(BasicBlock &BB) {}
290
291
  Variable *reduceVariableRef(Variable *Ovd) { return Ovd; }
292
  BasicBlock *reduceBasicBlockRef(BasicBlock *Obb) { return Obb; }
293
294
public:
295
  bool traverse(SExpr *E, TraversalKind K = TRV_Normal) {
296
    Success = Success && this->traverseByCase(E);
297
    return Success;
298
  }
299
300
  static bool visit(SExpr *E) {
301
    Self Visitor;
302
    return Visitor.traverse(E, TRV_Normal);
303
  }
304
305
private:
306
  bool Success;
307
};
308
309
// Basic class for comparison operations over expressions.
310
template <typename Self>
311
class Comparator {
312
protected:
313
46.2k
  Self *self() { return reinterpret_cast<Self *>(this); }
clang::threadSafety::til::Comparator<clang::threadSafety::til::EqualsComparator>::self()
Line
Count
Source
313
4.12k
  Self *self() { return reinterpret_cast<Self *>(this); }
clang::threadSafety::til::Comparator<clang::threadSafety::til::MatchComparator>::self()
Line
Count
Source
313
42.1k
  Self *self() { return reinterpret_cast<Self *>(this); }
314
315
public:
316
46.2k
  bool compareByCase(const SExpr *E1, const SExpr* E2) {
317
46.2k
    switch (E1->opcode()) {
318
46.2k
#define TIL_OPCODE_DEF(X)                                                     \
319
46.2k
    case COP_##X:                                                             \
320
46.2k
      return cast<X>(E1)->compare(cast<X>(E2), *self());
321
46.2k
#include 
"ThreadSafetyOps.def"0
322
46.2k
#undef TIL_OPCODE_DEF
323
46.2k
    }
324
46.2k
    
return false0
;
325
46.2k
  }
clang::threadSafety::til::Comparator<clang::threadSafety::til::EqualsComparator>::compareByCase(clang::threadSafety::til::SExpr const*, clang::threadSafety::til::SExpr const*)
Line
Count
Source
316
4.12k
  bool compareByCase(const SExpr *E1, const SExpr* E2) {
317
4.12k
    switch (E1->opcode()) {
318
4.12k
#define TIL_OPCODE_DEF(X)                                                     \
319
4.12k
    case COP_##X:                                                             \
320
4.12k
      return cast<X>(E1)->compare(cast<X>(E2), *self());
321
4.12k
#include 
"ThreadSafetyOps.def"0
322
4.12k
#undef TIL_OPCODE_DEF
323
4.12k
    }
324
4.12k
    
return false0
;
325
4.12k
  }
clang::threadSafety::til::Comparator<clang::threadSafety::til::MatchComparator>::compareByCase(clang::threadSafety::til::SExpr const*, clang::threadSafety::til::SExpr const*)
Line
Count
Source
316
42.1k
  bool compareByCase(const SExpr *E1, const SExpr* E2) {
317
42.1k
    switch (E1->opcode()) {
318
42.1k
#define TIL_OPCODE_DEF(X)                                                     \
319
42.1k
    case COP_##X:                                                             \
320
42.1k
      return cast<X>(E1)->compare(cast<X>(E2), *self());
321
42.1k
#include 
"ThreadSafetyOps.def"0
322
42.1k
#undef TIL_OPCODE_DEF
323
42.1k
    }
324
42.1k
    
return false0
;
325
42.1k
  }
326
};
327
328
class EqualsComparator : public Comparator<EqualsComparator> {
329
public:
330
  // Result type for the comparison, e.g. bool for simple equality,
331
  // or int for lexigraphic comparison (-1, 0, 1).  Must have one value which
332
  // denotes "true".
333
  using CType = bool;
334
335
0
  CType trueResult() { return true; }
336
2.06k
  bool notTrue(CType ct) { return !ct; }
337
338
0
  bool compareIntegers(unsigned i, unsigned j) { return i == j; }
339
0
  bool compareStrings (StringRef s, StringRef r) { return s == r; }
340
2.24k
  bool comparePointers(const void* P, const void* Q) { return P == Q; }
341
342
4.17k
  bool compare(const SExpr *E1, const SExpr* E2) {
343
4.17k
    if (E1->opcode() != E2->opcode())
344
48
      return false;
345
4.12k
    return compareByCase(E1, E2);
346
4.12k
  }
347
348
  // TODO -- handle alpha-renaming of variables
349
0
  void enterScope(const Variable *V1, const Variable *V2) {}
350
0
  void leaveScope() {}
351
352
832
  bool compareVariableRefs(const Variable *V1, const Variable *V2) {
353
832
    return V1 == V2;
354
832
  }
355
356
1.09k
  static bool compareExprs(const SExpr *E1, const SExpr* E2) {
357
1.09k
    EqualsComparator Eq;
358
1.09k
    return Eq.compare(E1, E2);
359
1.09k
  }
360
};
361
362
class MatchComparator : public Comparator<MatchComparator> {
363
public:
364
  // Result type for the comparison, e.g. bool for simple equality,
365
  // or int for lexigraphic comparison (-1, 0, 1).  Must have one value which
366
  // denotes "true".
367
  using CType = bool;
368
369
392
  CType trueResult() { return true; }
370
21.1k
  bool notTrue(CType ct) { return !ct; }
371
372
428
  bool compareIntegers(unsigned i, unsigned j) { return i == j; }
373
0
  bool compareStrings (StringRef s, StringRef r) { return s == r; }
374
17.1k
  bool comparePointers(const void *P, const void *Q) { return P == Q; }
375
376
43.7k
  bool compare(const SExpr *E1, const SExpr *E2) {
377
43.7k
    // Wildcards match anything.
378
43.7k
    if (E1->opcode() == COP_Wildcard || 
E2->opcode() == COP_Wildcard43.7k
)
379
144
      return true;
380
43.6k
    // otherwise normal equality.
381
43.6k
    if (E1->opcode() != E2->opcode())
382
1.51k
      return false;
383
42.1k
    return compareByCase(E1, E2);
384
42.1k
  }
385
386
  // TODO -- handle alpha-renaming of variables
387
0
  void enterScope(const Variable* V1, const Variable* V2) {}
388
0
  void leaveScope() {}
389
390
10.7k
  bool compareVariableRefs(const Variable* V1, const Variable* V2) {
391
10.7k
    return V1 == V2;
392
10.7k
  }
393
394
11.6k
  static bool compareExprs(const SExpr *E1, const SExpr* E2) {
395
11.6k
    MatchComparator Matcher;
396
11.6k
    return Matcher.compare(E1, E2);
397
11.6k
  }
398
};
399
400
// inline std::ostream& operator<<(std::ostream& SS, StringRef R) {
401
//   return SS.write(R.data(), R.size());
402
// }
403
404
// Pretty printer for TIL expressions
405
template <typename Self, typename StreamType>
406
class PrettyPrinter {
407
private:
408
  // Print out additional information.
409
  bool Verbose;
410
411
  // Omit redundant decls.
412
  bool Cleanup;
413
414
  // Print exprs in C-like syntax.
415
  bool CStyle;
416
417
public:
418
  PrettyPrinter(bool V = false, bool C = true, bool CS = true)
419
2.97k
      : Verbose(V), Cleanup(C), CStyle(CS) {}
420
421
2.97k
  static void print(const SExpr *E, StreamType &SS) {
422
2.97k
    Self printer;
423
2.97k
    printer.printSExpr(E, SS, Prec_MAX);
424
2.97k
  }
425
426
protected:
427
9.59k
  Self *self() { return reinterpret_cast<Self *>(this); }
428
429
0
  void newline(StreamType &SS) {
430
0
    SS << "\n";
431
0
  }
432
433
  // TODO: further distinguish between binary operations.
434
  static const unsigned Prec_Atom = 0;
435
  static const unsigned Prec_Postfix = 1;
436
  static const unsigned Prec_Unary = 2;
437
  static const unsigned Prec_Binary = 3;
438
  static const unsigned Prec_Other = 4;
439
  static const unsigned Prec_Decl = 5;
440
  static const unsigned Prec_MAX = 6;
441
442
  // Return the precedence of a given node, for use in pretty printing.
443
4.19k
  unsigned precedence(const SExpr *E) {
444
4.19k
    switch (E->opcode()) {
445
4.19k
      
case COP_Future: return Prec_Atom0
;
446
4.19k
      
case COP_Undefined: return Prec_Atom0
;
447
4.19k
      
case COP_Wildcard: return Prec_Atom4
;
448
4.19k
449
4.19k
      
case COP_Literal: return Prec_Atom12
;
450
4.19k
      
case COP_LiteralPtr: return Prec_Atom855
;
451
4.19k
      
case COP_Variable: return Prec_Atom4
;
452
4.19k
      
case COP_Function: return Prec_Decl0
;
453
4.19k
      
case COP_SFunction: return Prec_Decl0
;
454
4.19k
      
case COP_Code: return Prec_Decl0
;
455
4.19k
      
case COP_Field: return Prec_Decl0
;
456
4.19k
457
4.19k
      
case COP_Apply: return Prec_Postfix0
;
458
4.19k
      
case COP_SApply: return Prec_Postfix518
;
459
4.19k
      
case COP_Project: return Prec_Postfix2.69k
;
460
4.19k
461
4.19k
      
case COP_Call: return Prec_Postfix36
;
462
4.19k
      
case COP_Alloc: return Prec_Other0
;
463
4.19k
      
case COP_Load: return Prec_Postfix0
;
464
4.19k
      
case COP_Store: return Prec_Other0
;
465
4.19k
      
case COP_ArrayIndex: return Prec_Postfix16
;
466
4.19k
      
case COP_ArrayAdd: return Prec_Postfix0
;
467
4.19k
468
4.19k
      
case COP_UnaryOp: return Prec_Unary0
;
469
4.19k
      
case COP_BinaryOp: return Prec_Binary16
;
470
4.19k
      
case COP_Cast: return Prec_Atom24
;
471
4.19k
472
4.19k
      
case COP_SCFG: return Prec_Decl0
;
473
4.19k
      
case COP_BasicBlock: return Prec_MAX0
;
474
4.19k
      
case COP_Phi: return Prec_Atom0
;
475
4.19k
      
case COP_Goto: return Prec_Atom0
;
476
4.19k
      
case COP_Branch: return Prec_Atom0
;
477
4.19k
      
case COP_Return: return Prec_Other0
;
478
4.19k
479
4.19k
      
case COP_Identifier: return Prec_Atom0
;
480
4.19k
      
case COP_IfThenElse: return Prec_Other16
;
481
4.19k
      
case COP_Let: return Prec_Decl0
;
482
0
    }
483
0
    return Prec_MAX;
484
0
  }
485
486
0
  void printBlockLabel(StreamType & SS, const BasicBlock *BB, int index) {
487
0
    if (!BB) {
488
0
      SS << "BB_null";
489
0
      return;
490
0
    }
491
0
    SS << "BB_";
492
0
    SS << BB->blockID();
493
0
    if (index >= 0) {
494
0
      SS << ":";
495
0
      SS << index;
496
0
    }
497
0
  }
498
499
4.19k
  void printSExpr(const SExpr *E, StreamType &SS, unsigned P, bool Sub=true) {
500
4.19k
    if (!E) {
501
0
      self()->printNull(SS);
502
0
      return;
503
0
    }
504
4.19k
    if (Sub && E->block() && 
E->opcode() != COP_Variable0
) {
505
0
      SS << "_x" << E->id();
506
0
      return;
507
0
    }
508
4.19k
    if (self()->precedence(E) > P) {
509
16
      // Wrap expr in () if necessary.
510
16
      SS << "(";
511
16
      self()->printSExpr(E, SS, Prec_MAX);
512
16
      SS << ")";
513
16
      return;
514
16
    }
515
4.17k
516
4.17k
    switch (E->opcode()) {
517
4.17k
#define TIL_OPCODE_DEF(X)                                                  \
518
4.17k
    case COP_##X:                                                          \
519
4.17k
      self()->print##X(cast<X>(E), SS);                                    \
520
4.17k
      return;
521
4.17k
#include 
"ThreadSafetyOps.def"0
522
4.17k
#undef TIL_OPCODE_DEF
523
4.17k
    }
524
4.17k
  }
525
526
0
  void printNull(StreamType &SS) {
527
0
    SS << "#null";
528
0
  }
529
530
0
  void printFuture(const Future *E, StreamType &SS) {
531
0
    self()->printSExpr(E->maybeGetResult(), SS, Prec_Atom);
532
0
  }
533
534
0
  void printUndefined(const Undefined *E, StreamType &SS) {
535
0
    SS << "#undefined";
536
0
  }
537
538
4
  void printWildcard(const Wildcard *E, StreamType &SS) {
539
4
    SS << "*";
540
4
  }
541
542
  template<class T>
543
0
  void printLiteralT(const LiteralT<T> *E, StreamType &SS) {
544
0
    SS << E->value();
545
0
  }
Unexecuted instantiation: void clang::threadSafety::til::PrettyPrinter<clang::threadSafety::til::StdPrinter, std::__1::basic_ostream<char, std::__1::char_traits<char> > >::printLiteralT<signed char>(clang::threadSafety::til::LiteralT<signed char> const*, std::__1::basic_ostream<char, std::__1::char_traits<char> >&)
Unexecuted instantiation: void clang::threadSafety::til::PrettyPrinter<clang::threadSafety::til::StdPrinter, std::__1::basic_ostream<char, std::__1::char_traits<char> > >::printLiteralT<short>(clang::threadSafety::til::LiteralT<short> const*, std::__1::basic_ostream<char, std::__1::char_traits<char> >&)
Unexecuted instantiation: void clang::threadSafety::til::PrettyPrinter<clang::threadSafety::til::StdPrinter, std::__1::basic_ostream<char, std::__1::char_traits<char> > >::printLiteralT<unsigned short>(clang::threadSafety::til::LiteralT<unsigned short> const*, std::__1::basic_ostream<char, std::__1::char_traits<char> >&)
Unexecuted instantiation: void clang::threadSafety::til::PrettyPrinter<clang::threadSafety::til::StdPrinter, std::__1::basic_ostream<char, std::__1::char_traits<char> > >::printLiteralT<int>(clang::threadSafety::til::LiteralT<int> const*, std::__1::basic_ostream<char, std::__1::char_traits<char> >&)
Unexecuted instantiation: void clang::threadSafety::til::PrettyPrinter<clang::threadSafety::til::StdPrinter, std::__1::basic_ostream<char, std::__1::char_traits<char> > >::printLiteralT<unsigned int>(clang::threadSafety::til::LiteralT<unsigned int> const*, std::__1::basic_ostream<char, std::__1::char_traits<char> >&)
Unexecuted instantiation: void clang::threadSafety::til::PrettyPrinter<clang::threadSafety::til::StdPrinter, std::__1::basic_ostream<char, std::__1::char_traits<char> > >::printLiteralT<long long>(clang::threadSafety::til::LiteralT<long long> const*, std::__1::basic_ostream<char, std::__1::char_traits<char> >&)
Unexecuted instantiation: void clang::threadSafety::til::PrettyPrinter<clang::threadSafety::til::StdPrinter, std::__1::basic_ostream<char, std::__1::char_traits<char> > >::printLiteralT<unsigned long long>(clang::threadSafety::til::LiteralT<unsigned long long> const*, std::__1::basic_ostream<char, std::__1::char_traits<char> >&)
Unexecuted instantiation: void clang::threadSafety::til::PrettyPrinter<clang::threadSafety::til::StdPrinter, std::__1::basic_ostream<char, std::__1::char_traits<char> > >::printLiteralT<float>(clang::threadSafety::til::LiteralT<float> const*, std::__1::basic_ostream<char, std::__1::char_traits<char> >&)
Unexecuted instantiation: void clang::threadSafety::til::PrettyPrinter<clang::threadSafety::til::StdPrinter, std::__1::basic_ostream<char, std::__1::char_traits<char> > >::printLiteralT<double>(clang::threadSafety::til::LiteralT<double> const*, std::__1::basic_ostream<char, std::__1::char_traits<char> >&)
Unexecuted instantiation: void clang::threadSafety::til::PrettyPrinter<clang::threadSafety::til::StdPrinter, std::__1::basic_ostream<char, std::__1::char_traits<char> > >::printLiteralT<llvm::StringRef>(clang::threadSafety::til::LiteralT<llvm::StringRef> const*, std::__1::basic_ostream<char, std::__1::char_traits<char> >&)
546
547
0
  void printLiteralT(const LiteralT<uint8_t> *E, StreamType &SS) {
548
0
    SS << "'" << E->value() << "'";
549
0
  }
550
551
12
  void printLiteral(const Literal *E, StreamType &SS) {
552
12
    if (E->clangExpr()) {
553
12
      SS << getSourceLiteralString(E->clangExpr());
554
12
      return;
555
12
    }
556
0
    else {
557
0
      ValueType VT = E->valueType();
558
0
      switch (VT.Base) {
559
0
      case ValueType::BT_Void:
560
0
        SS << "void";
561
0
        return;
562
0
      case ValueType::BT_Bool:
563
0
        if (E->as<bool>().value())
564
0
          SS << "true";
565
0
        else
566
0
          SS << "false";
567
0
        return;
568
0
      case ValueType::BT_Int:
569
0
        switch (VT.Size) {
570
0
        case ValueType::ST_8:
571
0
          if (VT.Signed)
572
0
            printLiteralT(&E->as<int8_t>(), SS);
573
0
          else
574
0
            printLiteralT(&E->as<uint8_t>(), SS);
575
0
          return;
576
0
        case ValueType::ST_16:
577
0
          if (VT.Signed)
578
0
            printLiteralT(&E->as<int16_t>(), SS);
579
0
          else
580
0
            printLiteralT(&E->as<uint16_t>(), SS);
581
0
          return;
582
0
        case ValueType::ST_32:
583
0
          if (VT.Signed)
584
0
            printLiteralT(&E->as<int32_t>(), SS);
585
0
          else
586
0
            printLiteralT(&E->as<uint32_t>(), SS);
587
0
          return;
588
0
        case ValueType::ST_64:
589
0
          if (VT.Signed)
590
0
            printLiteralT(&E->as<int64_t>(), SS);
591
0
          else
592
0
            printLiteralT(&E->as<uint64_t>(), SS);
593
0
          return;
594
0
        default:
595
0
          break;
596
0
        }
597
0
        break;
598
0
      case ValueType::BT_Float:
599
0
        switch (VT.Size) {
600
0
        case ValueType::ST_32:
601
0
          printLiteralT(&E->as<float>(), SS);
602
0
          return;
603
0
        case ValueType::ST_64:
604
0
          printLiteralT(&E->as<double>(), SS);
605
0
          return;
606
0
        default:
607
0
          break;
608
0
        }
609
0
        break;
610
0
      case ValueType::BT_String:
611
0
        SS << "\"";
612
0
        printLiteralT(&E->as<StringRef>(), SS);
613
0
        SS << "\"";
614
0
        return;
615
0
      case ValueType::BT_Pointer:
616
0
        SS << "#ptr";
617
0
        return;
618
0
      case ValueType::BT_ValueRef:
619
0
        SS << "#vref";
620
0
        return;
621
0
      }
622
0
    }
623
0
    SS << "#lit";
624
0
  }
625
626
855
  void printLiteralPtr(const LiteralPtr *E, StreamType &SS) {
627
855
    SS << E->clangDecl()->getNameAsString();
628
855
  }
629
630
4
  void printVariable(const Variable *V, StreamType &SS, bool IsVarDecl=false) {
631
4
    if (CStyle && V->kind() == Variable::VK_SFun)
632
4
      SS << "this";
633
0
    else
634
0
      SS << V->name() << V->id();
635
4
  }
636
637
0
  void printFunction(const Function *E, StreamType &SS, unsigned sugared = 0) {
638
0
    switch (sugared) {
639
0
      default:
640
0
        SS << "\\(";   // Lambda
641
0
        break;
642
0
      case 1:
643
0
        SS << "(";     // Slot declarations
644
0
        break;
645
0
      case 2:
646
0
        SS << ", ";    // Curried functions
647
0
        break;
648
0
    }
649
0
    self()->printVariable(E->variableDecl(), SS, true);
650
0
    SS << ": ";
651
0
    self()->printSExpr(E->variableDecl()->definition(), SS, Prec_MAX);
652
0
653
0
    const SExpr *B = E->body();
654
0
    if (B && B->opcode() == COP_Function)
655
0
      self()->printFunction(cast<Function>(B), SS, 2);
656
0
    else {
657
0
      SS << ")";
658
0
      self()->printSExpr(B, SS, Prec_Decl);
659
0
    }
660
0
  }
661
662
0
  void printSFunction(const SFunction *E, StreamType &SS) {
663
0
    SS << "@";
664
0
    self()->printVariable(E->variableDecl(), SS, true);
665
0
    SS << " ";
666
0
    self()->printSExpr(E->body(), SS, Prec_Decl);
667
0
  }
668
669
0
  void printCode(const Code *E, StreamType &SS) {
670
0
    SS << ": ";
671
0
    self()->printSExpr(E->returnType(), SS, Prec_Decl-1);
672
0
    SS << " -> ";
673
0
    self()->printSExpr(E->body(), SS, Prec_Decl);
674
0
  }
675
676
0
  void printField(const Field *E, StreamType &SS) {
677
0
    SS << ": ";
678
0
    self()->printSExpr(E->range(), SS, Prec_Decl-1);
679
0
    SS << " = ";
680
0
    self()->printSExpr(E->body(), SS, Prec_Decl);
681
0
  }
682
683
40
  void printApply(const Apply *E, StreamType &SS, bool sugared = false) {
684
40
    const SExpr *F = E->fun();
685
40
    if (F->opcode() == COP_Apply) {
686
16
      printApply(cast<Apply>(F), SS, true);
687
16
      SS << ", ";
688
24
    } else {
689
24
      self()->printSExpr(F, SS, Prec_Postfix);
690
24
      SS << "(";
691
24
    }
692
40
    self()->printSExpr(E->arg(), SS, Prec_MAX);
693
40
    if (!sugared)
694
0
      SS << ")$";
695
40
  }
696
697
518
  void printSApply(const SApply *E, StreamType &SS) {
698
518
    self()->printSExpr(E->sfun(), SS, Prec_Postfix);
699
518
    if (E->isDelegation()) {
700
0
      SS << "@(";
701
0
      self()->printSExpr(E->arg(), SS, Prec_MAX);
702
0
      SS << ")";
703
0
    }
704
518
  }
705
706
2.69k
  void printProject(const Project *E, StreamType &SS) {
707
2.69k
    if (CStyle) {
708
2.69k
      // Omit the  this->
709
2.69k
      if (const auto *SAP = dyn_cast<SApply>(E->record())) {
710
2.67k
        if (const auto *V = dyn_cast<Variable>(SAP->sfun())) {
711
2.15k
          if (!SAP->isDelegation() && V->kind() == Variable::VK_SFun) {
712
2.15k
            SS << E->slotName();
713
2.15k
            return;
714
2.15k
          }
715
538
        }
716
2.67k
      }
717
538
      if (isa<Wildcard>(E->record())) {
718
20
        // handle existentials
719
20
        SS << "&";
720
20
        SS << E->clangDecl()->getQualifiedNameAsString();
721
20
        return;
722
20
      }
723
518
    }
724
518
    self()->printSExpr(E->record(), SS, Prec_Postfix);
725
518
    if (CStyle && E->isArrow())
726
193
      SS << "->";
727
325
    else
728
325
      SS << ".";
729
518
    SS << E->slotName();
730
518
  }
731
732
36
  void printCall(const Call *E, StreamType &SS) {
733
36
    const SExpr *T = E->target();
734
36
    if (T->opcode() == COP_Apply) {
735
24
      self()->printApply(cast<Apply>(T), SS, true);
736
24
      SS << ")";
737
24
    }
738
12
    else {
739
12
      self()->printSExpr(T, SS, Prec_Postfix);
740
12
      SS << "()";
741
12
    }
742
36
  }
743
744
0
  void printAlloc(const Alloc *E, StreamType &SS) {
745
0
    SS << "new ";
746
0
    self()->printSExpr(E->dataType(), SS, Prec_Other-1);
747
0
  }
748
749
0
  void printLoad(const Load *E, StreamType &SS) {
750
0
    self()->printSExpr(E->pointer(), SS, Prec_Postfix);
751
0
    if (!CStyle)
752
0
      SS << "^";
753
0
  }
754
755
0
  void printStore(const Store *E, StreamType &SS) {
756
0
    self()->printSExpr(E->destination(), SS, Prec_Other-1);
757
0
    SS << " := ";
758
0
    self()->printSExpr(E->source(), SS, Prec_Other-1);
759
0
  }
760
761
16
  void printArrayIndex(const ArrayIndex *E, StreamType &SS) {
762
16
    self()->printSExpr(E->array(), SS, Prec_Postfix);
763
16
    SS << "[";
764
16
    self()->printSExpr(E->index(), SS, Prec_MAX);
765
16
    SS << "]";
766
16
  }
767
768
0
  void printArrayAdd(const ArrayAdd *E, StreamType &SS) {
769
0
    self()->printSExpr(E->array(), SS, Prec_Postfix);
770
0
    SS << " + ";
771
0
    self()->printSExpr(E->index(), SS, Prec_Atom);
772
0
  }
773
774
0
  void printUnaryOp(const UnaryOp *E, StreamType &SS) {
775
0
    SS << getUnaryOpcodeString(E->unaryOpcode());
776
0
    self()->printSExpr(E->expr(), SS, Prec_Unary);
777
0
  }
778
779
8
  void printBinaryOp(const BinaryOp *E, StreamType &SS) {
780
8
    self()->printSExpr(E->expr0(), SS, Prec_Binary-1);
781
8
    SS << " " << getBinaryOpcodeString(E->binaryOpcode()) << " ";
782
8
    self()->printSExpr(E->expr1(), SS, Prec_Binary-1);
783
8
  }
784
785
24
  void printCast(const Cast *E, StreamType &SS) {
786
24
    if (!CStyle) {
787
0
      SS << "cast[";
788
0
      switch (E->castOpcode()) {
789
0
      case CAST_none:
790
0
        SS << "none";
791
0
        break;
792
0
      case CAST_extendNum:
793
0
        SS << "extendNum";
794
0
        break;
795
0
      case CAST_truncNum:
796
0
        SS << "truncNum";
797
0
        break;
798
0
      case CAST_toFloat:
799
0
        SS << "toFloat";
800
0
        break;
801
0
      case CAST_toInt:
802
0
        SS << "toInt";
803
0
        break;
804
0
      case CAST_objToPtr:
805
0
        SS << "objToPtr";
806
0
        break;
807
0
      }
808
0
      SS << "](";
809
0
      self()->printSExpr(E->expr(), SS, Prec_Unary);
810
0
      SS << ")";
811
0
      return;
812
0
    }
813
24
    self()->printSExpr(E->expr(), SS, Prec_Unary);
814
24
  }
815
816
0
  void printSCFG(const SCFG *E, StreamType &SS) {
817
0
    SS << "CFG {\n";
818
0
    for (const auto *BBI : *E)
819
0
      printBasicBlock(BBI, SS);
820
0
    SS << "}";
821
0
    newline(SS);
822
0
  }
823
824
0
  void printBBInstr(const SExpr *E, StreamType &SS) {
825
0
    bool Sub = false;
826
0
    if (E->opcode() == COP_Variable) {
827
0
      const auto *V = cast<Variable>(E);
828
0
      SS << "let " << V->name() << V->id() << " = ";
829
0
      E = V->definition();
830
0
      Sub = true;
831
0
    }
832
0
    else if (E->opcode() != COP_Store) {
833
0
      SS << "let _x" << E->id() << " = ";
834
0
    }
835
0
    self()->printSExpr(E, SS, Prec_MAX, Sub);
836
0
    SS << ";";
837
0
    newline(SS);
838
0
  }
839
840
0
  void printBasicBlock(const BasicBlock *E, StreamType &SS) {
841
0
    SS << "BB_" << E->blockID() << ":";
842
0
    if (E->parent())
843
0
      SS << " BB_" << E->parent()->blockID();
844
0
    newline(SS);
845
0
846
0
    for (const auto *A : E->arguments())
847
0
      printBBInstr(A, SS);
848
0
849
0
    for (const auto *I : E->instructions())
850
0
      printBBInstr(I, SS);
851
0
852
0
    const SExpr *T = E->terminator();
853
0
    if (T) {
854
0
      self()->printSExpr(T, SS, Prec_MAX, false);
855
0
      SS << ";";
856
0
      newline(SS);
857
0
    }
858
0
    newline(SS);
859
0
  }
860
861
0
  void printPhi(const Phi *E, StreamType &SS) {
862
0
    SS << "phi(";
863
0
    if (E->status() == Phi::PH_SingleVal)
864
0
      self()->printSExpr(E->values()[0], SS, Prec_MAX);
865
0
    else {
866
0
      unsigned i = 0;
867
0
      for (const auto *V : E->values()) {
868
0
        if (i++ > 0)
869
0
          SS << ", ";
870
0
        self()->printSExpr(V, SS, Prec_MAX);
871
0
      }
872
0
    }
873
0
    SS << ")";
874
0
  }
875
876
0
  void printGoto(const Goto *E, StreamType &SS) {
877
0
    SS << "goto ";
878
0
    printBlockLabel(SS, E->targetBlock(), E->index());
879
0
  }
880
881
0
  void printBranch(const Branch *E, StreamType &SS) {
882
0
    SS << "branch (";
883
0
    self()->printSExpr(E->condition(), SS, Prec_MAX);
884
0
    SS << ") ";
885
0
    printBlockLabel(SS, E->thenBlock(), -1);
886
0
    SS << " ";
887
0
    printBlockLabel(SS, E->elseBlock(), -1);
888
0
  }
889
890
0
  void printReturn(const Return *E, StreamType &SS) {
891
0
    SS << "return ";
892
0
    self()->printSExpr(E->returnValue(), SS, Prec_Other);
893
0
  }
894
895
0
  void printIdentifier(const Identifier *E, StreamType &SS) {
896
0
    SS << E->name();
897
0
  }
898
899
8
  void printIfThenElse(const IfThenElse *E, StreamType &SS) {
900
8
    if (CStyle) {
901
8
      printSExpr(E->condition(), SS, Prec_Unary);
902
8
      SS << " ? ";
903
8
      printSExpr(E->thenExpr(), SS, Prec_Unary);
904
8
      SS << " : ";
905
8
      printSExpr(E->elseExpr(), SS, Prec_Unary);
906
8
      return;
907
8
    }
908
0
    SS << "if (";
909
0
    printSExpr(E->condition(), SS, Prec_MAX);
910
0
    SS << ") then ";
911
0
    printSExpr(E->thenExpr(), SS, Prec_Other);
912
0
    SS << " else ";
913
0
    printSExpr(E->elseExpr(), SS, Prec_Other);
914
0
  }
915
916
0
  void printLet(const Let *E, StreamType &SS) {
917
0
    SS << "let ";
918
0
    printVariable(E->variableDecl(), SS, true);
919
0
    SS << " = ";
920
0
    printSExpr(E->variableDecl()->definition(), SS, Prec_Decl-1);
921
0
    SS << "; ";
922
0
    printSExpr(E->body(), SS, Prec_Decl-1);
923
0
  }
924
};
925
926
class StdPrinter : public PrettyPrinter<StdPrinter, std::ostream> {};
927
928
} // namespace til
929
} // namespace threadSafety
930
} // namespace clang
931
932
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETYTRAVERSE_H