Skip to content

Commit

Permalink
[scan-build-py] move function report_directory from report module to …
Browse files Browse the repository at this point in the history
…analyze module

Differential Revision: https://reviews.llvm.org/D29255

llvm-svn: 295045
  • Loading branch information
Laszlo Nagy committed Feb 14, 2017
1 parent 93a8e9d commit 258ff25
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 54 deletions.
38 changes: 37 additions & 1 deletion clang/tools/scan-build-py/libscanbuild/analyze.py
Expand Up @@ -18,13 +18,16 @@
import json
import argparse
import logging
import tempfile
import subprocess
import multiprocessing
import contextlib
import datetime
from libscanbuild import initialize_logging, tempdir, command_entry_point, \
run_build
from libscanbuild.runner import run
from libscanbuild.intercept import capture
from libscanbuild.report import report_directory, document
from libscanbuild.report import document
from libscanbuild.clang import get_checkers
from libscanbuild.compilation import split_command

Expand Down Expand Up @@ -190,6 +193,39 @@ def analyze_build_wrapper(cplusplus):
return result


@contextlib.contextmanager
def report_directory(hint, keep):
""" Responsible for the report directory.
hint -- could specify the parent directory of the output directory.
keep -- a boolean value to keep or delete the empty report directory. """

stamp_format = 'scan-build-%Y-%m-%d-%H-%M-%S-%f-'
stamp = datetime.datetime.now().strftime(stamp_format)
parent_dir = os.path.abspath(hint)
if not os.path.exists(parent_dir):
os.makedirs(parent_dir)
name = tempfile.mkdtemp(prefix=stamp, dir=parent_dir)

logging.info('Report directory created: %s', name)

try:
yield name
finally:
if os.listdir(name):
msg = "Run 'scan-view %s' to examine bug reports."
keep = True
else:
if keep:
msg = "Report directory '%s' contains no report, but kept."
else:
msg = "Removing directory '%s' because it contains no report."
logging.warning(msg, name)

if not keep:
os.rmdir(name)


def analyzer_params(args):
""" A group of command line arguments can mapped to command
line arguments of the analyzer. This method generates those. """
Expand Down
41 changes: 1 addition & 40 deletions clang/tools/scan-build-py/libscanbuild/report.py
Expand Up @@ -13,54 +13,15 @@
import os.path
import sys
import shutil
import time
import tempfile
import itertools
import plistlib
import glob
import json
import logging
import contextlib
import datetime
from libscanbuild import duplicate_check
from libscanbuild.clang import get_version

__all__ = ['report_directory', 'document']


@contextlib.contextmanager
def report_directory(hint, keep):
""" Responsible for the report directory.
hint -- could specify the parent directory of the output directory.
keep -- a boolean value to keep or delete the empty report directory. """

stamp_format = 'scan-build-%Y-%m-%d-%H-%M-%S-%f-'
stamp = datetime.datetime.now().strftime(stamp_format)

parentdir = os.path.abspath(hint)
if not os.path.exists(parentdir):
os.makedirs(parentdir)

name = tempfile.mkdtemp(prefix=stamp, dir=parentdir)

logging.info('Report directory created: %s', name)

try:
yield name
finally:
if os.listdir(name):
msg = "Run 'scan-view %s' to examine bug reports."
keep = True
else:
if keep:
msg = "Report directory '%s' contans no report, but kept."
else:
msg = "Removing directory '%s' because it contains no report."
logging.warning(msg, name)

if not keep:
os.rmdir(name)
__all__ = ['document']


def document(args, output_dir, use_cdb):
Expand Down
15 changes: 15 additions & 0 deletions clang/tools/scan-build-py/tests/unit/test_analyze.py
Expand Up @@ -4,4 +4,19 @@
# This file is distributed under the University of Illinois Open Source
# License. See LICENSE.TXT for details.

import libear
import libscanbuild.analyze as sut
import unittest

class ReportDirectoryTest(unittest.TestCase):

# Test that successive report directory names ascend in lexicographic
# order. This is required so that report directories from two runs of
# scan-build can be easily matched up to compare results.
def test_directory_name_comparison(self):
with libear.TemporaryDirectory() as tmpdir, \
sut.report_directory(tmpdir, False) as report_dir1, \
sut.report_directory(tmpdir, False) as report_dir2, \
sut.report_directory(tmpdir, False) as report_dir3:
self.assertLess(report_dir1, report_dir2)
self.assertLess(report_dir2, report_dir3)
13 changes: 0 additions & 13 deletions clang/tools/scan-build-py/tests/unit/test_report.py
Expand Up @@ -146,16 +146,3 @@ def test_with_single_file(self):
def test_empty(self):
self.assertEqual(
sut.commonprefix([]), '')

class ReportDirectoryTest(unittest.TestCase):

# Test that successive report directory names ascend in lexicographic
# order. This is required so that report directories from two runs of
# scan-build can be easily matched up to compare results.
def test_directory_name_comparison(self):
with libear.TemporaryDirectory() as tmpdir, \
sut.report_directory(tmpdir, False) as report_dir1, \
sut.report_directory(tmpdir, False) as report_dir2, \
sut.report_directory(tmpdir, False) as report_dir3:
self.assertLess(report_dir1, report_dir2)
self.assertLess(report_dir2, report_dir3)

0 comments on commit 258ff25

Please sign in to comment.