1 //== Yaml.h ---------------------------------------------------- -*- C++ -*--=//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines convenience functions for handling YAML configuration files
10 // for checkers/packages.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LIB_STATICANALYZER_CHECKER_YAML_H
15 #define LLVM_CLANG_LIB_STATICANALYZER_CHECKER_YAML_H
17 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
18 #include "llvm/Support/VirtualFileSystem.h"
19 #include "llvm/Support/YAMLTraits.h"
25 /// Read the given file from the filesystem and parse it as a yaml file. The
26 /// template parameter must have a yaml MappingTraits.
27 /// Emit diagnostic error in case of any failure.
28 template <class T
, class Checker
>
29 std::optional
<T
> getConfiguration(CheckerManager
&Mgr
, Checker
*Chk
,
30 StringRef Option
, StringRef ConfigFile
) {
31 if (ConfigFile
.trim().empty())
34 llvm::vfs::FileSystem
*FS
= llvm::vfs::getRealFileSystem().get();
35 llvm::ErrorOr
<std::unique_ptr
<llvm::MemoryBuffer
>> Buffer
=
36 FS
->getBufferForFile(ConfigFile
.str());
38 if (Buffer
.getError()) {
39 Mgr
.reportInvalidCheckerOptionValue(Chk
, Option
,
40 "a valid filename instead of '" +
41 std::string(ConfigFile
) + "'");
45 llvm::yaml::Input
Input(Buffer
.get()->getBuffer());
49 if (std::error_code ec
= Input
.error()) {
50 Mgr
.reportInvalidCheckerOptionValue(Chk
, Option
,
51 "a valid yaml file: " + ec
.message());
61 #endif // LLVM_CLANG_LIB_STATICANALYZER_CHECKER_YAML_H