Coverage Report

Created: 2019-02-21 13:17

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
0
  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
657k
    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.77M
    Storage *Allocate() {
96
1.77M
      if (NumFreeListEntries == 0)
97
26.6k
        return new Storage;
98
1.74M
99
1.74M
      Storage *Result = FreeList[--NumFreeListEntries];
100
1.74M
      Result->NumDiagArgs = 0;
101
1.74M
      Result->DiagRanges.clear();
102
1.74M
      Result->FixItHints.clear();
103
1.74M
      return Result;
104
1.74M
    }
105
106
    /// Free the given storage object.
107
1.76M
    void Deallocate(Storage *S) {
108
1.76M
      if (S >= Cached && 
S <= Cached + NumCached1.74M
) {
109
1.74M
        FreeList[NumFreeListEntries++] = S;
110
1.74M
        return;
111
1.74M
      }
112
17.1k
113
17.1k
      delete S;
114
17.1k
    }
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.77M
  Storage *getStorage() const {
133
1.77M
    if (DiagStorage)
134
0
      return DiagStorage;
135
1.77M
136
1.77M
    if (Allocator)
137
1.77M
      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.77M
    return DiagStorage;
143
1.77M
  }
144
145
11.8M
  void freeStorage() {
146
11.8M
    if (!DiagStorage)
147
10.1M
      return;
148
1.76M
149
1.76M
    // The hot path for PartialDiagnostic is when we just used it to wrap an ID
150
1.76M
    // (typically so we have the flexibility of passing a more complex
151
1.76M
    // diagnostic into the callee, but that does not commonly occur).
152
1.76M
    //
153
1.76M
    // Split this out into a slow function for silly compilers (*cough*) which
154
1.76M
    // can't do decent partial inlining.
155
1.76M
    freeStorageSlow();
156
1.76M
  }
157
158
1.76M
  void freeStorageSlow() {
159
1.76M
    if (Allocator)
160
1.76M
      Allocator->Deallocate(DiagStorage);
161
0
    else if (Allocator != reinterpret_cast<StorageAllocator *>(~uintptr_t(0)))
162
0
      delete DiagStorage;
163
1.76M
    DiagStorage = nullptr;
164
1.76M
  }
165
166
323k
  void AddSourceRange(const CharSourceRange &R) const {
167
323k
    if (!DiagStorage)
168
47.8k
      DiagStorage = getStorage();
169
323k
170
323k
    DiagStorage->DiagRanges.push_back(R);
171
323k
  }
172
173
16.6k
  void AddFixItHint(const FixItHint &Hint) const {
174
16.6k
    if (Hint.isNull())
175
16.3k
      return;
176
257
177
257
    if (!DiagStorage)
178
109
      DiagStorage = getStorage();
179
257
180
257
    DiagStorage->FixItHints.push_back(Hint);
181
257
  }
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
123k
  PartialDiagnostic(NullDiagnostic) {}
189
190
  PartialDiagnostic(unsigned DiagID, StorageAllocator &Allocator)
191
4.98M
      : DiagID(DiagID), Allocator(&Allocator) {}
192
193
  PartialDiagnostic(const PartialDiagnostic &Other)
194
2.86M
      : DiagID(Other.DiagID), Allocator(Other.Allocator) {
195
2.86M
    if (Other.DiagStorage) {
196
77.2k
      DiagStorage = getStorage();
197
77.2k
      *DiagStorage = *Other.DiagStorage;
198
77.2k
    }
199
2.86M
  }
200
201
  PartialDiagnostic(PartialDiagnostic &&Other)
202
      : DiagID(Other.DiagID), DiagStorage(Other.DiagStorage),
203
1.94M
        Allocator(Other.Allocator) {
204
1.94M
    Other.DiagStorage = nullptr;
205
1.94M
  }
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
135k
      : DiagID(Other.getID()), Allocator(&Allocator) {
216
135k
    // Copy arguments.
217
292k
    for (unsigned I = 0, N = Other.getNumArgs(); I != N; 
++I157k
) {
218
157k
      if (Other.getArgKind(I) == DiagnosticsEngine::ak_std_string)
219
97.4k
        AddString(Other.getArgStdStr(I));
220
59.6k
      else
221
59.6k
        AddTaggedVal(Other.getRawArg(I), Other.getArgKind(I));
222
157k
    }
223
135k
224
135k
    // Copy source ranges.
225
264k
    for (unsigned I = 0, N = Other.getNumRanges(); I != N; 
++I129k
)
226
129k
      AddSourceRange(Other.getRange(I));
227
135k
228
135k
    // Copy fix-its.
229
135k
    for (unsigned I = 0, N = Other.getNumFixItHints(); I != N; 
++I45
)
230
45
      AddFixItHint(Other.getFixItHint(I));
231
135k
  }
