Coverage Report

Created: 2025-06-24 06:43

/src/hermes/lib/Support/SimpleDiagHandler.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) Meta Platforms, Inc. and affiliates.
3
 *
4
 * This source code is licensed under the MIT license found in the
5
 * LICENSE file in the root directory of this source tree.
6
 */
7
8
#include "hermes/Support/SimpleDiagHandler.h"
9
10
#include "llvh/ADT/Twine.h"
11
12
namespace hermes {
13
14
247
void SimpleDiagHandler::installInto(hermes::SourceErrorManager &sm) {
15
247
  sm.setDiagHandler(handler, this);
16
247
}
17
18
252
void SimpleDiagHandler::handler(const llvh::SMDiagnostic &msg, void *ctx) {
19
252
  auto *mgr = static_cast<SimpleDiagHandler *>(ctx);
20
252
  if (msg.getKind() == llvh::SourceMgr::DK_Error) {
21
26
    if (!mgr->haveErrors()) {
22
13
      mgr->firstMessage_ = msg;
23
13
    }
24
26
  }
25
252
}
26
27
13
std::string SimpleDiagHandler::getErrorString() const {
28
13
  const auto &msg = getFirstMessage();
29
13
  return (Twine(msg.getLineNo()) + ":" + Twine(msg.getColumnNo() + 1) + ":" +
30
13
          msg.getMessage())
31
13
      .str();
32
13
}
33
34
SimpleDiagHandlerRAII::SimpleDiagHandlerRAII(
35
    SourceErrorManager &sourceErrorManager)
36
247
    : sourceErrorManager_(sourceErrorManager),
37
247
      oldHandler_(sourceErrorManager.getDiagHandler()),
38
247
      oldContext_(sourceErrorManager.getDiagContext()),
39
247
      oldErrorLimit_(sourceErrorManager.getErrorLimit()) {
40
247
  installInto(sourceErrorManager);
41
247
  sourceErrorManager.clearErrorLimitReached();
42
247
  sourceErrorManager.setErrorLimit(1);
43
247
}
44
45
247
SimpleDiagHandlerRAII::~SimpleDiagHandlerRAII() {
46
247
  sourceErrorManager_.clearErrorLimitReached();
47
247
  sourceErrorManager_.setErrorLimit(oldErrorLimit_);
48
247
  sourceErrorManager_.setDiagHandler(oldHandler_, oldContext_);
49
247
}
50
51
} // namespace hermes