[cfe-dev] Learning how to use and deploy 'scan-build'

Martin J. O'Riordan via cfe-dev cfe-dev at lists.llvm.org
Thu Nov 17 08:37:54 PST 2016


Thanks Anna,

 

I have it all working really well now, but I want to make sure that I have not broken anything else, so I will wait until I have tried some other scenarios before proposing a general patch; one case that occurred to me is when ‘clang.exe’ is built with the Cygwin or MINGW version of GCC my patch might break them, or at the very least result in warnings about the path conventions.  I think I can make this more adaptive by examining the output from the ‘-###’ invocation and make the ‘cygpath’ trick automatic and appropriate to the actual compiler.

 

The feedback I got from you all on this has my build working very nicely now :)

 

            MartinO

 

From: ganna at apple.com [mailto:ganna at apple.com] 
Sent: 14 November 2016 19:42
To: Martin.ORiordan at Movidius.com
Cc: cfe-dev; Aleksei Sidorin; Laszlo Nagy
Subject: Re: [cfe-dev] Learning how to use and deploy 'scan-build'

 

Great to hear that you found a solution!

 

Feel free to submit a patch; it might help others with the same setup:

http://llvm.org/docs/Phabricator.html

http://llvm.org/docs/DeveloperPolicy.html

 

Anna.

On Nov 13, 2016, at 2:55 AM, Martin J. O'Riordan <Martin.ORiordan at movidius.com <mailto:Martin.ORiordan at movidius.com> > wrote:

 

Following up on this, I have made a change to my local copy of ‘ccc-analyzer’ that compensates for the Windows vs Cygwin path differences.  In the subroutine ‘sub Analyze’ I have changed the lines:

 

    # Create arguments for doing static analysis.

    if (defined $ResultFile) {

      push @Args, '-o', $ResultFile;

    }

    elsif (defined $HtmlDir) {

      push @Args, '-o', $HtmlDir;

    }

to:

    # Create arguments for doing static analysis.

    if (defined $ResultFile) {

      push @Args, '-o', $ResultFile;

    }

    elsif (defined $HtmlDir) {

+     # Convert the output path to the Windows form on Cygwin

+     if ($^O =~/cygwin/) {

+       my $winHtmlDir = `cygpath -m $HtmlDir`;

+       $winHtmlDir =~ tr/\n\r//d;  # Strip newlines

+       push @Args, '-o', $winHtmlDir;

+     } else {

        push @Args, '-o', $HtmlDir;

+     }

    }

 

I have used ‘tr’ rather than ‘chomp’ because I have found that ‘chomp’ does not always remove CRs and is not as robust as using this ‘tr’ pattern.  Also, I use the ‘-m’ option to ‘cygpath’ rather than ‘-w’ because it uses the ‘/’ character for directory path separators rather than ‘\’.  The latter causes all sorts of trouble in Perl, Python, Bash and so on; while Windows applications are perfectly happy with the ‘/’ character (WIN32’s file open functions accept either).

 

All the best and thanks to each of you for your advice and help,

 

            MartinO

 

From: Martin J. O'Riordan [ <mailto:martin.oriordan at movidius.com> mailto:martin.oriordan at movidius.com] 
Sent: 12 November 2016 21:48
To: ' <mailto:ganna at apple.com> ganna at apple.com' < <mailto:ganna at apple.com> ganna at apple.com>
Cc: 'Aleksei Sidorin' < <mailto:a.sidorin at samsung.com> a.sidorin at samsung.com>; 'cfe-dev' < <mailto:cfe-dev at lists.llvm.org> cfe-dev at lists.llvm.org>
Subject: RE: [cfe-dev] Learning how to use and deploy 'scan-build'

 

Hi again.  I have solved the problem that I was having but it is obscure so I will explain here.

 

Turns out that the problem I am having is a consequence of Windows versus Cygwin paths.  Running the same scenario on Linux is fine (after I had an example with something worth reporting).

 

In summary the issue is the path that is passed using ‘-o’ to the Visual Studio built ‘clang.exe’ which only understands Windows paths, but at line #1490 in ‘scan-build’ the path is constructed using:

 

