Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Support/SourceMgr.h
Line
Count
Source (jump to first uncovered line)
1
//===- SourceMgr.h - Manager for Source Buffers & Diagnostics ---*- 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
// This file declares the SMDiagnostic and SourceMgr classes.  This
10
// provides a simple substrate for diagnostics, #include handling, and other low
11
// level things for simple parsers.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#ifndef LLVM_SUPPORT_SOURCEMGR_H
16
#define LLVM_SUPPORT_SOURCEMGR_H
17
18
#include "llvm/ADT/ArrayRef.h"
19
#include "llvm/ADT/None.h"
20
#include "llvm/ADT/PointerUnion.h"
21
#include "llvm/ADT/SmallVector.h"
22
#include "llvm/ADT/StringRef.h"
23
#include "llvm/ADT/Twine.h"
24
#include "llvm/Support/MemoryBuffer.h"
25
#include "llvm/Support/SMLoc.h"
26
#include <algorithm>
27
#include <cassert>
28
#include <memory>
29
#include <string>
30
#include <utility>
31
#include <vector>
32
33
namespace llvm {
34
35
class raw_ostream;
36
class SMDiagnostic;
37
class SMFixIt;
38
39
/// This owns the files read by a parser, handles include stacks,
40
/// and handles diagnostic wrangling.
41
class SourceMgr {
42
public:
43
  enum DiagKind {
44
    DK_Error,
45
    DK_Warning,
46
    DK_Remark,
47
    DK_Note,
48
  };
49
50
  /// Clients that want to handle their own diagnostics in a custom way can
51
  /// register a function pointer+context as a diagnostic handler.
52
  /// It gets called each time PrintMessage is invoked.
53
  using DiagHandlerTy = void (*)(const SMDiagnostic &, void *Context);
54
55
private:
56
  struct SrcBuffer {
57
    /// The memory buffer for the file.
58
    std::unique_ptr<MemoryBuffer> Buffer;
59
60
    /// Helper type for OffsetCache below: since we're storing many offsets
61
    /// into relatively small files (often smaller than 2^8 or 2^16 bytes),
62
    /// we select the offset vector element type dynamically based on the
63
    /// size of Buffer.
64
    using VariableSizeOffsets = PointerUnion4<std::vector<uint8_t> *,
65
                                              std::vector<uint16_t> *,
66
                                              std::vector<uint32_t> *,
67
                                              std::vector<uint64_t> *>;
68
69
    /// Vector of offsets into Buffer at which there are line-endings
70
    /// (lazily populated). Once populated, the '\n' that marks the end of
71
    /// line number N from [1..] is at Buffer[OffsetCache[N-1]]. Since
72
    /// these offsets are in sorted (ascending) order, they can be
73
    /// binary-searched for the first one after any given offset (eg. an
74
    /// offset corresponding to a particular SMLoc).
75
    mutable VariableSizeOffsets OffsetCache;
76
77
    /// Populate \c OffsetCache and look up a given \p Ptr in it, assuming
78
    /// it points somewhere into \c Buffer. The static type parameter \p T
79
    /// must be an unsigned integer type from uint{8,16,32,64}_t large
80
    /// enough to store offsets inside \c Buffer.
81
    template<typename T>
82
    unsigned getLineNumber(const char *Ptr) const;
83
84
    /// This is the location of the parent include, or null if at the top level.
85
    SMLoc IncludeLoc;
86
87
306k
    SrcBuffer() = default;
88
    SrcBuffer(SrcBuffer &&);
89
    SrcBuffer(const SrcBuffer &) = delete;
90
    SrcBuffer &operator=(const SrcBuffer &) = delete;
91
    ~SrcBuffer();
92
  };
93
94
  /// This is all of the buffers that we are reading from.
95
  std::vector<SrcBuffer> Buffers;
96
97
  // This is the list of directories we should search for include files in.
98
  std::vector<std::string> IncludeDirectories;
99
100
  DiagHandlerTy DiagHandler = nullptr;
101
  void *DiagContext = nullptr;
102
103
0
  bool isValidBufferID(unsigned i) const { return i && i <= Buffers.size(); }
104
105
public:
106
84.2k
  SourceMgr() = default;
107
  SourceMgr(const SourceMgr &) = delete;
108
  SourceMgr &operator=(const SourceMgr &) = delete;
109
  SourceMgr(SourceMgr &&) = default;
110
  SourceMgr &operator=(SourceMgr &&) = default;
111
83.8k
  ~SourceMgr() = default;
112
113
18.6k
  void setIncludeDirs(const std::vector<std::string> &Dirs) {
114
18.6k
    IncludeDirectories = Dirs;
115
18.6k
  }
116
117
  /// Specify a diagnostic handler to be invoked every time PrintMessage is
118
  /// called. \p Ctx is passed into the handler when it is invoked.
119
41.2k
  void setDiagHandler(DiagHandlerTy DH, void *Ctx = nullptr) {
120
41.2k
    DiagHandler = DH;
121
41.2k
    DiagContext = Ctx;
122
41.2k
  }
123
124
18.4k
  DiagHandlerTy getDiagHandler() const { return DiagHandler; }
125
18.4k
  void *getDiagContext() const { return DiagContext; }
126
127
100k
  const SrcBuffer &getBufferInfo(unsigned i) const {
128
100k
    assert(isValidBufferID(i));
129
100k
    return Buffers[i - 1];
130
100k
  }
131
132
559k
  const MemoryBuffer *getMemoryBuffer(unsigned i) const {
133
559k
    assert(isValidBufferID(i));
134
559k
    return Buffers[i - 1].Buffer.get();
135
559k
  }
136
137
12.0k
  unsigned getNumBuffers() const {
138
12.0k
    return Buffers.size();
139
12.0k
  }
140
141
111k
  unsigned getMainFileID() const {
142
111k
    assert(getNumBuffers());
143
111k
    return 1;
144
111k
  }
145
146
28.0k
  SMLoc getParentIncludeLoc(unsigned i) const {
147
28.0k
    assert(isValidBufferID(i));
148
28.0k
    return Buffers[i - 1].IncludeLoc;
149
28.0k
  }
150
151
  /// Add a new source buffer to this source manager. This takes ownership of
152
  /// the memory buffer.
153
  unsigned AddNewSourceBuffer(std::unique_ptr<MemoryBuffer> F,
154
306k
                              SMLoc IncludeLoc) {
155
306k
    SrcBuffer NB;
156
306k
    NB.Buffer = std::move(F);
157
306k
    NB.IncludeLoc = IncludeLoc;
158
306k
    Buffers.push_back(std::move(NB));
159
306k
    return Buffers.size();
160
306k
  }
161
162
  /// Search for a file with the specified name in the current directory or in
163
  /// one of the IncludeDirs.
164
  ///
165
  /// If no file is found, this returns 0, otherwise it returns the buffer ID
166
  /// of the stacked file. The full path to the included file can be found in
167
  /// \p IncludedFile.
168
  unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc,
169
                          std::string &IncludedFile);
170
171
  /// Return the ID of the buffer containing the specified location.
172
  ///
173
  /// 0 is returned if the buffer is not found.
174
  unsigned FindBufferContainingLoc(SMLoc Loc) const;
175
176
  /// Find the line number for the specified location in the specified file.
177
  /// This is not a fast method.
178
2.69k
  unsigned FindLineNumber(SMLoc Loc, unsigned BufferID = 0) const {
179
2.69k
    return getLineAndColumn(Loc, BufferID).first;
180
2.69k
  }
181
182
  /// Find the line and column number for the specified location in the
183
  /// specified file. This is not a fast method.
184
  std::pair<unsigned, unsigned> getLineAndColumn(SMLoc Loc,
185
                                                 unsigned BufferID = 0) const;
186
187
  /// Emit a message about the specified location with the specified string.
188
  ///
189
  /// \param ShowColors Display colored messages if output is a terminal and
190
  /// the default error handler is used.
191
  void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind,
192
                    const Twine &Msg,
193
                    ArrayRef<SMRange> Ranges = None,
194
                    ArrayRef<SMFixIt> FixIts = None,
195
                    bool ShowColors = true) const;
