Skip to content

Commit

Permalink
refactoring: Convert stats to enum class (ccache#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderLanin authored Sep 4, 2020
1 parent baf9120 commit c0248cf
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 193 deletions.
16 changes: 9 additions & 7 deletions src/Counters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,29 @@

#include <algorithm>

Counters::Counters() : m_counters(STATS_END)
Counters::Counters() : m_counters(static_cast<size_t>(Statistic::END))
{
}

// clang-format off
unsigned&
Counters::operator[](size_t index)
Counters::operator[](Statistic index)
// clang-format on
{
if (index >= m_counters.size()) {
m_counters.resize(index + 1);
const size_t i = static_cast<size_t>(index);
if (i >= m_counters.size()) {
m_counters.resize(i + 1);
}
return m_counters.at(index);
return m_counters.at(i);
}

// clang-format off
unsigned
Counters::operator[](size_t index) const
Counters::operator[](Statistic index) const
// clang-format on
{
return index < m_counters.size() ? m_counters.at(index) : 0;
const size_t i = static_cast<size_t>(index);
return i < m_counters.size() ? m_counters.at(i) : 0;
}

size_t
Expand Down
6 changes: 4 additions & 2 deletions src/Counters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@

#include <vector>

enum class Statistic;

// A simple wrapper around a vector of integers
// used for the statistics counters.
class Counters
{
public:
Counters();

unsigned& operator[](size_t index);
unsigned operator[](size_t index) const;
unsigned& operator[](Statistic index);
unsigned operator[](Statistic index) const;

size_t size() const;

Expand Down
78 changes: 39 additions & 39 deletions src/argprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ add_depend_mode_extra_original_args(Context& ctx, const std::string& arg)
}
}

optional<enum stats>
optional<Statistic>
process_arg(Context& ctx,
Args& args,
size_t& args_index,
Expand All @@ -228,15 +228,15 @@ process_arg(Context& ctx,
i++;
if (i == args.size()) {
log("--ccache-skip lacks an argument");
return STATS_ARGS;
return Statistic::bad_compiler_arguments;
}
state.common_args.push_back(args[i]);
return nullopt;
}

// Special case for -E.
if (args[i] == "-E") {
return STATS_PREPROCESSING;
return Statistic::called_for_preprocessing;
}

// Handle "@file" argument.
Expand All @@ -249,7 +249,7 @@ process_arg(Context& ctx,
auto file_args = Args::from_gcc_atfile(argpath);
if (!file_args) {
log("Couldn't read arg file {}", argpath);
return STATS_ARGS;
return Statistic::bad_compiler_arguments;
}

args.replace(i, *file_args);
Expand All @@ -262,7 +262,7 @@ process_arg(Context& ctx,
&& (args[i] == "-optf" || args[i] == "--options-file")) {
if (i == args.size() - 1) {
log("Expected argument after {}", args[i]);
return STATS_ARGS;
return Statistic::bad_compiler_arguments;
}
++i;

Expand All @@ -272,7 +272,7 @@ process_arg(Context& ctx,
auto file_args = Args::from_gcc_atfile(*it);
if (!file_args) {
log("Couldn't read CUDA options file {}", *it);
return STATS_ARGS;
return Statistic::bad_compiler_arguments;
}

args.insert(i + 1, *file_args);
Expand All @@ -285,7 +285,7 @@ process_arg(Context& ctx,
if (compopt_too_hard(args[i]) || Util::starts_with(args[i], "-fdump-")
|| Util::starts_with(args[i], "-MJ")) {
log("Compiler option {} is unsupported", args[i]);
return STATS_UNSUPPORTED_OPTION;
return Statistic::unsupported_compiler_option;
}

// These are too hard in direct mode.
Expand All @@ -297,7 +297,7 @@ process_arg(Context& ctx,
// -Xarch_* options are too hard.
if (Util::starts_with(args[i], "-Xarch_")) {
log("Unsupported compiler option: {}", args[i]);
return STATS_UNSUPPORTED_OPTION;
return Statistic::unsupported_compiler_option;
}

// Handle -arch options.
Expand Down Expand Up @@ -335,7 +335,7 @@ process_arg(Context& ctx,
&& args[i] == "-Werror")) {
if (i == args.size() - 1) {
log("Missing argument to {}", args[i]);
return STATS_ARGS;
return Statistic::bad_compiler_arguments;
}
state.compiler_only_args.push_back(args[i + 1]);
++i;
Expand All @@ -359,12 +359,12 @@ process_arg(Context& ctx,
if (!config.depend_mode() || !config.direct_mode()) {
log("Compiler option {} is unsupported without direct depend mode",
args[i]);
return STATS_CANTUSEMODULES;
return Statistic::could_not_use_modules;
} else if (!(config.sloppiness() & SLOPPY_MODULES)) {
log(
"You have to specify \"modules\" sloppiness when using"
" -fmodules to get hits");
return STATS_CANTUSEMODULES;
return Statistic::could_not_use_modules;
}
}

Expand Down Expand Up @@ -402,7 +402,7 @@ process_arg(Context& ctx,
if (args[i] == "-x") {
if (i == args.size() - 1) {
log("Missing argument to {}", args[i]);
return STATS_ARGS;
return Statistic::bad_compiler_arguments;
}
if (args_info.input_file.empty()) {
state.explicit_language = args[i + 1];
Expand All @@ -421,7 +421,7 @@ process_arg(Context& ctx,
if (args[i] == "-o") {
if (i == args.size() - 1) {
log("Missing argument to {}", args[i]);
return STATS_ARGS;
return Statistic::bad_compiler_arguments;
}
args_info.output_obj = Util::make_relative_path(ctx, args[i + 1]);
i++;
Expand Down Expand Up @@ -496,7 +496,7 @@ process_arg(Context& ctx,
// -MF arg
if (i == args.size() - 1) {
log("Missing argument to {}", args[i]);
return STATS_ARGS;
return Statistic::bad_compiler_arguments;
}
dep_file = args[i + 1];
i++;
Expand All @@ -522,7 +522,7 @@ process_arg(Context& ctx,
// -MQ arg or -MT arg
if (i == args.size() - 1) {
log("Missing argument to {}", args[i]);
return STATS_ARGS;
return Statistic::bad_compiler_arguments;
}
state.dep_args.push_back(args[i]);
std::string relpath = Util::make_relative_path(ctx, args[i + 1]);
Expand Down Expand Up @@ -568,7 +568,7 @@ process_arg(Context& ctx,
|| args[i] == "-fbranch-probabilities") {
if (!process_profiling_option(ctx, args[i])) {
// The failure is logged by process_profiling_option.
return STATS_UNSUPPORTED_OPTION;
return Statistic::unsupported_compiler_option;
}
state.common_args.push_back(args[i]);
return nullopt;
Expand All @@ -591,7 +591,7 @@ process_arg(Context& ctx,
if (args[i] == "--sysroot") {
if (i == args.size() - 1) {
log("Missing argument to {}", args[i]);
return STATS_ARGS;
return Statistic::bad_compiler_arguments;
}
state.common_args.push_back(args[i]);
auto relpath = Util::make_relative_path(ctx, args[i + 1]);
Expand All @@ -604,7 +604,7 @@ process_arg(Context& ctx,
if (args[i] == "-target") {
if (i == args.size() - 1) {
log("Missing argument to {}", args[i]);
return STATS_ARGS;
return Statistic::bad_compiler_arguments;
}
state.common_args.push_back(args[i]);
state.common_args.push_back(args[i + 1]);
Expand All @@ -619,7 +619,7 @@ process_arg(Context& ctx,
// from compiling the preprocessed file will not be equal to the object
// file produced when compiling without ccache.
log("Too hard option -Wp,-P detected");
return STATS_UNSUPPORTED_OPTION;
return Statistic::unsupported_compiler_option;
} else if (Util::starts_with(args[i], "-Wp,-MD,")
&& args[i].find(',', 8) == std::string::npos) {
args_info.generating_dependencies = true;
Expand Down Expand Up @@ -676,7 +676,7 @@ process_arg(Context& ctx,
if (args[i] == "--serialize-diagnostics") {
if (i == args.size() - 1) {
log("Missing argument to {}", args[i]);
return STATS_ARGS;
return Statistic::bad_compiler_arguments;
}
args_info.generating_diagnostics = true;
args_info.output_dia = Util::make_relative_path(ctx, args[i + 1]);
Expand Down Expand Up @@ -741,7 +741,7 @@ process_arg(Context& ctx,
if (compopt_takes_path(args[i])) {
if (i == args.size() - 1) {
log("Missing argument to {}", args[i]);
return STATS_ARGS;
return Statistic::bad_compiler_arguments;
}

// In the -Xclang -include-(pch/pth) -Xclang <path> case, the path is one
Expand All @@ -753,7 +753,7 @@ process_arg(Context& ctx,

if (!detect_pch(
ctx, args[i], args[i + next], next == 2, &state.found_pch)) {
return STATS_ARGS;
return Statistic::bad_compiler_arguments;
}

std::string relpath = Util::make_relative_path(ctx, args[i + next]);
Expand Down Expand Up @@ -793,7 +793,7 @@ process_arg(Context& ctx,
if (compopt_takes_arg(args[i])) {
if (i == args.size() - 1) {
log("Missing argument to {}", args[i]);
return STATS_ARGS;
return Statistic::bad_compiler_arguments;
}

if (compopt_affects_cpp(args[i])) {
Expand Down Expand Up @@ -835,17 +835,17 @@ process_arg(Context& ctx,
if (!args_info.input_file.empty()) {
if (!language_for_file(args[i]).empty()) {
log("Multiple input files: {} and {}", args_info.input_file, args[i]);
return STATS_MULTIPLE;
return Statistic::multiple_source_files;
} else if (!state.found_c_opt && !state.found_dc_opt) {
log("Called for link with {}", args[i]);
if (args[i].find("conftest.") != std::string::npos) {
return STATS_CONFTEST;
return Statistic::autoconf_test;
} else {
return STATS_LINK;
return Statistic::called_for_link;
}
} else {
log("Unsupported source extension: {}", args[i]);
return STATS_SOURCELANG;
return Statistic::unsupported_source_language;
}
}

Expand Down Expand Up @@ -920,7 +920,7 @@ handle_dependency_environment_variables(Context& ctx,

} // namespace

optional<enum stats>
optional<Statistic>
process_args(Context& ctx,
Args& preprocessor_args,
Args& extra_args_to_hash,
Expand Down Expand Up @@ -955,7 +955,7 @@ process_args(Context& ctx,

if (args_info.input_file.empty()) {
log("No input file found");
return STATS_NOINPUT;
return Statistic::no_input_file;
}

if (state.found_pch || state.found_fpch_preprocess) {
Expand All @@ -965,7 +965,7 @@ process_args(Context& ctx,
"You have to specify \"time_macros\" sloppiness when using"
" precompiled headers to get direct hits");
log("Disabling direct mode");
return STATS_CANTUSEPCH;
return Statistic::could_not_use_precompiled_header;
}
}

Expand All @@ -980,7 +980,7 @@ process_args(Context& ctx,
if (!state.explicit_language.empty()) {
if (!language_is_supported(state.explicit_language)) {
log("Unsupported language: {}", state.explicit_language);
return STATS_SOURCELANG;
return Statistic::unsupported_source_language;
}
args_info.actual_language = state.explicit_language;
} else {
Expand All @@ -996,7 +996,7 @@ process_args(Context& ctx,
log(
"You have to specify \"pch_defines,time_macros\" sloppiness when"
" creating precompiled headers");
return STATS_CANTUSEPCH;
return Statistic::could_not_use_precompiled_header;
}

if (!state.found_c_opt && !state.found_dc_opt && !state.found_S_opt) {
Expand All @@ -1007,14 +1007,14 @@ process_args(Context& ctx,
// Having a separate statistic for autoconf tests is useful, as they are
// the dominant form of "called for link" in many cases.
return args_info.input_file.find("conftest.") != std::string::npos
? STATS_CONFTEST
: STATS_LINK;
? Statistic::autoconf_test
: Statistic::called_for_link;
}
}

if (args_info.actual_language.empty()) {
log("Unsupported source extension: {}", args_info.input_file);
return STATS_SOURCELANG;
return Statistic::unsupported_source_language;
}

if (!config.run_second_cpp() && args_info.actual_language == "cu") {
Expand All @@ -1038,7 +1038,7 @@ process_args(Context& ctx,
// Don't try to second guess the compilers heuristics for stdout handling.
if (args_info.output_obj == "-") {
log("Output file is -");
return STATS_OUTSTDOUT;
return Statistic::output_to_stdout;
}

if (args_info.output_obj.empty()) {
Expand All @@ -1055,7 +1055,7 @@ process_args(Context& ctx,
size_t pos = args_info.output_obj.rfind('.');
if (pos == std::string::npos || pos == args_info.output_obj.size() - 1) {
log("Badly formed object filename");
return STATS_ARGS;
return Statistic::bad_compiler_arguments;
}

args_info.output_dwo = Util::change_extension(args_info.output_obj, ".dwo");
Expand All @@ -1066,15 +1066,15 @@ process_args(Context& ctx,
auto st = Stat::stat(args_info.output_obj);
if (st && !st.is_regular()) {
log("Not a regular file: {}", args_info.output_obj);
return STATS_BADOUTPUTFILE;
return Statistic::bad_output_file;
}
}

auto output_dir = std::string(Util::dir_name(args_info.output_obj));
auto st = Stat::stat(output_dir);
if (!st || !st.is_directory()) {
log("Directory does not exist: {}", output_dir);
return STATS_BADOUTPUTFILE;
return Statistic::bad_output_file;
}

// Some options shouldn't be passed to the real compiler when it compiles
Expand Down
8 changes: 4 additions & 4 deletions src/argprocessing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Args;
//
// Returns nullopt on success, otherwise the statistics counter that should be
// incremented.
nonstd::optional<enum stats> process_args(Context& ctx,
Args& preprocessor_args,
Args& extra_args_to_hash,
Args& compiler_args);
nonstd::optional<Statistic> process_args(Context& ctx,
Args& preprocessor_args,
Args& extra_args_to_hash,
Args& compiler_args);
Loading

0 comments on commit c0248cf

Please sign in to comment.