-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Partial fix for #13875 (Document preprocessorErrorDirective) [ci skip] #7542
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
Merged
danmar
merged 1 commit into
cppcheck-opensource:main
from
cppchecksolutions:13875-preprocessorErrorDirective
May 20, 2025
+55
−0
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
|
|
||
| # preprocessorErrorDirective | ||
|
|
||
| **Message**: #error message<br/> | ||
| **Category**: Configuration<br/> | ||
| **Severity**: Error<br/> | ||
| **Language**: C and C++ | ||
|
|
||
| ## Description | ||
|
|
||
| The `#error` directive is a preprocessor instruction in C and C++ that explicitly generates a compilation error. It is typically used as a safeguard to prevent compilation under incorrect conditions—like unsupported configurations, platforms, or missing defines. | ||
|
|
||
| These warnings from Cppcheck do not indicate a bug in your code. These warnings indicate that the Cppcheck configuration is not working. | ||
|
|
||
| ## How to fix | ||
|
|
||
| The warning is typically reported for an `#error` directive that is located inside some conditional preprocessor block (`#if..`, `#else`, etc): | ||
| ```cpp | ||
| #ifndef __BYTE_ORDER__ | ||
| #error Byte order is not defined | ||
| #endif | ||
| ``` | ||
|
|
||
| The code here is correct and you should not try to change it. | ||
|
|
||
| Somehow it will be necessary to define `__BYTE_ORDER__` in Cppcheck analysis. | ||
|
|
||
| ### gcc compiler macro | ||
| If you compile your code with gcc and the macro is provided by gcc, then you can define all gcc-macros using these commands: | ||
| ``` | ||
| echo x > dummy.c | ||
| gcc -dM -E dummy.c > gcc-macros.h | ||
| ``` | ||
| The gcc-macros.h that is generated can be included in cppcheck using the `--include` option: | ||
| ``` | ||
| cppcheck --include=gcc-macros.h .... | ||
| ``` | ||
|
|
||
| ### library macro | ||
| If the macro that is needed is defined in some library header it might be possible to fix the issue by using an extra `--library` option: | ||
| ``` | ||
| cppcheck --library=foo ..... | ||
| ``` | ||
|
|
||
| ### manually defined macro | ||
| To define extra macros manually you can use `-D`: | ||
| ``` | ||
| cppcheck -D__BYTE_ORDER__=123 ..... | ||
| ``` | ||
|
|
||
| ### use --force or --max-configs | ||
| You can let Cppcheck try to resolve the required defines: | ||
| ``` | ||
| cppcheck --force ..... | ||
| ``` | ||
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.
Uh oh!
There was an error while loading. Please reload this page.