• 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

92.66
/lib/passes/configuration/EnvironmentConfiguration.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 "EnvironmentConfiguration.h"
14

15
#include "Configuration.h"
16
#include "analysis/MemInstFinder.h"
17
#include "support/ConfigurationBase.h"
18
#include "support/Logger.h"
19
#include "support/Util.h"
20
#include "typegen/TypeGenerator.h"
21

22
#include "llvm/ADT/StringSwitch.h"
23

24
#include <charconv>
25
#include <string>
26
#include <string_view>
27
#include <type_traits>
28

29
using namespace llvm;
30

31
namespace typeart::config::env {
32
std::optional<std::string> get_env_flag(std::string_view flag) {
53,778✔
33
  const char* env_value = std::getenv(flag.data());
53,778✔
34
  const bool exists     = env_value != nullptr;
53,778✔
35
  if (exists) {
53,778✔
36
    LOG_DEBUG("Using env var " << flag << "=" << env_value)
37
    return std::string{env_value};
2,220✔
38
  }
39
  LOG_DEBUG("Not using env var " << flag << "=<unset>")
40
  return {};
51,558✔
41
}
53,778✔
42

43
}  // namespace typeart::config::env
44

45
namespace typeart::config::env {
46

47
namespace detail {
48
template <typename... Strings>
49
bool with_any_of(std::string_view lhs, Strings&&... rhs) {
32,600✔
50
  return !lhs.empty() && ((lhs == rhs) || ...);
65,200✔
51
}
52

53
template <typename ClType>
54
ClType string_to_enum(std::string_view cl_value) {
3,260✔
55
  using ::typeart::TypegenImplementation;
56
  using ::typeart::analysis::FilterImplementation;
57
  if constexpr (std::is_same_v<TypegenImplementation, ClType>) {
58
    auto val = llvm::StringSwitch<ClType>(cl_value.data())
3,260✔
59
                   .Case("ir", TypegenImplementation::IR)
1,630✔
60
                   .Case("dimeta", TypegenImplementation::DIMETA)
1,630✔
61
                   .Default(TypegenImplementation::DIMETA);
1,630✔
62
    return val;
1,630✔
63
  } else {
64
    auto val = llvm::StringSwitch<ClType>(cl_value.data())
3,260✔
65
                   .Case("cg", FilterImplementation::cg)
1,630✔
66
                   .Case("none", FilterImplementation::none)
1,630✔
67
                   .Case("std", FilterImplementation::standard)
1,630✔
68
                   .Default(FilterImplementation::standard);
1,630✔
69
    return val;
1,630✔
70
  }
71
}
72

73
template <typename ClType>
74
config::OptionValue make_opt(std::string_view cl_value) {
26,080✔
75
  LOG_DEBUG("Parsing value " << cl_value)
76
  if constexpr (std::is_same_v<bool, ClType>) {
77
    const bool is_true_val  = with_any_of(cl_value, "true", "TRUE", "1");
16,300✔
78
    const bool is_false_val = with_any_of(cl_value, "false", "FALSE", "0");
16,300✔
79
    if (!(is_true_val || is_false_val)) {
16,300✔
NEW
80
      LOG_WARNING("Illegal bool value")
×
NEW
81
    }
×
82
    assert((is_true_val || is_false_val) && "Illegal bool value for environment flag");
32,600✔
83
    return config::OptionValue{is_true_val};
16,300✔
84
  } else {
85
    if constexpr (std::is_enum_v<ClType>) {
86
      auto enum_value = string_to_enum<ClType>(cl_value);
3,260✔
87
      return config::OptionValue{static_cast<int>(enum_value)};
3,260✔
88
    } else {
89
      return config::OptionValue{std::string{cl_value}};
6,520✔
90
    }
91
  }
NEW
92
}
×
93

94
template <typename ClType>
95
std::pair<StringRef, typename OptionsMap::mapped_type> make_entry(std::string&& key, std::string_view cl_opt,
26,080✔
96
                                                                  const std::string& default_value) {
97
  const auto env_value = get_env_flag(cl_opt);
26,080✔
98
  return {key, make_opt<ClType>(env_value.value_or(default_value))};
26,080✔
99
}
26,080✔
100

101
template <typename ClOpt>
102
std::pair<StringRef, typename OptOccurrenceMap::mapped_type> make_occurr_entry(std::string&& key, ClOpt&& cl_opt) {
26,080✔
103
  const bool occurred = (get_env_flag(cl_opt).has_value());
26,080✔
104
  // LOG_DEBUG("Key :" << key << ":" << occurred)
105
  return {key, occurred};
26,080✔
106
}
107
}  // namespace detail
108

109
EnvironmentFlagsOptions::EnvironmentFlagsOptions() {
1,630✔
110
  using namespace config;
111
  using namespace typeart::config::env::detail;
112

113
  LOG_DEBUG("Construct environment flag options")
114

115
  mapping_ = {
27,710✔
116
      make_entry<std::string>(ConfigStdArgs::types, "TYPEART_TYPE_FILE", ConfigStdArgValues::types),
1,630✔
117
      make_entry<ConfigStdArgTypes::stats_ty>(ConfigStdArgs::stats, EnvironmentStdArgs::stats,
1,630✔
118
                                              EnvironmentStdArgsValues::stats),
1,630✔
119
      make_entry<ConfigStdArgTypes::heap_ty>(ConfigStdArgs::heap, EnvironmentStdArgs::heap,
1,630✔
120
                                             EnvironmentStdArgsValues::heap),
1,630✔
121
      make_entry<ConfigStdArgTypes::global_ty>(ConfigStdArgs::global, EnvironmentStdArgs::global,
1,630✔
122
                                               EnvironmentStdArgsValues::global),
1,630✔
123
      make_entry<ConfigStdArgTypes::stack_ty>(ConfigStdArgs::stack, EnvironmentStdArgs::stack,
1,630✔
124
                                              EnvironmentStdArgsValues::stack),
1,630✔
125
      make_entry<ConfigStdArgTypes::stack_lifetime_ty>(
1,630✔
126
          ConfigStdArgs::stack_lifetime, EnvironmentStdArgs::stack_lifetime, EnvironmentStdArgsValues::stack_lifetime),
1,630✔
127
      make_entry<typeart::TypegenImplementation>(ConfigStdArgs::typegen, EnvironmentStdArgs::typegen,
1,630✔
128
                                                 ConfigStdArgValues::typegen),
1,630✔
129
      make_entry<ConfigStdArgTypes::filter_ty>(ConfigStdArgs::filter, EnvironmentStdArgs::filter,
1,630✔
130
                                               EnvironmentStdArgsValues::filter),
1,630✔
131
      make_entry<typeart::analysis::FilterImplementation>(ConfigStdArgs::filter_impl, EnvironmentStdArgs::filter_impl,
1,630✔
132
                                                          ConfigStdArgValues::filter_impl),
1,630✔
133

134
      make_entry<ConfigStdArgTypes::filter_glob_ty>(ConfigStdArgs::filter_glob, EnvironmentStdArgs::filter_glob,
1,630✔
135
                                                    ConfigStdArgValues::filter_glob),
1,630✔
136
      make_entry<ConfigStdArgTypes::filter_glob_deep_ty>(
1,630✔
137
          ConfigStdArgs::filter_glob_deep, EnvironmentStdArgs::filter_glob_deep, ConfigStdArgValues::filter_glob_deep),
1,630✔
138
      make_entry<ConfigStdArgTypes::filter_cg_file_ty>(
1,630✔
139
          ConfigStdArgs::filter_cg_file, EnvironmentStdArgs::filter_cg_file, ConfigStdArgValues::filter_cg_file),
1,630✔
140
      make_entry<ConfigStdArgTypes::analysis_filter_global_ty>(ConfigStdArgs::analysis_filter_global,
3,260✔
141
                                                               EnvironmentStdArgs::analysis_filter_global,
1,630✔
142
                                                               EnvironmentStdArgsValues::analysis_filter_global),
1,630✔
143
      make_entry<ConfigStdArgTypes::analysis_filter_heap_alloc_ty>(
1,630✔
144
          ConfigStdArgs::analysis_filter_heap_alloc, EnvironmentStdArgs::analysis_filter_heap_alloc,
1,630✔
145
          EnvironmentStdArgsValues::analysis_filter_heap_alloc),
1,630✔
146
      make_entry<ConfigStdArgTypes::analysis_filter_pointer_alloc_ty>(
1,630✔
147
          ConfigStdArgs::analysis_filter_pointer_alloc, EnvironmentStdArgs::analysis_filter_pointer_alloc,
1,630✔
148
          EnvironmentStdArgsValues::analysis_filter_pointer_alloc),
1,630✔
149
      make_entry<ConfigStdArgTypes::analysis_filter_alloca_non_array_ty>(
1,630✔
150
          ConfigStdArgs::analysis_filter_alloca_non_array, EnvironmentStdArgs::analysis_filter_alloca_non_array,
1,630✔
151
          EnvironmentStdArgsValues::analysis_filter_alloca_non_array),
1,630✔
152
  };
153

154
  occurence_mapping_ = {
27,710✔
155
      make_occurr_entry(ConfigStdArgs::types, "TYPEART_TYPE_FILE"),
1,630✔
156
      make_occurr_entry(ConfigStdArgs::stats, EnvironmentStdArgs::stats),
1,630✔
157
      make_occurr_entry(ConfigStdArgs::heap, EnvironmentStdArgs::heap),
1,630✔
158
      make_occurr_entry(ConfigStdArgs::global, EnvironmentStdArgs::global),
1,630✔
159
      make_occurr_entry(ConfigStdArgs::stack, EnvironmentStdArgs::stack),
1,630✔
160
      make_occurr_entry(ConfigStdArgs::stack_lifetime, EnvironmentStdArgs::stack_lifetime),
1,630✔
161
      make_occurr_entry(ConfigStdArgs::typegen, EnvironmentStdArgs::typegen),
1,630✔
162
      make_occurr_entry(ConfigStdArgs::filter, EnvironmentStdArgs::filter),
1,630✔
163
      make_occurr_entry(ConfigStdArgs::filter_impl, EnvironmentStdArgs::filter_impl),
1,630✔
164
      make_occurr_entry(ConfigStdArgs::filter_glob, EnvironmentStdArgs::filter_glob),
1,630✔
165
      make_occurr_entry(ConfigStdArgs::filter_glob_deep, EnvironmentStdArgs::filter_glob_deep),
1,630✔
166
      make_occurr_entry(ConfigStdArgs::filter_cg_file, EnvironmentStdArgs::filter_cg_file),
1,630✔
167
      make_occurr_entry(ConfigStdArgs::analysis_filter_global, EnvironmentStdArgs::analysis_filter_global),
1,630✔
168
      make_occurr_entry(ConfigStdArgs::analysis_filter_heap_alloc, EnvironmentStdArgs::analysis_filter_heap_alloc),
1,630✔
169
      make_occurr_entry(ConfigStdArgs::analysis_filter_pointer_alloc,
1,630✔
170
                        EnvironmentStdArgs::analysis_filter_pointer_alloc),
171
      make_occurr_entry(ConfigStdArgs::analysis_filter_alloca_non_array,
1,630✔
172
                        EnvironmentStdArgs::analysis_filter_alloca_non_array),
173
  };
174

175
  if (!occurence_mapping_.lookup(ConfigStdArgs::global) && occurence_mapping_.lookup(ConfigStdArgs::stack)) {
1,630✔
NEW
176
    const auto stack_value                    = mapping_.lookup(ConfigStdArgs::stack);
×
NEW
177
    mapping_[ConfigStdArgs::global]           = OptionValue{static_cast<bool>(stack_value)};
×
NEW
178
    occurence_mapping_[ConfigStdArgs::global] = true;
×
NEW
179
  }
×
180
}
1,630✔
181

182
std::optional<typeart::config::OptionValue> EnvironmentFlagsOptions::getValue(std::string_view opt_path) const {
2,960✔
183
  auto key = llvm::StringRef(opt_path.data());
2,960✔
184
  if (mapping_.count(key) != 0U) {
2,960✔
185
    return mapping_.lookup(key);
2,960✔
186
  }
NEW
187
  return {};
×
188
}
2,960✔
189

190
[[maybe_unused]] bool EnvironmentFlagsOptions::valueSpecified(std::string_view opt_path) const {
95,264✔
191
  auto key = llvm::StringRef(opt_path.data());
95,264✔
192
  return occurence_mapping_.lookup(key);
95,264✔
193
}
194

195
}  // namespace typeart::config::env
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