Coverage Report

Created: 2020-09-15 12:33

/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.38M
    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.35M
    Storage *Allocate() {
96
1.35M
      if (NumFreeListEntries == 0)
97
41.0k
        return new Storage;
98
1.31M
99
1.31M
      Storage *Result = FreeList[--NumFreeListEntries];
100
1.31M
      Result->NumDiagArgs = 0;
101
1.31M
      Result->DiagRanges.clear();
102
1.31M
      Result->FixItHints.clear();
103
1.31M
      return Result;
104
1.31M
    }
105
106
    /// Free the given storage object.
107
1.35M
    void Deallocate(Storage *S) {
108
1.35M
      if (S >= Cached && 
S <= Cached + NumCached1.31M
) {
109
1.31M
        FreeList[NumFreeListEntries++] = S;
110
1.31M
        return;
111
1.31M
      }
112
39.3k
113
39.3k
      delete S;
114
39.3k
    }
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.35M
  Storage *getStorage() const {
133
1.35M
    if (DiagStorage)
134
0
      return DiagStorage;
135
1.35M
136
1.35M
    if (Allocator)
137
1.35M
      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.35M
    return DiagStorage;
143
1.35M
  }
144
145
7.82M
  void freeStorage() {
146
7.82M
    if (!DiagStorage)
147
6.47M
      return;
148
1.35M
149
    // The hot path for PartialDiagnostic is when we just used it to wrap an ID
150
    // (typically so we have the flexibility of passing a more complex
151
    // diagnostic into the callee, but that does not commonly occur).
152
    //
153
    // Split this out into a slow function for silly compilers (*cough*) which
154
    // can't do decent partial inlining.
155
1.35M
    freeStorageSlow();
156
1.35M
  }
157
158
1.35M
  void freeStorageSlow() {
159
1.35M
    if (Allocator)
160
1.35M
      Allocator->Deallocate(DiagStorage);
161
0
    else if (Allocator != reinterpret_cast<StorageAllocator *>(~uintptr_t(0)))
162
0
      delete DiagStorage;
163
1.35M
    DiagStorage = nullptr;
164
1.35M
  }
165
166
352k
  void AddSourceRange(const CharSourceRange &R) const {
167
352k
    if (!DiagStorage)
168
36.3k
      DiagStorage = getStorage();
169
352k
170
352k
    DiagStorage->DiagRanges.push_back(R);
171
352k
  }
172
173
295
  void AddFixItHint(const FixItHint &Hint) const {
174
295
    if (Hint.isNull())
175
0
      return;
176
295
177
295
    if (!DiagStorage)
178
122
      DiagStorage = getStorage();
179
295
180
295
    DiagStorage->FixItHints.push_back(Hint);
181
295
  }
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
88.2k
  PartialDiagnostic(NullDiagnostic) {}
189
190
  PartialDiagnostic(unsigned DiagID, StorageAllocator &Allocator)
191
3.47M
      : DiagID(DiagID), Allocator(&Allocator) {}
192
193
  PartialDiagnostic(const PartialDiagnostic &Other)
194
1.83M
      : DiagID(Other.DiagID), Allocator(Other.Allocator) {
195
1.83M
    if (Other.DiagStorage) {
196
102k
      DiagStorage = getStorage();
197
102k
      *DiagStorage = *Other.DiagStorage;
198
102k
    }
199
1.83M
  }
200
201
  PartialDiagnostic(PartialDiagnostic &&Other)
202
      : DiagID(Other.DiagID), DiagStorage(Other.DiagStorage),
203
1.40M
        Allocator(Other.Allocator) {
204
1.40M
    Other.DiagStorage = nullptr;
205
1.40M
  }
206
207
  PartialDiagnostic(const PartialDiagnostic &Other, Storage *DiagStorage)
208
      : DiagID(Other.DiagID), DiagStorage(DiagStorage),
209
97
        Allocator(reinterpret_cast<StorageAllocator *>(~uintptr_t(0))) {
210
97
    if (Other.DiagStorage)
211
2
      *this->DiagStorage = *Other.DiagStorage;
212
97
  }
213
214
  PartialDiagnostic(const Diagnostic &Other, StorageAllocator &Allocator)
215
113k
      : DiagID(Other.getID()), Allocator(&Allocator) {
216
    // Copy arguments.
217
256k
    for (unsigned I = 0, N = Other.getNumArgs(); I != N; 
++I142k
) {
218
142k
      if (Other.getArgKind(I) == DiagnosticsEngine::ak_std_string)
219
49.2k
        AddString(Other.getArgStdStr(I));
220
93.3k
      else
221
93.3k
        AddTaggedVal(Other.getRawArg(I), Other.getArgKind(I));
222
142k
    }
223
113k
224
    // Copy source ranges.
225
220k
    for (unsigned I = 0, N = Other.getNumRanges(); I != N; 
++I107k
)
226
107k
      AddSourceRange(Other.getRange(I));
227
113k
228
    // Copy fix-its.
229
113k
    for (unsigned I = 0, N = Other.getNumFixItHints(); I != N; 
++I75
)
230
75
      AddFixItHint(Other.getFixItHint(I));
231
113k
  }
232
233
71.4k
  PartialDiagnostic &operator=(const PartialDiagnostic &Other) {
234
71.4k
    DiagID = Other.DiagID;
235
71.4k
    if (Other.DiagStorage) {
236
5.61k
      if (!DiagStorage)
237
5.61k
        DiagStorage = getStorage();
238
5.61k
239
5.61k
      *DiagStorage = *Other.DiagStorage;
240
65.7k
    } else {
241
65.7k
      freeStorage();
242
65.7k
    }
243
71.4k
244
71.4k
    return *this;
245
71.4k
  }
246
247
6.94k
  PartialDiagnostic &operator=(PartialDiagnostic &&Other) {
248
6.94k
    freeStorage();
249
6.94k
250
6.94k
    DiagID = Other.DiagID;
251
6.94k
    DiagStorage = Other.DiagStorage;
252
6.94k
    Allocator = Other.Allocator;
253
6.94k
254
6.94k
    Other.DiagStorage = nullptr;
255
6.94k
    return *this;
256
6.94k
  }
257
258
6.90M
  ~PartialDiagnostic() {
259
6.90M
    freeStorage();
260
6.90M
  }
261
262
85.0k
  void swap(PartialDiagnostic &PD) {
263
85.0k
    std::swap(DiagID, PD.DiagID);
264
85.0k
    std::swap(DiagStorage, PD.DiagStorage);
265
85.0k
    std::swap(Allocator, PD.Allocator);
266
85.0k
  }
267
268
1.09M
  unsigned getDiagID() const { return DiagID; }
269
270
1.54M
  void AddTaggedVal(intptr_t V, DiagnosticsEngine::ArgumentKind Kind) const {
271
1.54M
    if (!DiagStorage)
272
1.14M
      DiagStorage = getStorage();
273
1.54M
274
1.54M
    assert(DiagStorage->NumDiagArgs < Storage::MaxArguments &&
275
1.54M
           "Too many arguments to diagnostic!");
276
1.54M
    DiagStorage->DiagArgumentsKind[DiagStorage->NumDiagArgs] = Kind;
277
1.54M
    DiagStorage->DiagArgumentsVal[DiagStorage->NumDiagArgs++] = V;
278
1.54M
  }
279
280
76.9k
  void AddString(StringRef V) const {
281
76.9k
    if (!DiagStorage)
282
58.2k
      DiagStorage = getStorage();
283
76.9k
284
76.9k
    assert(DiagStorage->NumDiagArgs < Storage::MaxArguments &&
285
76.9k
           "Too many arguments to diagnostic!");
286
76.9k
    DiagStorage->DiagArgumentsKind[DiagStorage->NumDiagArgs]
287
76.9k
      = DiagnosticsEngine::ak_std_string;
288
76.9k
    DiagStorage->DiagArgumentsStr[DiagStorage->NumDiagArgs++] = std::string(V);
289
76.9k
  }
