Bug Summary

File:tools/clang/include/clang/Basic/SourceManager.h
Warning:line 833, column 29
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name Refactoring.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -relaxed-aliasing -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -D CLANG_VENDOR="Debian " -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-10~svn374877/build-llvm/tools/clang/lib/Tooling -I /build/llvm-toolchain-snapshot-10~svn374877/tools/clang/lib/Tooling -I /build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include -I /build/llvm-toolchain-snapshot-10~svn374877/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-10~svn374877/build-llvm/include -I /build/llvm-toolchain-snapshot-10~svn374877/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-10~svn374877/build-llvm/tools/clang/lib/Tooling -fdebug-prefix-map=/build/llvm-toolchain-snapshot-10~svn374877=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fno-common -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2019-10-15-233810-7101-1 -x c++ /build/llvm-toolchain-snapshot-10~svn374877/tools/clang/lib/Tooling/Refactoring.cpp

/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/lib/Tooling/Refactoring.cpp

1//===--- Refactoring.cpp - Framework for clang refactoring tools ----------===//
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// Implements tools to support refactorings.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/Tooling/Refactoring.h"
14#include "clang/Basic/DiagnosticOptions.h"
15#include "clang/Basic/FileManager.h"
16#include "clang/Basic/SourceManager.h"
17#include "clang/Format/Format.h"
18#include "clang/Frontend/TextDiagnosticPrinter.h"
19#include "clang/Lex/Lexer.h"
20#include "clang/Rewrite/Core/Rewriter.h"
21#include "llvm/Support/Path.h"
22#include "llvm/Support/raw_os_ostream.h"
23
24namespace clang {
25namespace tooling {
26
27RefactoringTool::RefactoringTool(
28 const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths,
29 std::shared_ptr<PCHContainerOperations> PCHContainerOps)
30 : ClangTool(Compilations, SourcePaths, std::move(PCHContainerOps)) {}
31
32std::map<std::string, Replacements> &RefactoringTool::getReplacements() {
33 return FileToReplaces;
34}
35
36int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) {
37 if (int Result = run(ActionFactory)) {
38 return Result;
39 }
40
41 LangOptions DefaultLangOptions;
42 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
43 TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts);
44 DiagnosticsEngine Diagnostics(
45 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
46 &*DiagOpts, &DiagnosticPrinter, false);
47 SourceManager Sources(Diagnostics, getFiles());
48 Rewriter Rewrite(Sources, DefaultLangOptions);
49
50 if (!applyAllReplacements(Rewrite)) {
51 llvm::errs() << "Skipped some replacements.\n";
52 }
53
54 return saveRewrittenFiles(Rewrite);
55}
56
57bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) {
58 bool Result = true;
59 for (const auto &Entry : groupReplacementsByFile(
60 Rewrite.getSourceMgr().getFileManager(), FileToReplaces))
61 Result = tooling::applyAllReplacements(Entry.second, Rewrite) && Result;
62 return Result;
63}
64
65int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) {
66 return Rewrite.overwriteChangedFiles() ? 1 : 0;
67}
68
69bool formatAndApplyAllReplacements(
70 const std::map<std::string, Replacements> &FileToReplaces,
71 Rewriter &Rewrite, StringRef Style) {
72 SourceManager &SM = Rewrite.getSourceMgr();
73 FileManager &Files = SM.getFileManager();
74
75 bool Result = true;
76 for (const auto &FileAndReplaces : groupReplacementsByFile(
77 Rewrite.getSourceMgr().getFileManager(), FileToReplaces)) {
78 const std::string &FilePath = FileAndReplaces.first;
79 auto &CurReplaces = FileAndReplaces.second;
80
81 const FileEntry *Entry = nullptr;
1
'Entry' initialized to a null pointer value
82 if (auto File = Files.getFile(FilePath))
2
Taking false branch
83 Entry = *File;
84
85 FileID ID = SM.getOrCreateFileID(Entry, SrcMgr::C_User);
3
Passing null pointer value via 1st parameter 'SourceFile'
4
Calling 'SourceManager::getOrCreateFileID'
86 StringRef Code = SM.getBufferData(ID);
87
88 auto CurStyle = format::getStyle(Style, FilePath, "LLVM");
89 if (!CurStyle) {
90 llvm::errs() << llvm::toString(CurStyle.takeError()) << "\n";
91 return false;
92 }
93
94 auto NewReplacements =
95 format::formatReplacements(Code, CurReplaces, *CurStyle);
96 if (!NewReplacements) {
97 llvm::errs() << llvm::toString(NewReplacements.takeError()) << "\n";
98 return false;
99 }
100 Result = applyAllReplacements(*NewReplacements, Rewrite) && Result;
101 }
102 return Result;
103}
104
105} // end namespace tooling
106} // end namespace clang

/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h

