clang 20.0.0git
SourceMgrAdapter.h
Go to the documentation of this file.
1//=== SourceMgrAdapter.h - SourceMgr to SourceManager Adapter ---*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file provides an adapter that maps diagnostics from llvm::SourceMgr
10// to Clang's SourceManager.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_SOURCEMGRADAPTER_H
15#define LLVM_CLANG_SOURCEMGRADAPTER_H
16
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/Support/SourceMgr.h"
20#include <string>
21#include <utility>
22
23namespace clang {
24
25class DiagnosticsEngine;
26class FileEntry;
27
28/// An adapter that can be used to translate diagnostics from one or more
29/// llvm::SourceMgr instances to a ,
31 /// Clang source manager.
32 SourceManager &SrcMgr;
33
34 /// Clang diagnostics engine.
35 DiagnosticsEngine &Diagnostics;
36
37 /// Diagnostic IDs for errors, warnings, and notes.
38 unsigned ErrorDiagID, WarningDiagID, NoteDiagID;
39
40 /// The default file to use when mapping buffers.
41 OptionalFileEntryRef DefaultFile;
42
43 /// A mapping from (LLVM source manager, buffer ID) pairs to the
44 /// corresponding file ID within the Clang source manager.
45 llvm::DenseMap<std::pair<const llvm::SourceMgr *, unsigned>, FileID>
46 FileIDMapping;
47
48 /// Diagnostic handler.
49 static void handleDiag(const llvm::SMDiagnostic &Diag, void *Context);
50
51public:
52 /// Create a new \c SourceMgr adaptor that maps to the given source
53 /// manager and diagnostics engine.
55 unsigned ErrorDiagID, unsigned WarningDiagID,
56 unsigned NoteDiagID,
57 OptionalFileEntryRef DefaultFile = std::nullopt);
58
60
61 /// Map a source location in the given LLVM source manager to its
62 /// corresponding location in the Clang source manager.
63 SourceLocation mapLocation(const llvm::SourceMgr &LLVMSrcMgr,
64 llvm::SMLoc Loc);
65
66 /// Map a source range in the given LLVM source manager to its corresponding
67 /// range in the Clang source manager.
68 SourceRange mapRange(const llvm::SourceMgr &LLVMSrcMgr, llvm::SMRange Range);
69
70 /// Handle the given diagnostic from an LLVM source manager.
71 void handleDiag(const llvm::SMDiagnostic &Diag);
72
73 /// Retrieve the diagnostic handler to use with the underlying SourceMgr.
74 llvm::SourceMgr::DiagHandlerTy getDiagHandler() {
75 return &SourceMgrAdapter::handleDiag;
76 }
77
78 /// Retrieve the context to use with the diagnostic handler produced by
79 /// \c getDiagHandler().
80 void *getDiagContext() { return this; }
81};
82
83} // end namespace clang
84
85#endif
#define SM(sm)
Definition: Cuda.cpp:84
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.
SourceRange Range
Definition: SemaObjC.cpp:758
SourceLocation Loc
Definition: SemaObjC.cpp:759
Defines the SourceManager interface.
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:231
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Encodes a location in the source.
This class handles loading and caching of source files into memory.
An adapter that can be used to translate diagnostics from one or more llvm::SourceMgr instances to a ...
llvm::SourceMgr::DiagHandlerTy getDiagHandler()
Retrieve the diagnostic handler to use with the underlying SourceMgr.
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...
void * getDiagContext()
Retrieve the context to use with the diagnostic handler produced by getDiagHandler().
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...
A trivial tuple used to represent a source range.
The JSON file list parser is used to communicate input to InstallAPI.