290
291
155k
  void Emit(const DiagnosticBuilder &DB) const {
292
155k
    if (!DiagStorage)
293
13.0k
      return;
294
142k
295
    // Add all arguments.
296
481k
    
for (unsigned i = 0, e = DiagStorage->NumDiagArgs; 142k
i != e;
++i338k
) {
297
338k
      if ((DiagnosticsEngine::ArgumentKind)DiagStorage->DiagArgumentsKind[i]
298
338k
            == DiagnosticsEngine::ak_std_string)
299
26.3k
        DB.AddString(DiagStorage->DiagArgumentsStr[i]);
300
312k
      else
301
312k
        DB.AddTaggedVal(DiagStorage->DiagArgumentsVal[i],
302
312k
            (DiagnosticsEngine::ArgumentKind)DiagStorage->DiagArgumentsKind[i]);
303
338k
    }
304
142k
305
    // Add all ranges.
306
142k
    for (const CharSourceRange &Range : DiagStorage->DiagRanges)
307
212k
      DB.AddSourceRange(Range);
308
142k
309
    // Add all fix-its.
310
142k
    for (const FixItHint &Fix : DiagStorage->FixItHints)
311
221
      DB.AddFixItHint(Fix);
312
142k
  }
313
314
  void EmitToString(DiagnosticsEngine &Diags,
315
2.41k
                    SmallVectorImpl<char> &Buf) const {
316
    // FIXME: It should be possible to render a diagnostic to a string without
317
    //        messing with the state of the diagnostics engine.
318
2.41k
    DiagnosticBuilder DB(Diags.Report(getDiagID()));
319
2.41k
    Emit(DB);
320
2.41k
    DB.FlushCounts();
321
2.41k
    Diagnostic(&Diags).FormatDiagnostic(Buf);
322
2.41k
    DB.Clear();
323
2.41k
    Diags.Clear();
324
2.41k
  }
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
845k
  void Reset(unsigned DiagID = 0) {
329
845k
    this->DiagID = DiagID;
330
845k
    freeStorage();
331
845k
  }
332
333
97
  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
51.5k
                                             unsigned I) {
346
51.5k
    PD.AddTaggedVal(I, DiagnosticsEngine::ak_uint);
347
51.5k
    return PD;
348
51.5k
  }
349
350
  friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
351
207k
                                             int I) {
352
207k
    PD.AddTaggedVal(I, DiagnosticsEngine::ak_sint);
353
207k
    return PD;
354
207k
  }
355
356
  friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
357
2.14k
                                                    const char *S) {
358
2.14k
    PD.AddTaggedVal(reinterpret_cast<intptr_t>(S),
359
2.14k
                    DiagnosticsEngine::ak_c_string);
360
2.14k
    return PD;
361
2.14k
  }
362
363
  friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
364
27.7k
                                                    StringRef S) {
365
27.7k
366
27.7k
    PD.AddString(S);
367
27.7k
    return PD;
368
27.7k
  }
369
370
  friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
371
524
                                                    const IdentifierInfo *II) {
372
524
    PD.AddTaggedVal(reinterpret_cast<intptr_t>(II),
373
524
                    DiagnosticsEngine::ak_identifierinfo);
374
524
    return PD;
375
524
  }
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
297
  operator<<(const PartialDiagnostic &PD, T *DC) {
385
297
    PD.AddTaggedVal(reinterpret_cast<intptr_t>(DC),
386
297
                    DiagnosticsEngine::ak_declcontext);
387
297
    return PD;
388
297
  }
389
390
  friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
391
241k
                                                    SourceRange R) {
392
241k
    PD.AddSourceRange(CharSourceRange::getTokenRange(R));
393
241k
    return PD;
394
241k
  }
395
396
  friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
397
3.56k
                                                    const CharSourceRange &R) {
398
3.56k
    PD.AddSourceRange(R);
399
3.56k
    return PD;
400
3.56k
  }
401
402
  friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
403
220
                                             const FixItHint &Hint) {
404
220
    PD.AddFixItHint(Hint);
405
220
    return PD;
406
220
  }
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