Coverage Report

Created: 2025-08-28 06:48

/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
103
void SimpleDiagHandler::installInto(hermes::SourceErrorManager &sm) {
15
103
  sm.setDiagHandler(handler, this);
16
103
}
17
18
420
void SimpleDiagHandler::handler(const llvh::SMDiagnostic &msg, void *ctx) {
19
420
  auto *mgr = static_cast<SimpleDiagHandler *>(ctx);
20
420
  if (msg.getKind() == llvh::SourceMgr::DK_Error) {
21
66
    if (!mgr->haveErrors()) {
22
33
      mgr->firstMessage_ = msg;
23
33
    }
24
66
  }
25
420
}
26
27
33
std::string SimpleDiagHandler::getErrorString() const {
28
33
  const auto &msg = getFirstMessage();
29
33
  return (Twine(msg.getLineNo()) + ":" + Twine(msg.getColumnNo() + 1) + ":" +
30
33
          msg.getMessage())
31
33
      .str();
32
33
}
33
34
SimpleDiagHandlerRAII::SimpleDiagHandlerRAII(
35
    SourceErrorManager &sourceErrorManager)
36
103
    : sourceErrorManager_(sourceErrorManager),
37
103
      oldHandler_(sourceErrorManager.getDiagHandler()),
38
103
      oldContext_(sourceErrorManager.getDiagContext()),
39
103
      oldErrorLimit_(sourceErrorManager.getErrorLimit()) {
40
103
  installInto(sourceErrorManager);
41
103
  sourceErrorManager.clearErrorLimitReached();
42
103
  sourceErrorManager.setErrorLimit(1);
43
103
}
44
45
103
SimpleDiagHandlerRAII::~SimpleDiagHandlerRAII() {
46
103
  sourceErrorManager_.clearErrorLimitReached();
47
103
  sourceErrorManager_.setErrorLimit(oldErrorLimit_);
48
103
  sourceErrorManager_.setDiagHandler(oldHandler_, oldContext_);
49
103
}
50
51
} // namespace hermes