1353942Sdim//== Yaml.h ---------------------------------------------------- -*- C++ -*--=// 2353942Sdim// 3353942Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353942Sdim// See https://llvm.org/LICENSE.txt for license information. 5353942Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6353942Sdim// 7353942Sdim//===----------------------------------------------------------------------===// 8353942Sdim// 9353942Sdim// This file defines convenience functions for handling YAML configuration files 10353942Sdim// for checkers/packages. 11353942Sdim// 12353942Sdim//===----------------------------------------------------------------------===// 13353942Sdim 14353942Sdim#ifndef LLVM_CLANG_LIB_STATICANALYZER_CHECKER_YAML_H 15353942Sdim#define LLVM_CLANG_LIB_STATICANALYZER_CHECKER_YAML_H 16353942Sdim 17353942Sdim#include "clang/StaticAnalyzer/Core/CheckerManager.h" 18353942Sdim#include "llvm/Support/YAMLTraits.h" 19353942Sdim 20353942Sdimnamespace clang { 21353942Sdimnamespace ento { 22353942Sdim 23353942Sdim/// Read the given file from the filesystem and parse it as a yaml file. The 24353942Sdim/// template parameter must have a yaml MappingTraits. 25353942Sdim/// Emit diagnostic error in case of any failure. 26353942Sdimtemplate <class T, class Checker> 27353942Sdimllvm::Optional<T> getConfiguration(CheckerManager &Mgr, Checker *Chk, 28353942Sdim StringRef Option, StringRef ConfigFile) { 29353942Sdim if (ConfigFile.trim().empty()) 30353942Sdim return None; 31353942Sdim 32353942Sdim llvm::vfs::FileSystem *FS = llvm::vfs::getRealFileSystem().get(); 33353942Sdim llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buffer = 34353942Sdim FS->getBufferForFile(ConfigFile.str()); 35353942Sdim 36353942Sdim if (std::error_code ec = Buffer.getError()) { 37353942Sdim Mgr.reportInvalidCheckerOptionValue(Chk, Option, 38353942Sdim "a valid filename instead of '" + 39353942Sdim std::string(ConfigFile) + "'"); 40353942Sdim return None; 41353942Sdim } 42353942Sdim 43353942Sdim llvm::yaml::Input Input(Buffer.get()->getBuffer()); 44353942Sdim T Config; 45353942Sdim Input >> Config; 46353942Sdim 47353942Sdim if (std::error_code ec = Input.error()) { 48353942Sdim Mgr.reportInvalidCheckerOptionValue(Chk, Option, 49353942Sdim "a valid yaml file: " + ec.message()); 50353942Sdim return None; 51353942Sdim } 52353942Sdim 53353942Sdim return Config; 54353942Sdim} 55353942Sdim 56353942Sdim} // namespace ento 57353942Sdim} // namespace clang 58353942Sdim 59353942Sdim#endif // LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_MOVE_H 60