clang: lib/Basic/SourceMgrAdapter.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
16
17using namespace clang;
18
19void SourceMgrAdapter::handleDiag(const llvm::SMDiagnostic &Diag,
20 void *Context) {
22}
23
26 unsigned ErrorDiagID, unsigned WarningDiagID,
27 unsigned NoteDiagID,
29 : SrcMgr(SM), Diagnostics(Diagnostics), ErrorDiagID(ErrorDiagID),
30 WarningDiagID(WarningDiagID), NoteDiagID(NoteDiagID),
31 DefaultFile(DefaultFile) {}
32
34
36 llvm::SMLoc Loc) {
37
38 if (!Loc.isValid())
40
41
42 unsigned BufferID = LLVMSrcMgr.FindBufferContainingLoc(Loc);
43 if (!BufferID)
45
46
47 auto Buffer = LLVMSrcMgr.getMemoryBuffer(BufferID);
48 auto KnownBuffer = FileIDMapping.find(std::make_pair(&LLVMSrcMgr, BufferID));
49 if (KnownBuffer == FileIDMapping.end()) {
51 if (DefaultFile) {
52
54
55
56 DefaultFile = std::nullopt;
57 } else {
58
59 StringRef bufferName = Buffer->getBufferIdentifier();
60 auto bufferCopy = std::unique_ptrllvm::MemoryBuffer(
61 llvm::MemoryBuffer::getMemBufferCopy(Buffer->getBuffer(),
62 bufferName));
63
64
65 FileID = SrcMgr.createFileID(std::move(bufferCopy));
66 }
67
68
69 KnownBuffer = FileIDMapping
70 .insert(std::make_pair(
71 std::make_pair(&LLVMSrcMgr, BufferID), FileID))
72 .first;
73 }
74
75
76 unsigned Offset = Loc.getPointer() - Buffer->getBufferStart();
77 return SrcMgr.getLocForStartOfFile(KnownBuffer->second)
78 .getLocWithOffset(Offset);
79}
80
82 llvm::SMRange Range) {
83 if (!Range.isValid())
85
89}
90
91void SourceMgrAdapter::handleDiag(const llvm::SMDiagnostic &Diag) {
92
94 if (auto *LLVMSrcMgr = Diag.getSourceMgr())
96
97
98 StringRef Message = Diag.getMessage();
99
100
101 unsigned DiagID;
102 switch (Diag.getKind()) {
103 case llvm::SourceMgr::DK_Error:
104 DiagID = ErrorDiagID;
105 break;
106
107 case llvm::SourceMgr::DK_Warning:
108 DiagID = WarningDiagID;
109 break;
110
111 case llvm::SourceMgr::DK_Remark:
112 llvm_unreachable("remarks not implemented");
113
114 case llvm::SourceMgr::DK_Note:
115 DiagID = NoteDiagID;
116 break;
117 }
118
119
120 DiagnosticBuilder Builder = Diagnostics.Report(Loc, DiagID) << Message;
121
122 if (auto *LLVMSrcMgr = Diag.getSourceMgr()) {
123
125 for (auto Range : Diag.getRanges()) {
128 }
129
130
131 for (const llvm::SMFixIt &FixIt : Diag.getFixIts()) {
134 }
135 }
136}
Defines the Diagnostic-related interfaces.
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
Represents a character-granular source range.
A little helper class used to produce diagnostics.
Concrete class used by the front-end to report problems and issues.
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
static FixItHint CreateReplacement(CharSourceRange RemoveRange, StringRef Code)
Create a code modification hint that replaces the given source range with the given code string.
Encodes a location in the source.
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
This class handles loading and caching of source files into memory.
SourceLocation mapLocation(const llvm::SourceMgr &LLVMSrcMgr, llvm::SMLoc Loc)
Map a source location in the given LLVM source manager to its corresponding location in the Clang sou...
Definition SourceMgrAdapter.cpp:35
SourceRange mapRange(const llvm::SourceMgr &LLVMSrcMgr, llvm::SMRange Range)
Map a source range in the given LLVM source manager to its corresponding range in the Clang source ma...
Definition SourceMgrAdapter.cpp:81
SourceMgrAdapter(SourceManager &SM, DiagnosticsEngine &Diagnostics, unsigned ErrorDiagID, unsigned WarningDiagID, unsigned NoteDiagID, OptionalFileEntryRef DefaultFile=std::nullopt)
Create a new SourceMgr adaptor that maps to the given source manager and diagnostics engine.
Definition SourceMgrAdapter.cpp:24
~SourceMgrAdapter()
Definition SourceMgrAdapter.cpp:33
A trivial tuple used to represent a source range.
The JSON file list parser is used to communicate input to InstallAPI.
CustomizableOptional< FileEntryRef > OptionalFileEntryRef