Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 3734f90

Browse files
authored
assert(false) after switch() (#171)
This stops gcc from warning about -Wreturn-type. Make -Wreturn-type and -Wswitch into errors. Fixes #79
1 parent 9db7640 commit 3734f90

File tree

7 files changed

+31
-19
lines changed

7 files changed

+31
-19
lines changed

opencensus/copts.bzl

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Flags specified here must not impact ABI. Code compiled with and without these
1818
opts will be linked together, and in some cases headers compiled with and
1919
without these options will be part of the same program.
2020
21-
We use the same flags as absl.
21+
We use the same flags as absl, plus turn some warnings into errors.
2222
"""
2323

2424
load(
@@ -31,18 +31,16 @@ load(
3131
"MSVC_TEST_FLAGS",
3232
)
3333

34+
WERROR = ["-Werror=return-type", "-Werror=switch"]
35+
3436
DEFAULT_COPTS = select({
35-
"//opencensus:llvm_compiler": LLVM_FLAGS,
36-
# Disable "not all control paths return a value"; functions that return
37-
# out of a switch on an enum cause build errors otherwise.
38-
"//opencensus:windows": MSVC_FLAGS + ["/wd4715"],
39-
"//conditions:default": GCC_FLAGS,
37+
"//opencensus:llvm_compiler": LLVM_FLAGS + WERROR,
38+
"//opencensus:windows": MSVC_FLAGS,
39+
"//conditions:default": GCC_FLAGS + WERROR,
4040
})
4141

4242
TEST_COPTS = DEFAULT_COPTS + select({
43-
"//opencensus:llvm_compiler": LLVM_TEST_FLAGS,
44-
# Disable "not all control paths return a value"; functions that return
45-
# out of a switch on an enum cause build errors otherwise.
46-
"//opencensus:windows": MSVC_TEST_FLAGS + ["/wd4715"],
47-
"//conditions:default": GCC_TEST_FLAGS,
43+
"//opencensus:llvm_compiler": LLVM_TEST_FLAGS + WERROR,
44+
"//opencensus:windows": MSVC_TEST_FLAGS,
45+
"//conditions:default": GCC_TEST_FLAGS + WERROR,
4846
})

opencensus/exporters/stats/prometheus/internal/prometheus_utils.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ prometheus::MetricType MetricType(opencensus::stats::Aggregation::Type type) {
5555
case opencensus::stats::Aggregation::Type::kDistribution:
5656
return prometheus::MetricType::Histogram;
5757
}
58+
ABSL_ASSERT(false && "Bad MetricType.");
59+
return prometheus::MetricType::Untyped;
5860
}
5961

6062
void SetValue(double value, prometheus::MetricType type,

opencensus/exporters/stats/stackdriver/internal/stackdriver_utils.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ google::api::MetricDescriptor::ValueType GetValueType(
8383
case opencensus::stats::Aggregation::Type::kDistribution:
8484
return google::api::MetricDescriptor::DISTRIBUTION;
8585
}
86+
ABSL_ASSERT(false && "Bad descriptor type.");
87+
return google::api::MetricDescriptor::DOUBLE;
8688
}
8789

8890
// Overloaded function for converting ViewData value types to Points. The
@@ -197,6 +199,8 @@ std::vector<google::monitoring::v3::TimeSeries> MakeTimeSeries(
197199
return DataToTimeSeries(view_descriptor, data.distribution_data(),
198200
base_time_series);
199201
}
202+
ABSL_ASSERT(false && "Bad ViewData.type().");
203+
return {};
200204
}
201205

202206
void SetTimestamp(absl::Time time, google::protobuf::Timestamp* proto) {

opencensus/stats/internal/aggregation.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,27 @@
1414

1515
#include "opencensus/stats/aggregation.h"
1616

17+
#include <cassert>
18+
1719
#include "absl/strings/str_cat.h"
1820

1921
namespace opencensus {
2022
namespace stats {
2123

2224
std::string Aggregation::DebugString() const {
2325
switch (type_) {
24-
case Type::kCount: {
26+
case Type::kCount:
2527
return "Count";
26-
}
27-
case Type::kSum: {
28+
case Type::kSum:
2829
return "Sum";
29-
}
30-
case Type::kDistribution: {
30+
case Type::kDistribution:
3131
return absl::StrCat("Distribution with ",
3232
bucket_boundaries_.DebugString());
33-
}
34-
case Type::kLastValue: {
33+
case Type::kLastValue:
3534
return "Last Value";
36-
}
3735
}
36+
assert(false && "Invalid Aggregation type.");
37+
return "BAD TYPE";
3838
}
3939

4040
} // namespace stats

opencensus/stats/internal/aggregation_window.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "opencensus/stats/internal/aggregation_window.h"
1616

17+
#include <cassert>
18+
1719
#include "absl/strings/str_cat.h"
1820

1921
namespace opencensus {
@@ -29,6 +31,8 @@ std::string AggregationWindow::DebugString() const {
2931
return absl::StrCat("Interval (", absl::ToDoubleSeconds(duration_),
3032
"s window)");
3133
}
34+
assert(false && "Bad AggregationWindow type.");
35+
return "BAD TYPE";
3236
}
3337

3438
} // namespace stats

opencensus/stats/internal/view_data.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ ViewData::Type ViewData::type() const {
4141
// map.
4242
return Type::kDouble;
4343
}
44+
ABSL_ASSERT(false && "Bad ViewData type.");
45+
return Type::kDouble;
4446
}
4547

4648
const ViewData::DataMap<double>& ViewData::double_data() const {

opencensus/stats/internal/view_data_impl.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ ViewDataImpl::Type ViewDataImpl::TypeForDescriptor(
4949
case AggregationWindow::Type::kInterval:
5050
return ViewDataImpl::Type::kStatsObject;
5151
}
52+
ABSL_ASSERT(false && "Bad ViewDataImpl type.");
53+
return ViewDataImpl::Type::kDouble;
5254
}
5355

5456
ViewDataImpl::ViewDataImpl(absl::Time start_time,

0 commit comments

Comments
 (0)