Coverage Report

Created: 2020-03-31 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Basic/PartialDiagnostic.h
Line
Count
Source (jump to first uncovered line)
1
//===- PartialDiagnostic.h - Diagnostic "closures" --------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
/// \file
10
/// Implements a partial diagnostic that can be emitted anwyhere
11
/// in a DiagnosticBuilder stream.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#ifndef LLVM_CLANG_BASIC_PARTIALDIAGNOSTIC_H
16
#define LLVM_CLANG_BASIC_PARTIALDIAGNOSTIC_H
17
18
#include "clang/Basic/Diagnostic.h"
19
#include "clang/Basic/LLVM.h"
20
#include "clang/Basic/SourceLocation.h"
21
#include "llvm/ADT/SmallVector.h"
22
#include "llvm/ADT/StringRef.h"
23
#include <cassert>
24
#include <cstdint>
25
#include <string>
26
#include <type_traits>
27
#include <utility>
28
29
namespace clang {
30
31
class DeclContext;
32
class IdentifierInfo;
33
34
class PartialDiagnostic {
35
public:
36
  enum {
37
      // The MaxArguments and MaxFixItHints member enum values from
38
      // DiagnosticsEngine are private but DiagnosticsEngine declares
39
      // PartialDiagnostic a friend.  These enum values are redeclared
40
      // here so that the nested Storage class below can access them.
41
      MaxArguments = DiagnosticsEngine::MaxArguments
42
  };
43
44
  struct Storage {
45
    enum {
46
        /// The maximum number of arguments we can hold. We
47
        /// currently only support up to 10 arguments (%0-%9).
48
        ///
49
        /// A single diagnostic with more than that almost certainly has to
50
        /// be simplified anyway.
51
        MaxArguments = PartialDiagnostic::MaxArguments
52
    };
53
54
    /// The number of entries in Arguments.
55
    unsigned char NumDiagArgs = 0;
56
57
    /// Specifies for each argument whether it is in DiagArgumentsStr
58
    /// or in DiagArguments.
59
    unsigned char DiagArgumentsKind[MaxArguments];
60
61
    /// The values for the various substitution positions.
62
    ///
63
    /// This is used when the argument is not an std::string. The specific value
64
    /// is mangled into an intptr_t and the interpretation depends on exactly
65
    /// what sort of argument kind it is.
66
    intptr_t DiagArgumentsVal[MaxArguments];
67
68
    /// The values for the various substitution positions that have
69
    /// string arguments.
70
    std::string DiagArgumentsStr[MaxArguments];
71
72
    /// The list of ranges added to this diagnostic.
73
    SmallVector<CharSourceRange, 8> DiagRanges;
74
75
    /// If valid, provides a hint with some code to insert, remove, or
76
    /// modify at a particular position.
77
    SmallVector<FixItHint, 6>  FixItHints;
78
79
1.04M
    Storage() = default;
80
  };
81
82
  /// An allocator for Storage objects, which uses a small cache to
83
  /// objects, used to reduce malloc()/free() traffic for partial diagnostics.
84
  class StorageAllocator {
85
    static const unsigned NumCached = 16;
86
    Storage Cached[NumCached];
87
    Storage *FreeList[NumCached];
88
    unsigned NumFreeListEntries;
89
90
  public:
91
    StorageAllocator();
92
    ~StorageAllocator();
93
94
    /// Allocate new storage.
95
1.17M
    Storage *Allocate() {
96
1.17M
      if (NumFreeListEntries == 0)
97
36.1k
        return new Storage;
98
1.13M
99
1.13M
      Storage *Result = FreeList[--NumFreeListEntries];
100
1.13M
      Result->NumDiagArgs = 0;
101
1.13M
      Result->DiagRanges.clear();
102
1.13M
      Result->FixItHints.clear();
103
1.13M
      return Result;
104
1.13M
    }
105
106
    /// Free the given storage object.
107
1.16M
    void Deallocate(Storage *S) {
108
1.16M
      if (S >= Cached && 
S <= Cached + NumCached1.15M
) {
109
1.13M
        FreeList[NumFreeListEntries++] = S;
110
1.13M
        return;
111
1.13M
      }
112
34.5k
113
34.5k
      delete S;
114
34.5k
    }
115
  };
116
117
private:
118
  // NOTE: Sema assumes that PartialDiagnostic is location-invariant
119
  // in the sense that its bits can be safely memcpy'ed and destructed
120
  // in the new location.
121
122
  /// The diagnostic ID.
123
  mutable unsigned DiagID = 0;
124
125
  /// Storage for args and ranges.
126
  mutable Storage *DiagStorage = nullptr;
127
128
  /// Allocator used to allocate storage for this diagnostic.
129
  StorageAllocator *Allocator = nullptr;
130
131
  /// Retrieve storage for this particular diagnostic.
132
1.17M
  Storage *getStorage() const {
133
1.17M
    if (DiagStorage)
134
0
      return DiagStorage;
135
1.17M
136
1.17M
    if (Allocator)
137
1.17M
      DiagStorage = Allocator->Allocate();
138
0
    else {
139
0
      assert(Allocator != reinterpret_cast<StorageAllocator *>(~uintptr_t(0)));
140
0
      DiagStorage = new Storage;
141
0
    }
142
1.17M
    return DiagStorage;
143
1.17M
  }
144
145
7.15M
  void freeStorage() {
146
7.15M
    if (!DiagStorage)
147
5.98M
      return;
148
1.16M
149
1.16M
    // The hot path for PartialDiagnostic is when we just used it to wrap an ID
150
1.16M
    // (typically so we have the flexibility of passing a more complex
151
1.16M
    // diagnostic into the callee, but that does not commonly occur).
152
1.16M
    //
153
1.16M
    // Split this out into a slow function for silly compilers (*cough*) which
154
1.16M
    // can't do decent partial inlining.
155
1.16M
    freeStorageSlow();
156
1.16M
  }
157
158
1.16M
  void freeStorageSlow() {
159
1.16M
    if (Allocator)
160
1.16M
      Allocator->Deallocate(DiagStorage);
161
0
    else if (Allocator != reinterpret_cast<StorageAllocator *>(~uintptr_t(0)))
162
0
      delete DiagStorage;
163
1.16M
    DiagStorage = nullptr;
164
1.16M
  }
165
166
334k
  void AddSourceRange(const CharSourceRange &R) const {
167
334k
    if (!DiagStorage)
168
34.0k
      DiagStorage = getStorage();
169
334k
170
334k
    DiagStorage->DiagRanges.push_back(R);
171
334k
  }
172
173
7.36k
  void AddFixItHint(const FixItHint &Hint) const {
174
7.36k
    if (Hint.isNull())
175
7.08k
      return;
176
275
177
275
    if (!DiagStorage)
178
112
      DiagStorage = getStorage();
179
275
180
275
    DiagStorage->FixItHints.push_back(Hint);
181
275
  }
182
183
public:
184
  struct NullDiagnostic {};
185
186
  /// Create a null partial diagnostic, which cannot carry a payload,
187
  /// and only exists to be swapped with a real partial diagnostic.
188
110k
  PartialDiagnostic(NullDiagnostic) {}
189
190
  PartialDiagnostic(unsigned DiagID, StorageAllocator &Allocator)
191
3.21M
      : DiagID(DiagID), Allocator(&Allocator) {}
192
193
  PartialDiagnostic(const PartialDiagnostic &Other)
194
1.60M
      : DiagID(Other.DiagID), Allocator(Other.Allocator) {
195
1.60M
    if (Other.DiagStorage) {
196
87.1k
      DiagStorage = getStorage();
197
87.1k
      *DiagStorage = *Other.DiagStorage;
198
87.1k
    }
199
1.60M
  }
200
201
  PartialDiagnostic(PartialDiagnostic &&Other)
202
      : DiagID(Other.DiagID), DiagStorage(Other.DiagStorage),
203
1.12M
        Allocator(Other.Allocator) {
204
1.12M
    Other.DiagStorage = nullptr;
205
1.12M
  }
206
207
  PartialDiagnostic(const PartialDiagnostic &Other, Storage *DiagStorage)
208
      : DiagID(Other.DiagID), DiagStorage(DiagStorage),
209
73
        Allocator(reinterpret_cast<StorageAllocator *>(~uintptr_t(0))) {
210
73
    if (Other.DiagStorage)
211
2
      *this->DiagStorage = *Other.DiagStorage;
212
73
  }
213
214
  PartialDiagnostic(const Diagnostic &Other, StorageAllocator &Allocator)
215
138k
      : DiagID(Other.getID()), Allocator(&Allocator) {
216
138k
    // Copy arguments.
217
303k
    for (unsigned I = 0, N = Other.getNumArgs(); I != N; 
++I164k
) {
218
164k
      if (Other.getArgKind(I) == DiagnosticsEngine::ak_std_string)
219
70.3k
        AddString(Other.getArgStdStr(I));
220
94.0k
      else
221
94.0k
        AddTaggedVal(Other.getRawArg(I), Other.getArgKind(I));
222
164k
    }
223
138k
224
138k
    // Copy source ranges.
225
268k
    for (unsigned I = 0, N = Other.getNumRanges(); I != N; 
++I129k
)
226
129k
      AddSourceRange(Other.getRange(I));
227
138k
228
138k
    // Copy fix-its.
229
138k
    for (unsigned I = 0, N = Other.getNumFixItHints(); I != N; 
++I75
)
230
75
      AddFixItHint(Other.getFixItHint(I));
231
138k
  }
232
233
61.2k
  PartialDiagnostic &operator=(const PartialDiagnostic &Other) {
234
61.2k
    DiagID = Other.DiagID;
235
61.2k
    if (Other.DiagStorage) {
236
5.77k
      if (!DiagStorage)
237
5.77k
        DiagStorage = getStorage();
238
5.77k
239
5.77k
      *DiagStorage = *Other.DiagStorage;
240
55.5k
    } else {
241
55.5k
      freeStorage();
242
55.5k
    }
243
61.2k
244
61.2k
    return *this;
245
61.2k
  }
246
247
6.69k
  PartialDiagnostic &operator=(PartialDiagnostic &&Other) {
248
6.69k
    freeStorage();
249
6.69k
250
6.69k
    DiagID = Other.DiagID;
251
6.69k
    DiagStorage = Other.DiagStorage;
252
6.69k
    Allocator = Other.Allocator;
253
6.69k
254
6.69k
    Other.DiagStorage = nullptr;
255
6.69k
    return *this;
256
6.69k
  }
257
258
6.19M
  ~PartialDiagnostic() {
259
6.19M
    freeStorage();
260
6.19M
  }
261
262
108k
  void swap(PartialDiagnostic &PD) {
263
108k
    std::swap(DiagID, PD.DiagID);
264
108k
    std::swap(DiagStorage, PD.DiagStorage);
265
108k
    std::swap(Allocator, PD.Allocator);
266
108k
  }
267
268
1.09M
  unsigned getDiagID() const { return DiagID; }
269
270
1.28M
  void AddTaggedVal(intptr_t V, DiagnosticsEngine::ArgumentKind Kind) const {
271
1.28M
    if (!DiagStorage)
272
963k
      DiagStorage = getStorage();
273
1.28M
274
1.28M
    assert(DiagStorage->NumDiagArgs < Storage::MaxArguments &&
275
1.28M
           "Too many arguments to diagnostic!");
276
1.28M
    DiagStorage->DiagArgumentsKind[DiagStorage->NumDiagArgs] = Kind;
277
1.28M
    DiagStorage->DiagArgumentsVal[DiagStorage->NumDiagArgs++] = V;
278
1.28M
  }
279
280
96.2k
  void AddString(StringRef V) const {
281
96.2k
    if (!DiagStorage)
282
79.7k
      DiagStorage = getStorage();
283
96.2k
284
96.2k
    assert(DiagStorage->NumDiagArgs < Storage::MaxArguments &&
285
96.2k
           "Too many arguments to diagnostic!");
286
96.2k
    DiagStorage->DiagArgumentsKind[DiagStorage->NumDiagArgs]
287
96.2k
      = DiagnosticsEngine::ak_std_string;
288
96.2k
    DiagStorage->DiagArgumentsStr[DiagStorage->NumDiagArgs++] = std::string(V);
289
96.2k
  }
290
291
118k
  void Emit(const DiagnosticBuilder &DB) const {
292
118k
    if (!DiagStorage)
293
6.41k
      return;
294
111k
295
111k
    // Add all arguments.
296
377k
    
for (unsigned i = 0, e = DiagStorage->NumDiagArgs; 111k
i != e;
++i265k
) {
297
265k
      if ((DiagnosticsEngine::ArgumentKind)DiagStorage->DiagArgumentsKind[i]
298
265k
            == DiagnosticsEngine::ak_std_string)
299
24.7k
        DB.AddString(DiagStorage->DiagArgumentsStr[i]);
300
240k
      else
301
240k
        DB.AddTaggedVal(DiagStorage->DiagArgumentsVal[i],
302
240k
            (DiagnosticsEngine::ArgumentKind)DiagStorage->DiagArgumentsKind[i]);
303
265k
    }
304
111k
305
111k
    // Add all ranges.
306
111k
    for (const CharSourceRange &Range : DiagStorage->DiagRanges)
307
170k
      DB.AddSourceRange(Range);
308
111k
309
111k
    // Add all fix-its.
310
111k
    for (const FixItHint &Fix : DiagStorage->FixItHints)
311
205
      DB.AddFixItHint(Fix);
312
111k
  }
313
314
  void EmitToString(DiagnosticsEngine &Diags,
315
1.38k
                    SmallVectorImpl<char> &Buf) const {
316
1.38k
    // FIXME: It should be possible to render a diagnostic to a string without
317
1.38k
    //        messing with the state of the diagnostics engine.
318
1.38k
    DiagnosticBuilder DB(Diags.Report(getDiagID()));
319
1.38k
    Emit(DB);
320
1.38k
    DB.FlushCounts();
321
1.38k
    Diagnostic(&Diags).FormatDiagnostic(Buf);
322
1.38k
    DB.Clear();
323
1.38k
    Diags.Clear();
324
1.38k
  }
325
326
  /// Clear out this partial diagnostic, giving it a new diagnostic ID
327
  /// and removing all of its arguments, ranges, and fix-it hints.
328
902k
  void Reset(unsigned DiagID = 0) {
329
902k
    this->DiagID = DiagID;
330
902k
    freeStorage();
331
902k
  }
332
333
73
  bool hasStorage() const { return DiagStorage != nullptr; }
334
335
  /// Retrieve the string argument at the given index.
336
18
  StringRef getStringArg(unsigned I) {
337
18
    assert(DiagStorage && "No diagnostic storage?");
338
18
    assert(I < DiagStorage->NumDiagArgs && "Not enough diagnostic args");
339
18
    assert(DiagStorage->DiagArgumentsKind[I]
340
18
             == DiagnosticsEngine::ak_std_string && "Not a string arg");
341
18
    return DiagStorage->DiagArgumentsStr[I];
342
18
  }
343
344
  friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
345
41.8k
                                             unsigned I) {
346
41.8k
    PD.AddTaggedVal(I, DiagnosticsEngine::ak_uint);
347
41.8k
    return PD;
348
41.8k
  }
349
350
  friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
351
190k
                                             int I) {
352
190k
    PD.AddTaggedVal(I, DiagnosticsEngine::ak_sint);
353
190k
    return PD;
354
190k
  }
