brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.2 KiB · ec521cb Raw
66 lines · c
1//== Yaml.h ---------------------------------------------------- -*- 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-exception6//7//===----------------------------------------------------------------------===//8//9// This file defines convenience functions for handling YAML configuration files10// for checkers/packages.11//12//===----------------------------------------------------------------------===//13 14#ifndef LLVM_CLANG_LIB_STATICANALYZER_CHECKER_YAML_H15#define LLVM_CLANG_LIB_STATICANALYZER_CHECKER_YAML_H16 17#include "clang/Basic/SourceManager.h"18#include "clang/StaticAnalyzer/Core/CheckerManager.h"19#include "llvm/Support/VirtualFileSystem.h"20#include "llvm/Support/YAMLTraits.h"21#include <optional>22 23namespace clang {24namespace ento {25 26/// Read the given file from the filesystem and parse it as a yaml file. The27/// template parameter must have a yaml MappingTraits.28/// Emit diagnostic error in case of any failure.29template <class T, class Checker>30std::optional<T> getConfiguration(CheckerManager &Mgr, Checker *Chk,31                                  StringRef Option, StringRef ConfigFile) {32  if (ConfigFile.trim().empty())33    return std::nullopt;34 35  auto &VFS = Mgr.getASTContext()36                  .getSourceManager()37                  .getFileManager()38                  .getVirtualFileSystem();39  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buffer =40      VFS.getBufferForFile(ConfigFile.str());41 42  if (Buffer.getError()) {43    Mgr.reportInvalidCheckerOptionValue(Chk, Option,44                                        "a valid filename instead of '" +45                                            std::string(ConfigFile) + "'");46    return std::nullopt;47  }48 49  llvm::yaml::Input Input(Buffer.get()->getBuffer());50  T Config;51  Input >> Config;52 53  if (std::error_code ec = Input.error()) {54    Mgr.reportInvalidCheckerOptionValue(Chk, Option,55                                        "a valid yaml file: " + ec.message());56    return std::nullopt;57  }58 59  return Config;60}61 62} // namespace ento63} // namespace clang64 65#endif // LLVM_CLANG_LIB_STATICANALYZER_CHECKER_YAML_H66