Skip to contentSkip to conversation

extend C++ compatibility checks

PATCH Applied Bruce Richardson 4 participants 8 messages Feb 15, 2022 – Feb 22, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover extend C++ compatibility checks

This set expands upon existing checks for C++ compatibility, adding in checks for both the SDK headers and a basic check for the presence of 'extern "C"' guards in each file.

Depends-on: series-21685 ("add missing C++ guards")

Bruce Richardson (3):
ethdev: fix missing cast for C++ compatibility buildtools/chkincs: check sdk headers for C++ compatibility buildtools/chkincs: add checks for missing C++ guards

 buildtools/chkincs/meson.build | 16 +++++++++++++++-
 lib/ethdev/ethdev_pci.h        |  5 +++--
 lib/meson.build                |  1 +
 3 files changed, 19 insertions(+), 3 deletions(-)

--
2.32.0

#1 lore raw

PATCH 1/3 ethdev: fix missing cast for C++ compatibility

C++ does not allow implicit conversion to/from void*, so we need an explicit cast to allow the driver sdk header to be included from C++ code.

---
 lib/ethdev/ethdev_pci.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/ethdev/ethdev_pci.h +3 -2
index 71aa4b2e98..d2bc3fe5e0 100644
@@ -46,8 +46,9 @@ rte_eth_copy_pci_info(struct rte_eth_dev *eth_dev,
 }
 
 static inline int
-eth_dev_pci_specific_init(struct rte_eth_dev *eth_dev, void *bus_device) {
-	struct rte_pci_device *pci_dev = bus_device;
+eth_dev_pci_specific_init(struct rte_eth_dev *eth_dev, void *bus_device)
+{
+	struct rte_pci_device *pci_dev = (struct rte_pci_device *)bus_device;
 
 	if (!pci_dev)
 		return -ENODEV;
Signature · 1 line
2.32.0
#2 lore raw

Re: [PATCH 1/3] ethdev: fix missing cast for C++ compatibility

Quoted 4 lines · Bruce Richardson

C++ does not allow implicit conversion to/from void*, so we need an explicit cast to allow the driver sdk header to be included from C++ code.

I remember patches removing explicit "void *" cast, in the past,

to document, is the rule as following:
- public and sdk headers should support c++, hence these files must have explicit "void *" cast
- .c files should NOT have explicit "void *" cast

Quoted 21 lines
---
  lib/ethdev/ethdev_pci.h | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
lib/ethdev/ethdev_pci.h +3 -2
index 71aa4b2e98..d2bc3fe5e0 100644
@@ -46,8 +46,9 @@ rte_eth_copy_pci_info(struct rte_eth_dev *eth_dev,
  }
 
  static inline int
-eth_dev_pci_specific_init(struct rte_eth_dev *eth_dev, void *bus_device) {
-	struct rte_pci_device *pci_dev = bus_device;
+eth_dev_pci_specific_init(struct rte_eth_dev *eth_dev, void *bus_device)
+{
+	struct rte_pci_device *pci_dev = (struct rte_pci_device *)bus_device;
 
  	if (!pci_dev)
  		return -ENODEV;
#3 lore raw

Re: [PATCH 1/3] ethdev: fix missing cast for C++ compatibility

Quoted 6 lines · Ferruh Yigit
Quoted 4 lines · Bruce Richardson

C++ does not allow implicit conversion to/from void*, so we need an explicit cast to allow the driver sdk header to be included from C++ code.

Quoted 7 lines

I remember patches removing explicit "void *" cast, in the past,

to document, is the rule as following:
- public and sdk headers should support c++, hence these files must have explicit "void *" cast
- .c files should NOT have explicit "void *" cast

That would be my understanding yes. It's annoying they conflict, so I would suggest softening the second rule to allow casts to be present. Thankfully with chkincs support we should be able to detect violations of rule #1, which is the more important of the two.

/Bruce

#4 lore raw

PATCH 2/3 buildtools/chkincs: check sdk headers for C++ compatibility

In reply to Bruce Richardson

With a one-line change to the lib meson.build file we can add the sdk headers to the list of files to be checked using the chkincs binary. Unfortunately, many of those sdk header depend upon headers in the PCI and vdev bus drivers, so we need to update chkincs build to ensure those dependencies are added. We also need to allow internal APIs to be present in these SDK headers.

---
 buildtools/chkincs/meson.build | 6 +++++-
 lib/meson.build                | 1 +
 2 files changed, 6 insertions(+), 1 deletion(-)
buildtools/chkincs/meson.build +5 -1
index 790f700619..9442235200 100644
@@ -13,11 +13,15 @@ gen_c_files = generator(gen_c_file_for_header,
 
 cflags = machine_args
 cflags += '-DALLOW_EXPERIMENTAL_API'
+cflags += '-DALLOW_INTERNAL_API'
 
 sources = files('main.c')
 sources += gen_c_files.process(dpdk_chkinc_headers)
 
-deps = []
+# some driver sdk headers depend on these two buses, which are mandatory in build
+# so we always include them in deps list
+deps = [get_variable('shared_rte_bus_vdev'), get_variable('shared_rte_bus_pci')]
+# add the rest of the libs to the dependencies
 foreach l:enabled_libs
     deps += get_variable('shared_rte_' + l)
 endforeach
lib/meson.build +1 -0
index 8e5acd7819..24adbe44c9 100644
@@ -171,6 +171,7 @@ foreach l:libraries
         install_headers(driver_sdk_headers)
     endif
     dpdk_chkinc_headers += headers
+    dpdk_chkinc_headers += driver_sdk_headers
 
     libname = 'rte_' + name
     includes += include_directories(l)

--
2.32.0

#5 lore raw

PATCH 3/3 buildtools/chkincs: add checks for missing C++ guards

In reply to Bruce Richardson

Simply compiling a C header with a C++ compiler is not enough to flag missing 'extern "C"' guards. To catch missing guards, we can just use a simple grep for the 'extern "C"' part, and error out if any files have a miss.

---

Depends-on: series-21685 ("add missing C++ guards")

---
 buildtools/chkincs/meson.build | 10 ++++++++++
 1 file changed, 10 insertions(+)
buildtools/chkincs/meson.build +10 -0
index 9442235200..77ef59fb13 100644
@@ -37,6 +37,16 @@ if not add_languages('cpp', required: false)
     subdir_done()
 endif
 
+# check for extern C in files, since this is not detected as an error by the compiler
+grep = find_program('grep', required: false)
+if grep.found()
+    errlist = run_command([grep, '--files-without-match', '^extern "C"', dpdk_chkinc_headers],
+            check: false, capture: true).stdout().split()
+    if errlist != []
+        error('Files missing C++ \'extern "C"\' guards:\n- ' + '\n- '.join(errlist))
+    endif
+endif
+
 gen_cpp_files = generator(gen_c_file_for_header,
         output: '@[email protected]',
         arguments: ['@INPUT@', '@OUTPUT@'])

--
2.32.0

#7 lore raw

17/02/2022 09:06, Tyler Retzlaff:

Quoted 8 lines · Bruce Richardson
Quoted 5 lines

This set expands upon existing checks for C++ compatibility, adding in checks for both the SDK headers and a basic check for the presence of 'extern "C"' guards in each file.

Depends-on: series-21685 ("add missing C++ guards")

Applied, thanks.