-
Notifications
You must be signed in to change notification settings - Fork 15k
[clang-tidy] Teach modernize-deprecated-headers about more no-op headers
#165674
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
Open
localspook
wants to merge
1
commit into
llvm:main
Choose a base branch
from
localspook:deprecated-headers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+134
−54
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,12 +3,55 @@ | |
| modernize-deprecated-headers | ||
| ============================ | ||
|
|
||
| Some headers from C library were deprecated in C++ and are no longer welcome in | ||
| C++ codebases. Some have no effect in C++. For more details refer to the C++14 | ||
| Standard [depr.c.headers] section. | ||
|
|
||
| This check replaces C standard library headers with their C++ alternatives and | ||
| removes redundant ones. | ||
|
Comment on lines
-10
to
-11
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. Could we leave this as first sentence in docs? But remove "This check" in favor of "Replaces ..." |
||
| There exist headers that produce no effect when included, which are there | ||
| solely to ease migrating code. The check will suggest removing them. | ||
| In C++, they are: | ||
|
|
||
| * ``stdalign.h`` / ``cstdalign`` | ||
| * ``stdbool.h`` / ``cstdbool`` | ||
| * ``iso646.h`` / ``ciso646`` | ||
|
|
||
| And in C they are: | ||
|
|
||
| * ``stdalign.h`` // No-op since C23 | ||
| * ``stdbool.h`` // No-op since C23 | ||
| * ``stdnoreturn.h`` // No-op since C23 | ||
|
|
||
| In C++, there is additionally a number of headers intended for | ||
| interoperability with C, which should not be used in pure C++ code. | ||
| The check will suggest replacing them with their C++ counterparts | ||
| (e.g. replacing ``<signal.h>`` with ``<csignal>``). These headers are: | ||
|
|
||
| * ``<assert.h>`` | ||
| * ``<complex.h>`` | ||
| * ``<ctype.h>`` | ||
| * ``<errno.h>`` | ||
| * ``<fenv.h>`` // deprecated since C++11 | ||
| * ``<float.h>`` | ||
| * ``<inttypes.h>`` | ||
| * ``<limits.h>`` | ||
| * ``<locale.h>`` | ||
| * ``<math.h>`` | ||
| * ``<setjmp.h>`` | ||
| * ``<signal.h>`` | ||
| * ``<stdarg.h>`` | ||
| * ``<stddef.h>`` | ||
| * ``<stdint.h>`` | ||
| * ``<stdio.h>`` | ||
| * ``<stdlib.h>`` | ||
| * ``<string.h>`` | ||
| * ``<tgmath.h>`` // deprecated since C++11 | ||
| * ``<time.h>`` | ||
| * ``<uchar.h>`` // deprecated since C++11 | ||
| * ``<wchar.h>`` | ||
| * ``<wctype.h>`` | ||
|
|
||
| Important note: the C++ headers are not identical to their C counterparts. | ||
| The C headers provide names in the global namespace (e.g. ``<stdio.h>`` | ||
| provides ``printf``), but the C++ headers might provide them only in the | ||
| ``std`` namespace (e.g. ``<cstdio>`` provides ``std::printf``, but not | ||
| necessarily ``printf``). The check can break code that uses the unqualified | ||
| names. | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
|
|
@@ -21,46 +64,9 @@ removes redundant ones. | |
| #include <cassert> | ||
| // No 'stdbool.h' here. | ||
|
|
||
| Important note: the Standard doesn't guarantee that the C++ headers declare all | ||
| the same functions in the global namespace. The check in its current form can | ||
| break the code that uses library symbols from the global namespace. | ||
|
|
||
| * `<assert.h>` | ||
| * `<complex.h>` | ||
| * `<ctype.h>` | ||
| * `<errno.h>` | ||
| * `<fenv.h>` // deprecated since C++11 | ||
| * `<float.h>` | ||
| * `<inttypes.h>` | ||
| * `<limits.h>` | ||
| * `<locale.h>` | ||
| * `<math.h>` | ||
| * `<setjmp.h>` | ||
| * `<signal.h>` | ||
| * `<stdarg.h>` | ||
| * `<stddef.h>` | ||
| * `<stdint.h>` | ||
| * `<stdio.h>` | ||
| * `<stdlib.h>` | ||
| * `<string.h>` | ||
| * `<tgmath.h>` // deprecated since C++11 | ||
| * `<time.h>` | ||
| * `<uchar.h>` // deprecated since C++11 | ||
| * `<wchar.h>` | ||
| * `<wctype.h>` | ||
|
|
||
| If the specified standard is older than C++11 the check will only replace | ||
| headers deprecated before C++11, otherwise -- every header that appeared in | ||
| the previous list. | ||
|
Comment on lines
-52
to
-54
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. Why do we completely remove this part? |
||
|
|
||
| These headers don't have effect in C++: | ||
|
|
||
| * `<iso646.h>` | ||
| * `<stdalign.h>` | ||
| * `<stdbool.h>` | ||
|
|
||
| The checker ignores `include` directives within `extern "C" { ... }` blocks, | ||
| since a library might want to expose some API for C and C++ libraries. | ||
| The check will ignore `include` directives within `extern "C" { ... }` | ||
| blocks, under the assumption that such code is an API meant to compile as | ||
| both C and C++: | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
|
|
||
Empty file.
Empty file.
Empty file.
Empty file.
25 changes: 25 additions & 0 deletions
25
clang-tools-extra/test/clang-tidy/checkers/modernize/deprecated-headers-c23.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // RUN: %check_clang_tidy -std=c23-or-later %s modernize-deprecated-headers %t -- -extra-arg-before=-isystem%S/Inputs/deprecated-headers | ||
|
|
||
| #include <stdalign.h> // <stdalign.h> | ||
| // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: including 'stdalign.h' has no effect since C23; consider removing it | ||
| // CHECK-FIXES: // <stdalign.h> | ||
| #include <stdbool.h> // <stdbool.h> | ||
| // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: including 'stdbool.h' has no effect since C23; consider removing it | ||
| // CHECK-FIXES: // <stdbool.h> | ||
| #include <stdnoreturn.h> // <stdnoreturn.h> | ||
| // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: including 'stdnoreturn.h' has no effect since C23; consider removing it | ||
| // CHECK-FIXES: // <stdnoreturn.h> | ||
|
|
||
| #include <stdio.h> // OK, not deprecated | ||
|
|
||
| #include "stdalign.h" // "stdalign.h" | ||
| // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: including 'stdalign.h' has no effect since C23; consider removing it | ||
| // CHECK-FIXES: // "stdalign.h" | ||
| #include "stdbool.h" // "stdbool.h" | ||
| // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: including 'stdbool.h' has no effect since C23; consider removing it | ||
| // CHECK-FIXES: // "stdbool.h" | ||
| #include "stdnoreturn.h" // "stdnoreturn.h" | ||
| // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: including 'stdnoreturn.h' has no effect since C23; consider removing it | ||
| // CHECK-FIXES: // "stdnoreturn.h" | ||
|
|
||
| #include "stdio.h" // OK, not deprecated |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we still mention "[depr.c.headers]" section somewhere?