Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:51

0001 //=== SourceMgrAdapter.h - SourceMgr to SourceManager Adapter ---*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file provides an adapter that maps diagnostics from llvm::SourceMgr
0010 // to Clang's SourceManager.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CLANG_SOURCEMGRADAPTER_H
0015 #define LLVM_CLANG_SOURCEMGRADAPTER_H
0016 
0017 #include "clang/Basic/SourceManager.h"
0018 #include "llvm/ADT/DenseMap.h"
0019 #include "llvm/Support/SourceMgr.h"
0020 #include <string>
0021 #include <utility>
0022 
0023 namespace clang {
0024 
0025 class DiagnosticsEngine;
0026 class FileEntry;
0027 
0028 /// An adapter that can be used to translate diagnostics from one or more
0029 /// llvm::SourceMgr instances to a ,
0030 class SourceMgrAdapter {
0031   /// Clang source manager.
0032   SourceManager &SrcMgr;
0033 
0034   /// Clang diagnostics engine.
0035   DiagnosticsEngine &Diagnostics;
0036 
0037   /// Diagnostic IDs for errors, warnings, and notes.
0038   unsigned ErrorDiagID, WarningDiagID, NoteDiagID;
0039 
0040   /// The default file to use when mapping buffers.
0041   OptionalFileEntryRef DefaultFile;
0042 
0043   /// A mapping from (LLVM source manager, buffer ID) pairs to the
0044   /// corresponding file ID within the Clang source manager.
0045   llvm::DenseMap<std::pair<const llvm::SourceMgr *, unsigned>, FileID>
0046       FileIDMapping;
0047 
0048   /// Diagnostic handler.
0049   static void handleDiag(const llvm::SMDiagnostic &Diag, void *Context);
0050 
0051 public:
0052   /// Create a new \c SourceMgr adaptor that maps to the given source
0053   /// manager and diagnostics engine.
0054   SourceMgrAdapter(SourceManager &SM, DiagnosticsEngine &Diagnostics,
0055                    unsigned ErrorDiagID, unsigned WarningDiagID,
0056                    unsigned NoteDiagID,
0057                    OptionalFileEntryRef DefaultFile = std::nullopt);
0058 
0059   ~SourceMgrAdapter();
0060 
0061   /// Map a source location in the given LLVM source manager to its
0062   /// corresponding location in the Clang source manager.
0063   SourceLocation mapLocation(const llvm::SourceMgr &LLVMSrcMgr,
0064                              llvm::SMLoc Loc);
0065 
0066   /// Map a source range in the given LLVM source manager to its corresponding
0067   /// range in the Clang source manager.
0068   SourceRange mapRange(const llvm::SourceMgr &LLVMSrcMgr, llvm::SMRange Range);
0069 
0070   /// Handle the given diagnostic from an LLVM source manager.
0071   void handleDiag(const llvm::SMDiagnostic &Diag);
0072 
0073   /// Retrieve the diagnostic handler to use with the underlying SourceMgr.
0074   llvm::SourceMgr::DiagHandlerTy getDiagHandler() {
0075     return &SourceMgrAdapter::handleDiag;
0076   }
0077 
0078   /// Retrieve the context to use with the diagnostic handler produced by
0079   /// \c getDiagHandler().
0080   void *getDiagContext() { return this; }
0081 };
0082 
0083 } // end namespace clang
0084 
0085 #endif