diff --git a/BUILD.bazel b/BUILD.bazel index 3f1ccd9c14..8144c51b11 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -28,11 +28,17 @@ genrule( outs = ["config.h"], # TODO: We should actually check these properly instead of just #undefing them. # Maybe select() can help here? + # CONFIG_PKGDATADIR is where p4c should look for p4include at runtime. + # This will work only if the binary is executed by Bazel. For a general + # solution, we would need to make p4c aware of Bazel, specifically: + # https://github.com/bazelbuild/bazel/blob/master/tools/cpp/runfiles/runfiles_src.h cmd = "sed -e 's|cmakedefine|define|g' \ -e 's|define HAVE_LIBGC 1|undef HAVE_LIBGC|g' \ -e 's|define HAVE_LIBBACKTRACE 1|undef HAVE_LIBBACKTRACE|g' \ -e 's|define HAVE_MM_MALLOC_H 1|undef HAVE_MM_MALLOC_H|g' \ - < $(SRCS) > $(OUTS)", + -e 's|@MAX_LOGGING_LEVEL@|10|g' \ + -e 's|@CONFIG_PKGDATADIR@|external/%s|g' \ + < $(SRCS) > $(OUTS)" % repository_name(), visibility = ["//visibility:private"], ) @@ -215,13 +221,6 @@ cc_library( "backends/dpdk/dbprint-dpdk.cpp", "backends/dpdk/printUtils.cpp", ], - copts = [ - # Where p4c should look for p4include at runtime. - ("-DCONFIG_PKGDATADIR=\\\"external/%s\\\"" % repository_name()), - # This will work only if the binary is executed by Bazel. For a general - # solution, we would need to make p4c aware of Bazel, specifically: - # https://github.com/bazelbuild/bazel/blob/master/tools/cpp/runfiles/runfiles_src.h - ], textual_hdrs = glob([ "ir/**/*.h", "frontends/**/*.h", diff --git a/CMakeLists.txt b/CMakeLists.txt index f519916729..00536c733f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,7 +75,6 @@ set (P4C_DRIVER_NAME "p4c" CACHE STRING "Customize the name of the driver script set(MAX_LOGGING_LEVEL 10 CACHE STRING "Control the maximum logging level for -T logs") set_property(CACHE MAX_LOGGING_LEVEL PROPERTY STRINGS 0 1 2 3 4 5 6 7 8 9 10) -add_definitions(-DMAX_LOGGING_LEVEL=${MAX_LOGGING_LEVEL}) if (NOT CMAKE_BUILD_TYPE) set (CMAKE_BUILD_TYPE "Release") @@ -198,6 +197,10 @@ find_package (BISON 3.0 REQUIRED) if (ENABLE_GTESTS) include(GoogleTest) p4c_obtain_googletest() + # Some P4C source files include gtest files. This will be propagated to the + # definition in config.h We need this definition to guard against compilation + # errors. + set(P4C_GTEST_ENABLED ON) endif () include(Abseil) p4c_obtain_abseil() @@ -373,8 +376,8 @@ include_directories ( ${P4C_BINARY_DIR} ${P4C_SOURCE_DIR}/extensions ) -add_definitions (-DCONFIG_PREFIX="${CMAKE_INSTALL_PREFIX}") -add_definitions (-DCONFIG_PKGDATADIR="${CMAKE_INSTALL_PREFIX}/${P4C_ARTIFACTS_OUTPUT_DIRECTORY}") +set(CONFIG_PREFIX ${CMAKE_INSTALL_PREFIX}) +set(CONFIG_PKGDATADIR ${CMAKE_INSTALL_PREFIX}/${P4C_ARTIFACTS_OUTPUT_DIRECTORY}) set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${P4C_CXX_FLAGS}") set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${P4C_CXX_FLAGS}") diff --git a/backends/tofino/bf-p4c/driver/p4c.tofino2.cfg b/backends/tofino/bf-p4c/driver/p4c.tofino2.cfg index 1a8852ffc4..1a8001e26f 100644 --- a/backends/tofino/bf-p4c/driver/p4c.tofino2.cfg +++ b/backends/tofino/bf-p4c/driver/p4c.tofino2.cfg @@ -38,7 +38,7 @@ class Tofino2Backend(bfn.BarefootBackend): def process_command_line_options(self, opts): if opts.enable_bf_asm or os.getenv("ENABLE_BF_ASM"): - self.config_assembler(self.target) + self.config_assembler(self._target) bfn.BarefootBackend.process_command_line_options(self, opts) for t in Tofino2Variants.keys(): diff --git a/cmake/GoogleTest.cmake b/cmake/GoogleTest.cmake index a7d59f2b61..ae43869b76 100644 --- a/cmake/GoogleTest.cmake +++ b/cmake/GoogleTest.cmake @@ -1,7 +1,4 @@ macro(p4c_obtain_googletest) - # Some P4C source files include gtest files. - # We need this definition to guard against compilation errors. - add_definitions(-DP4C_GTEST_ENABLED) # Print download state while setting up GTest. set(FETCHCONTENT_QUIET_PREV ${FETCHCONTENT_QUIET}) set(FETCHCONTENT_QUIET OFF) diff --git a/cmake/config.h.cmake b/cmake/config.h.cmake index 661a54576a..7d34101a0e 100644 --- a/cmake/config.h.cmake +++ b/cmake/config.h.cmake @@ -33,3 +33,12 @@ /* Define to 1 if you have the mm_malloc.h header */ #cmakedefine HAVE_MM_MALLOC_H 1 + +#cmakedefine CONFIG_PKGDATADIR "@CONFIG_PKGDATADIR@" + +#cmakedefine CONFIG_PREFIX "@CONFIG_PREFIX@" + +/* The maximum logging level for -T logs */ +#cmakedefine MAX_LOGGING_LEVEL @MAX_LOGGING_LEVEL@ + +#cmakedefine P4C_GTEST_ENABLED 1 \ No newline at end of file diff --git a/lib/error.h b/lib/error.h index 9eedcb414d..953417c901 100644 --- a/lib/error.h +++ b/lib/error.h @@ -30,17 +30,24 @@ 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 infos are specified using boost::format format strings, i.e., // %1%, %2%, etc (starting at 1, not at 0). // Some compatibility for printf-style arguments is also supported. diff --git a/lib/json.h b/lib/json.h index 939aa410bb..e15fdd1bbf 100644 --- a/lib/json.h +++ b/lib/json.h @@ -22,6 +22,7 @@ limitations under the License. #include #include +#include "config.h" #ifdef P4C_GTEST_ENABLED #include "gtest/gtest_prod.h" #endif diff --git a/lib/source_file.h b/lib/source_file.h old mode 100755 new mode 100644 index 32627a3bb4..e6cca17efe --- a/lib/source_file.h +++ b/lib/source_file.h @@ -27,6 +27,7 @@ limitations under the License. #include #include "absl/strings/str_format.h" +#include "config.h" #include "cstring.h" #include "stringify.h" diff --git a/test/gtest/diagnostics.cpp b/test/gtest/diagnostics.cpp index b9d0d9908a..2a9a054c28 100644 --- a/test/gtest/diagnostics.cpp +++ b/test/gtest/diagnostics.cpp @@ -69,7 +69,10 @@ 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) { @@ -77,8 +80,9 @@ TEST_F(Diagnostics, P4_16_Warn) { @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) { @@ -86,8 +90,9 @@ TEST_F(Diagnostics, P4_16_Error) { @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) { @@ -95,7 +100,7 @@ TEST_F(Diagnostics, DISABLED_P4_14_Disable) { @pragma diagnostic uninitialized_use disable )")); EXPECT_TRUE(test); - EXPECT_EQ(0u, ::P4::diagnosticCount()); + EXPECT_EQ(0u, diagnosticCount()); } TEST_F(Diagnostics, DISABLED_P4_14_Warn) { @@ -103,8 +108,9 @@ TEST_F(Diagnostics, DISABLED_P4_14_Warn) { @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) { @@ -112,8 +118,9 @@ TEST_F(Diagnostics, DISABLED_P4_14_Error) { @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) { @@ -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 @@ -141,7 +149,7 @@ 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 @@ -149,12 +157,13 @@ TEST_F(Diagnostics, NestedCompileContexts) { // 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(1u, warningCount()); + EXPECT_EQ(0u, errorCount()); } TEST_F(Diagnostics, CompilerOptions) { - using CommandLineOptions = P4::IOptionPragmaParser::CommandLineOptions; + using CommandLineOptions = IOptionPragmaParser::CommandLineOptions; auto parseWithCompilerOptions = [](const CommandLineOptions &args) -> std::optional { @@ -170,23 +179,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 @@ -196,39 +207,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 @@ -243,8 +258,17 @@ 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()); +} + } // namespace P4::Test