355
356
  friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
357
2.17k
                                                    const char *S) {
358
2.17k
    PD.AddTaggedVal(reinterpret_cast<intptr_t>(S),
359
2.17k
                    DiagnosticsEngine::ak_c_string);
360
2.17k
    return PD;
361
2.17k
  }
362
363
  friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
364
25.9k
                                                    StringRef S) {
365
25.9k
366
25.9k
    PD.AddString(S);
367
25.9k
    return PD;
368
25.9k
  }
369
370
  friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
371
349
                                                    const IdentifierInfo *II) {
372
349
    PD.AddTaggedVal(reinterpret_cast<intptr_t>(II),
373
349
                    DiagnosticsEngine::ak_identifierinfo);
374
349
    return PD;
375
349
  }
376
377
  // Adds a DeclContext to the diagnostic. The enable_if template magic is here
378
  // so that we only match those arguments that are (statically) DeclContexts;
379
  // other arguments that derive from DeclContext (e.g., RecordDecls) will not
380
  // match.
381
  template <typename T>
382
  friend inline std::enable_if_t<std::is_same<T, DeclContext>::value,
383
                                 const PartialDiagnostic &>
384
294
  operator<<(const PartialDiagnostic &PD, T *DC) {
385
294
    PD.AddTaggedVal(reinterpret_cast<intptr_t>(DC),
386
294
                    DiagnosticsEngine::ak_declcontext);
387
294
    return PD;
388
294
  }
389
390
  friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
391
201k
                                                    SourceRange R) {
392
201k
    PD.AddSourceRange(CharSourceRange::getTokenRange(R));
393
201k
    return PD;
394
201k
  }
395
396
  friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
397
3.55k
                                                    const CharSourceRange &R) {
398
3.55k
    PD.AddSourceRange(R);
399
3.55k
    return PD;
400
3.55k
  }
401
402
  friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
403
7.28k
                                             const FixItHint &Hint) {
404
7.28k
    PD.AddFixItHint(Hint);
405
7.28k
    return PD;
406
7.28k
  }
407
};
408
409
inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
410
0
                                           const PartialDiagnostic &PD) {
411
0
  PD.Emit(DB);
412
0
  return DB;
413
0
}
414
415
/// A partial diagnostic along with the source location where this
416
/// diagnostic occurs.
417
using PartialDiagnosticAt = std::pair<SourceLocation, PartialDiagnostic>;
418
419
} // namespace clang
420
421
#endif // LLVM_CLANG_BASIC_PARTIALDIAGNOSTIC_H