Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang-tools-extra/pseudo/lib/cli/CLI.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- CLI.cpp -  ----------------------------------------------*- 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
#include "clang-pseudo/cli/CLI.h"
10
#include "clang-pseudo/cxx/CXX.h"
11
#include "clang-pseudo/grammar/Grammar.h"
12
#include "llvm/Support/CommandLine.h"
13
#include "llvm/Support/ErrorOr.h"
14
#include "llvm/Support/MemoryBuffer.h"
15
16
static llvm::cl::opt<std::string> Grammar(
17
    "grammar",
18
    llvm::cl::desc(
19
        "Specify a BNF grammar file path, or a builtin language (cxx)."),
20
    llvm::cl::init("cxx"));
21
22
namespace clang {
23
namespace pseudo {
24
25
9.56k
const Language &getLanguageFromFlags() {
26
9.56k
  if (::Grammar == "cxx")
27
9.56k
    return cxx::getLanguage();
28
29
0
  static Language *Lang = []() {
30
    // Read from a bnf grammar file.
31
0
    llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> GrammarText =
32
0
        llvm::MemoryBuffer::getFile(::Grammar);
33
0
    if (std::error_code EC = GrammarText.getError()) {
34
0
      llvm::errs() << "Error: can't read grammar file '" << ::Grammar
35
0
                   << "': " << EC.message() << "\n";
36
0
      std::exit(1);
37
0
    }
38
0
    std::vector<std::string> Diags;
39
0
    auto G = Grammar::parseBNF(GrammarText->get()->getBuffer(), Diags);
40
0
    for (const auto &Diag : Diags)
41
0
      llvm::errs() << Diag << "\n";
42
0
    auto Table = LRTable::buildSLR(G);
43
0
    return new Language{
44
0
        std::move(G),
45
0
        std::move(Table),
46
0
        llvm::DenseMap<ExtensionID, RuleGuard>(),
47
0
        llvm::DenseMap<ExtensionID, RecoveryStrategy>(),
48
0
    };
49
0
  }();
50
0
  return *Lang;
51
9.56k
}
52
53
} // namespace pseudo
54
} // namespace clang