-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[clang] Introduce diagnostics suppression mappings #112517
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
80aab8c
14b5aa1
c3dc089
fa55619
f57a293
ae0ff15
d790e84
f7c033c
a423312
2da8e43
cad42ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,6 +151,10 @@ Options to Control Error and Warning Messages | |
instantiation backtrace for a single warning or error. The default is 10, and | ||
the limit can be disabled with `-ftemplate-backtrace-limit=0`. | ||
|
||
.. option:: --warning-suppression-mappings=foo.txt | ||
|
||
:ref:`Suppress certain diagnostics for certain files. <warning_suppression_mappings>` | ||
|
||
.. _cl_diag_formatting: | ||
|
||
Formatting of Diagnostics | ||
|
@@ -1315,6 +1319,34 @@ with its corresponding `Wno-` option. | |
Note that when combined with :option:`-w` (which disables all warnings), | ||
disabling all warnings wins. | ||
|
||
.. _warning_suppression_mappings: | ||
|
||
Controlling Diagnostics via Suppression Mappings | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
Warning suppression mappings enable users to suppress Clang's diagnostics in a | ||
per-file granular manner. Enabling enforcement of diagnostics in specific parts | ||
of the project, even if there are violations in some headers. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove comma: project even if there There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But this covers headers even inside the project (i.e. the incremental cleanup of a new warning). So I feel like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's true, but I think we want to guide users towards fixing diagnostics rather than suppressing them. Users will use any suppression mechanisms we provide regardless of how we document it, so I don't think it's harmful to imply this is more about cases where you can't modify the code. But I also don't feel strongly. WDYT? |
||
|
||
.. code-block:: console | ||
|
||
$ cat mappings.txt | ||
[unused] | ||
src:foo/* | ||
|
||
$ clang --warning-suppression-mappings=mapping.txt -Wunused foo/bar.cc | ||
kadircet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# This compilation won't emit any unused findings for sources under foo/ | ||
# directory. But it'll still complain for all the other sources, e.g: | ||
$ cat foo/bar.cc | ||
#include "dir/include.h" // Clang flags unused declarations here. | ||
#include "foo/include.h" // but unused warnings under this source is omitted. | ||
#include "next_to_bar_cc.h" // as are unused warnings from this header file. | ||
// Further, unused warnings in the remainder of bar.cc are also omitted. | ||
|
||
|
||
See :doc:`WarningSuppressionMappings` for details about the file format and | ||
functionality. | ||
|
||
Controlling Static Analyzer Diagnostics | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
|
kadircet marked this conversation as resolved.
Show resolved
Hide resolved
kadircet marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
============================ | ||
Warning suppression mappings | ||
============================ | ||
|
||
.. contents:: | ||
:local: | ||
|
||
Introduction | ||
============ | ||
|
||
Warning suppression mappings enable users to suppress Clang's diagnostics in a | ||
per-file granular manner. Enabling enforcement of diagnostics in specific parts | ||
of the project, even if there are violations in some headers. | ||
kadircet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Goal and usage | ||
============== | ||
|
||
Clang allows diagnostics to be configured at a translation-unit granularity. | ||
If a ``foo.cpp`` is compiled with ``-Wfoo``, all transitively included headers | ||
also need to be clean. Hence turning on new warnings in large codebases requires | ||
kadircet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cleaning up all the existing warnings. This might not be possible when some | ||
dependencies aren't in the project owner's control or because new violations are | ||
creeping up quicker than the clean up. | ||
|
||
Warning suppression mappings aim to alleviate some of these concerns by making | ||
diagnostic configuration granularity finer, at a source file level. | ||
|
||
To achieve this, user can create a file that lists which :doc:`diagnostic | ||
groups <DiagnosticsReference>` to suppress in which files or paths, and pass it | ||
as a command line argument to Clang with the ``--warning-suppression-mappings`` | ||
flag. | ||
|
||
Note that this mechanism won't enable any diagnostics on its own. Users should | ||
still turn on warnings in their compilations with explicit ``-Wfoo`` flags. | ||
`Controlling diagnostics pragmas | ||
<https://clang.llvm.org/docs/UsersManual.html#controlling-diagnostics-via-pragmas>`_ | ||
take precedence over suppression mappings. Ensuring code author's explicit | ||
intent is always preserved. | ||
|
||
Example | ||
======= | ||
|
||
.. code-block:: bash | ||
|
||
$ cat my/user/code.cpp | ||
#include <foo/bar.h> | ||
namespace { void unused_func1(); } | ||
|
||
$ cat foo/bar.h | ||
namespace { void unused_func2(); } | ||
|
||
$ cat suppression_mappings.txt | ||
# Suppress -Wunused warnings in all files, apart from the ones under `foo/`. | ||
[unused] | ||
src:* | ||
src:*foo/*=emit | ||
$ clang -Wunused --warning-suppression-mappings=suppression_mappings.txt my/user/code.cpp | ||
# prints warning: unused function 'unused_func2', but no warnings for `unused_func1`. | ||
|
||
Format | ||
====== | ||
|
||
Warning suppression mappings uses the same format as | ||
:doc:`SanitizerSpecialCaseList`. | ||
|
||
Sections describe which diagnostic group's behaviour to change, e.g. | ||
``[unused]``. When a diagnostic is matched by multiple sections, the latest | ||
section takes precedence. | ||
|
||
Afterwards in each section, users can have multiple entities that match source | ||
files based on the globs. These entities look like ``src:*/my/dir/*``. | ||
Users can also use the ``emit`` category to exclude a subdirectory from | ||
kadircet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
suppression. | ||
Source files are matched against these globs either: | ||
|
||
- as paths relative to the current working directory | ||
- as absolute paths. | ||
|
||
When a source file matches multiple globs in a section, the longest one takes | ||
precedence. | ||
|
||
.. code-block:: bash | ||
|
||
# Lines starting with # are ignored. | ||
# Configure suppression globs for `-Wunused` warnings | ||
[unused] | ||
# Suppress on all files by default. | ||
src:* | ||
# But enforce for all the sources under foo/. | ||
src:*foo/*=emit | ||
|
||
# unused-function warnings are a subgroup of `-Wunused`. So this section | ||
# takes precedence over the previous one for unused-function warnings, but | ||
# not for unused-variable warnings. | ||
[unused-function] | ||
# Only suppress for sources under bar/. | ||
src:*bar/* |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9017,3 +9017,8 @@ def wasm_opt : Flag<["--"], "wasm-opt">, | |
Group<m_Group>, | ||
HelpText<"Enable the wasm-opt optimizer (default)">, | ||
MarshallingInfoNegativeFlag<LangOpts<"NoWasmOpt">>; | ||
|
||
def warning_suppression_mappings_EQ : Joined<["--"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should move elsewhere in Options.td, it's currently listed under Also, should this be exposed in clang-cl as well? What about flang? CC @zmodem @rnk @MaskRay @jansvoboda11 for opinions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Oh didn't notice we actually had "lexical" sections in the doc (AFAICT it doesn't have any semantic affect afterwards, flag is still only visible for cc1 and clang-driver). Moved it near other warning options.
Going to carry this discussion into RFC to make sure it doesn't get lost. https://discourse.llvm.org/t/rfc-add-support-for-controlling-diagnostics-severities-at-file-level-granularity-through-command-line/81292/21?u=kadircet |
||
"warning-suppression-mappings=">, Group<Diag_Group>, | ||
HelpText<"File containing diagnostic suppresion mappings. See user manual " | ||
"for file format.">, Visibility<[ClangOption, CC1Option]>; |
Uh oh!
There was an error while loading. Please reload this page.