clang  14.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 
18 #include "llvm/Support/VirtualFileSystem.h"
19 #include "llvm/Support/YAMLTraits.h"
20 
21 namespace clang {
22 namespace ento {
23 
24 /// Read the given file from the filesystem and parse it as a yaml file. The
25 /// template parameter must have a yaml MappingTraits.
26 /// Emit diagnostic error in case of any failure.
27 template <class T, class Checker>
29  StringRef Option, StringRef ConfigFile) {
30  if (ConfigFile.trim().empty())
31  return None;
32 
33  llvm::vfs::FileSystem *FS = llvm::vfs::getRealFileSystem().get();
34  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buffer =
35  FS->getBufferForFile(ConfigFile.str());
36 
37  if (std::error_code ec = Buffer.getError()) {
38  Mgr.reportInvalidCheckerOptionValue(Chk, Option,
39  "a valid filename instead of '" +
40  std::string(ConfigFile) + "'");
41  return None;
42  }
43 
44  llvm::yaml::Input Input(Buffer.get()->getBuffer());
45  T Config;
46  Input >> Config;
47 
48  if (std::error_code ec = Input.error()) {
49  Mgr.reportInvalidCheckerOptionValue(Chk, Option,
50  "a valid yaml file: " + ec.message());
51  return None;
52  }
53 
54  return Config;
55 }
56 
57 } // namespace ento
58 } // namespace clang
59 
60 #endif // LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_MOVE_H
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::ento::getConfiguration
llvm::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:28
llvm::Optional
Definition: LLVM.h:40
CheckerManager.h
clang::ento::CheckerManager::reportInvalidCheckerOptionValue
void reportInvalidCheckerOptionValue(const CheckerBase *C, StringRef OptionName, StringRef ExpectedValueDesc) const
Emits an error through a DiagnosticsEngine about an invalid user supplied checker option value.
Definition: CheckerManager.cpp:62
clang::ento::CheckerManager
Definition: CheckerManager.h:127
clang
Definition: CalledOnceCheck.h:17
clang::ento::Checker
Definition: Checker.h:517