1//===- SourceManager.h - Track and cache source files -----------*- 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/// Defines the SourceManager interface.
11///
12/// There are three different types of locations in a %file: a spelling
13/// location, an expansion location, and a presumed location.
14///
15/// Given an example of:
16/// \code
17/// #define min(x, y) x < y ? x : y
18/// \endcode
19///
20/// and then later on a use of min:
21/// \code
22/// #line 17
23/// return min(a, b);
24/// \endcode
25///
26/// The expansion location is the line in the source code where the macro
27/// was expanded (the return statement), the spelling location is the
28/// location in the source where the macro was originally defined,
29/// and the presumed location is where the line directive states that
30/// the line is 17, or any other line.
31//
32//===----------------------------------------------------------------------===//
33
34#ifndef LLVM_CLANG_BASIC_SOURCEMANAGER_H
35#define LLVM_CLANG_BASIC_SOURCEMANAGER_H
36
37#include "clang/Basic/Diagnostic.h"
38#include "clang/Basic/FileManager.h"
39#include "clang/Basic/SourceLocation.h"
40#include "llvm/ADT/ArrayRef.h"
41#include "llvm/ADT/BitVector.h"
42#include "llvm/ADT/DenseMap.h"
43#include "llvm/ADT/DenseSet.h"
44#include "llvm/ADT/IntrusiveRefCntPtr.h"
45#include "llvm/ADT/PointerIntPair.h"
46#include "llvm/ADT/SmallVector.h"
47#include "llvm/ADT/StringRef.h"
48#include "llvm/Support/Allocator.h"
49#include "llvm/Support/Compiler.h"
50#include "llvm/Support/MemoryBuffer.h"
51#include <cassert>
52#include <cstddef>
53#include <map>
54#include <memory>
55#include <string>
56#include <utility>
57#include <vector>
58
59namespace clang {
60
61class ASTReader;
62class ASTWriter;
63class LineTableInfo;
64class SourceManager;
65
66/// Public enums and private classes that are part of the
67/// SourceManager implementation.
68namespace SrcMgr {
69
70 /// Indicates whether a file or directory holds normal user code,
71 /// system code, or system code which is implicitly 'extern "C"' in C++ mode.
72 ///
73 /// Entire directories can be tagged with this (this is maintained by
74 /// DirectoryLookup and friends) as can specific FileInfos when a \#pragma
75 /// system_header is seen or in various other cases.
76 ///
77 enum CharacteristicKind {
78 C_User, C_System, C_ExternCSystem, C_User_ModuleMap, C_System_ModuleMap
79 };
80
81 /// Determine whether a file / directory characteristic is for system code.
82 inline bool isSystem(CharacteristicKind CK) {
83 return CK != C_User && CK != C_User_ModuleMap;
84 }
85
86 /// Determine whether a file characteristic is for a module map.
87 inline bool isModuleMap(CharacteristicKind CK) {
88 return CK == C_User_ModuleMap || CK == C_System_ModuleMap;
89 }
90
91 /// One instance of this struct is kept for every file loaded or used.
92 ///
93 /// This object owns the MemoryBuffer object.
94 class alignas(8) ContentCache {
95 enum CCFlags {
96 /// Whether the buffer is invalid.
97 InvalidFlag = 0x01,
98
99 /// Whether the buffer should not be freed on destruction.
100 DoNotFreeFlag = 0x02
101 };
102
103 /// The actual buffer containing the characters from the input
104 /// file.
105 ///
106 /// This is owned by the ContentCache object. The bits indicate
107 /// whether the buffer is invalid.
108 mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
109
110 public:
111 /// Reference to the file entry representing this ContentCache.
112 ///
113 /// This reference does not own the FileEntry object.
114 ///
115 /// It is possible for this to be NULL if the ContentCache encapsulates
116 /// an imaginary text buffer.
117 const FileEntry *OrigEntry;
118
119 /// References the file which the contents were actually loaded from.
120 ///
121 /// Can be different from 'Entry' if we overridden the contents of one file
122 /// with the contents of another file.
123 const FileEntry *ContentsEntry;
124
125 /// A bump pointer allocated array of offsets for each source line.
126 ///
127 /// This is lazily computed. This is owned by the SourceManager
128 /// BumpPointerAllocator object.
129 unsigned *SourceLineCache = nullptr;
130
131 /// The number of lines in this ContentCache.
132 ///
133 /// This is only valid if SourceLineCache is non-null.
134 unsigned NumLines = 0;
135
136 /// Indicates whether the buffer itself was provided to override
137 /// the actual file contents.
138 ///
139 /// When true, the original entry may be a virtual file that does not
140 /// exist.
141 unsigned BufferOverridden : 1;
142
143 /// True if this content cache was initially created for a source file
144 /// considered to be volatile (likely to change between stat and open).
145 unsigned IsFileVolatile : 1;
146
147 /// True if this file may be transient, that is, if it might not
148 /// exist at some later point in time when this content entry is used,
149 /// after serialization and deserialization.
150 unsigned IsTransient : 1;
151
152 ContentCache(const FileEntry *Ent = nullptr) : ContentCache(Ent, Ent) {}
153
154 ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
155 : Buffer(nullptr, false), OrigEntry(Ent), ContentsEntry(contentEnt),
156 BufferOverridden(false), IsFileVolatile(false), IsTransient(false) {}
157
158 /// The copy ctor does not allow copies where source object has either
159 /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory
160 /// is not transferred, so this is a logical error.
161 ContentCache(const ContentCache &RHS)
162 : Buffer(nullptr, false), BufferOverridden(false),
163 IsFileVolatile(false), IsTransient(false) {
164 OrigEntry = RHS.OrigEntry;
165 ContentsEntry = RHS.ContentsEntry;
166
167 assert(RHS.Buffer.getPointer() == nullptr &&((RHS.Buffer.getPointer() == nullptr && RHS.SourceLineCache
== nullptr && "Passed ContentCache object cannot own a buffer."
) ? static_cast<void> (0) : __assert_fail ("RHS.Buffer.getPointer() == nullptr && RHS.SourceLineCache == nullptr && \"Passed ContentCache object cannot own a buffer.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 169, __PRETTY_FUNCTION__))
168 RHS.SourceLineCache == nullptr &&((RHS.Buffer.getPointer() == nullptr && RHS.SourceLineCache
== nullptr && "Passed ContentCache object cannot own a buffer."
) ? static_cast<void> (0) : __assert_fail ("RHS.Buffer.getPointer() == nullptr && RHS.SourceLineCache == nullptr && \"Passed ContentCache object cannot own a buffer.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 169, __PRETTY_FUNCTION__))
169 "Passed ContentCache object cannot own a buffer.")((RHS.Buffer.getPointer() == nullptr && RHS.SourceLineCache
== nullptr && "Passed ContentCache object cannot own a buffer."
) ? static_cast<void> (0) : __assert_fail ("RHS.Buffer.getPointer() == nullptr && RHS.SourceLineCache == nullptr && \"Passed ContentCache object cannot own a buffer.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 169, __PRETTY_FUNCTION__))
;
170
171 NumLines = RHS.NumLines;
172 }
173
174 ContentCache &operator=(const ContentCache& RHS) = delete;
175
176 ~ContentCache();
177
178 /// Returns the memory buffer for the associated content.
179 ///
180 /// \param Diag Object through which diagnostics will be emitted if the
181 /// buffer cannot be retrieved.
182 ///
183 /// \param Loc If specified, is the location that invalid file diagnostics
184 /// will be emitted at.
185 ///
186 /// \param Invalid If non-NULL, will be set \c true if an error occurred.
187 const llvm::MemoryBuffer *getBuffer(DiagnosticsEngine &Diag,
188 FileManager &FM,
189 SourceLocation Loc = SourceLocation(),
190 bool *Invalid = nullptr) const;
191
192 /// Returns the size of the content encapsulated by this
193 /// ContentCache.
194 ///
195 /// This can be the size of the source file or the size of an
196 /// arbitrary scratch buffer. If the ContentCache encapsulates a source
197 /// file this size is retrieved from the file's FileEntry.
198 unsigned getSize() const;
199
200 /// Returns the number of bytes actually mapped for this
201 /// ContentCache.
202 ///
203 /// This can be 0 if the MemBuffer was not actually expanded.
204 unsigned getSizeBytesMapped() const;
205
206 /// Returns the kind of memory used to back the memory buffer for
207 /// this content cache. This is used for performance analysis.
208 llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const;
209
210 /// Get the underlying buffer, returning NULL if the buffer is not
211 /// yet available.
212 const llvm::MemoryBuffer *getRawBuffer() const {
213 return Buffer.getPointer();
214 }
215
216 /// Replace the existing buffer (which will be deleted)
217 /// with the given buffer.
218 void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
219
220 /// Determine whether the buffer itself is invalid.
221 bool isBufferInvalid() const {
222 return Buffer.getInt() & InvalidFlag;
223 }
224
225 /// Determine whether the buffer should be freed.
226 bool shouldFreeBuffer() const {
227 return (Buffer.getInt() & DoNotFreeFlag) == 0;
228 }
229 };
230
231 // Assert that the \c ContentCache objects will always be 8-byte aligned so
232 // that we can pack 3 bits of integer into pointers to such objects.
233 static_assert(alignof(ContentCache) >= 8,
234 "ContentCache must be 8-byte aligned.");
235
236 /// Information about a FileID, basically just the logical file
237 /// that it represents and include stack information.
238 ///
239 /// Each FileInfo has include stack information, indicating where it came
240 /// from. This information encodes the \#include chain that a token was
241 /// expanded from. The main include file has an invalid IncludeLoc.
242 ///
243 /// FileInfos contain a "ContentCache *", with the contents of the file.
244 ///
245 class FileInfo {
246 friend class clang::SourceManager;
247 friend class clang::ASTWriter;
248 friend class clang::ASTReader;
249
250 /// The location of the \#include that brought in this file.
251 ///
252 /// This is an invalid SLOC for the main file (top of the \#include chain).
253 unsigned IncludeLoc; // Really a SourceLocation
254
255 /// Number of FileIDs (files and macros) that were created during
256 /// preprocessing of this \#include, including this SLocEntry.
257 ///
258 /// Zero means the preprocessor didn't provide such info for this SLocEntry.
259 unsigned NumCreatedFIDs : 31;
260
261 /// Whether this FileInfo has any \#line directives.
262 unsigned HasLineDirectives : 1;
263
264 /// The content cache and the characteristic of the file.
265 llvm::PointerIntPair<const ContentCache*, 3, CharacteristicKind>
266 ContentAndKind;
267
268 /// The filename that is used to access the file entry represented by the
269 /// content cache.
270 StringRef Filename;
271
272 public:
273 /// Return a FileInfo object.
274 static FileInfo get(SourceLocation IL, const ContentCache *Con,
275 CharacteristicKind FileCharacter, StringRef Filename) {
276 FileInfo X;
277 X.IncludeLoc = IL.getRawEncoding();
278 X.NumCreatedFIDs = 0;
279 X.HasLineDirectives = false;
280 X.ContentAndKind.setPointer(Con);
281 X.ContentAndKind.setInt(FileCharacter);
282 X.Filename = Filename;
283 return X;
284 }
285
286 SourceLocation getIncludeLoc() const {
287 return SourceLocation::getFromRawEncoding(IncludeLoc);
288 }
289
290 const ContentCache *getContentCache() const {
291 return ContentAndKind.getPointer();
292 }
293
294 /// Return whether this is a system header or not.
295 CharacteristicKind getFileCharacteristic() const {
296 return ContentAndKind.getInt();
297 }
298
299 /// Return true if this FileID has \#line directives in it.
300 bool hasLineDirectives() const { return HasLineDirectives; }
301
302 /// Set the flag that indicates that this FileID has
303 /// line table entries associated with it.
304 void setHasLineDirectives() {
305 HasLineDirectives = true;
306 }
307
308 /// Returns the name of the file that was used when the file was loaded from
309 /// the underlying file system.
310 StringRef getName() const { return Filename; }
311 };
312
313 /// Each ExpansionInfo encodes the expansion location - where
314 /// the token was ultimately expanded, and the SpellingLoc - where the actual
315 /// character data for the token came from.
316 class ExpansionInfo {
317 // Really these are all SourceLocations.
318
319 /// Where the spelling for the token can be found.
320 unsigned SpellingLoc;
321
322 /// In a macro expansion, ExpansionLocStart and ExpansionLocEnd
323 /// indicate the start and end of the expansion. In object-like macros,
324 /// they will be the same. In a function-like macro expansion, the start
325 /// will be the identifier and the end will be the ')'. Finally, in
326 /// macro-argument instantiations, the end will be 'SourceLocation()', an
327 /// invalid location.
328 unsigned ExpansionLocStart, ExpansionLocEnd;
329
330 /// Whether the expansion range is a token range.
331 bool ExpansionIsTokenRange;
332
333 public:
334 SourceLocation getSpellingLoc() const {
335 SourceLocation SpellLoc = SourceLocation::getFromRawEncoding(SpellingLoc);
336 return SpellLoc.isInvalid() ? getExpansionLocStart() : SpellLoc;
337 }
338
339 SourceLocation getExpansionLocStart() const {
340 return SourceLocation::getFromRawEncoding(ExpansionLocStart);
341 }
342
343 SourceLocation getExpansionLocEnd() const {
344 SourceLocation EndLoc =
345 SourceLocation::getFromRawEncoding(ExpansionLocEnd);
346 return EndLoc.isInvalid() ? getExpansionLocStart() : EndLoc;
347 }
348
349 bool isExpansionTokenRange() const {
350 return ExpansionIsTokenRange;
351 }
352
353 CharSourceRange getExpansionLocRange() const {
354 return CharSourceRange(
355 SourceRange(getExpansionLocStart(), getExpansionLocEnd()),
356 isExpansionTokenRange());
357 }
358
359 bool isMacroArgExpansion() const {
360 // Note that this needs to return false for default constructed objects.
361 return getExpansionLocStart().isValid() &&
362 SourceLocation::getFromRawEncoding(ExpansionLocEnd).isInvalid();
363 }
364
365 bool isMacroBodyExpansion() const {
366 return getExpansionLocStart().isValid() &&
367 SourceLocation::getFromRawEncoding(ExpansionLocEnd).isValid();
368 }
369
370 bool isFunctionMacroExpansion() const {
371 return getExpansionLocStart().isValid() &&
372 getExpansionLocStart() != getExpansionLocEnd();
373 }
374
375 /// Return a ExpansionInfo for an expansion.
376 ///
377 /// Start and End specify the expansion range (where the macro is
378 /// expanded), and SpellingLoc specifies the spelling location (where
379 /// the characters from the token come from). All three can refer to
380 /// normal File SLocs or expansion locations.
381 static ExpansionInfo create(SourceLocation SpellingLoc,
382 SourceLocation Start, SourceLocation End,
383 bool ExpansionIsTokenRange = true) {
384 ExpansionInfo X;
385 X.SpellingLoc = SpellingLoc.getRawEncoding();
386 X.ExpansionLocStart = Start.getRawEncoding();
387 X.ExpansionLocEnd = End.getRawEncoding();
388 X.ExpansionIsTokenRange = ExpansionIsTokenRange;
389 return X;
390 }
391
392 /// Return a special ExpansionInfo for the expansion of
393 /// a macro argument into a function-like macro's body.
394 ///
395 /// ExpansionLoc specifies the expansion location (where the macro is
396 /// expanded). This doesn't need to be a range because a macro is always
397 /// expanded at a macro parameter reference, and macro parameters are
398 /// always exactly one token. SpellingLoc specifies the spelling location
399 /// (where the characters from the token come from). ExpansionLoc and
400 /// SpellingLoc can both refer to normal File SLocs or expansion locations.
401 ///
402 /// Given the code:
403 /// \code
404 /// #define F(x) f(x)
405 /// F(42);
406 /// \endcode
407 ///
408 /// When expanding '\c F(42)', the '\c x' would call this with an
409 /// SpellingLoc pointing at '\c 42' and an ExpansionLoc pointing at its
410 /// location in the definition of '\c F'.
411 static ExpansionInfo createForMacroArg(SourceLocation SpellingLoc,
412 SourceLocation ExpansionLoc) {
413 // We store an intentionally invalid source location for the end of the
414 // expansion range to mark that this is a macro argument location rather
415 // than a normal one.
416 return create(SpellingLoc, ExpansionLoc, SourceLocation());
417 }
418
419 /// Return a special ExpansionInfo representing a token that ends
420 /// prematurely. This is used to model a '>>' token that has been split
421 /// into '>' tokens and similar cases. Unlike for the other forms of
422 /// expansion, the expansion range in this case is a character range, not
423 /// a token range.
424 static ExpansionInfo createForTokenSplit(SourceLocation SpellingLoc,
425 SourceLocation Start,
426 SourceLocation End) {
427 return create(SpellingLoc, Start, End, false);
428 }
429 };
430
431 /// This is a discriminated union of FileInfo and ExpansionInfo.
432 ///
433 /// SourceManager keeps an array of these objects, and they are uniquely
434 /// identified by the FileID datatype.
435 class SLocEntry {
436 unsigned Offset : 31;
437 unsigned IsExpansion : 1;
438 union {
439 FileInfo File;
440 ExpansionInfo Expansion;
441 };
442
443 public:
444 SLocEntry() : Offset(), IsExpansion(), File() {}
445
446 unsigned getOffset() const { return Offset; }
447
448 bool isExpansion() const { return IsExpansion; }
449 bool isFile() const { return !isExpansion(); }
450
451 const FileInfo &getFile() const {
452 assert(isFile() && "Not a file SLocEntry!")((isFile() && "Not a file SLocEntry!") ? static_cast<
void> (0) : __assert_fail ("isFile() && \"Not a file SLocEntry!\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 452, __PRETTY_FUNCTION__))
;
453 return File;
454 }
455
456 const ExpansionInfo &getExpansion() const {
457 assert(isExpansion() && "Not a macro expansion SLocEntry!")((isExpansion() && "Not a macro expansion SLocEntry!"
) ? static_cast<void> (0) : __assert_fail ("isExpansion() && \"Not a macro expansion SLocEntry!\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 457, __PRETTY_FUNCTION__))
;
458 return Expansion;
459 }
460
461 static SLocEntry get(unsigned Offset, const FileInfo &FI) {
462 assert(!(Offset & (1u << 31)) && "Offset is too large")((!(Offset & (1u << 31)) && "Offset is too large"
) ? static_cast<void> (0) : __assert_fail ("!(Offset & (1u << 31)) && \"Offset is too large\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 462, __PRETTY_FUNCTION__))
;
463 SLocEntry E;
464 E.Offset = Offset;
465 E.IsExpansion = false;
466 E.File = FI;
467 return E;
468 }
469
470 static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) {
471 assert(!(Offset & (1u << 31)) && "Offset is too large")((!(Offset & (1u << 31)) && "Offset is too large"
) ? static_cast<void> (0) : __assert_fail ("!(Offset & (1u << 31)) && \"Offset is too large\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 471, __PRETTY_FUNCTION__))
;
472 SLocEntry E;
473 E.Offset = Offset;
474 E.IsExpansion = true;
475 E.Expansion = Expansion;
476 return E;
477 }
478 };
479
480} // namespace SrcMgr
481
482/// External source of source location entries.
483class ExternalSLocEntrySource {
484public:
485 virtual ~ExternalSLocEntrySource();
486
487 /// Read the source location entry with index ID, which will always be
488 /// less than -1.
489 ///
490 /// \returns true if an error occurred that prevented the source-location
491 /// entry from being loaded.
492 virtual bool ReadSLocEntry(int ID) = 0;
493
494 /// Retrieve the module import location and name for the given ID, if
495 /// in fact it was loaded from a module (rather than, say, a precompiled
496 /// header).
497 virtual std::pair<SourceLocation, StringRef> getModuleImportLoc(int ID) = 0;
498};
499
500/// Holds the cache used by isBeforeInTranslationUnit.
501///
502/// The cache structure is complex enough to be worth breaking out of
503/// SourceManager.
504class InBeforeInTUCacheEntry {
505 /// The FileID's of the cached query.
506 ///
507 /// If these match up with a subsequent query, the result can be reused.
508 FileID LQueryFID, RQueryFID;
509
510 /// True if LQueryFID was created before RQueryFID.
511 ///
512 /// This is used to compare macro expansion locations.
513 bool IsLQFIDBeforeRQFID;
514
515 /// The file found in common between the two \#include traces, i.e.,
516 /// the nearest common ancestor of the \#include tree.
517 FileID CommonFID;
518
519 /// The offset of the previous query in CommonFID.
520 ///
521 /// Usually, this represents the location of the \#include for QueryFID, but
522 /// if LQueryFID is a parent of RQueryFID (or vice versa) then these can be a
523 /// random token in the parent.
524 unsigned LCommonOffset, RCommonOffset;
525
526public:
527 /// Return true if the currently cached values match up with
528 /// the specified LHS/RHS query.
529 ///
530 /// If not, we can't use the cache.
531 bool isCacheValid(FileID LHS, FileID RHS) const {
532 return LQueryFID == LHS && RQueryFID == RHS;
533 }
534
535 /// If the cache is valid, compute the result given the
536 /// specified offsets in the LHS/RHS FileID's.
537 bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
538 // If one of the query files is the common file, use the offset. Otherwise,
539 // use the #include loc in the common file.
540 if (LQueryFID != CommonFID) LOffset = LCommonOffset;
541 if (RQueryFID != CommonFID) ROffset = RCommonOffset;
542
543 // It is common for multiple macro expansions to be "included" from the same
544 // location (expansion location), in which case use the order of the FileIDs
545 // to determine which came first. This will also take care the case where
546 // one of the locations points at the inclusion/expansion point of the other
547 // in which case its FileID will come before the other.
548 if (LOffset == ROffset)
549 return IsLQFIDBeforeRQFID;
550
551 return LOffset < ROffset;
552 }
553
554 /// Set up a new query.
555 void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) {
556 assert(LHS != RHS)((LHS != RHS) ? static_cast<void> (0) : __assert_fail (
"LHS != RHS", "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 556, __PRETTY_FUNCTION__))
;
557 LQueryFID = LHS;
558 RQueryFID = RHS;
559 IsLQFIDBeforeRQFID = isLFIDBeforeRFID;
560 }
561
562 void clear() {
563 LQueryFID = RQueryFID = FileID();
564 IsLQFIDBeforeRQFID = false;
565 }
566
567 void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
568 unsigned rCommonOffset) {
569 CommonFID = commonFID;
570 LCommonOffset = lCommonOffset;
571 RCommonOffset = rCommonOffset;
572 }
573};
574
575/// The stack used when building modules on demand, which is used
576/// to provide a link between the source managers of the different compiler
577/// instances.
578using ModuleBuildStack = ArrayRef<std::pair<std::string, FullSourceLoc>>;
579
580/// This class handles loading and caching of source files into memory.
581///
582/// This object owns the MemoryBuffer objects for all of the loaded
583/// files and assigns unique FileID's for each unique \#include chain.
584///
585/// The SourceManager can be queried for information about SourceLocation
586/// objects, turning them into either spelling or expansion locations. Spelling
587/// locations represent where the bytes corresponding to a token came from and
588/// expansion locations represent where the location is in the user's view. In
589/// the case of a macro expansion, for example, the spelling location indicates
590/// where the expanded token came from and the expansion location specifies
591/// where it was expanded.
592class SourceManager : public RefCountedBase<SourceManager> {
593 /// DiagnosticsEngine object.
594 DiagnosticsEngine &Diag;
595
596 FileManager &FileMgr;
597
598 mutable llvm::BumpPtrAllocator ContentCacheAlloc;
599
600 /// Memoized information about all of the files tracked by this
601 /// SourceManager.
602 ///
603 /// This map allows us to merge ContentCache entries based
604 /// on their FileEntry*. All ContentCache objects will thus have unique,
605 /// non-null, FileEntry pointers.
606 llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
607
608 /// True if the ContentCache for files that are overridden by other
609 /// files, should report the original file name. Defaults to true.
610 bool OverridenFilesKeepOriginalName = true;
611
612 /// True if non-system source files should be treated as volatile
613 /// (likely to change while trying to use them). Defaults to false.
614 bool UserFilesAreVolatile;
615
616 /// True if all files read during this compilation should be treated
617 /// as transient (may not be present in later compilations using a module
618 /// file created from this compilation). Defaults to false.
619 bool FilesAreTransient = false;
620
621 struct OverriddenFilesInfoTy {
622 /// Files that have been overridden with the contents from another
623 /// file.
624 llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
625
626 /// Files that were overridden with a memory buffer.
627 llvm::DenseSet<const FileEntry *> OverriddenFilesWithBuffer;
628 };
629
630 /// Lazily create the object keeping overridden files info, since
631 /// it is uncommonly used.
632 std::unique_ptr<OverriddenFilesInfoTy> OverriddenFilesInfo;
633
634 OverriddenFilesInfoTy &getOverriddenFilesInfo() {
635 if (!OverriddenFilesInfo)
636 OverriddenFilesInfo.reset(new OverriddenFilesInfoTy);
637 return *OverriddenFilesInfo;
638 }
639
640 /// Information about various memory buffers that we have read in.
641 ///
642 /// All FileEntry* within the stored ContentCache objects are NULL,
643 /// as they do not refer to a file.
644 std::vector<SrcMgr::ContentCache*> MemBufferInfos;
645
646 /// The table of SLocEntries that are local to this module.
647 ///
648 /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid
649 /// expansion.
650 SmallVector<SrcMgr::SLocEntry, 0> LocalSLocEntryTable;
651
652 /// The table of SLocEntries that are loaded from other modules.
653 ///
654 /// Negative FileIDs are indexes into this table. To get from ID to an index,
655 /// use (-ID - 2).
656 mutable SmallVector<SrcMgr::SLocEntry, 0> LoadedSLocEntryTable;
657
658 /// The starting offset of the next local SLocEntry.
659 ///
660 /// This is LocalSLocEntryTable.back().Offset + the size of that entry.
661 unsigned NextLocalOffset;
662
663 /// The starting offset of the latest batch of loaded SLocEntries.
664 ///
665 /// This is LoadedSLocEntryTable.back().Offset, except that that entry might
666 /// not have been loaded, so that value would be unknown.
667 unsigned CurrentLoadedOffset;
668
669 /// The highest possible offset is 2^31-1, so CurrentLoadedOffset
670 /// starts at 2^31.
671 static const unsigned MaxLoadedOffset = 1U << 31U;
672
673 /// A bitmap that indicates whether the entries of LoadedSLocEntryTable
674 /// have already been loaded from the external source.
675 ///
676 /// Same indexing as LoadedSLocEntryTable.
677 llvm::BitVector SLocEntryLoaded;
678
679 /// An external source for source location entries.
680 ExternalSLocEntrySource *ExternalSLocEntries = nullptr;
681
682 /// A one-entry cache to speed up getFileID.
683 ///
684 /// LastFileIDLookup records the last FileID looked up or created, because it
685 /// is very common to look up many tokens from the same file.
686 mutable FileID LastFileIDLookup;
687
688 /// Holds information for \#line directives.
689 ///
690 /// This is referenced by indices from SLocEntryTable.
691 std::unique_ptr<LineTableInfo> LineTable;
692
693 /// These ivars serve as a cache used in the getLineNumber
694 /// method which is used to speedup getLineNumber calls to nearby locations.
695 mutable FileID LastLineNoFileIDQuery;
696 mutable SrcMgr::ContentCache *LastLineNoContentCache;
697 mutable unsigned LastLineNoFilePos;
698 mutable unsigned LastLineNoResult;
699
700 /// The file ID for the main source file of the translation unit.
701 FileID MainFileID;
702
703 /// The file ID for the precompiled preamble there is one.
704 FileID PreambleFileID;
705
706 // Statistics for -print-stats.
707 mutable unsigned NumLinearScans = 0;
708 mutable unsigned NumBinaryProbes = 0;
709
710 /// Associates a FileID with its "included/expanded in" decomposed
711 /// location.
712 ///
713 /// Used to cache results from and speed-up \c getDecomposedIncludedLoc
714 /// function.
715 mutable llvm::DenseMap<FileID, std::pair<FileID, unsigned>> IncludedLocMap;
716
717 /// The key value into the IsBeforeInTUCache table.
718 using IsBeforeInTUCacheKey = std::pair<FileID, FileID>;
719
720 /// The IsBeforeInTranslationUnitCache is a mapping from FileID pairs
721 /// to cache results.
722 using InBeforeInTUCache =
723 llvm::DenseMap<IsBeforeInTUCacheKey, InBeforeInTUCacheEntry>;
724
725 /// Cache results for the isBeforeInTranslationUnit method.
726 mutable InBeforeInTUCache IBTUCache;
727 mutable InBeforeInTUCacheEntry IBTUCacheOverflow;
728
729 /// Return the cache entry for comparing the given file IDs
730 /// for isBeforeInTranslationUnit.
731 InBeforeInTUCacheEntry &getInBeforeInTUCache(FileID LFID, FileID RFID) const;
732
733 // Cache for the "fake" buffer used for error-recovery purposes.
734 mutable std::unique_ptr<llvm::MemoryBuffer> FakeBufferForRecovery;
735
736 mutable std::unique_ptr<SrcMgr::ContentCache> FakeContentCacheForRecovery;
737
738 /// Lazily computed map of macro argument chunks to their expanded
739 /// source location.
740 using MacroArgsMap = std::map<unsigned, SourceLocation>;
741
742 mutable llvm::DenseMap<FileID, std::unique_ptr<MacroArgsMap>>
743 MacroArgsCacheMap;
744
745 /// The stack of modules being built, which is used to detect
746 /// cycles in the module dependency graph as modules are being built, as
747 /// well as to describe why we're rebuilding a particular module.
748 ///
749 /// There is no way to set this value from the command line. If we ever need
750 /// to do so (e.g., if on-demand module construction moves out-of-process),
751 /// we can add a cc1-level option to do so.
752 SmallVector<std::pair<std::string, FullSourceLoc>, 2> StoredModuleBuildStack;
753
754public:
755 SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr,
756 bool UserFilesAreVolatile = false);
757 explicit SourceManager(const SourceManager &) = delete;
758 SourceManager &operator=(const SourceManager &) = delete;
759 ~SourceManager();
760
761 void clearIDTables();
762
763 /// Initialize this source manager suitably to replay the compilation
764 /// described by \p Old. Requires that \p Old outlive \p *this.
765 void initializeForReplay(const SourceManager &Old);
766
767 DiagnosticsEngine &getDiagnostics() const { return Diag; }
768
769 FileManager &getFileManager() const { return FileMgr; }
770
771 /// Set true if the SourceManager should report the original file name
772 /// for contents of files that were overridden by other files. Defaults to
773 /// true.
774 void setOverridenFilesKeepOriginalName(bool value) {
775 OverridenFilesKeepOriginalName = value;
776 }
777
778 /// True if non-system source files should be treated as volatile
779 /// (likely to change while trying to use them).
780 bool userFilesAreVolatile() const { return UserFilesAreVolatile; }
781
782 /// Retrieve the module build stack.
783 ModuleBuildStack getModuleBuildStack() const {
784 return StoredModuleBuildStack;
785 }
786
787 /// Set the module build stack.
788 void setModuleBuildStack(ModuleBuildStack stack) {
789 StoredModuleBuildStack.clear();
790 StoredModuleBuildStack.append(stack.begin(), stack.end());
791 }
792
793 /// Push an entry to the module build stack.
794 void pushModuleBuildStack(StringRef moduleName, FullSourceLoc importLoc) {
795 StoredModuleBuildStack.push_back(std::make_pair(moduleName.str(),importLoc));
796 }
797
798 //===--------------------------------------------------------------------===//
799 // MainFileID creation and querying methods.
800 //===--------------------------------------------------------------------===//
801
802 /// Returns the FileID of the main source file.
803 FileID getMainFileID() const { return MainFileID; }
804
805 /// Set the file ID for the main source file.
806 void setMainFileID(FileID FID) {
807 MainFileID = FID;
808 }
809
810 /// Set the file ID for the precompiled preamble.
811 void setPreambleFileID(FileID Preamble) {
812 assert(PreambleFileID.isInvalid() && "PreambleFileID already set!")((PreambleFileID.isInvalid() && "PreambleFileID already set!"
) ? static_cast<void> (0) : __assert_fail ("PreambleFileID.isInvalid() && \"PreambleFileID already set!\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 812, __PRETTY_FUNCTION__))
;
813 PreambleFileID = Preamble;
814 }
815
816 /// Get the file ID for the precompiled preamble if there is one.
817 FileID getPreambleFileID() const { return PreambleFileID; }
818
819 //===--------------------------------------------------------------------===//
820 // Methods to create new FileID's and macro expansions.
821 //===--------------------------------------------------------------------===//
822
823 /// Create a new FileID that represents the specified file
824 /// being \#included from the specified IncludePosition.
825 ///
826 /// This translates NULL into standard input.
827 FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
828 SrcMgr::CharacteristicKind FileCharacter,
829 int LoadedID = 0, unsigned LoadedOffset = 0) {
830 const SrcMgr::ContentCache *IR =
831 getOrCreateContentCache(SourceFile, isSystem(FileCharacter));
832 assert(IR && "getOrCreateContentCache() cannot return NULL")((IR && "getOrCreateContentCache() cannot return NULL"
) ? static_cast<void> (0) : __assert_fail ("IR && \"getOrCreateContentCache() cannot return NULL\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 832, __PRETTY_FUNCTION__))
;
12
Assuming 'IR' is non-null
13
'?' condition is true
833 return createFileID(IR, SourceFile->getName(), IncludePos, FileCharacter,
14
Called C++ object pointer is null
834 LoadedID, LoadedOffset);
835 }
836
837 FileID createFileID(FileEntryRef SourceFile, SourceLocation IncludePos,
838 SrcMgr::CharacteristicKind FileCharacter,
839 int LoadedID = 0, unsigned LoadedOffset = 0) {
840 const SrcMgr::ContentCache *IR = getOrCreateContentCache(
841 &SourceFile.getFileEntry(), isSystem(FileCharacter));
842 assert(IR && "getOrCreateContentCache() cannot return NULL")((IR && "getOrCreateContentCache() cannot return NULL"
) ? static_cast<void> (0) : __assert_fail ("IR && \"getOrCreateContentCache() cannot return NULL\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 842, __PRETTY_FUNCTION__))
;
843 return createFileID(IR, SourceFile.getName(), IncludePos, FileCharacter,
844 LoadedID, LoadedOffset);
845 }
846
847 /// Create a new FileID that represents the specified memory buffer.
848 ///
849 /// This does no caching of the buffer and takes ownership of the
850 /// MemoryBuffer, so only pass a MemoryBuffer to this once.
851 FileID createFileID(std::unique_ptr<llvm::MemoryBuffer> Buffer,
852 SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User,
853 int LoadedID = 0, unsigned LoadedOffset = 0,
854 SourceLocation IncludeLoc = SourceLocation()) {
855 StringRef Name = Buffer->getBufferIdentifier();
856 return createFileID(
857 createMemBufferContentCache(Buffer.release(), /*DoNotFree*/ false),
858 Name, IncludeLoc, FileCharacter, LoadedID, LoadedOffset);
859 }
860
861 enum UnownedTag { Unowned };
862
863 /// Create a new FileID that represents the specified memory buffer.
864 ///
865 /// This does not take ownership of the MemoryBuffer. The memory buffer must
866 /// outlive the SourceManager.
867 FileID createFileID(UnownedTag, const llvm::MemoryBuffer *Buffer,
868 SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User,
869 int LoadedID = 0, unsigned LoadedOffset = 0,
870 SourceLocation IncludeLoc = SourceLocation()) {
871 return createFileID(createMemBufferContentCache(Buffer, /*DoNotFree*/ true),
872 Buffer->getBufferIdentifier(), IncludeLoc,
873 FileCharacter, LoadedID, LoadedOffset);
874 }
875
876 /// Get the FileID for \p SourceFile if it exists. Otherwise, create a
877 /// new FileID for the \p SourceFile.
878 FileID getOrCreateFileID(const FileEntry *SourceFile,
879 SrcMgr::CharacteristicKind FileCharacter) {
880 FileID ID = translateFile(SourceFile);
881 return ID.isValid() ? ID : createFileID(SourceFile, SourceLocation(),
5
Calling 'FileID::isValid'
8
Returning from 'FileID::isValid'
9
'?' condition is false
10
Passing null pointer value via 1st parameter 'SourceFile'
11
Calling 'SourceManager::createFileID'
882 FileCharacter);
883 }
884
885 /// Return a new SourceLocation that encodes the
886 /// fact that a token from SpellingLoc should actually be referenced from
887 /// ExpansionLoc, and that it represents the expansion of a macro argument
888 /// into the function-like macro body.
889 SourceLocation createMacroArgExpansionLoc(SourceLocation Loc,
890 SourceLocation ExpansionLoc,
891 unsigned TokLength);
892
893 /// Return a new SourceLocation that encodes the fact
894 /// that a token from SpellingLoc should actually be referenced from
895 /// ExpansionLoc.
896 SourceLocation createExpansionLoc(SourceLocation Loc,
897 SourceLocation ExpansionLocStart,
898 SourceLocation ExpansionLocEnd,
899 unsigned TokLength,
900 bool ExpansionIsTokenRange = true,
901 int LoadedID = 0,
902 unsigned LoadedOffset = 0);
903
904 /// Return a new SourceLocation that encodes that the token starting
905 /// at \p TokenStart ends prematurely at \p TokenEnd.
906 SourceLocation createTokenSplitLoc(SourceLocation SpellingLoc,
907 SourceLocation TokenStart,
908 SourceLocation TokenEnd);
909
910 /// Retrieve the memory buffer associated with the given file.
911 ///
912 /// \param Invalid If non-NULL, will be set \c true if an error
913 /// occurs while retrieving the memory buffer.
914 const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
915 bool *Invalid = nullptr);
916
917 /// Override the contents of the given source file by providing an
918 /// already-allocated buffer.
919 ///
920 /// \param SourceFile the source file whose contents will be overridden.
921 ///
922 /// \param Buffer the memory buffer whose contents will be used as the
923 /// data in the given source file.
924 ///
925 /// \param DoNotFree If true, then the buffer will not be freed when the
926 /// source manager is destroyed.
927 void overrideFileContents(const FileEntry *SourceFile,
928 llvm::MemoryBuffer *Buffer, bool DoNotFree);
929 void overrideFileContents(const FileEntry *SourceFile,
930 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
931 overrideFileContents(SourceFile, Buffer.release(), /*DoNotFree*/ false);
932 }
933
934 /// Override the given source file with another one.
935 ///
936 /// \param SourceFile the source file which will be overridden.
937 ///
938 /// \param NewFile the file whose contents will be used as the
939 /// data instead of the contents of the given source file.
940 void overrideFileContents(const FileEntry *SourceFile,
941 const FileEntry *NewFile);
942
943 /// Returns true if the file contents have been overridden.
944 bool isFileOverridden(const FileEntry *File) const {
945 if (OverriddenFilesInfo) {
946 if (OverriddenFilesInfo->OverriddenFilesWithBuffer.count(File))
947 return true;
948 if (OverriddenFilesInfo->OverriddenFiles.find(File) !=
949 OverriddenFilesInfo->OverriddenFiles.end())
950 return true;
951 }
952 return false;
953 }
954
955 /// Bypass the overridden contents of a file. This creates a new FileEntry
956 /// and initializes the content cache for it. Returns nullptr if there is no
957 /// such file in the filesystem.
958 ///
959 /// This should be called before parsing has begun.
960 const FileEntry *bypassFileContentsOverride(const FileEntry &File);
961
962 /// Specify that a file is transient.
963 void setFileIsTransient(const FileEntry *SourceFile);
964
965 /// Specify that all files that are read during this compilation are
966 /// transient.
967 void setAllFilesAreTransient(bool Transient) {
968 FilesAreTransient = Transient;
969 }
970
971 //===--------------------------------------------------------------------===//
972 // FileID manipulation methods.
973 //===--------------------------------------------------------------------===//
974
975 /// Return the buffer for the specified FileID.
976 ///
977 /// If there is an error opening this buffer the first time, this
978 /// manufactures a temporary buffer and returns a non-empty error string.
979 const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
980 bool *Invalid = nullptr) const {
981 bool MyInvalid = false;
982 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
983 if (MyInvalid || !Entry.isFile()) {
984 if (Invalid)
985 *Invalid = true;
986
987 return getFakeBufferForRecovery();
988 }
989
990 return Entry.getFile().getContentCache()->getBuffer(Diag, getFileManager(),
991 Loc, Invalid);
992 }
993
994 const llvm::MemoryBuffer *getBuffer(FileID FID,
995 bool *Invalid = nullptr) const {
996 bool MyInvalid = false;
997 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
998 if (MyInvalid || !Entry.isFile()) {
999 if (Invalid)
1000 *Invalid = true;
1001
1002 return getFakeBufferForRecovery();
1003 }
1004
1005 return Entry.getFile().getContentCache()->getBuffer(
1006 Diag, getFileManager(), SourceLocation(), Invalid);
1007 }
1008
1009 /// Returns the FileEntry record for the provided FileID.
1010 const FileEntry *getFileEntryForID(FileID FID) const {
1011 bool MyInvalid = false;
1012 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
1013 if (MyInvalid || !Entry.isFile())
1014 return nullptr;
1015
1016 const SrcMgr::ContentCache *Content = Entry.getFile().getContentCache();
1017 if (!Content)
1018 return nullptr;
1019 return Content->OrigEntry;
1020 }
1021
1022 /// Returns the FileEntryRef for the provided FileID.
1023 Optional<FileEntryRef> getFileEntryRefForID(FileID FID) const {
1024 bool Invalid = false;
1025 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1026 if (Invalid || !Entry.isFile())
1027 return None;
1028
1029 const SrcMgr::ContentCache *Content = Entry.getFile().getContentCache();
1030 if (!Content || !Content->OrigEntry)
1031 return None;
1032 return FileEntryRef(Entry.getFile().getName(), *Content->OrigEntry);
1033 }
1034
1035 /// Returns the FileEntry record for the provided SLocEntry.
1036 const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
1037 {
1038 const SrcMgr::ContentCache *Content = sloc.getFile().getContentCache();
1039 if (!Content)
1040 return nullptr;
1041 return Content->OrigEntry;
1042 }
1043
1044 /// Return a StringRef to the source buffer data for the
1045 /// specified FileID.
1046 ///
1047 /// \param FID The file ID whose contents will be returned.
1048 /// \param Invalid If non-NULL, will be set true if an error occurred.
1049 StringRef getBufferData(FileID FID, bool *Invalid = nullptr) const;
1050
1051 /// Get the number of FileIDs (files and macros) that were created
1052 /// during preprocessing of \p FID, including it.
1053 unsigned getNumCreatedFIDsForFileID(FileID FID) const {
1054 bool Invalid = false;
1055 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1056 if (Invalid || !Entry.isFile())
1057 return 0;
1058
1059 return Entry.getFile().NumCreatedFIDs;
1060 }
1061
1062 /// Set the number of FileIDs (files and macros) that were created
1063 /// during preprocessing of \p FID, including it.
1064 void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs,
1065 bool Force = false) const {
1066 bool Invalid = false;
1067 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1068 if (Invalid || !Entry.isFile())
1069 return;
1070
1071 assert((Force || Entry.getFile().NumCreatedFIDs == 0) && "Already set!")(((Force || Entry.getFile().NumCreatedFIDs == 0) && "Already set!"
) ? static_cast<void> (0) : __assert_fail ("(Force || Entry.getFile().NumCreatedFIDs == 0) && \"Already set!\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 1071, __PRETTY_FUNCTION__))
;
1072 const_cast<SrcMgr::FileInfo &>(Entry.getFile()).NumCreatedFIDs = NumFIDs;
1073 }
1074
1075 //===--------------------------------------------------------------------===//
1076 // SourceLocation manipulation methods.
1077 //===--------------------------------------------------------------------===//
1078
1079 /// Return the FileID for a SourceLocation.
1080 ///
1081 /// This is a very hot method that is used for all SourceManager queries
1082 /// that start with a SourceLocation object. It is responsible for finding
1083 /// the entry in SLocEntryTable which contains the specified location.
1084 ///
1085 FileID getFileID(SourceLocation SpellingLoc) const {
1086 unsigned SLocOffset = SpellingLoc.getOffset();
1087
1088 // If our one-entry cache covers this offset, just return it.
1089 if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
1090 return LastFileIDLookup;
1091
1092 return getFileIDSlow(SLocOffset);
1093 }
1094
1095 /// Return the filename of the file containing a SourceLocation.
1096 StringRef getFilename(SourceLocation SpellingLoc) const {
1097 if (const FileEntry *F = getFileEntryForID(getFileID(SpellingLoc)))
1098 return F->getName();
1099 return StringRef();
1100 }
1101
1102 /// Return the source location corresponding to the first byte of
1103 /// the specified file.
1104 SourceLocation getLocForStartOfFile(FileID FID) const {
1105 bool Invalid = false;
1106 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1107 if (Invalid || !Entry.isFile())
1108 return SourceLocation();
1109
1110 unsigned FileOffset = Entry.getOffset();
1111 return SourceLocation::getFileLoc(FileOffset);
1112 }
1113
1114 /// Return the source location corresponding to the last byte of the
1115 /// specified file.
1116 SourceLocation getLocForEndOfFile(FileID FID) const {
1117 bool Invalid = false;
1118 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1119 if (Invalid || !Entry.isFile())
1120 return SourceLocation();
1121
1122 unsigned FileOffset = Entry.getOffset();
1123 return SourceLocation::getFileLoc(FileOffset + getFileIDSize(FID));
1124 }
1125
1126 /// Returns the include location if \p FID is a \#include'd file
1127 /// otherwise it returns an invalid location.
1128 SourceLocation getIncludeLoc(FileID FID) const {
1129 bool Invalid = false;
1130 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1131 if (Invalid || !Entry.isFile())
1132 return SourceLocation();
1133
1134 return Entry.getFile().getIncludeLoc();
1135 }
1136
1137 // Returns the import location if the given source location is
1138 // located within a module, or an invalid location if the source location
1139 // is within the current translation unit.
1140 std::pair<SourceLocation, StringRef>
1141 getModuleImportLoc(SourceLocation Loc) const {
1142 FileID FID = getFileID(Loc);
1143
1144 // Positive file IDs are in the current translation unit, and -1 is a
1145 // placeholder.
1146 if (FID.ID >= -1)
1147 return std::make_pair(SourceLocation(), "");
1148
1149 return ExternalSLocEntries->getModuleImportLoc(FID.ID);
1150 }
1151
1152 /// Given a SourceLocation object \p Loc, return the expansion
1153 /// location referenced by the ID.
1154 SourceLocation getExpansionLoc(SourceLocation Loc) const {
1155 // Handle the non-mapped case inline, defer to out of line code to handle
1156 // expansions.
1157 if (Loc.isFileID()) return Loc;
1158 return getExpansionLocSlowCase(Loc);
1159 }
1160
1161 /// Given \p Loc, if it is a macro location return the expansion
1162 /// location or the spelling location, depending on if it comes from a
1163 /// macro argument or not.
1164 SourceLocation getFileLoc(SourceLocation Loc) const {
1165 if (Loc.isFileID()) return Loc;
1166 return getFileLocSlowCase(Loc);
1167 }
1168
1169 /// Return the start/end of the expansion information for an
1170 /// expansion location.
1171 ///
1172 /// \pre \p Loc is required to be an expansion location.
1173 CharSourceRange getImmediateExpansionRange(SourceLocation Loc) const;
1174
1175 /// Given a SourceLocation object, return the range of
1176 /// tokens covered by the expansion in the ultimate file.
1177 CharSourceRange getExpansionRange(SourceLocation Loc) const;
1178
1179 /// Given a SourceRange object, return the range of
1180 /// tokens or characters covered by the expansion in the ultimate file.
1181 CharSourceRange getExpansionRange(SourceRange Range) const {
1182 SourceLocation Begin = getExpansionRange(Range.getBegin()).getBegin();
1183 CharSourceRange End = getExpansionRange(Range.getEnd());
1184 return CharSourceRange(SourceRange(Begin, End.getEnd()),
1185 End.isTokenRange());
1186 }
1187
1188 /// Given a CharSourceRange object, return the range of
1189 /// tokens or characters covered by the expansion in the ultimate file.
1190 CharSourceRange getExpansionRange(CharSourceRange Range) const {
1191 CharSourceRange Expansion = getExpansionRange(Range.getAsRange());
1192 if (Expansion.getEnd() == Range.getEnd())
1193 Expansion.setTokenRange(Range.isTokenRange());
1194 return Expansion;
1195 }
1196
1197 /// Given a SourceLocation object, return the spelling
1198 /// location referenced by the ID.
1199 ///
1200 /// This is the place where the characters that make up the lexed token
1201 /// can be found.
1202 SourceLocation getSpellingLoc(SourceLocation Loc) const {
1203 // Handle the non-mapped case inline, defer to out of line code to handle
1204 // expansions.
1205 if (Loc.isFileID()) return Loc;
1206 return getSpellingLocSlowCase(Loc);
1207 }
1208
1209 /// Given a SourceLocation object, return the spelling location
1210 /// referenced by the ID.
1211 ///
1212 /// This is the first level down towards the place where the characters
1213 /// that make up the lexed token can be found. This should not generally
1214 /// be used by clients.
1215 SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
1216
1217 /// Form a SourceLocation from a FileID and Offset pair.
1218 SourceLocation getComposedLoc(FileID FID, unsigned Offset) const {
1219 bool Invalid = false;
1220 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1221 if (Invalid)
1222 return SourceLocation();
1223
1224 unsigned GlobalOffset = Entry.getOffset() + Offset;
1225 return Entry.isFile() ? SourceLocation::getFileLoc(GlobalOffset)
1226 : SourceLocation::getMacroLoc(GlobalOffset);
1227 }
1228
1229 /// Decompose the specified location into a raw FileID + Offset pair.
1230 ///
1231 /// The first element is the FileID, the second is the offset from the
1232 /// start of the buffer of the location.
1233 std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
1234 FileID FID = getFileID(Loc);
1235 bool Invalid = false;
1236 const SrcMgr::SLocEntry &E = getSLocEntry(FID, &Invalid);
1237 if (Invalid)
1238 return std::make_pair(FileID(), 0);
1239 return std::make_pair(FID, Loc.getOffset()-E.getOffset());
1240 }
1241
1242 /// Decompose the specified location into a raw FileID + Offset pair.
1243 ///
1244 /// If the location is an expansion record, walk through it until we find
1245 /// the final location expanded.
1246 std::pair<FileID, unsigned>
1247 getDecomposedExpansionLoc(SourceLocation Loc) const {
1248 FileID FID = getFileID(Loc);
1249 bool Invalid = false;
1250 const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
1251 if (Invalid)
1252 return std::make_pair(FileID(), 0);
1253
1254 unsigned Offset = Loc.getOffset()-E->getOffset();
1255 if (Loc.isFileID())
1256 return std::make_pair(FID, Offset);
1257
1258 return getDecomposedExpansionLocSlowCase(E);
1259 }
1260
1261 /// Decompose the specified location into a raw FileID + Offset pair.
1262 ///
1263 /// If the location is an expansion record, walk through it until we find
1264 /// its spelling record.
1265 std::pair<FileID, unsigned>
1266 getDecomposedSpellingLoc(SourceLocation Loc) const {
1267 FileID FID = getFileID(Loc);
1268 bool Invalid = false;
1269 const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
1270 if (Invalid)
1271 return std::make_pair(FileID(), 0);
1272
1273 unsigned Offset = Loc.getOffset()-E->getOffset();
1274 if (Loc.isFileID())
1275 return std::make_pair(FID, Offset);
1276 return getDecomposedSpellingLocSlowCase(E, Offset);
1277 }
1278
1279 /// Returns the "included/expanded in" decomposed location of the given
1280 /// FileID.
1281 std::pair<FileID, unsigned> getDecomposedIncludedLoc(FileID FID) const;
1282
1283 /// Returns the offset from the start of the file that the
1284 /// specified SourceLocation represents.
1285 ///
1286 /// This is not very meaningful for a macro ID.
1287 unsigned getFileOffset(SourceLocation SpellingLoc) const {
1288 return getDecomposedLoc(SpellingLoc).second;
1289 }
1290
1291 /// Tests whether the given source location represents a macro
1292 /// argument's expansion into the function-like macro definition.
1293 ///
1294 /// \param StartLoc If non-null and function returns true, it is set to the
1295 /// start location of the macro argument expansion.
1296 ///
1297 /// Such source locations only appear inside of the expansion
1298 /// locations representing where a particular function-like macro was
1299 /// expanded.
1300 bool isMacroArgExpansion(SourceLocation Loc,
1301 SourceLocation *StartLoc = nullptr) const;
1302
1303 /// Tests whether the given source location represents the expansion of
1304 /// a macro body.
1305 ///
1306 /// This is equivalent to testing whether the location is part of a macro
1307 /// expansion but not the expansion of an argument to a function-like macro.
1308 bool isMacroBodyExpansion(SourceLocation Loc) const;
1309
1310 /// Returns true if the given MacroID location points at the beginning
1311 /// of the immediate macro expansion.
1312 ///
1313 /// \param MacroBegin If non-null and function returns true, it is set to the
1314 /// begin location of the immediate macro expansion.
1315 bool isAtStartOfImmediateMacroExpansion(SourceLocation Loc,
1316 SourceLocation *MacroBegin = nullptr) const;
1317
1318 /// Returns true if the given MacroID location points at the character
1319 /// end of the immediate macro expansion.
1320 ///
1321 /// \param MacroEnd If non-null and function returns true, it is set to the
1322 /// character end location of the immediate macro expansion.
1323 bool
1324 isAtEndOfImmediateMacroExpansion(SourceLocation Loc,
1325 SourceLocation *MacroEnd = nullptr) const;
1326
1327 /// Returns true if \p Loc is inside the [\p Start, +\p Length)
1328 /// chunk of the source location address space.
1329 ///
1330 /// If it's true and \p RelativeOffset is non-null, it will be set to the
1331 /// relative offset of \p Loc inside the chunk.
1332 bool isInSLocAddrSpace(SourceLocation Loc,
1333 SourceLocation Start, unsigned Length,
1334 unsigned *RelativeOffset = nullptr) const {
1335 assert(((Start.getOffset() < NextLocalOffset &&((((Start.getOffset() < NextLocalOffset && Start.getOffset
()+Length <= NextLocalOffset) || (Start.getOffset() >= CurrentLoadedOffset
&& Start.getOffset()+Length < MaxLoadedOffset)) &&
"Chunk is not valid SLoc address space") ? static_cast<void
> (0) : __assert_fail ("((Start.getOffset() < NextLocalOffset && Start.getOffset()+Length <= NextLocalOffset) || (Start.getOffset() >= CurrentLoadedOffset && Start.getOffset()+Length < MaxLoadedOffset)) && \"Chunk is not valid SLoc address space\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 1339, __PRETTY_FUNCTION__))
1336 Start.getOffset()+Length <= NextLocalOffset) ||((((Start.getOffset() < NextLocalOffset && Start.getOffset
()+Length <= NextLocalOffset) || (Start.getOffset() >= CurrentLoadedOffset
&& Start.getOffset()+Length < MaxLoadedOffset)) &&
"Chunk is not valid SLoc address space") ? static_cast<void
> (0) : __assert_fail ("((Start.getOffset() < NextLocalOffset && Start.getOffset()+Length <= NextLocalOffset) || (Start.getOffset() >= CurrentLoadedOffset && Start.getOffset()+Length < MaxLoadedOffset)) && \"Chunk is not valid SLoc address space\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 1339, __PRETTY_FUNCTION__))
1337 (Start.getOffset() >= CurrentLoadedOffset &&((((Start.getOffset() < NextLocalOffset && Start.getOffset
()+Length <= NextLocalOffset) || (Start.getOffset() >= CurrentLoadedOffset
&& Start.getOffset()+Length < MaxLoadedOffset)) &&
"Chunk is not valid SLoc address space") ? static_cast<void
> (0) : __assert_fail ("((Start.getOffset() < NextLocalOffset && Start.getOffset()+Length <= NextLocalOffset) || (Start.getOffset() >= CurrentLoadedOffset && Start.getOffset()+Length < MaxLoadedOffset)) && \"Chunk is not valid SLoc address space\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 1339, __PRETTY_FUNCTION__))
1338 Start.getOffset()+Length < MaxLoadedOffset)) &&((((Start.getOffset() < NextLocalOffset && Start.getOffset
()+Length <= NextLocalOffset) || (Start.getOffset() >= CurrentLoadedOffset
&& Start.getOffset()+Length < MaxLoadedOffset)) &&
"Chunk is not valid SLoc address space") ? static_cast<void
> (0) : __assert_fail ("((Start.getOffset() < NextLocalOffset && Start.getOffset()+Length <= NextLocalOffset) || (Start.getOffset() >= CurrentLoadedOffset && Start.getOffset()+Length < MaxLoadedOffset)) && \"Chunk is not valid SLoc address space\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 1339, __PRETTY_FUNCTION__))
1339 "Chunk is not valid SLoc address space")((((Start.getOffset() < NextLocalOffset && Start.getOffset
()+Length <= NextLocalOffset) || (Start.getOffset() >= CurrentLoadedOffset
&& Start.getOffset()+Length < MaxLoadedOffset)) &&
"Chunk is not valid SLoc address space") ? static_cast<void
> (0) : __assert_fail ("((Start.getOffset() < NextLocalOffset && Start.getOffset()+Length <= NextLocalOffset) || (Start.getOffset() >= CurrentLoadedOffset && Start.getOffset()+Length < MaxLoadedOffset)) && \"Chunk is not valid SLoc address space\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 1339, __PRETTY_FUNCTION__))
;
1340 unsigned LocOffs = Loc.getOffset();
1341 unsigned BeginOffs = Start.getOffset();
1342 unsigned EndOffs = BeginOffs + Length;
1343 if (LocOffs >= BeginOffs && LocOffs < EndOffs) {
1344 if (RelativeOffset)
1345 *RelativeOffset = LocOffs - BeginOffs;
1346 return true;
1347 }
1348
1349 return false;
1350 }
1351
1352 /// Return true if both \p LHS and \p RHS are in the local source
1353 /// location address space or the loaded one.
1354 ///
1355 /// If it's true and \p RelativeOffset is non-null, it will be set to the
1356 /// offset of \p RHS relative to \p LHS.
1357 bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS,
1358 int *RelativeOffset) const {
1359 unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset();
1360 bool LHSLoaded = LHSOffs >= CurrentLoadedOffset;
1361 bool RHSLoaded = RHSOffs >= CurrentLoadedOffset;
1362
1363 if (LHSLoaded == RHSLoaded) {
1364 if (RelativeOffset)
1365 *RelativeOffset = RHSOffs - LHSOffs;
1366 return true;
1367 }
1368
1369 return false;
1370 }
1371
1372 //===--------------------------------------------------------------------===//
1373 // Queries about the code at a SourceLocation.
1374 //===--------------------------------------------------------------------===//
1375
1376 /// Return a pointer to the start of the specified location
1377 /// in the appropriate spelling MemoryBuffer.
1378 ///
1379 /// \param Invalid If non-NULL, will be set \c true if an error occurs.
1380 const char *getCharacterData(SourceLocation SL,
1381 bool *Invalid = nullptr) const;
1382
1383 /// Return the column # for the specified file position.
1384 ///
1385 /// This is significantly cheaper to compute than the line number. This
1386 /// returns zero if the column number isn't known. This may only be called
1387 /// on a file sloc, so you must choose a spelling or expansion location
1388 /// before calling this method.
1389 unsigned getColumnNumber(FileID FID, unsigned FilePos,
1390 bool *Invalid = nullptr) const;
1391 unsigned getSpellingColumnNumber(SourceLocation Loc,
1392 bool *Invalid = nullptr) const;
1393 unsigned getExpansionColumnNumber(SourceLocation Loc,
1394 bool *Invalid = nullptr) const;
1395 unsigned getPresumedColumnNumber(SourceLocation Loc,
1396 bool *Invalid = nullptr) const;
1397
1398 /// Given a SourceLocation, return the spelling line number
1399 /// for the position indicated.
1400 ///
1401 /// This requires building and caching a table of line offsets for the
1402 /// MemoryBuffer, so this is not cheap: use only when about to emit a
1403 /// diagnostic.
1404 unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = nullptr) const;
1405 unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
1406 unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
1407 unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
1408
1409 /// Return the filename or buffer identifier of the buffer the
1410 /// location is in.
1411 ///
1412 /// Note that this name does not respect \#line directives. Use
1413 /// getPresumedLoc for normal clients.
1414 StringRef getBufferName(SourceLocation Loc, bool *Invalid = nullptr) const;
1415
1416 /// Return the file characteristic of the specified source
1417 /// location, indicating whether this is a normal file, a system
1418 /// header, or an "implicit extern C" system header.
1419 ///
1420 /// This state can be modified with flags on GNU linemarker directives like:
1421 /// \code
1422 /// # 4 "foo.h" 3
1423 /// \endcode
1424 /// which changes all source locations in the current file after that to be
1425 /// considered to be from a system header.
1426 SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
1427
1428 /// Returns the "presumed" location of a SourceLocation specifies.
1429 ///
1430 /// A "presumed location" can be modified by \#line or GNU line marker
1431 /// directives. This provides a view on the data that a user should see
1432 /// in diagnostics, for example.
1433 ///
1434 /// Note that a presumed location is always given as the expansion point of
1435 /// an expansion location, not at the spelling location.
1436 ///
1437 /// \returns The presumed location of the specified SourceLocation. If the
1438 /// presumed location cannot be calculated (e.g., because \p Loc is invalid
1439 /// or the file containing \p Loc has changed on disk), returns an invalid
1440 /// presumed location.
1441 PresumedLoc getPresumedLoc(SourceLocation Loc,
1442 bool UseLineDirectives = true) const;
1443
1444 /// Returns whether the PresumedLoc for a given SourceLocation is
1445 /// in the main file.
1446 ///
1447 /// This computes the "presumed" location for a SourceLocation, then checks
1448 /// whether it came from a file other than the main file. This is different
1449 /// from isWrittenInMainFile() because it takes line marker directives into
1450 /// account.
1451 bool isInMainFile(SourceLocation Loc) const;
1452
1453 /// Returns true if the spelling locations for both SourceLocations
1454 /// are part of the same file buffer.
1455 ///
1456 /// This check ignores line marker directives.
1457 bool isWrittenInSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
1458 return getFileID(Loc1) == getFileID(Loc2);
1459 }
1460
1461 /// Returns true if the spelling location for the given location
1462 /// is in the main file buffer.
1463 ///
1464 /// This check ignores line marker directives.
1465 bool isWrittenInMainFile(SourceLocation Loc) const {
1466 return getFileID(Loc) == getMainFileID();
1467 }
1468
1469 /// Returns whether \p Loc is located in a <built-in> file.
1470 bool isWrittenInBuiltinFile(SourceLocation Loc) const {
1471 StringRef Filename(getPresumedLoc(Loc).getFilename());
1472 return Filename.equals("<built-in>");
1473 }
1474
1475 /// Returns whether \p Loc is located in a <command line> file.
1476 bool isWrittenInCommandLineFile(SourceLocation Loc) const {
1477 StringRef Filename(getPresumedLoc(Loc).getFilename());
1478 return Filename.equals("<command line>");
1479 }
1480
1481 /// Returns whether \p Loc is located in a <scratch space> file.
1482 bool isWrittenInScratchSpace(SourceLocation Loc) const {
1483 StringRef Filename(getPresumedLoc(Loc).getFilename());
1484 return Filename.equals("<scratch space>");
1485 }
1486
1487 /// Returns if a SourceLocation is in a system header.
1488 bool isInSystemHeader(SourceLocation Loc) const {
1489 return isSystem(getFileCharacteristic(Loc));
1490 }
1491
1492 /// Returns if a SourceLocation is in an "extern C" system header.
1493 bool isInExternCSystemHeader(SourceLocation Loc) const {
1494 return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
1495 }
1496
1497 /// Returns whether \p Loc is expanded from a macro in a system header.
1498 bool isInSystemMacro(SourceLocation loc) const {
1499 if (!loc.isMacroID())
1500 return false;
1501
1502 // This happens when the macro is the result of a paste, in that case
1503 // its spelling is the scratch memory, so we take the parent context.
1504 // There can be several level of token pasting.
1505 if (isWrittenInScratchSpace(getSpellingLoc(loc))) {
1506 do {
1507 loc = getImmediateMacroCallerLoc(loc);
1508 } while (isWrittenInScratchSpace(getSpellingLoc(loc)));
1509 return isInSystemMacro(loc);
1510 }
1511
1512 return isInSystemHeader(getSpellingLoc(loc));
1513 }
1514
1515 /// The size of the SLocEntry that \p FID represents.
1516 unsigned getFileIDSize(FileID FID) const;
1517
1518 /// Given a specific FileID, returns true if \p Loc is inside that
1519 /// FileID chunk and sets relative offset (offset of \p Loc from beginning
1520 /// of FileID) to \p relativeOffset.
1521 bool isInFileID(SourceLocation Loc, FileID FID,
1522 unsigned *RelativeOffset = nullptr) const {
1523 unsigned Offs = Loc.getOffset();
1524 if (isOffsetInFileID(FID, Offs)) {
1525 if (RelativeOffset)
1526 *RelativeOffset = Offs - getSLocEntry(FID).getOffset();
1527 return true;
1528 }
1529
1530 return false;
1531 }
1532
1533 //===--------------------------------------------------------------------===//
1534 // Line Table Manipulation Routines
1535 //===--------------------------------------------------------------------===//
1536
1537 /// Return the uniqued ID for the specified filename.
1538 unsigned getLineTableFilenameID(StringRef Str);
1539
1540 /// Add a line note to the line table for the FileID and offset
1541 /// specified by Loc.
1542 ///
1543 /// If FilenameID is -1, it is considered to be unspecified.
1544 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
1545 bool IsFileEntry, bool IsFileExit,
1546 SrcMgr::CharacteristicKind FileKind);
1547
1548 /// Determine if the source manager has a line table.
1549 bool hasLineTable() const { return LineTable != nullptr; }
1550
1551 /// Retrieve the stored line table.
1552 LineTableInfo &getLineTable();
1553
1554 //===--------------------------------------------------------------------===//
1555 // Queries for performance analysis.
1556 //===--------------------------------------------------------------------===//
1557
1558 /// Return the total amount of physical memory allocated by the
1559 /// ContentCache allocator.
1560 size_t getContentCacheSize() const {
1561 return ContentCacheAlloc.getTotalMemory();
1562 }
1563
1564 struct MemoryBufferSizes {
1565 const size_t malloc_bytes;
1566 const size_t mmap_bytes;
1567
1568 MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
1569 : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
1570 };
1571
1572 /// Return the amount of memory used by memory buffers, breaking down
1573 /// by heap-backed versus mmap'ed memory.
1574 MemoryBufferSizes getMemoryBufferSizes() const;
1575
1576 /// Return the amount of memory used for various side tables and
1577 /// data structures in the SourceManager.
1578 size_t getDataStructureSizes() const;
1579
1580 //===--------------------------------------------------------------------===//
1581 // Other miscellaneous methods.
1582 //===--------------------------------------------------------------------===//
1583
1584 /// Get the source location for the given file:line:col triplet.
1585 ///
1586 /// If the source file is included multiple times, the source location will
1587 /// be based upon the first inclusion.
1588 SourceLocation translateFileLineCol(const FileEntry *SourceFile,
1589 unsigned Line, unsigned Col) const;
1590
1591 /// Get the FileID for the given file.
1592 ///
1593 /// If the source file is included multiple times, the FileID will be the
1594 /// first inclusion.
1595 FileID translateFile(const FileEntry *SourceFile) const;
1596
1597 /// Get the source location in \p FID for the given line:col.
1598 /// Returns null location if \p FID is not a file SLocEntry.
1599 SourceLocation translateLineCol(FileID FID,
1600 unsigned Line, unsigned Col) const;
1601
1602 /// If \p Loc points inside a function macro argument, the returned
1603 /// location will be the macro location in which the argument was expanded.
1604 /// If a macro argument is used multiple times, the expanded location will
1605 /// be at the first expansion of the argument.
1606 /// e.g.
1607 /// MY_MACRO(foo);
1608 /// ^
1609 /// Passing a file location pointing at 'foo', will yield a macro location
1610 /// where 'foo' was expanded into.
1611 SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const;
1612
1613 /// Determines the order of 2 source locations in the translation unit.
1614 ///
1615 /// \returns true if LHS source location comes before RHS, false otherwise.
1616 bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
1617
1618 /// Determines whether the two decomposed source location is in the
1619 /// same translation unit. As a byproduct, it also calculates the order
1620 /// of the source locations in case they are in the same TU.
1621 ///
1622 /// \returns Pair of bools the first component is true if the two locations
1623 /// are in the same TU. The second bool is true if the first is true
1624 /// and \p LOffs is before \p ROffs.
1625 std::pair<bool, bool>
1626 isInTheSameTranslationUnit(std::pair<FileID, unsigned> &LOffs,
1627 std::pair<FileID, unsigned> &ROffs) const;
1628
1629 /// Determines the order of 2 source locations in the "source location
1630 /// address space".
1631 bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const {
1632 return isBeforeInSLocAddrSpace(LHS, RHS.getOffset());
1633 }
1634
1635 /// Determines the order of a source location and a source location
1636 /// offset in the "source location address space".
1637 ///
1638 /// Note that we always consider source locations loaded from
1639 bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const {
1640 unsigned LHSOffset = LHS.getOffset();
1641 bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1642 bool RHSLoaded = RHS >= CurrentLoadedOffset;
1643 if (LHSLoaded == RHSLoaded)
1644 return LHSOffset < RHS;
1645
1646 return LHSLoaded;
1647 }
1648
1649 /// Return true if the Point is within Start and End.
1650 bool isPointWithin(SourceLocation Location, SourceLocation Start,
1651 SourceLocation End) const {
1652 return Location == Start || Location == End ||
1653 (isBeforeInTranslationUnit(Start, Location) &&
1654 isBeforeInTranslationUnit(Location, End));
1655 }
1656
1657 // Iterators over FileInfos.
1658 using fileinfo_iterator =
1659 llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::const_iterator;
1660
1661 fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
1662 fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
1663 bool hasFileInfo(const FileEntry *File) const {
1664 return FileInfos.find(File) != FileInfos.end();
1665 }
1666
1667 /// Print statistics to stderr.
1668 void PrintStats() const;
1669
1670 void dump() const;
1671
1672 /// Get the number of local SLocEntries we have.
1673 unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); }
1674
1675 /// Get a local SLocEntry. This is exposed for indexing.
1676 const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index,
1677 bool *Invalid = nullptr) const {
1678 assert(Index < LocalSLocEntryTable.size() && "Invalid index")((Index < LocalSLocEntryTable.size() && "Invalid index"
) ? static_cast<void> (0) : __assert_fail ("Index < LocalSLocEntryTable.size() && \"Invalid index\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 1678, __PRETTY_FUNCTION__))
;
1679 return LocalSLocEntryTable[Index];
1680 }
1681
1682 /// Get the number of loaded SLocEntries we have.
1683 unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();}
1684
1685 /// Get a loaded SLocEntry. This is exposed for indexing.
1686 const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index,
1687 bool *Invalid = nullptr) const {
1688 assert(Index < LoadedSLocEntryTable.size() && "Invalid index")((Index < LoadedSLocEntryTable.size() && "Invalid index"
) ? static_cast<void> (0) : __assert_fail ("Index < LoadedSLocEntryTable.size() && \"Invalid index\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 1688, __PRETTY_FUNCTION__))
;
1689 if (SLocEntryLoaded[Index])
1690 return LoadedSLocEntryTable[Index];
1691 return loadSLocEntry(Index, Invalid);
1692 }
1693
1694 const SrcMgr::SLocEntry &getSLocEntry(FileID FID,
1695 bool *Invalid = nullptr) const {
1696 if (FID.ID == 0 || FID.ID == -1) {
1697 if (Invalid) *Invalid = true;
1698 return LocalSLocEntryTable[0];
1699 }
1700 return getSLocEntryByID(FID.ID, Invalid);
1701 }
1702
1703 unsigned getNextLocalOffset() const { return NextLocalOffset; }
1704
1705 void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
1706 assert(LoadedSLocEntryTable.empty() &&((LoadedSLocEntryTable.empty() && "Invalidating existing loaded entries"
) ? static_cast<void> (0) : __assert_fail ("LoadedSLocEntryTable.empty() && \"Invalidating existing loaded entries\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 1707, __PRETTY_FUNCTION__))
1707 "Invalidating existing loaded entries")((LoadedSLocEntryTable.empty() && "Invalidating existing loaded entries"
) ? static_cast<void> (0) : __assert_fail ("LoadedSLocEntryTable.empty() && \"Invalidating existing loaded entries\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 1707, __PRETTY_FUNCTION__))
;
1708 ExternalSLocEntries = Source;
1709 }
1710
1711 /// Allocate a number of loaded SLocEntries, which will be actually
1712 /// loaded on demand from the external source.
1713 ///
1714 /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
1715 /// in the global source view. The lowest ID and the base offset of the
1716 /// entries will be returned.
1717 std::pair<int, unsigned>
1718 AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize);
1719
1720 /// Returns true if \p Loc came from a PCH/Module.
1721 bool isLoadedSourceLocation(SourceLocation Loc) const {
1722 return Loc.getOffset() >= CurrentLoadedOffset;
1723 }
1724
1725 /// Returns true if \p Loc did not come from a PCH/Module.
1726 bool isLocalSourceLocation(SourceLocation Loc) const {
1727 return Loc.getOffset() < NextLocalOffset;
1728 }
1729
1730 /// Returns true if \p FID came from a PCH/Module.
1731 bool isLoadedFileID(FileID FID) const {
1732 assert(FID.ID != -1 && "Using FileID sentinel value")((FID.ID != -1 && "Using FileID sentinel value") ? static_cast
<void> (0) : __assert_fail ("FID.ID != -1 && \"Using FileID sentinel value\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 1732, __PRETTY_FUNCTION__))
;
1733 return FID.ID < 0;
1734 }
1735
1736 /// Returns true if \p FID did not come from a PCH/Module.
1737 bool isLocalFileID(FileID FID) const {
1738 return !isLoadedFileID(FID);
1739 }
1740
1741 /// Gets the location of the immediate macro caller, one level up the stack
1742 /// toward the initial macro typed into the source.
1743 SourceLocation getImmediateMacroCallerLoc(SourceLocation Loc) const {
1744 if (!Loc.isMacroID()) return Loc;
1745
1746 // When we have the location of (part of) an expanded parameter, its
1747 // spelling location points to the argument as expanded in the macro call,
1748 // and therefore is used to locate the macro caller.
1749 if (isMacroArgExpansion(Loc))
1750 return getImmediateSpellingLoc(Loc);
1751
1752 // Otherwise, the caller of the macro is located where this macro is
1753 // expanded (while the spelling is part of the macro definition).
1754 return getImmediateExpansionRange(Loc).getBegin();
1755 }
1756
1757 /// \return Location of the top-level macro caller.
1758 SourceLocation getTopMacroCallerLoc(SourceLocation Loc) const;
1759
1760private:
1761 friend class ASTReader;
1762 friend class ASTWriter;
1763
1764 llvm::MemoryBuffer *getFakeBufferForRecovery() const;
1765 const SrcMgr::ContentCache *getFakeContentCacheForRecovery() const;
1766
1767 const SrcMgr::SLocEntry &loadSLocEntry(unsigned Index, bool *Invalid) const;
1768
1769 /// Get the entry with the given unwrapped FileID.
1770 const SrcMgr::SLocEntry &getSLocEntryByID(int ID,
1771 bool *Invalid = nullptr) const {
1772 assert(ID != -1 && "Using FileID sentinel value")((ID != -1 && "Using FileID sentinel value") ? static_cast
<void> (0) : __assert_fail ("ID != -1 && \"Using FileID sentinel value\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 1772, __PRETTY_FUNCTION__))
;
1773 if (ID < 0)
1774 return getLoadedSLocEntryByID(ID, Invalid);
1775 return getLocalSLocEntry(static_cast<unsigned>(ID), Invalid);
1776 }
1777
1778 const SrcMgr::SLocEntry &
1779 getLoadedSLocEntryByID(int ID, bool *Invalid = nullptr) const {
1780 return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2), Invalid);
1781 }
1782
1783 /// Implements the common elements of storing an expansion info struct into
1784 /// the SLocEntry table and producing a source location that refers to it.
1785 SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion,
1786 unsigned TokLength,
1787 int LoadedID = 0,
1788 unsigned LoadedOffset = 0);
1789
1790 /// Return true if the specified FileID contains the
1791 /// specified SourceLocation offset. This is a very hot method.
1792 inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
1793 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1794 // If the entry is after the offset, it can't contain it.
1795 if (SLocOffset < Entry.getOffset()) return false;
1796
1797 // If this is the very last entry then it does.
1798 if (FID.ID == -2)
1799 return true;
1800
1801 // If it is the last local entry, then it does if the location is local.
1802 if (FID.ID+1 == static_cast<int>(LocalSLocEntryTable.size()))
1803 return SLocOffset < NextLocalOffset;
1804
1805 // Otherwise, the entry after it has to not include it. This works for both
1806 // local and loaded entries.
1807 return SLocOffset < getSLocEntryByID(FID.ID+1).getOffset();
1808 }
1809
1810 /// Returns the previous in-order FileID or an invalid FileID if there
1811 /// is no previous one.
1812 FileID getPreviousFileID(FileID FID) const;
1813
1814 /// Returns the next in-order FileID or an invalid FileID if there is
1815 /// no next one.
1816 FileID getNextFileID(FileID FID) const;
1817
1818 /// Create a new fileID for the specified ContentCache and
1819 /// include position.
1820 ///
1821 /// This works regardless of whether the ContentCache corresponds to a
1822 /// file or some other input source.
1823 FileID createFileID(const SrcMgr::ContentCache *File, StringRef Filename,
1824 SourceLocation IncludePos,
1825 SrcMgr::CharacteristicKind DirCharacter, int LoadedID,
1826 unsigned LoadedOffset);
1827
1828 const SrcMgr::ContentCache *
1829 getOrCreateContentCache(const FileEntry *SourceFile,
1830 bool isSystemFile = false);
1831
1832 /// Create a new ContentCache for the specified memory buffer.
1833 const SrcMgr::ContentCache *
1834 createMemBufferContentCache(const llvm::MemoryBuffer *Buf, bool DoNotFree);
1835
1836 FileID getFileIDSlow(unsigned SLocOffset) const;
1837 FileID getFileIDLocal(unsigned SLocOffset) const;
1838 FileID getFileIDLoaded(unsigned SLocOffset) const;
1839
1840 SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
1841 SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
1842 SourceLocation getFileLocSlowCase(SourceLocation Loc) const;
1843
1844 std::pair<FileID, unsigned>
1845 getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const;
1846 std::pair<FileID, unsigned>
1847 getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1848 unsigned Offset) const;
1849 void computeMacroArgsCache(MacroArgsMap &MacroArgsCache, FileID FID) const;
1850 void associateFileChunkWithMacroArgExp(MacroArgsMap &MacroArgsCache,
1851 FileID FID,
1852 SourceLocation SpellLoc,
1853 SourceLocation ExpansionLoc,
1854 unsigned ExpansionLength) const;
1855};
1856
1857/// Comparison function object.
1858template<typename T>
1859class BeforeThanCompare;
1860
1861/// Compare two source locations.
1862template<>
1863class BeforeThanCompare<SourceLocation> {
1864 SourceManager &SM;
1865
1866public:
1867 explicit BeforeThanCompare(SourceManager &SM) : SM(SM) {}
1868
1869 bool operator()(SourceLocation LHS, SourceLocation RHS) const {
1870 return SM.isBeforeInTranslationUnit(LHS, RHS);
1871 }
1872};
1873
1874/// Compare two non-overlapping source ranges.
1875template<>
1876class BeforeThanCompare<SourceRange> {
1877 SourceManager &SM;
1878
1879public:
1880 explicit BeforeThanCompare(SourceManager &SM) : SM(SM) {}
1881
1882 bool operator()(SourceRange LHS, SourceRange RHS) const {
1883 return SM.isBeforeInTranslationUnit(LHS.getBegin(), RHS.getBegin());
1884 }
1885};
1886
1887/// SourceManager and necessary depdencies (e.g. VFS, FileManager) for a single
1888/// in-memorty file.
1889class SourceManagerForFile {
1890public:
1891 /// Creates SourceManager and necessary depdencies (e.g. VFS, FileManager).
1892 /// The main file in the SourceManager will be \p FileName with \p Content.
1893 SourceManagerForFile(StringRef FileName, StringRef Content);
1894
1895 SourceManager &get() {
1896 assert(SourceMgr)((SourceMgr) ? static_cast<void> (0) : __assert_fail ("SourceMgr"
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceManager.h"
, 1896, __PRETTY_FUNCTION__))
;
1897 return *SourceMgr;
1898 }
1899
1900private:
1901 // The order of these fields are important - they should be in the same order
1902 // as they are created in `createSourceManagerForFile` so that they can be
1903 // deleted in the reverse order as they are created.
1904 std::unique_ptr<FileManager> FileMgr;
1905 std::unique_ptr<DiagnosticsEngine> Diagnostics;
1906 std::unique_ptr<SourceManager> SourceMgr;
1907};
1908
1909} // namespace clang
1910
1911#endif // LLVM_CLANG_BASIC_SOURCEMANAGER_H

/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceLocation.h

1//===- SourceLocation.h - Compact identifier for Source Files ---*- 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/// Defines the clang::SourceLocation class and associated facilities.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_BASIC_SOURCELOCATION_H
15#define LLVM_CLANG_BASIC_SOURCELOCATION_H
16
17#include "clang/Basic/LLVM.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/Support/PointerLikeTypeTraits.h"
20#include <cassert>
21#include <cstdint>
22#include <string>
23#include <utility>
24
25namespace llvm {
26
27template <typename T> struct DenseMapInfo;
28
29} // namespace llvm
30
31namespace clang {
32
33class SourceManager;
34
35/// An opaque identifier used by SourceManager which refers to a
36/// source file (MemoryBuffer) along with its \#include path and \#line data.
37///
38class FileID {
39 /// A mostly-opaque identifier, where 0 is "invalid", >0 is
40 /// this module, and <-1 is something loaded from another module.
41 int ID = 0;
42
43public:
44 bool isValid() const { return ID != 0; }
6
Assuming field 'ID' is equal to 0
7
Returning zero, which participates in a condition later
45 bool isInvalid() const { return ID == 0; }
46
47 bool operator==(const FileID &RHS) const { return ID == RHS.ID; }
48 bool operator<(const FileID &RHS) const { return ID < RHS.ID; }
49 bool operator<=(const FileID &RHS) const { return ID <= RHS.ID; }
50 bool operator!=(const FileID &RHS) const { return !(*this == RHS); }
51 bool operator>(const FileID &RHS) const { return RHS < *this; }
52 bool operator>=(const FileID &RHS) const { return RHS <= *this; }
53
54 static FileID getSentinel() { return get(-1); }
55 unsigned getHashValue() const { return static_cast<unsigned>(ID); }
56
57private:
58 friend class ASTWriter;
59 friend class ASTReader;
60 friend class SourceManager;
61
62 static FileID get(int V) {
63 FileID F;
64 F.ID = V;
65 return F;
66 }
67
68 int getOpaqueValue() const { return ID; }
69};
70
71/// Encodes a location in the source. The SourceManager can decode this
72/// to get at the full include stack, line and column information.
73///
74/// Technically, a source location is simply an offset into the manager's view
75/// of the input source, which is all input buffers (including macro
76/// expansions) concatenated in an effectively arbitrary order. The manager
77/// actually maintains two blocks of input buffers. One, starting at offset
78/// 0 and growing upwards, contains all buffers from this module. The other,
79/// starting at the highest possible offset and growing downwards, contains
80/// buffers of loaded modules.
81///
82/// In addition, one bit of SourceLocation is used for quick access to the
83/// information whether the location is in a file or a macro expansion.
84///
85/// It is important that this type remains small. It is currently 32 bits wide.
86class SourceLocation {
87 friend class ASTReader;
88 friend class ASTWriter;
89 friend class SourceManager;
90
91 unsigned ID = 0;
92
93 enum : unsigned {
94 MacroIDBit = 1U << 31
95 };
96
97public:
98 bool isFileID() const { return (ID & MacroIDBit) == 0; }
99 bool isMacroID() const { return (ID & MacroIDBit) != 0; }
100
101 /// Return true if this is a valid SourceLocation object.
102 ///
103 /// Invalid SourceLocations are often used when events have no corresponding
104 /// location in the source (e.g. a diagnostic is required for a command line
105 /// option).
106 bool isValid() const { return ID != 0; }
107 bool isInvalid() const { return ID == 0; }
108
109private:
110 /// Return the offset into the manager's global input view.
111 unsigned getOffset() const {
112 return ID & ~MacroIDBit;
113 }
114
115 static SourceLocation getFileLoc(unsigned ID) {
116 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!")(((ID & MacroIDBit) == 0 && "Ran out of source locations!"
) ? static_cast<void> (0) : __assert_fail ("(ID & MacroIDBit) == 0 && \"Ran out of source locations!\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceLocation.h"
, 116, __PRETTY_FUNCTION__))
;
117 SourceLocation L;
118 L.ID = ID;
119 return L;
120 }
121
122 static SourceLocation getMacroLoc(unsigned ID) {
123 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!")(((ID & MacroIDBit) == 0 && "Ran out of source locations!"
) ? static_cast<void> (0) : __assert_fail ("(ID & MacroIDBit) == 0 && \"Ran out of source locations!\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceLocation.h"
, 123, __PRETTY_FUNCTION__))
;
124 SourceLocation L;
125 L.ID = MacroIDBit | ID;
126 return L;
127 }
128
129public:
130 /// Return a source location with the specified offset from this
131 /// SourceLocation.
132 SourceLocation getLocWithOffset(int Offset) const {
133 assert(((getOffset()+Offset) & MacroIDBit) == 0 && "offset overflow")((((getOffset()+Offset) & MacroIDBit) == 0 && "offset overflow"
) ? static_cast<void> (0) : __assert_fail ("((getOffset()+Offset) & MacroIDBit) == 0 && \"offset overflow\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceLocation.h"
, 133, __PRETTY_FUNCTION__))
;
134 SourceLocation L;
135 L.ID = ID+Offset;
136 return L;
137 }
138
139 /// When a SourceLocation itself cannot be used, this returns
140 /// an (opaque) 32-bit integer encoding for it.
141 ///
142 /// This should only be passed to SourceLocation::getFromRawEncoding, it
143 /// should not be inspected directly.
144 unsigned getRawEncoding() const { return ID; }
145
146 /// Turn a raw encoding of a SourceLocation object into
147 /// a real SourceLocation.
148 ///
149 /// \see getRawEncoding.
150 static SourceLocation getFromRawEncoding(unsigned Encoding) {
151 SourceLocation X;
152 X.ID = Encoding;
153 return X;
154 }
155
156 /// When a SourceLocation itself cannot be used, this returns
157 /// an (opaque) pointer encoding for it.
158 ///
159 /// This should only be passed to SourceLocation::getFromPtrEncoding, it
160 /// should not be inspected directly.
161 void* getPtrEncoding() const {
162 // Double cast to avoid a warning "cast to pointer from integer of different
163 // size".
164 return (void*)(uintptr_t)getRawEncoding();
165 }
166
167 /// Turn a pointer encoding of a SourceLocation object back
168 /// into a real SourceLocation.
169 static SourceLocation getFromPtrEncoding(const void *Encoding) {
170 return getFromRawEncoding((unsigned)(uintptr_t)Encoding);
171 }
172
173 static bool isPairOfFileLocations(SourceLocation Start, SourceLocation End) {
174 return Start.isValid() && Start.isFileID() && End.isValid() &&
175 End.isFileID();
176 }
177
178 void print(raw_ostream &OS, const SourceManager &SM) const;
179 std::string printToString(const SourceManager &SM) const;
180 void dump(const SourceManager &SM) const;
181};
182
183inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
184 return LHS.getRawEncoding() == RHS.getRawEncoding();
185}
186
187inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
188 return !(LHS == RHS);
189}
190
191inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
192 return LHS.getRawEncoding() < RHS.getRawEncoding();
193}
194
195/// A trivial tuple used to represent a source range.
196class SourceRange {
197 SourceLocation B;
198 SourceLocation E;
199
200public:
201 SourceRange() = default;
202 SourceRange(SourceLocation loc) : B(loc), E(loc) {}
203 SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
204
205 SourceLocation getBegin() const { return B; }
206 SourceLocation getEnd() const { return E; }
207
208 void setBegin(SourceLocation b) { B = b; }
209 void setEnd(SourceLocation e) { E = e; }
210
211 bool isValid() const { return B.isValid() && E.isValid(); }
212 bool isInvalid() const { return !isValid(); }
213
214 bool operator==(const SourceRange &X) const {
215 return B == X.B && E == X.E;
216 }
217
218 bool operator!=(const SourceRange &X) const {
219 return B != X.B || E != X.E;
220 }
221
222 void print(raw_ostream &OS, const SourceManager &SM) const;
223 std::string printToString(const SourceManager &SM) const;
224 void dump(const SourceManager &SM) const;
225};
226
227/// Represents a character-granular source range.
228///
229/// The underlying SourceRange can either specify the starting/ending character
230/// of the range, or it can specify the start of the range and the start of the
231/// last token of the range (a "token range"). In the token range case, the
232/// size of the last token must be measured to determine the actual end of the
233/// range.
234class CharSourceRange {
235 SourceRange Range;
236 bool IsTokenRange = false;
237
238public:
239 CharSourceRange() = default;
240 CharSourceRange(SourceRange R, bool ITR) : Range(R), IsTokenRange(ITR) {}
241
242 static CharSourceRange getTokenRange(SourceRange R) {
243 return CharSourceRange(R, true);
244 }
245
246 static CharSourceRange getCharRange(SourceRange R) {
247 return CharSourceRange(R, false);
248 }
249
250 static CharSourceRange getTokenRange(SourceLocation B, SourceLocation E) {
251 return getTokenRange(SourceRange(B, E));
252 }
253
254 static CharSourceRange getCharRange(SourceLocation B, SourceLocation E) {
255 return getCharRange(SourceRange(B, E));
256 }
257
258 /// Return true if the end of this range specifies the start of
259 /// the last token. Return false if the end of this range specifies the last
260 /// character in the range.
261 bool isTokenRange() const { return IsTokenRange; }
262 bool isCharRange() const { return !IsTokenRange; }
263
264 SourceLocation getBegin() const { return Range.getBegin(); }
265 SourceLocation getEnd() const { return Range.getEnd(); }
266 SourceRange getAsRange() const { return Range; }
267
268 void setBegin(SourceLocation b) { Range.setBegin(b); }
269 void setEnd(SourceLocation e) { Range.setEnd(e); }
270 void setTokenRange(bool TR) { IsTokenRange = TR; }
271
272 bool isValid() const { return Range.isValid(); }
273 bool isInvalid() const { return !isValid(); }
274};
275
276/// Represents an unpacked "presumed" location which can be presented
277/// to the user.
278///
279/// A 'presumed' location can be modified by \#line and GNU line marker
280/// directives and is always the expansion point of a normal location.
281///
282/// You can get a PresumedLoc from a SourceLocation with SourceManager.
283class PresumedLoc {
284 const char *Filename = nullptr;
285 FileID ID;
286 unsigned Line, Col;
287 SourceLocation IncludeLoc;
288
289public:
290 PresumedLoc() = default;
291 PresumedLoc(const char *FN, FileID FID, unsigned Ln, unsigned Co,
292 SourceLocation IL)
293 : Filename(FN), ID(FID), Line(Ln), Col(Co), IncludeLoc(IL) {}
294
295 /// Return true if this object is invalid or uninitialized.
296 ///
297 /// This occurs when created with invalid source locations or when walking
298 /// off the top of a \#include stack.
299 bool isInvalid() const { return Filename == nullptr; }
300 bool isValid() const { return Filename != nullptr; }
301
302 /// Return the presumed filename of this location.
303 ///
304 /// This can be affected by \#line etc.
305 const char *getFilename() const {
306 assert(isValid())((isValid()) ? static_cast<void> (0) : __assert_fail ("isValid()"
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceLocation.h"
, 306, __PRETTY_FUNCTION__))
;
307 return Filename;
308 }
309
310 FileID getFileID() const {
311 assert(isValid())((isValid()) ? static_cast<void> (0) : __assert_fail ("isValid()"
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceLocation.h"
, 311, __PRETTY_FUNCTION__))
;
312 return ID;
313 }
314
315 /// Return the presumed line number of this location.
316 ///
317 /// This can be affected by \#line etc.
318 unsigned getLine() const {
319 assert(isValid())((isValid()) ? static_cast<void> (0) : __assert_fail ("isValid()"
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceLocation.h"
, 319, __PRETTY_FUNCTION__))
;
320 return Line;
321 }
322
323 /// Return the presumed column number of this location.
324 ///
325 /// This cannot be affected by \#line, but is packaged here for convenience.
326 unsigned getColumn() const {
327 assert(isValid())((isValid()) ? static_cast<void> (0) : __assert_fail ("isValid()"
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceLocation.h"
, 327, __PRETTY_FUNCTION__))
;
328 return Col;
329 }
330
331 /// Return the presumed include location of this location.
332 ///
333 /// This can be affected by GNU linemarker directives.
334 SourceLocation getIncludeLoc() const {
335 assert(isValid())((isValid()) ? static_cast<void> (0) : __assert_fail ("isValid()"
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceLocation.h"
, 335, __PRETTY_FUNCTION__))
;
336 return IncludeLoc;
337 }
338};
339
340class FileEntry;
341
342/// A SourceLocation and its associated SourceManager.
343///
344/// This is useful for argument passing to functions that expect both objects.
345class FullSourceLoc : public SourceLocation {
346 const SourceManager *SrcMgr = nullptr;
347
348public:
349 /// Creates a FullSourceLoc where isValid() returns \c false.
350 FullSourceLoc() = default;
351
352 explicit FullSourceLoc(SourceLocation Loc, const SourceManager &SM)
353 : SourceLocation(Loc), SrcMgr(&SM) {}
354
355 bool hasManager() const {
356 bool hasSrcMgr = SrcMgr != nullptr;
357 assert(hasSrcMgr == isValid() && "FullSourceLoc has location but no manager")((hasSrcMgr == isValid() && "FullSourceLoc has location but no manager"
) ? static_cast<void> (0) : __assert_fail ("hasSrcMgr == isValid() && \"FullSourceLoc has location but no manager\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceLocation.h"
, 357, __PRETTY_FUNCTION__))
;
358 return hasSrcMgr;
359 }
360
361 /// \pre This FullSourceLoc has an associated SourceManager.
362 const SourceManager &getManager() const {
363 assert(SrcMgr && "SourceManager is NULL.")((SrcMgr && "SourceManager is NULL.") ? static_cast<
void> (0) : __assert_fail ("SrcMgr && \"SourceManager is NULL.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceLocation.h"
, 363, __PRETTY_FUNCTION__))
;
364 return *SrcMgr;
365 }
366
367 FileID getFileID() const;
368
369 FullSourceLoc getExpansionLoc() const;
370 FullSourceLoc getSpellingLoc() const;
371 FullSourceLoc getFileLoc() const;
372 PresumedLoc getPresumedLoc(bool UseLineDirectives = true) const;
373 bool isMacroArgExpansion(FullSourceLoc *StartLoc = nullptr) const;
374 FullSourceLoc getImmediateMacroCallerLoc() const;
375 std::pair<FullSourceLoc, StringRef> getModuleImportLoc() const;
376 unsigned getFileOffset() const;
377
378 unsigned getExpansionLineNumber(bool *Invalid = nullptr) const;
379 unsigned getExpansionColumnNumber(bool *Invalid = nullptr) const;
380
381 unsigned getSpellingLineNumber(bool *Invalid = nullptr) const;
382 unsigned getSpellingColumnNumber(bool *Invalid = nullptr) const;
383
384 const char *getCharacterData(bool *Invalid = nullptr) const;
385
386 unsigned getLineNumber(bool *Invalid = nullptr) const;
387 unsigned getColumnNumber(bool *Invalid = nullptr) const;
388
389 const FileEntry *getFileEntry() const;
390
391 /// Return a StringRef to the source buffer data for the
392 /// specified FileID.
393 StringRef getBufferData(bool *Invalid = nullptr) const;
394
395 /// Decompose the specified location into a raw FileID + Offset pair.
396 ///
397 /// The first element is the FileID, the second is the offset from the
398 /// start of the buffer of the location.
399 std::pair<FileID, unsigned> getDecomposedLoc() const;
400
401 bool isInSystemHeader() const;
402
403 /// Determines the order of 2 source locations in the translation unit.
404 ///
405 /// \returns true if this source location comes before 'Loc', false otherwise.
406 bool isBeforeInTranslationUnitThan(SourceLocation Loc) const;
407
408 /// Determines the order of 2 source locations in the translation unit.
409 ///
410 /// \returns true if this source location comes before 'Loc', false otherwise.
411 bool isBeforeInTranslationUnitThan(FullSourceLoc Loc) const {
412 assert(Loc.isValid())((Loc.isValid()) ? static_cast<void> (0) : __assert_fail
("Loc.isValid()", "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceLocation.h"
, 412, __PRETTY_FUNCTION__))
;
413 assert(SrcMgr == Loc.SrcMgr && "Loc comes from another SourceManager!")((SrcMgr == Loc.SrcMgr && "Loc comes from another SourceManager!"
) ? static_cast<void> (0) : __assert_fail ("SrcMgr == Loc.SrcMgr && \"Loc comes from another SourceManager!\""
, "/build/llvm-toolchain-snapshot-10~svn374877/tools/clang/include/clang/Basic/SourceLocation.h"
, 413, __PRETTY_FUNCTION__))
;
414 return isBeforeInTranslationUnitThan((SourceLocation)Loc);
415 }
416
417 /// Comparison function class, useful for sorting FullSourceLocs.
418 struct BeforeThanCompare {
419 bool operator()(const FullSourceLoc& lhs, const FullSourceLoc& rhs) const {
420 return lhs.isBeforeInTranslationUnitThan(rhs);
421 }
422 };
423
424 /// Prints information about this FullSourceLoc to stderr.
425 ///
426 /// This is useful for debugging.
427 void dump() const;
428
429 friend bool
430 operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
431 return LHS.getRawEncoding() == RHS.getRawEncoding() &&
432 LHS.SrcMgr == RHS.SrcMgr;
433 }
434
435 friend bool
436 operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
437 return !(LHS == RHS);
438 }
439};
440
441} // namespace clang
442
443namespace llvm {
444
445 /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
446 /// DenseSets.
447 template <>
448 struct DenseMapInfo<clang::FileID> {
449 static clang::FileID getEmptyKey() {
450 return {};
451 }
452
453 static clang::FileID getTombstoneKey() {
454 return clang::FileID::getSentinel();
455 }
456
457 static unsigned getHashValue(clang::FileID S) {
458 return S.getHashValue();
459 }
460
461 static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
462 return LHS == RHS;
463 }
464 };
465
466 // Teach SmallPtrSet how to handle SourceLocation.
467 template<>
468 struct PointerLikeTypeTraits<clang::SourceLocation> {
469 enum { NumLowBitsAvailable = 0 };
470
471 static void *getAsVoidPointer(clang::SourceLocation L) {
472 return L.getPtrEncoding();
473 }
474
475 static clang::SourceLocation getFromVoidPointer(void *P) {
476 return clang::SourceLocation::getFromRawEncoding((unsigned)(uintptr_t)P);
477 }
478 };
479
480} // namespace llvm
481
482#endif // LLVM_CLANG_BASIC_SOURCELOCATION_H