$Options{OutputDir} = abs_path($OutDir);

 

In my case the path for ‘-o check’ becomes:

 

/src/tests/ScanBuild/check

 

But in my Cygwin setup, ‘/src/’ is actually a ‘mount’ of the directory ‘S:\Projects\’ - I use mounts so that my scripts are portable across Linux and Windows systems.  I tend to live in the ‘bash’ shell on Windows and Linux.  If I was passing the path to ‘clang.exe’ from a Makefile, I would typically use ‘cygpath -m’ to get the Windows path, but because this is happening in the generic ‘scan-build’ Perl wrapper, it passes the Linux/Cygwin version of the path, but to the VS built ‘clang.exe’.

 

So the data for the reports “is” being created by ‘clang’, but it is being placed at:

 

D:\src\tests\ScanBuild\check

 

which is not the same place.  The when ‘scan-build’ tries to collate the data and generate the report, the directory Perl looks in is empty.  I can fix this, I just need to swot up my OS Perl knowledge and I should be able to fix it.  I may need to do something similar in ‘ccc-analyzer’.  I will feedback any changes that may be useful to others once I have it figured out.

 

Thanks for your help, I would never have found this if you hadn’t given me good pointers to prove that I could generate the PList files.

 

            MartinO

 

From: Martin J. O'Riordan [ <mailto:martin.oriordan at movidius.com> mailto:martin.oriordan at movidius.com] 
Sent: 12 November 2016 20:36
To: ' <mailto:ganna at apple.com> ganna at apple.com' < <mailto:ganna at apple.com> ganna at apple.com>
Cc: 'Aleksei Sidorin' < <mailto:a.sidorin at samsung.com> a.sidorin at samsung.com>; 'cfe-dev' < <mailto:cfe-dev at lists.llvm.org> cfe-dev at lists.llvm.org>
Subject: RE: [cfe-dev] Learning how to use and deploy 'scan-build'

 

Thanks Anna,

 

Actually there was a typo in my message and I actually had the identifier ‘zero’ not the constant ‘0’:

int main () {

  int zero = 0;

  return 5 / zero;

}

but I have changed this to just contain:

int foo () {

  int x = 0;

  return 5/x;

}

 

Compiling this as:

clang++ --analyze testScanBuild.cpp

does report the warning, and creates the file ‘testScanBuild.plist’ which on inspection appears to contain the expected diagnostic information, and compiling it as:

clang++ -c testScanBuild.cpp

does not produce a warning  (no ‘-Wall’ option).

 

I just changed my Makefile to simply:

build:  testScanBuild.o

 

%o.%cpp:

    $(CXX) -c $<

and:

scan-build --use-cc clang --use-cxx clang++ -v -v -v -o check --keep-empty make build

 

I had not realised that the analyzers would not produce a report if the compiler already warned.

 

Thanks again,

 

            MartinO

 

From:  <mailto:ganna at apple.com> ganna at apple.com [ <mailto:ganna at apple.com> mailto:ganna at apple.com] 
Sent: 12 November 2016 19:54
To:  <mailto:Martin.ORiordan at Movidius.com> Martin.ORiordan at Movidius.com
Cc: Aleksei Sidorin < <mailto:a.sidorin at samsung.com> a.sidorin at samsung.com>; cfe-dev < <mailto:cfe-dev at lists.llvm.org> cfe-dev at lists.llvm.org>
Subject: Re: [cfe-dev] Learning how to use and deploy 'scan-build'

 

Please, use the examples I provided in the previous email. The analyzer tries to avoid reporting the issues that can be found by the compiler.

 

Annas-MBP-3:compiler-rt anna$ clang --analyze ~/tmp/ex.c

/Users/anna/tmp/ex.c:6:11: warning: Division by zero

  return 5/x + 5/0;

         ~^~

1 warning generated.

Annas-MBP-3:compiler-rt anna$ clang -fsyntax-only ~/tmp/ex.c

/Users/anna/tmp/ex.c:6:17: warning: division by zero is undefined [-Wdivision-by-zero]

  return 5/x + 5/0;

                ^~

1 warning generated.

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20161117/ad63141e/attachment.html>


More information about the cfe-dev mailing list