Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: Add P4::warningCount() and P4::infoCount() #5098

Merged
merged 7 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,22 @@ limitations under the License.

namespace P4 {

/// @return the number of errors encountered so far in the current compilation
/// context.
/// @return the number of errors encountered so far in the current compilation context.
inline unsigned errorCount() { return BaseCompileContext::get().errorReporter().getErrorCount(); }

/// @return the number of diagnostics (either errors or warnings) encountered so
/// far in the current compilation context.
/// @return the number of warnings encountered so far in the current compilation context.
inline unsigned warningCount() { return BaseCompileContext::get().errorReporter().getWarningCount(); }

/// @return the number of infos encountered so far in the current compilation context.
inline unsigned infoCount() { return BaseCompileContext::get().errorReporter().getInfoCount(); }

/// @return the number of diagnostics (either errors, warnings or infos) encountered so far in the
/// current compilation context.
inline unsigned diagnosticCount() {
return BaseCompileContext::get().errorReporter().getDiagnosticCount();
}

// Errors (and warnings) are specified using boost::format format strings, i.e.,
// Errors, warnings, and info are specified using boost::format format strings, i.e.,
vlstill marked this conversation as resolved.
Show resolved Hide resolved
// %1%, %2%, etc (starting at 1, not at 0).
// Some compatibility for printf-style arguments is also supported.

Expand Down
86 changes: 55 additions & 31 deletions test/gtest/diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,51 +69,58 @@ TEST_F(Diagnostics, P4_16_Disable) {
@diagnostic("uninitialized_out_param", "disable")
)"));
EXPECT_TRUE(test);
EXPECT_EQ(0u, ::P4::diagnosticCount());
EXPECT_EQ(0u, diagnosticCount());
EXPECT_EQ(0u, errorCount());
EXPECT_EQ(0u, warningCount());
EXPECT_EQ(0u, infoCount());
}