232
233
97.0k
  PartialDiagnostic &operator=(const PartialDiagnostic &Other) {
234
97.0k
    DiagID = Other.DiagID;
235
97.0k
    if (Other.DiagStorage) {
236
15.1k
      if (!DiagStorage)
237
15.1k
        DiagStorage = getStorage();
238
15.1k
239
15.1k
      *DiagStorage = *Other.DiagStorage;
240
81.8k
    } else {
241
81.8k
      freeStorage();
242
81.8k
    }
243
97.0k
244
97.0k
    return *this;
245
97.0k
  }
246
247
13.9k
  PartialDiagnostic &operator=(PartialDiagnostic &&Other) {
248
13.9k
    freeStorage();
249
13.9k
250
13.9k
    DiagID = Other.DiagID;
251
13.9k
    DiagStorage = Other.DiagStorage;
252
13.9k
    Allocator = Other.Allocator;
253
13.9k
254
13.9k
    Other.DiagStorage = nullptr;
255
13.9k
    return *this;
256
13.9k
  }
257
258
10.0M
  ~PartialDiagnostic() {
259
10.0M
    freeStorage();
260
10.0M
  }
261
262
123k
  void swap(PartialDiagnostic &PD) {
263
123k
    std::swap(DiagID, PD.DiagID);
264
123k
    std::swap(DiagStorage, PD.DiagStorage);
265
123k
    std::swap(Allocator, PD.Allocator);
266
123k
  }
267
268
100k
  unsigned getDiagID() const { return DiagID; }
269
270
1.92M
  void AddTaggedVal(intptr_t V, DiagnosticsEngine::ArgumentKind Kind) const {
271
1.92M
    if (!DiagStorage)
272
1.51M
      DiagStorage = getStorage();
273
1.92M
274
1.92M
    assert(DiagStorage->NumDiagArgs < Storage::MaxArguments &&
275
1.92M
           "Too many arguments to diagnostic!");
276
1.92M
    DiagStorage->DiagArgumentsKind[DiagStorage->NumDiagArgs] = Kind;
277
1.92M
    DiagStorage->DiagArgumentsVal[DiagStorage->NumDiagArgs++] = V;
278
1.92M
  }
279
280
135k
  void AddString(StringRef V) const {
281
135k
    if (!DiagStorage)
282
111k
      DiagStorage = getStorage();
283
135k
284
135k
    assert(DiagStorage->NumDiagArgs < Storage::MaxArguments &&
285
135k
           "Too many arguments to diagnostic!");
286
135k
    DiagStorage->DiagArgumentsKind[DiagStorage->NumDiagArgs]
287
135k
      = DiagnosticsEngine::ak_std_string;
288
135k
    DiagStorage->DiagArgumentsStr[DiagStorage->NumDiagArgs++] = V;
289
135k
  }
290
291
86.6k
  void Emit(const DiagnosticBuilder &DB) const {
292
86.6k
    if (!DiagStorage)
293
5.09k
      return;
294
81.5k
295
81.5k
    // Add all arguments.
296
302k
    
for (unsigned i = 0, e = DiagStorage->NumDiagArgs; 81.5k
i != e;
++i221k
) {
297
221k
      if ((DiagnosticsEngine::ArgumentKind)DiagStorage->DiagArgumentsKind[i]
298
221k
            == DiagnosticsEngine::ak_std_string)
299
18.6k
        DB.AddString(DiagStorage->DiagArgumentsStr[i]);
300
202k
      else
301
202k
        DB.AddTaggedVal(DiagStorage->DiagArgumentsVal[i],
302
202k
            (DiagnosticsEngine::ArgumentKind)DiagStorage->DiagArgumentsKind[i]);
303
221k
    }
304
81.5k
305
81.5k
    // Add all ranges.
306
81.5k
    for (const CharSourceRange &Range : DiagStorage->DiagRanges)
307
112k
      DB.AddSourceRange(Range);
308
81.5k
309
81.5k
    // Add all fix-its.
310
81.5k
    for (const FixItHint &Fix : DiagStorage->FixItHints)
311
217
      DB.AddFixItHint(Fix);
312
81.5k
  }
