xref: /freebsd/contrib/llvm-project/clang/include/clang/Basic/SourceMgrAdapter.h (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
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 
17 #include "clang/Basic/SourceManager.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/Support/SourceMgr.h"
20 #include <string>
21 #include <utility>
22 
23 namespace clang {
24 
25 class DiagnosticsEngine;
26 class FileEntry;
27 
28 /// An adapter that can be used to translate diagnostics from one or more
29 /// llvm::SourceMgr instances to a ,
30 class SourceMgrAdapter {
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 
51 public:
52   /// Create a new \c SourceMgr adaptor that maps to the given source
53   /// manager and diagnostics engine.
54   SourceMgrAdapter(SourceManager &SM, DiagnosticsEngine &Diagnostics,
55                    unsigned ErrorDiagID, unsigned WarningDiagID,
56                    unsigned NoteDiagID,
57                    OptionalFileEntryRef DefaultFile = std::nullopt);
58 
59   ~SourceMgrAdapter();
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
86