[libvirt] [PATCH v2] syntax check: update header guard check

Jonathon Jongsma posted 1 patch 4 years, 9 months ago
Test syntax-check passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20190619194836.29799-1-jjongsma@redhat.com
build-aux/header-ifdef.pl | 41 +++++++++++++++++++++++++++++----------
1 file changed, 31 insertions(+), 10 deletions(-)
[libvirt] [PATCH v2] syntax check: update header guard check
Posted by Jonathon Jongsma 4 years, 9 months ago
Internal headers should use #pragma once instead of the standard #ifndef
guard. Public headers still require the existing header guard.

Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com>
---

Changes in v2:
 - fix error messages when no header guard is found (#ifndef for public headers, #pragma for
   internal headers)
 - fix test for identifying public headers (omit initial '/')
 - fix code spacing

 build-aux/header-ifdef.pl | 41 +++++++++++++++++++++++++++++----------
 1 file changed, 31 insertions(+), 10 deletions(-)

diff --git a/build-aux/header-ifdef.pl b/build-aux/header-ifdef.pl
index 401f25fb81..dba3dbcbdc 100644
--- a/build-aux/header-ifdef.pl
+++ b/build-aux/header-ifdef.pl
@@ -6,18 +6,26 @@
 #  ...copyright header...
 #  */
 # <one blank line>
-# #ifndef SYMBOL
-# # define SYMBOL
+# #pragma once
 # ....content....
-# #endif /* SYMBOL */
 #
-# For any file ending priv.h, before the #ifndef
+#---
+#
+# For any file ending priv.h, before the #pragma once
 # We will have a further section
 #
 # #ifndef SYMBOL_ALLOW
 # # error ....
 # #endif /* SYMBOL_ALLOW */
 # <one blank line>
+#
+#---
+#
+# For public headers (files in include/), use the standard header guard instead of #pragma once:
+# #ifndef SYMBOL
+# # define SYMBOL
+# ....content....
+# #endif /* SYMBOL */
 
 use strict;
 use warnings;
@@ -38,6 +46,7 @@ my $file = " ";
 my $ret = 0;
 my $ifdef = "";
 my $ifdefpriv = "";
+my $publicheader = 0;
 
 my $state = $STATE_EOF;
 my $mistake = 0;
@@ -64,7 +73,11 @@ while (<>) {
         } elsif ($state == $STATE_PRIV_BLANK) {
             &mistake("$file: missing blank line after priv header check");
         } elsif ($state == $STATE_GUARD_START) {
-            &mistake("$file: missing '#ifndef $ifdef'");
+            if ($publicheader) {
+                &mistake("$file: missing '#ifndef $ifdef'");
+            } else {
+                &mistake("$file: missing '#pragma once' header guard");
+            }
         } elsif ($state == $STATE_GUARD_DEFINE) {
             &mistake("$file: missing '# define $ifdef'");
         } elsif ($state == $STATE_GUARD_END) {
@@ -83,6 +96,7 @@ while (<>) {
         $file = $ARGV;
         $state = $STATE_COPYRIGHT_COMMENT;
         $mistake = 0;
+        $publicheader = ($ARGV =~ /include\//);
     }
 
     if ($mistake ||
@@ -133,12 +147,19 @@ while (<>) {
     } elsif ($state == $STATE_GUARD_START) {
         if (/^$/) {
             &mistake("$file: too many blank lines after copyright header");
-        } elsif(/#pragma once/) {
-            $state = $STATE_PRAGMA;
-        } elsif (/#ifndef $ifdef$/) {
-            $state = $STATE_GUARD_DEFINE;
+        }
+        if ($publicheader) {
+            if (/#ifndef $ifdef$/) {
+                $state = $STATE_GUARD_DEFINE;
+            } else {
+                &mistake("$file: missing '#ifndef $ifdef'");
+            }
         } else {
-            &mistake("$file: missing '#ifndef $ifdef'");
+            if (/#pragma once/) {
+                $state = $STATE_PRAGMA;
+            } else {
+                &mistake("$file: missing '#pragma once' header guard");
+            }
         }
     } elsif ($state == $STATE_GUARD_DEFINE) {
         if (/# define $ifdef$/) {
-- 
2.20.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v2] syntax check: update header guard check
Posted by Ján Tomko 4 years, 9 months ago
On Wed, Jun 19, 2019 at 02:48:36PM -0500, Jonathon Jongsma wrote:
>Internal headers should use #pragma once instead of the standard #ifndef
>guard. Public headers still require the existing header guard.
>
>Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com>
>---
>
>Changes in v2:
> - fix error messages when no header guard is found (#ifndef for public headers, #pragma for
>   internal headers)
> - fix test for identifying public headers (omit initial '/')
> - fix code spacing
>
> build-aux/header-ifdef.pl | 41 +++++++++++++++++++++++++++++----------
> 1 file changed, 31 insertions(+), 10 deletions(-)

Reviewed-by: Ján Tomko <jtomko@redhat.com>

Jano
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list