]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
checksrc: ignore preprocessor lines
authorDaniel Stenberg <daniel@haxx.se>
Sat, 16 Oct 2021 11:50:25 +0000 (13:50 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Sun, 17 Oct 2021 09:19:49 +0000 (11:19 +0200)
In order to check the actual code better, checksrc now ignores
everything that look like preprocessor instructions. It also means
that code in macros are now longer checked.

Note that some rules then still don't need to be followed when code is
exactly below a cpp instruction.

Removes two checksrc exceptions we needed previously because of
preprocessor lines being checked.

Reported-by: Marcel Raad
Fixes #7863
Closes #7864

lib/checksrc.pl
tests/libtest/lib557.c
tests/libtest/test.h

index ac7e3847e7056538db0eca7ba24d570d9489933d..bb3495f792bfc2d0f874d7f0436ea7ad6b6cb96b 100755 (executable)
@@ -370,7 +370,10 @@ sub scanfile {
 
     my $line = 1;
     my $prevl="";
-    my $l;
+    my $prevpl="";
+    my $l = "";
+    my $prep = 0;
+    my $prevp = 0;
     open(R, "<$file") || die "failed to open $file";
 
     my $incomment=0;
@@ -452,6 +455,14 @@ sub scanfile {
         # comments
         # ------------------------------------------------------------
 
+        # prev line was a preprocessor **and** ended with a backslash
+        if($prep && ($prevpl =~ /\\ *\z/)) {
+            # this is still a preprocessor line
+            $prep = 1;
+            goto preproc;
+        }
+        $prep = 0;
+
         # crude attempt to detect // comments without too many false
         # positives
         if($l =~ /^(([^"\*]*)[^:"]|)\/\//) {
@@ -459,6 +470,13 @@ sub scanfile {
                       $line, length($1), $file, $l, "\/\/ comment");
         }
 
+        # detect and strip preprocessor directives
+        if($l =~ /^[ \t]*\#/) {
+            # preprocessor line
+            $prep = 1;
+            goto preproc;
+        }
+
         my $nostr = nostrings($l);
         # check spaces after for/if/while/function call
         if($nostr =~ /^(.*)(for|if|while| ([a-zA-Z0-9_]+)) \((.)/) {
@@ -676,10 +694,9 @@ sub scanfile {
             }
         }
 
-        # check for open brace first on line but not first column
-        # only alert if previous line ended with a close paren and wasn't a cpp
-        # line
-        if((($prevl =~ /\)\z/) && ($prevl !~ /^ *#/)) && ($l =~ /^( +)\{/)) {
+        # check for open brace first on line but not first column only alert
+        # if previous line ended with a close paren and it wasn't a cpp line
+        if(($prevl =~ /\)\z/) && ($l =~ /^( +)\{/) && !$prevp) {
             checkwarn("BRACEPOS",
                       $line, length($1), $file, $ol, "badly placed open brace");
         }
@@ -687,11 +704,11 @@ sub scanfile {
         # if the previous line starts with if/while/for AND ends with an open
         # brace, or an else statement, check that this line is indented $indent
         # more steps, if not a cpp line
-        if($prevl =~ /^( *)((if|while|for)\(.*\{|else)\z/) {
+        if(!$prevp && ($prevl =~ /^( *)((if|while|for)\(.*\{|else)\z/)) {
             my $first = length($1);
 
             # this line has some character besides spaces
-            if(($l !~ /^ *#/) && ($l =~ /^( *)[^ ]/)) {
+            if($l =~ /^( *)[^ ]/) {
                 my $second = length($1);
                 my $expect = $first+$indent;
                 if($expect != $second) {
@@ -786,9 +803,11 @@ sub scanfile {
             print STDERR "L: $l\n";
             print STDERR "nostr: $nostr\n";
         }
-
+      preproc:
         $line++;
-        $prevl = $ol;
+        $prevp = $prep;
+        $prevl = $ol if(!$prep);
+        $prevpl = $ol if($prep);
     }
 
     if(!scalar(@copyright)) {
index b6b8bec1fb278c28cc71803aa80b5d9c75c9597f..c17fab276294f455b2f657e373c3fcd9fd61b868 100644 (file)
@@ -1556,7 +1556,6 @@ static int test_weird_arguments(void)
 }
 
 /* DBL_MAX value from Linux */
-/* !checksrc! disable PLUSNOSPACE 1 */
 #define MAXIMIZE -1.7976931348623157081452E+308
 
 static int test_float_formatting(void)
index ffa78d00df39cd2f5ae3bf5a0d134fa05f83cff7..e3bad1748ac65666c921b62e847713b3cc9aa89c 100644 (file)
@@ -5,7 +5,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
@@ -20,8 +20,6 @@
  *
  ***************************************************************************/
 
-/* !checksrc! disable ASSIGNWITHINCONDITION 14 */
-
 /* Now include the curl_setup.h file from libcurl's private libdir (the source
    version, but that might include "curl_config.h" from the build dir so we
    need both of them in the include path), so that we get good in-depth