Skip to content

Commit

Permalink
test: Use warningCount in diagnostics test
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimír Štill <vladimir.still@intel.com>
  • Loading branch information
vlstill committed Jan 14, 2025
1 parent 0c60b62 commit 9e8d633
Showing 1 changed file with 54 additions and 30 deletions.
84 changes: 54 additions & 30 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, P4::diagnosticCount());
EXPECT_EQ(0u, P4::errorCount());
EXPECT_EQ(0u, P4::warningCount());
EXPECT_EQ(0u, P4::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, P4::diagnosticCount());
EXPECT_EQ(1u, P4::warningCount());
EXPECT_EQ(0u, P4::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, P4::diagnosticCount());
EXPECT_EQ(0u, P4::warningCount());
EXPECT_EQ(1u, P4::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, P4::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, P4::diagnosticCount());
EXPECT_EQ(1u, P4::warningCount());
EXPECT_EQ(0u, P4::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, P4::diagnosticCount());
EXPECT_EQ(0u, P4::warningCount());
EXPECT_EQ(1u, P4::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, P4::diagnosticCount());
EXPECT_EQ(0u, P4::warningCount());
EXPECT_EQ(1u, P4::errorCount());
}

// Run a test with `uninitialized_out_param` disabled. The error from
Expand All @@ -141,16 +149,16 @@ TEST_F(Diagnostics, NestedCompileContexts) {
@diagnostic("uninitialized_out_param", "disable")
)"));
EXPECT_TRUE(test);
EXPECT_EQ(0u, ::P4::diagnosticCount());
EXPECT_EQ(0u, P4::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, P4::diagnosticCount());
EXPECT_EQ(0u, P4::errorCount());
}

TEST_F(Diagnostics, CompilerOptions) {
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, P4::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, P4::diagnosticCount());
EXPECT_EQ(1u, P4::warningCount());
EXPECT_EQ(0u, P4::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, P4::diagnosticCount());
EXPECT_EQ(0u, P4::warningCount());
EXPECT_EQ(1u, P4::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, P4::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, P4::diagnosticCount());
EXPECT_EQ(1u, P4::warningCount());
EXPECT_EQ(0u, P4::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, P4::diagnosticCount());
EXPECT_EQ(1u, P4::warningCount());
EXPECT_EQ(0u, P4::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, P4::diagnosticCount());
EXPECT_EQ(0u, P4::warningCount());
EXPECT_EQ(1u, P4::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, P4::diagnosticCount());
EXPECT_EQ(1u, P4::warningCount());
EXPECT_EQ(0u, P4::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, P4::diagnosticCount());
}
}

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


} // namespace P4::Test

0 comments on commit 9e8d633

Please sign in to comment.