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

tudasc / TypeART / 12973752603

26 Jan 2025 10:22AM UTC coverage: 87.53%. First build
12973752603

Pull #152

github

web-flow
Merge 47346d838 into bddfb53e4
Pull Request #152: Pass configuration parser

90 of 215 new or added lines in 10 files covered. (41.86%)

3959 of 4523 relevant lines covered (87.53%)

113474.42 hits per line

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

86.0
/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 "OptionsUtil.h"
17
#include "PassConfiguration.h"
18
#include "configuration/TypeARTOptions.h"
19
#include "support/ConfigurationBase.h"
20
#include "support/Logger.h"
21
#include "support/Util.h"
22

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

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

30
using namespace llvm;
31

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

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

46
namespace typeart::config::env {
47

48
struct EnvironmentStdArgsValues final {
49
#define TYPEART_CONFIG_OPTION(name, path, type, def_value, description, upper_path) \
50
  static constexpr char name[] = #def_value;
51
#include "support/ConfigurationBaseOptions.h"
52
#undef TYPEART_CONFIG_OPTION
53
};
54

55
struct EnvironmentStdArgs final {
56
#define TYPEART_CONFIG_OPTION(name, path, type, def_value, description, upper_path) \
57
  static constexpr char name[] = "TYPEART_" upper_path;
58
#include "support/ConfigurationBaseOptions.h"
59
#undef TYPEART_CONFIG_OPTION
60
};
61

62
namespace detail {
63
template <typename ClType>
64
OptionValue make_opt(std::string_view cl_value) {
26,080✔
65
  LOG_DEBUG("Parsing value " << cl_value)
66
  auto value = util::make_opt<ClType>(cl_value.data());
26,080✔
67
  if constexpr (std::is_enum_v<ClType>) {
68
    return OptionValue{static_cast<int>(value)};
3,260✔
69
  } else {
70
    return OptionValue{value};
22,820✔
71
  }
72
}
6,520✔
73

74
template <typename ClType>
75
std::pair<StringRef, typename OptionsMap::mapped_type> make_entry(std::string_view key, std::string_view cl_opt,
26,080✔
76
                                                                  const std::string& default_value) {
77
  const auto env_value = get_env_flag(cl_opt);
26,080✔
78
  return {key, detail::make_opt<ClType>(env_value.value_or(default_value))};
26,080✔
79
}
26,080✔
80

81
template <typename ClOpt>
82
std::pair<StringRef, typename OptOccurrenceMap::mapped_type> make_occurr_entry(std::string_view key, ClOpt&& cl_opt) {
26,080✔
83
  const bool occurred = (get_env_flag(cl_opt).has_value());
26,080✔
84
  return {key, occurred};
26,080✔
85
}
86
}  // namespace detail
87

88
EnvironmentFlagsOptions::EnvironmentFlagsOptions() {
1,630✔
89
  using namespace config;
90
  using namespace typeart::config::env::detail;
91

92
  LOG_DEBUG("Construct environment flag options")
93

94
  mapping_ = {
27,710✔
95
      make_entry<std::string>(ConfigStdArgs::types, "TYPEART_TYPE_FILE", ConfigStdArgValues::types),
1,630✔
96
      make_entry<ConfigStdArgTypes::stats_ty>(ConfigStdArgs::stats, EnvironmentStdArgs::stats,
1,630✔
97
                                              EnvironmentStdArgsValues::stats),
1,630✔
98
      make_entry<ConfigStdArgTypes::heap_ty>(ConfigStdArgs::heap, EnvironmentStdArgs::heap,
1,630✔
99
                                             EnvironmentStdArgsValues::heap),
1,630✔
100
      make_entry<ConfigStdArgTypes::global_ty>(ConfigStdArgs::global, EnvironmentStdArgs::global,
1,630✔
101
                                               EnvironmentStdArgsValues::global),
1,630✔
102
      make_entry<ConfigStdArgTypes::stack_ty>(ConfigStdArgs::stack, EnvironmentStdArgs::stack,
1,630✔
103
                                              EnvironmentStdArgsValues::stack),
1,630✔
104
      make_entry<ConfigStdArgTypes::stack_lifetime_ty>(
1,630✔
105
          ConfigStdArgs::stack_lifetime, EnvironmentStdArgs::stack_lifetime, EnvironmentStdArgsValues::stack_lifetime),
1,630✔
106
      make_entry<typeart::TypegenImplementation>(ConfigStdArgs::typegen, EnvironmentStdArgs::typegen,
1,630✔
107
                                                 ConfigStdArgValues::typegen),
1,630✔
108
      make_entry<ConfigStdArgTypes::filter_ty>(ConfigStdArgs::filter, EnvironmentStdArgs::filter,
1,630✔
109
                                               EnvironmentStdArgsValues::filter),
1,630✔
110
      make_entry<typeart::analysis::FilterImplementation>(ConfigStdArgs::filter_impl, EnvironmentStdArgs::filter_impl,
1,630✔
111
                                                          ConfigStdArgValues::filter_impl),
1,630✔
112

113
      make_entry<ConfigStdArgTypes::filter_glob_ty>(ConfigStdArgs::filter_glob, EnvironmentStdArgs::filter_glob,
1,630✔
114
                                                    ConfigStdArgValues::filter_glob),
1,630✔
115
      make_entry<ConfigStdArgTypes::filter_glob_deep_ty>(
1,630✔
116
          ConfigStdArgs::filter_glob_deep, EnvironmentStdArgs::filter_glob_deep, ConfigStdArgValues::filter_glob_deep),
1,630✔
117
      make_entry<ConfigStdArgTypes::filter_cg_file_ty>(
1,630✔
118
          ConfigStdArgs::filter_cg_file, EnvironmentStdArgs::filter_cg_file, ConfigStdArgValues::filter_cg_file),
1,630✔
119
      make_entry<ConfigStdArgTypes::analysis_filter_global_ty>(ConfigStdArgs::analysis_filter_global,
1,630✔
120
                                                               EnvironmentStdArgs::analysis_filter_global,
1,630✔
121
                                                               EnvironmentStdArgsValues::analysis_filter_global),
1,630✔
122
      make_entry<ConfigStdArgTypes::analysis_filter_heap_alloc_ty>(
1,630✔
123
          ConfigStdArgs::analysis_filter_heap_alloc, EnvironmentStdArgs::analysis_filter_heap_alloc,
1,630✔
124
          EnvironmentStdArgsValues::analysis_filter_heap_alloc),
1,630✔
125
      make_entry<ConfigStdArgTypes::analysis_filter_pointer_alloc_ty>(
1,630✔
126
          ConfigStdArgs::analysis_filter_pointer_alloc, EnvironmentStdArgs::analysis_filter_pointer_alloc,
1,630✔
127
          EnvironmentStdArgsValues::analysis_filter_pointer_alloc),
1,630✔
128
      make_entry<ConfigStdArgTypes::analysis_filter_alloca_non_array_ty>(
1,630✔
129
          ConfigStdArgs::analysis_filter_alloca_non_array, EnvironmentStdArgs::analysis_filter_alloca_non_array,
1,630✔
130
          EnvironmentStdArgsValues::analysis_filter_alloca_non_array),
1,630✔
131
  };
132

133
  occurence_mapping_ = {
1,630✔
134
      make_occurr_entry(ConfigStdArgs::types, "TYPEART_TYPE_FILE"),
1,630✔
135
      make_occurr_entry(ConfigStdArgs::stats, EnvironmentStdArgs::stats),
1,630✔
136
      make_occurr_entry(ConfigStdArgs::heap, EnvironmentStdArgs::heap),
1,630✔
137
      make_occurr_entry(ConfigStdArgs::global, EnvironmentStdArgs::global),
1,630✔
138
      make_occurr_entry(ConfigStdArgs::stack, EnvironmentStdArgs::stack),
1,630✔
139
      make_occurr_entry(ConfigStdArgs::stack_lifetime, EnvironmentStdArgs::stack_lifetime),
1,630✔
140
      make_occurr_entry(ConfigStdArgs::typegen, EnvironmentStdArgs::typegen),
1,630✔
141
      make_occurr_entry(ConfigStdArgs::filter, EnvironmentStdArgs::filter),
1,630✔
142
      make_occurr_entry(ConfigStdArgs::filter_impl, EnvironmentStdArgs::filter_impl),
1,630✔
143
      make_occurr_entry(ConfigStdArgs::filter_glob, EnvironmentStdArgs::filter_glob),
1,630✔
144
      make_occurr_entry(ConfigStdArgs::filter_glob_deep, EnvironmentStdArgs::filter_glob_deep),
1,630✔
145
      make_occurr_entry(ConfigStdArgs::filter_cg_file, EnvironmentStdArgs::filter_cg_file),
1,630✔
146
      make_occurr_entry(ConfigStdArgs::analysis_filter_global, EnvironmentStdArgs::analysis_filter_global),
1,630✔
147
      make_occurr_entry(ConfigStdArgs::analysis_filter_heap_alloc, EnvironmentStdArgs::analysis_filter_heap_alloc),
1,630✔
148
      make_occurr_entry(ConfigStdArgs::analysis_filter_pointer_alloc,
1,630✔
149
                        EnvironmentStdArgs::analysis_filter_pointer_alloc),
150
      make_occurr_entry(ConfigStdArgs::analysis_filter_alloca_non_array,
1,630✔
151
                        EnvironmentStdArgs::analysis_filter_alloca_non_array),
152
  };
153

154
  if (!occurence_mapping_.lookup(ConfigStdArgs::global) && occurence_mapping_.lookup(ConfigStdArgs::stack)) {
1,630✔
155
    const auto stack_value                    = mapping_.lookup(ConfigStdArgs::stack);
×
156
    mapping_[ConfigStdArgs::global]           = OptionValue{static_cast<bool>(stack_value)};
×
157
    occurence_mapping_[ConfigStdArgs::global] = true;
×
158
  }
×
159

160
  auto result = pass::parse_typeart_config_with_occurrence(get_env_flag("TYPEART_OPTIONS").value_or(""));
1,630✔
161
  if (!result.first) {
1,630✔
NEW
162
    LOG_INFO("No parseable TYPEART_OPTIONS: " << result.first.takeError())
×
NEW
163
  } else {
×
164
    LOG_DEBUG("Parsed TYPEART_OPTIONS\n" << *result.first)
165
    const auto typeart_options = helper::options_to_map(result.first.get());
1,630✔
166
    for (const auto& entry : result.second) {
1,630✔
NEW
167
      const auto key = entry.getKey();
×
168
      // LOG_DEBUG("Looking at " << key << " " << entry.second << ":" << occurence_mapping_[key])
NEW
169
      if (entry.second && !occurence_mapping_[key]) {  // single ENV priority over TYPEART_OPTIONS
×
170
        LOG_DEBUG("Replacing " << key)
NEW
171
        mapping_[key]           = typeart_options.lookup(key);
×
NEW
172
        occurence_mapping_[key] = true;
×
NEW
173
      }
×
174
    }
175
  }
1,630✔
176
}
1,630✔
177

178
std::optional<typeart::config::OptionValue> EnvironmentFlagsOptions::getValue(std::string_view opt_path) const {
142,636✔
179
  auto key = llvm::StringRef(opt_path.data());
142,636✔
180
  if (occurence_mapping_.lookup(key)) {  // only have value if it occurred
142,636✔
181
    return mapping_.lookup(key);
2,220✔
182
  }
183
  return {};
140,416✔
184
}
142,636✔
185

186
[[maybe_unused]] bool EnvironmentFlagsOptions::valueSpecified(std::string_view opt_path) const {
×
187
  auto key = llvm::StringRef(opt_path.data());
×
188
  return occurence_mapping_.lookup(key);
×
189
}
190

191
}  // 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