/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Checkers/Yaml.h
Line | Count | Source |
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-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file defines convenience functions for handling YAML configuration files |
10 | | // for checkers/packages. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #ifndef LLVM_CLANG_LIB_STATICANALYZER_CHECKER_YAML_H |
15 | | #define LLVM_CLANG_LIB_STATICANALYZER_CHECKER_YAML_H |
16 | | |
17 | | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
18 | | #include "llvm/Support/VirtualFileSystem.h" |
19 | | #include "llvm/Support/YAMLTraits.h" |
20 | | #include <optional> |
21 | | |
22 | | namespace clang { |
23 | | namespace ento { |
24 | | |
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 | 26 | StringRef Option, StringRef ConfigFile) { |
31 | 26 | if (ConfigFile.trim().empty()) |
32 | 20 | return std::nullopt; |
33 | | |
34 | 6 | llvm::vfs::FileSystem *FS = llvm::vfs::getRealFileSystem().get(); |
35 | 6 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buffer = |
36 | 6 | FS->getBufferForFile(ConfigFile.str()); |
37 | | |
38 | 6 | if (Buffer.getError()) { |
39 | 1 | Mgr.reportInvalidCheckerOptionValue(Chk, Option, |
40 | 1 | "a valid filename instead of '" + |
41 | 1 | std::string(ConfigFile) + "'"); |
42 | 1 | return std::nullopt; |
43 | 1 | } |
44 | | |
45 | 5 | llvm::yaml::Input Input(Buffer.get()->getBuffer()); |
46 | 5 | T Config; |
47 | 5 | Input >> Config; |
48 | | |
49 | 5 | if (std::error_code ec = Input.error()) { |
50 | 1 | Mgr.reportInvalidCheckerOptionValue(Chk, Option, |
51 | 1 | "a valid yaml file: " + ec.message()); |
52 | 1 | return std::nullopt; |
53 | 1 | } |
54 | | |
55 | 4 | return Config; |
56 | 5 | } |
57 | | |
58 | | } // namespace ento |
59 | | } // namespace clang |
60 | | |
61 | | #endif // LLVM_CLANG_LIB_STATICANALYZER_CHECKER_YAML_H |