196
197
  /// Emits a diagnostic to llvm::errs().
198
  void PrintMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
199
                    ArrayRef<SMRange> Ranges = None,
200
                    ArrayRef<SMFixIt> FixIts = None,
201
                    bool ShowColors = true) const;
202
203
  /// Emits a manually-constructed diagnostic to the given output stream.
204
  ///
205
  /// \param ShowColors Display colored messages if output is a terminal and
206
  /// the default error handler is used.
207
  void PrintMessage(raw_ostream &OS, const SMDiagnostic &Diagnostic,
208
                    bool ShowColors = true) const;
209
210
  /// Return an SMDiagnostic at the specified location with the specified
211
  /// string.
212
  ///
213
  /// \param Msg If non-null, the kind of message (e.g., "error") which is
214
  /// prefixed to the message.
215
  SMDiagnostic GetMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
216
                          ArrayRef<SMRange> Ranges = None,
217
                          ArrayRef<SMFixIt> FixIts = None) const;
218
219
  /// Prints the names of included files and the line of the file they were
220
  /// included from. A diagnostic handler can use this before printing its
221
  /// custom formatted message.
222
  ///
223
  /// \param IncludeLoc The location of the include.
224
  /// \param OS the raw_ostream to print on.
225
  void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
226
};
227
228
/// Represents a single fixit, a replacement of one range of text with another.
229
class SMFixIt {
230
  SMRange Range;
231
232
  std::string Text;
233
234
public:
235
  // FIXME: Twine.str() is not very efficient.
236
  SMFixIt(SMLoc Loc, const Twine &Insertion)
237
0
    : Range(Loc, Loc), Text(Insertion.str()) {
238
0
    assert(Loc.isValid());
239
0
  }
240
241
  // FIXME: Twine.str() is not very efficient.
242
  SMFixIt(SMRange R, const Twine &Replacement)
243
23
    : Range(R), Text(Replacement.str()) {
244
23
    assert(R.isValid());
245
23
  }
246
247
92
  StringRef getText() const { return Text; }
248
23
  SMRange getRange() const { return Range; }
249
250
0
  bool operator<(const SMFixIt &Other) const {
251
0
    if (Range.Start.getPointer() != Other.Range.Start.getPointer())
252
0
      return Range.Start.getPointer() < Other.Range.Start.getPointer();
253
0
    if (Range.End.getPointer() != Other.Range.End.getPointer())
254
0
      return Range.End.getPointer() < Other.Range.End.getPointer();
255
0
    return Text < Other.Text;
256
0
  }
257
};
258
259
/// Instances of this class encapsulate one diagnostic report, allowing
260
/// printing to a raw_ostream as a caret diagnostic.
261
class SMDiagnostic {
262
  const SourceMgr *SM = nullptr;
263
  SMLoc Loc;
264
  std::string Filename;
265
  int LineNo = 0;
266
  int ColumnNo = 0;
267
  SourceMgr::DiagKind Kind = SourceMgr::DK_Error;
268
  std::string Message, LineContents;
269
  std::vector<std::pair<unsigned, unsigned>> Ranges;
270
  SmallVector<SMFixIt, 4> FixIts;
271
272
public:
273
  // Null diagnostic.
274
81.3k
  SMDiagnostic() = default;
275
  // Diagnostic with no location (e.g. file not found, command line arg error).
276
  SMDiagnostic(StringRef filename, SourceMgr::DiagKind Knd, StringRef Msg)
277
9
    : Filename(filename), LineNo(-1), ColumnNo(-1), Kind(Knd), Message(Msg) {}
278
279
  // Diagnostic with a location.
280
  SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN,
281
               int Line, int Col, SourceMgr::DiagKind Kind,
282
               StringRef Msg, StringRef LineStr,
283
               ArrayRef<std::pair<unsigned,unsigned>> Ranges,
284
               ArrayRef<SMFixIt> FixIts = None);
285
286
87.6k
  const SourceMgr *getSourceMgr() const { return SM; }
287
97.8k
  SMLoc getLoc() const { return Loc; }
288
  StringRef getFilename() const { return Filename; }
289
124
  int getLineNo() const { return LineNo; }
290
219
  int getColumnNo() const { return ColumnNo; }
291
669
  SourceMgr::DiagKind getKind() const { return Kind; }
292
249
  StringRef getMessage() const { return Message; }
293
303
  StringRef getLineContents() const { return LineContents; }
294
205
  ArrayRef<std::pair<unsigned, unsigned>> getRanges() const { return Ranges; }
295
296
0
  void addFixIt(const SMFixIt &Hint) {
297
0
    FixIts.push_back(Hint);
298
0
  }
299
300
129
  ArrayRef<SMFixIt> getFixIts() const {
301
129
    return FixIts;
302
129
  }
303
304
  void print(const char *ProgName, raw_ostream &S, bool ShowColors = true,
305
             bool ShowKindLabel = true) const;
306
};
307
308
} // end namespace llvm
309
310
#endif // LLVM_SUPPORT_SOURCEMGR_H