313
314
  void EmitToString(DiagnosticsEngine &Diags,
315
201
                    SmallVectorImpl<char> &Buf) const {
316
201
    // FIXME: It should be possible to render a diagnostic to a string without
317
201
    //        messing with the state of the diagnostics engine.
318
201
    DiagnosticBuilder DB(Diags.Report(getDiagID()));
319
201
    Emit(DB);
320
201
    DB.FlushCounts();
321
201
    Diagnostic(&Diags).FormatDiagnostic(Buf);
322
201
    DB.Clear();
323
201
    Diags.Clear();
324
201
  }
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
1.73M
  void Reset(unsigned DiagID = 0) {
329
1.73M
    this->DiagID = DiagID;
330
1.73M
    freeStorage();
331
1.73M
  }
332
333
73
  bool hasStorage() const { return DiagStorage != nullptr; }
334
335
  /// Retrieve the string argument at the given index.
336
16
  StringRef getStringArg(unsigned I) {
337
16
    assert(DiagStorage && "No diagnostic storage?");
338
16
    assert(I < DiagStorage->NumDiagArgs && "Not enough diagnostic args");
339
16
    assert(DiagStorage->DiagArgumentsKind[I]
340
16
             == DiagnosticsEngine::ak_std_string && "Not a string arg");
341
16
    return DiagStorage->DiagArgumentsStr[I];
342
16
  }
343
344
  friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
345
42.9k
                                             unsigned I) {
346
42.9k
    PD.AddTaggedVal(I, DiagnosticsEngine::ak_uint);
347
42.9k
    return PD;
348
42.9k
  }
349
350
  friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
351
348k
                                             int I) {
352
348k
    PD.AddTaggedVal(I, DiagnosticsEngine::ak_sint);
353
348k
    return PD;
354
348k
  }
355
356
  friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
357
1.42k
                                                    const char *S) {
358
1.42k
    PD.AddTaggedVal(reinterpret_cast<intptr_t>(S),
359
1.42k
                    DiagnosticsEngine::ak_c_string);
360
1.42k
    return PD;
361
1.42k
  }
362
363
  friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
364
38.0k
                                                    StringRef S) {
365
38.0k
366
38.0k
    PD.AddString(S);
367
38.0k
    return PD;
368
38.0k
  }
369
370
  friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
371
389
                                                    const IdentifierInfo *II) {
372
389
    PD.AddTaggedVal(reinterpret_cast<intptr_t>(II),
373
389
                    DiagnosticsEngine::ak_identifierinfo);
374
389
    return PD;
375
389
  }
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
383
  typename std::enable_if<std::is_same<T, DeclContext>::value,
384
                          const PartialDiagnostic &>::type
385
268
  operator<<(const PartialDiagnostic &PD, T *DC) {
386
268
    PD.AddTaggedVal(reinterpret_cast<intptr_t>(DC),
387
268
                    DiagnosticsEngine::ak_declcontext);
388
268
    return PD;
389
268
  }
390
391
  friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
392
194k
                                                    SourceRange R) {
393
194k
    PD.AddSourceRange(CharSourceRange::getTokenRange(R));
394
194k
    return PD;
395
194k
  }
396
397
  friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
398
331
                                                    const CharSourceRange &R) {
399
331
    PD.AddSourceRange(R);
400
331
    return PD;
401
331
  }
402
403
  friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
404
16.5k
                                             const FixItHint &Hint) {
405
16.5k
    PD.AddFixItHint(Hint);
406
16.5k
    return PD;
407
16.5k
  }
408
};
409
410
inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
411
0
                                           const PartialDiagnostic &PD) {
412
0
  PD.Emit(DB);
413
0
  return DB;
414
0
}
415
416
/// A partial diagnostic along with the source location where this
417
/// diagnostic occurs.
418
using PartialDiagnosticAt = std::pair<SourceLocation, PartialDiagnostic>;
419
420
} // namespace clang
421
422
#endif // LLVM_CLANG_BASIC_PARTIALDIAGNOSTIC_H