clang 22.0.0git
Yaml.h
Go to the documentation of this file.
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
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. The
27/// 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()
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 ento
63} // namespace clang
64
65#endif // LLVM_CLANG_LIB_STATICANALYZER_CHECKER_YAML_H
Defines the SourceManager interface.
SourceManager & getSourceManager()
Definition ASTContext.h:798
llvm::vfs::FileSystem & getVirtualFileSystem() const
FileManager & getFileManager() const
ASTContext & getASTContext() const
void reportInvalidCheckerOptionValue(const CheckerFrontend *Checker, StringRef OptionName, StringRef ExpectedValueDesc) const
Emits an error through a DiagnosticsEngine about an invalid user supplied checker option value.
Simple checker classes that implement one frontend (i.e.
Definition Checker.h:553
std::optional< T > getConfiguration(CheckerManager &Mgr, Checker *Chk, StringRef Option, StringRef ConfigFile)
Read the given file from the filesystem and parse it as a yaml file.
Definition Yaml.h:30
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T