TEST_F(Diagnostics, P4_16_Warn) {
auto test = createP4_16DiagnosticsTestCase(P4_SOURCE(R"(
@diagnostic("uninitialized_out_param", "warn")
)"));
EXPECT_TRUE(test);
EXPECT_EQ(1u, ::P4::diagnosticCount());
EXPECT_EQ(0u, ::P4::errorCount());
EXPECT_EQ(1u, diagnosticCount());
EXPECT_EQ(1u, warningCount());
EXPECT_EQ(0u, errorCount());
}

TEST_F(Diagnostics, P4_16_Error) {
auto test = createP4_16DiagnosticsTestCase(P4_SOURCE(R"(
@diagnostic("uninitialized_out_param", "error")
)"));
EXPECT_FALSE(test);
EXPECT_EQ(1u, ::P4::diagnosticCount());
EXPECT_EQ(1u, ::P4::errorCount());
EXPECT_EQ(1u, diagnosticCount());
EXPECT_EQ(0u, warningCount());
EXPECT_EQ(1u, errorCount());
}

TEST_F(Diagnostics, DISABLED_P4_14_Disable) {
auto test = createP4_14DiagnosticsTestCase(P4_SOURCE(R"(
@pragma diagnostic uninitialized_use disable
)"));
EXPECT_TRUE(test);
EXPECT_EQ(0u, ::P4::diagnosticCount());
EXPECT_EQ(0u, diagnosticCount());
}

TEST_F(Diagnostics, DISABLED_P4_14_Warn) {
auto test = createP4_14DiagnosticsTestCase(P4_SOURCE(R"(
@pragma diagnostic uninitialized_use warn
)"));
EXPECT_TRUE(test);
EXPECT_EQ(1u, ::P4::diagnosticCount());
EXPECT_EQ(0u, ::P4::errorCount());
EXPECT_EQ(1u, diagnosticCount());
EXPECT_EQ(1u, warningCount());
EXPECT_EQ(0u, errorCount());
}

TEST_F(Diagnostics, DISABLED_P4_14_Error) {
auto test = createP4_14DiagnosticsTestCase(P4_SOURCE(R"(
@pragma diagnostic uninitialized_use error
)"));
EXPECT_FALSE(test);
EXPECT_EQ(1u, ::P4::diagnosticCount());
EXPECT_EQ(1u, ::P4::errorCount());
EXPECT_EQ(1u, diagnosticCount());
EXPECT_EQ(0u, warningCount());
EXPECT_EQ(1u, errorCount());
}

TEST_F(Diagnostics, NestedCompileContexts) {
Expand All @@ -131,8 +138,9 @@ TEST_F(Diagnostics, NestedCompileContexts) {
@diagnostic("uninitialized_out_param", "error")
)"));
EXPECT_FALSE(test);
EXPECT_EQ(1u, ::P4::diagnosticCount());
EXPECT_EQ(1u, ::P4::errorCount());
EXPECT_EQ(1u, diagnosticCount());
EXPECT_EQ(0u, warningCount());
EXPECT_EQ(1u, errorCount());
}

// Run a test with `uninitialized_out_param` disabled. The error from
Expand All @@ -141,20 +149,20 @@ TEST_F(Diagnostics, NestedCompileContexts) {
@diagnostic("uninitialized_out_param", "disable")
)"));
EXPECT_TRUE(test);
EXPECT_EQ(0u, ::P4::diagnosticCount());
EXPECT_EQ(0u, diagnosticCount());
}

// Run a test with no diagnostic pragma for `uninitialized_out_param`. It
// should default to triggering a warning; the diagnostic actions configured
// by the previous tests should be gone.
auto test = createP4_16DiagnosticsTestCase(P4_SOURCE(R"()"));
EXPECT_TRUE(test);
EXPECT_EQ(1u, ::P4::diagnosticCount());
EXPECT_EQ(0u, ::P4::errorCount());
EXPECT_EQ(1u, diagnosticCount());
EXPECT_EQ(0u, errorCount());
vlstill marked this conversation as resolved.
Show resolved Hide resolved
}

TEST_F(Diagnostics, CompilerOptions) {
using CommandLineOptions = P4::IOptionPragmaParser::CommandLineOptions;
using CommandLineOptions = IOptionPragmaParser::CommandLineOptions;

auto parseWithCompilerOptions =
[](const CommandLineOptions &args) -> std::optional<FrontendTestCase> {
Expand All @@ -170,23 +178,25 @@ TEST_F(Diagnostics, CompilerOptions) {
AutoCompileContext autoContext(new GTestContext);
auto test = parseWithCompilerOptions({"(test)", "--Wdisable"});
EXPECT_TRUE(test);
EXPECT_EQ(0u, ::P4::diagnosticCount());
EXPECT_EQ(0u, diagnosticCount());
}

{
AutoCompileContext autoContext(new GTestContext);
auto test = parseWithCompilerOptions({"(test)", "--Wwarn"});
EXPECT_TRUE(test);
EXPECT_EQ(1u, ::P4::diagnosticCount());
EXPECT_EQ(0u, ::P4::errorCount());
EXPECT_EQ(1u, diagnosticCount());
EXPECT_EQ(1u, warningCount());
EXPECT_EQ(0u, errorCount());
}

{
AutoCompileContext autoContext(new GTestContext);
auto test = parseWithCompilerOptions({"(test)", "--Werror"});
EXPECT_FALSE(test);
EXPECT_EQ(1u, ::P4::diagnosticCount());
EXPECT_EQ(1u, ::P4::errorCount());
EXPECT_EQ(1u, diagnosticCount());
EXPECT_EQ(0u, warningCount());
EXPECT_EQ(1u, errorCount());
}

// Check that `--Wdisable`, `--Wwarn`, and `--Werror`, when used with an
Expand All @@ -196,39 +206,43 @@ TEST_F(Diagnostics, CompilerOptions) {
AutoCompileContext autoContext(new GTestContext);
auto test = parseWithCompilerOptions({"(test)", "--Wdisable=uninitialized_out_param"});
EXPECT_TRUE(test);
EXPECT_EQ(0u, ::P4::diagnosticCount());
EXPECT_EQ(0u, diagnosticCount());
}

{
AutoCompileContext autoContext(new GTestContext);
auto test = parseWithCompilerOptions({"(test)", "--Wdisable=unknown_diagnostic"});
EXPECT_TRUE(test);
EXPECT_EQ(1u, ::P4::diagnosticCount());
EXPECT_EQ(0u, ::P4::errorCount());
EXPECT_EQ(1u, diagnosticCount());
EXPECT_EQ(1u, warningCount());
EXPECT_EQ(0u, errorCount());
}

{
AutoCompileContext autoContext(new GTestContext);
auto test = parseWithCompilerOptions({"(test)", "--Wwarn=uninitialized_out_param"});
EXPECT_TRUE(test);
EXPECT_EQ(1u, ::P4::diagnosticCount());
EXPECT_EQ(0u, ::P4::errorCount());
EXPECT_EQ(1u, diagnosticCount());
EXPECT_EQ(1u, warningCount());
EXPECT_EQ(0u, errorCount());
}

{
AutoCompileContext autoContext(new GTestContext);
auto test = parseWithCompilerOptions({"(test)", "--Werror=uninitialized_out_param"});
EXPECT_FALSE(test);
EXPECT_EQ(1u, ::P4::diagnosticCount());
EXPECT_EQ(1u, ::P4::errorCount());
EXPECT_EQ(1u, diagnosticCount());
EXPECT_EQ(0u, warningCount());
EXPECT_EQ(1u, errorCount());
}

{
AutoCompileContext autoContext(new GTestContext);
auto test = parseWithCompilerOptions({"(test)", "--Werror=unknown_diagnostic"});
EXPECT_TRUE(test);
EXPECT_EQ(1u, ::P4::diagnosticCount());
EXPECT_EQ(0u, ::P4::errorCount());
EXPECT_EQ(1u, diagnosticCount());
EXPECT_EQ(1u, warningCount());
EXPECT_EQ(0u, errorCount());
}

// Check that e.g. `--Wdisable foo` is treated as two arguments, rather than
Expand All @@ -243,8 +257,18 @@ TEST_F(Diagnostics, CompilerOptions) {
// treated as an argument to `--Wdisable`, then
// `uninitialized_out_param` would still be enabled and a warning would
// fire.
EXPECT_EQ(0u, ::P4::diagnosticCount());
EXPECT_EQ(0u, diagnosticCount());
}
}

TEST_F(Diagnostics, BasicInfo) {
AutoCompileContext autoContext(new GTestContext);
info(P4::ErrorType::INFO_INFERRED, "test");
EXPECT_EQ(1u, diagnosticCount());
EXPECT_EQ(1u, infoCount());
EXPECT_EQ(0u, warningCount());
EXPECT_EQ(0u, errorCount());
}


vlstill marked this conversation as resolved.
Show resolved Hide resolved
} // namespace P4::Test
Loading