• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tudasc / TypeART / 12949933829

24 Jan 2025 12:47PM UTC coverage: 89.797%. First build
12949933829

Pull #151

github

web-flow
Merge b586944fa into b4faedd01
Pull Request #151: Environment Variable Configuration

333 of 385 new or added lines in 9 files covered. (86.49%)

3934 of 4381 relevant lines covered (89.8%)

112238.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/lib/passes/TypeARTConfiguration.cpp
1
// TypeART library
2
//
3
// Copyright (c) 2017-2025 TypeART Authors
4
// Distributed under the BSD 3-Clause license.
5
// (See accompanying file LICENSE.txt or copy at
6
// https://opensource.org/licenses/BSD-3-Clause)
7
//
8
// Project home: https://github.com/tudasc/TypeART
9
//
10
// SPDX-License-Identifier: BSD-3-Clause
11
//
12

13
#include "TypeARTConfiguration.h"
14

15
#include "Commandline.h"
16
#include "configuration/Configuration.h"
17
#include "configuration/EnvironmentConfiguration.h"
18
#include "configuration/FileConfiguration.h"
19
#include "configuration/TypeARTOptions.h"
20
#include "support/Logger.h"
21

22
#include <string>
23
#include <string_view>
24

25
namespace typeart::config {
26

27
TypeARTConfiguration::TypeARTConfiguration(std::unique_ptr<file::FileOptions> config_options,
3,260✔
28
                                           std::unique_ptr<cl::CommandLineOptions> commandline_options,
29
                                           std::unique_ptr<env::EnvironmentFlagsOptions> env_options)
30
    : configuration_options_(std::move(config_options)),
1,630✔
31
      commandline_options_(std::move(commandline_options)),
1,630✔
32
      env_options_(std::move(env_options)) {
4,890✔
33
}
1,630✔
34

35
std::optional<OptionValue> TypeARTConfiguration::getValue(std::string_view opt_path) const {
×
NEW
36
  auto get_value = [&](const auto& options, const char* source) -> std::optional<OptionValue> {
×
37
    LOG_DEBUG("Query " << source << " " << opt_path.data())
NEW
38
    if (prioritize_commandline && options.valueSpecified(opt_path)) {
×
39
      LOG_DEBUG("Take " << source << " arg for " << opt_path.data());
40
      return options.getValue(opt_path);
1,630✔
41
    }
NEW
42
    return std::nullopt;
×
NEW
43
  };
×
NEW
44
  if (auto value = get_value(*env_options_, "ENV")) {
×
NEW
45
    return value;
×
46
  }
NEW
47
  if (auto value = get_value(*commandline_options_, "CL")) {
×
NEW
48
    return value;
×
49
  }
50
  return configuration_options_->getValue(opt_path);
×
51
}
×
52

53
OptionValue TypeARTConfiguration::getValueOr(std::string_view opt_path, OptionValue alt) const {
3,260✔
54
  auto get_value = [&](const auto& options, const char* source) -> std::optional<OptionValue> {
9,040✔
55
    LOG_DEBUG("Query " << source << " " << opt_path.data())
56
    if (prioritize_commandline && options.valueSpecified(opt_path)) {
5,780✔
57
      LOG_DEBUG("Take " << source << " arg for " << opt_path.data());
58
      return options.getValueOr(opt_path, alt);
1,892✔
59
    }
60
    return std::nullopt;
3,888✔
61
  };
5,780✔
62
  if (auto value = get_value(*env_options_, "ENV")) {
4,000✔
63
    return value.value();
740✔
64
  }
65
  if (auto value = get_value(*commandline_options_, "CL")) {
3,672✔
66
    return value.value();
1,152✔
67
  }
68
  return configuration_options_->getValueOr(opt_path, alt);
1,368✔
69
}
3,260✔
70

71
OptionValue TypeARTConfiguration::operator[](std::string_view opt_path) const {
92,004✔
72
  auto get_value = [&](const auto& options, const char* source) -> std::optional<OptionValue> {
273,792✔
73
    LOG_DEBUG("Query " << source << " " << opt_path.data())
74
    if (prioritize_commandline && options.valueSpecified(opt_path)) {
181,788✔
75
      LOG_DEBUG("Take " << source << " arg for " << opt_path.data());
76
      return options.operator[](opt_path);
39,612✔
77
    }
78
    return std::nullopt;
142,176✔
79
  };
181,788✔
80
  if (auto value = get_value(*env_options_, "ENV")) {
94,224✔
81
    return value.value();
2,220✔
82
  }
83
  if (auto value = get_value(*commandline_options_, "CL")) {
127,176✔
84
    return value.value();
37,392✔
85
  }
86
  auto result = configuration_options_->operator[](opt_path);
52,392✔
87
  LOG_DEBUG("Query file " << static_cast<std::string>(result))
88
  return result;
52,392✔
89
}
92,004✔
90

91
void TypeARTConfiguration::prioritizeCommandline(bool do_prioritize) {
1,630✔
92
  prioritize_commandline = do_prioritize;
1,630✔
93
}
1,630✔
94

95
void TypeARTConfiguration::emitTypeartFileConfiguration(llvm::raw_ostream& out_stream) {
×
96
  out_stream << configuration_options_->getConfigurationAsString();
×
97
}
×
98

99
template <typename ClOpt>
100
std::pair<llvm::StringRef, typename OptionsMap::mapped_type> make_occurr_entry(std::string&& key, ClOpt&& cl_opt) {
101
  return {key, (cl_opt.getNumOccurrences() > 0)};
102
}
103

104
TypeARTConfigOptions TypeARTConfiguration::getOptions() const {
1,630✔
105
  return helper::config_to_options(*this);
1,630✔
106
}
107

108
inline llvm::ErrorOr<std::unique_ptr<TypeARTConfiguration>> make_config(
1,636✔
109
    llvm::ErrorOr<std::unique_ptr<file::FileOptions>> file_opts) {
110
  if (file_opts) {
1,636✔
111
    auto cl_opts  = std::make_unique<config::cl::CommandLineOptions>();
1,630✔
112
    auto env_opts = std::make_unique<config::env::EnvironmentFlagsOptions>();
1,630✔
113
    auto config   = std::make_unique<config::TypeARTConfiguration>(std::move(file_opts.get()), std::move(cl_opts),
3,260✔
114
                                                                 std::move(env_opts));
1,630✔
115
    config->prioritizeCommandline(true);
1,630✔
116
    return config;
1,630✔
117
  }
1,630✔
118
  LOG_FATAL("Could not initialize file configuration. Reason: " << file_opts.getError().message())
6✔
119
  return file_opts.getError();
6✔
120
}
1,636✔
121

122
llvm::ErrorOr<std::unique_ptr<TypeARTConfiguration>> make_typeart_configuration(const TypeARTConfigInit& init) {
1,636✔
123
  auto file_opts = init.mode != TypeARTConfigInit::FileConfigurationMode::Empty
1,636✔
124
                       ? config::file::make_file_configuration(init.file_path)
36✔
125
                       : config::file::make_default_file_configuration();
1,600✔
126
  return make_config(std::move(file_opts));
1,636✔
127
}
1,636✔
128

NEW
129
llvm::ErrorOr<std::unique_ptr<TypeARTConfiguration>> make_typeart_configuration_from_opts(
×
130
    const TypeARTConfigOptions& opts) {
NEW
131
  auto file_opts = config::file::make_from_configuration(opts);
×
NEW
132
  return make_config(std::move(file_opts));
×
NEW
133
}
×
134

135
}  // namespace typeart::config
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc