clang 20.0.0git
SourceLocation.cpp
Go to the documentation of this file.
1//===- SourceLocation.cpp - Compact identifier for Source Files -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines accessor methods for the FullSourceLoc class.
10//
11//===----------------------------------------------------------------------===//
12
14#include "clang/Basic/LLVM.h"
17#include "llvm/ADT/DenseMapInfo.h"
18#include "llvm/ADT/FoldingSet.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/Support/Compiler.h"
21#include "llvm/Support/MemoryBuffer.h"
22#include "llvm/Support/raw_ostream.h"
23#include <cassert>
24#include <string>
25#include <utility>
26
27using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// PrettyStackTraceLoc
31//===----------------------------------------------------------------------===//
32
33void PrettyStackTraceLoc::print(raw_ostream &OS) const {
34 if (Loc.isValid()) {
35 Loc.print(OS, SM);
36 OS << ": ";
37 }
38 OS << Message << '\n';
39}
40
41//===----------------------------------------------------------------------===//
42// SourceLocation
43//===----------------------------------------------------------------------===//
44
45static_assert(std::is_trivially_destructible_v<SourceLocation>,
46 "SourceLocation must be trivially destructible because it is "
47 "used in unions");
48
49static_assert(std::is_trivially_destructible_v<SourceRange>,
50 "SourceRange must be trivially destructible because it is "
51 "used in unions");
52
54 return llvm::DenseMapInfo<UIntTy>::getHashValue(ID);
55}
56
58 const SourceLocation &X, llvm::FoldingSetNodeID &ID) {
59 ID.AddInteger(X.ID);
60}
61
62void SourceLocation::print(raw_ostream &OS, const SourceManager &SM)const{
63 if (!isValid()) {
64 OS << "<invalid loc>";
65 return;
66 }
67
68 if (isFileID()) {
69 PresumedLoc PLoc = SM.getPresumedLoc(*this);
70
71 if (PLoc.isInvalid()) {
72 OS << "<invalid>";
73 return;
74 }
75 // The macro expansion and spelling pos is identical for file locs.
76 OS << PLoc.getFilename() << ':' << PLoc.getLine()
77 << ':' << PLoc.getColumn();
78 return;
79 }
80
81 SM.getExpansionLoc(*this).print(OS, SM);
82
83 OS << " <Spelling=";
84 SM.getSpellingLoc(*this).print(OS, SM);
85 OS << '>';
86}
87
88LLVM_DUMP_METHOD std::string
90 std::string S;
91 llvm::raw_string_ostream OS(S);
92 print(OS, SM);
93 return S;
94}
95
96LLVM_DUMP_METHOD void SourceLocation::dump(const SourceManager &SM) const {
97 print(llvm::errs(), SM);
98 llvm::errs() << '\n';
99}
100
101LLVM_DUMP_METHOD void SourceRange::dump(const SourceManager &SM) const {
102 print(llvm::errs(), SM);