Skip to content

Commit

Permalink
[NFC] Eliminate the "W" form of clang-tidy warnings
Browse files Browse the repository at this point in the history
Earlier "clang-diagnostic-..." checkers could have been provided with
their original "W" warning flag form. For example Wformat vs.
clang-diagnostic-format. The "W" form is deprecated for a while and the
late CodeChecker versions emit a hard error when using them.
For this reason there is no need to distinguish "W" form as a warning
anymore.
  • Loading branch information
bruntib committed Jan 27, 2025
1 parent 66efe25 commit 15cf9df
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 71 deletions.
65 changes: 25 additions & 40 deletions analyzer/codechecker_analyzer/analyzers/clangtidy/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
from codechecker_analyzer.analyzers.clangsa.analyzer import ClangSA

from .. import analyzer_base
from ..config_handler import CheckerState, CheckerType, \
get_compiler_warning_name_and_type
from ..config_handler import CheckerState
from ..flag import has_flag
from ..flag import prepend_all

Expand Down Expand Up @@ -247,6 +246,15 @@ class ClangTidy(analyzer_base.SourceAnalyzer):

ANALYZER_NAME = 'clang-tidy'

# Compiler Warnings that we don't want to expose on the interface as a
# valid checker name.
__warning_blacklist = {
# It is a parameterized checker and there is no infrastructure to
# provide a parameter for a checker.
'frame-larger-than',
'frame-larger-than='
}

# Cache object for get_analyzer_checkers().
__analyzer_checkers = None

Expand Down Expand Up @@ -304,7 +312,8 @@ def get_analyzer_checkers(cls):

checker_description.extend(
("clang-diagnostic-" + warning, "")
for warning in get_warnings())
for warning in get_warnings()
if warning not in cls.__warning_blacklist)

checker_description.append(("clang-diagnostic-error",
"Indicates compiler errors."))
Expand Down Expand Up @@ -393,48 +402,24 @@ def get_checker_list(self, config) -> Tuple[List[str], List[str]]:
for checker_name, value in config.checks().items():
state, _ = value

warning_name, warning_type = \
get_compiler_warning_name_and_type(checker_name)

# This warning must be given a parameter separated by either '=' or
# space. This warning is not supported as a checker name so its
# special usage is avoided.
if warning_name and warning_name.startswith('frame-larger-than'):
continue

if warning_name is not None:
# -W and clang-diagnostic- are added as compiler warnings.
if warning_type == CheckerType.COMPILER:
LOG.warning("As of CodeChecker v6.22, the following usage"
f"of '{checker_name}' compiler warning as a "
"checker name is deprecated, please use "
f"'clang-diagnostic-{checker_name[1:]}' "
"instead.")
if state == CheckerState.ENABLED:
compiler_warnings.append('-W' + warning_name)
enabled_checkers.append(checker_name)
elif state == CheckerState.DISABLED:
if config.enable_all:
LOG.warning("Disabling compiler warning with "
f"compiler flag '-d W{warning_name}' "
"is not supported.")
if checker_name.startswith('clang-diagnostic-'):
# If a clang-diagnostic-... is enabled add it as a compiler
# warning as -W..., if it is disabled, tidy can suppress when
# specified in the -checks parameter list, so we add it there
# as -clang-diagnostic-... .
elif warning_type == CheckerType.ANALYZER:
if state == CheckerState.ENABLED:
if checker_name == "clang-diagnostic-error":
# Disable warning of clang-diagnostic-error to
# avoid generated compiler errors.
compiler_warnings.append('-Wno-' + warning_name)
else:
compiler_warnings.append('-W' + warning_name)
enabled_checkers.append(checker_name)
else:
compiler_warnings.append('-Wno-' + warning_name)

continue
# TODO: str.removeprefix() available in Python 3.9
warning_name = checker_name[len('clang-diagnostic-'):]

if state == CheckerState.ENABLED:
if checker_name == 'clang-diagnostic-error':
# Disable warning of clang-diagnostic-error to
# avoid generated compiler errors.
compiler_warnings.append('-Wno-' + warning_name)
else:
compiler_warnings.append('-W' + warning_name)
else:
compiler_warnings.append('-Wno-' + warning_name)

if state == CheckerState.ENABLED:
enabled_checkers.append(checker_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@

from codechecker_common.logger import get_logger

from ..config_handler import AnalyzerConfigHandler, CheckerState, \
get_compiler_warning_name_and_type


def is_compiler_warning(checker_name):
name, _ = get_compiler_warning_name_and_type(checker_name)
return name is not None
from ..config_handler import AnalyzerConfigHandler, CheckerState


LOG = get_logger('analyzer.tidy')
Expand All @@ -37,7 +31,7 @@ def add_checker(self, checker_name, description='',
"""
if self.analyzer_config and \
self.analyzer_config.get('take-config-from-directory') == 'true':
if is_compiler_warning(checker_name):
if checker_name.startswith('clang-diagnostic-'):
return

super().add_checker(checker_name, description, state)
19 changes: 0 additions & 19 deletions analyzer/codechecker_analyzer/analyzers/config_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,6 @@ class CheckerType(Enum):
COMPILER = 1 # A checker which specified as "-W<name>" or "-Wno-<name>".


def get_compiler_warning_name_and_type(checker_name):
"""
Removes 'W' or 'Wno' from the compiler warning name, if this is a
compiler warning and returns the name and CheckerType.compiler.
If it is a clang-diagnostic-<name> warning then it returns the name
and CheckerType.analyzer.
Otherwise returns None and CheckerType.analyzer.
"""
# Checker name is a compiler warning.
if checker_name.startswith('W'):
name = checker_name[4:] if \
checker_name.startswith('Wno-') else checker_name[1:]
return name, CheckerType.COMPILER
elif checker_name.startswith('clang-diagnostic-'):
return checker_name[17:], CheckerType.ANALYZER
else:
return None, CheckerType.ANALYZER


class AnalyzerConfigHandler(metaclass=ABCMeta):
"""
Handle the checker configurations and enabled disabled checkers lists.
Expand Down
5 changes: 1 addition & 4 deletions analyzer/tests/unit/test_checker_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from codechecker_analyzer.analyzers.cppcheck.analyzer import Cppcheck
from codechecker_analyzer.analyzers.config_handler import CheckerState
from codechecker_analyzer.analyzers.clangtidy.config_handler \
import is_compiler_warning, ClangTidyConfigHandler
import ClangTidyConfigHandler
from codechecker_analyzer.arg import AnalyzerConfig, CheckerConfig
from codechecker_analyzer.cmd.analyze import \
is_analyzer_config_valid, is_checker_config_valid
Expand Down Expand Up @@ -643,9 +643,6 @@ def test_disable_clangsa_checkers(self):
'clang-analyzer',
analyzer.construct_analyzer_cmd(result_handler)))

self.assertTrue(is_compiler_warning('Wreserved-id-macro'))
self.assertFalse(is_compiler_warning('hicpp'))

analyzer = create_analyzer_tidy(['--enable', 'Wreserved-id-macro'])
result_handler = create_result_handler(analyzer)

Expand Down

0 comments on commit 15cf9df

Please sign in to comment.