Skip to content

Fix #15214, #13957 (misra.py: 2.5 for a macro defined in several files or in an inactive branch) - #9059

Open
g-chauvel wants to merge 3 commits into
cppcheck-opensource:mainfrom
g-chauvel:misra-ctu-macros
Open

g-chauvel wants to merge 3 commits into
cppcheck-opensource:mainfrom
g-chauvel:misra-ctu-macros

Conversation

@g-chauvel

@g-chauvel g-chauvel commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #9053 for 2.5: misra_2_5() keeps only the last #define of a name in each translation unit, and analyse_ctu_info() merges the definitions of a name into the first one read.

This PR keeps every #define and uses the same add_declarations() as 2.3 and 2.4, so each unused definition is reported.

m1.c

#define M 1
int f1(void);

m2.c

#define M 2
int v = M;
cppcheck -q --template=simple --addon=misra --enable=style --suppress=misra-c2012-8.4 --suppress=misra-c2012-8.7 m1.c m2.c

Before PR: no output.

Now:

m1.c:1:0: style: misra violation (use --rule-texts=<file> to get proper output) [misra-c2012-2.5]

This PR also fixes #13957 (inactive #define, R10 below) and handles paths that are not found from the current directory, for example with --relative-paths (R13 below). The tables list every change of misra.py, its visible effects, and what stays unchanged.

Added during the review

Two false positives of the first versions of this PR are fixed, each with tests.

A header reached through two path spellings (review comment). The files are compared by real path, so the header is one location. 2.3, 2.4, 2.5, 5.6 and 5.7 report it with a spelling that does not go through a link, absolute when possible, and a suppression written with the relative or the absolute spelling matches it. This also fixes the same false positives of 2.3, 2.4, 5.6 and 5.7 on upstream/main. 8.5 and 8.6 are fixed in a separate PR, and 5.9 still compares the file names as strings.

inc/h.h

#define HM 3
typedef int T;
struct S { int x; };

src/a.c

#include "h.h"

src/b.c

#include "../inc/h.h"
struct S s = { 0 };
T v = HM;
cppcheck -q --template={file}:{line}:{id} --addon=misra --enable=style --suppress=misra-c2012-8.4 --suppress=misra-c2012-8.7 -I$PWD/inc src/a.c src/b.c

First version of this PR:

$PWD/inc/h.h:2:misra-c2012-5.6
inc/h.h:2:misra-c2012-5.6
$PWD/inc/h.h:3:misra-c2012-5.7
inc/h.h:3:misra-c2012-5.7
$PWD/inc/h.h:2:misra-c2012-2.3
$PWD/inc/h.h:3:misra-c2012-2.4
$PWD/inc/h.h:1:misra-c2012-2.5

On upstream/main, the same output without the 2.5 line.

Now: no output.

A macro defined again by an inactive fallback (review comment). Fixed by keeping every #define (C1): a.c marks config.h:1 as used, and the inactive fallback in defaults.h is not checked (#13957).

config.h

#define BUFSIZE 2

defaults.h

#ifndef BUFSIZE
#define BUFSIZE 5
#endif

a.c

#include "config.h"
#include "defaults.h"
int x = BUFSIZE;

b.c

#include "config.h"
int b1(void);
cppcheck -q --template=simple --addon=misra --enable=style --suppress=misra-c2012-8.4 --suppress=misra-c2012-8.7 a.c b.c

Previous versions of this PR:

config.h:1:0: style: misra violation (use --rule-texts=<file> to get proper output) [misra-c2012-2.5]

Now: no output.

Code changes

# Code change Origin Visible effects
C1 misra_2_5(): every active #define of a name (C7) goes into the summary, not only the last one review 2 R1, R2, R7, R8
C2 misra_2_5(): an #undef marks as used every earlier #define of its name consequence of C1 R1
C3 add_declarations(), shared by 2.3, 2.4, 2.5, 5.6, 5.7: declarations of a name grouped by real path and line (see below) initial commit, review 1 (real path) R1 to R10, R12, R14, S1, S2, S4 to S8
C4 spelling_order(): choice of the reported path among the paths of one file follow-up of review 1 R3 to R6, R13, R14, S1, S2, S4, S6
C5 report_declarations(): 5.6 and 5.7 reported once the .ctu-info files are read (see below) follow-up of review 1 S1 to S4, S6
C6 Caches in real_path(), spelling_order(), is_named() and the lookup of an #include name that no file of the dump has (C7), path_found() (C8), last path kept in add_declarations() review (real_path()), follow-up (the others) none (performance only, tests: test_ctu_realpath_called_once_per_path, test_ctu_macro_header_name_looked_up_once, test_ctu_macro_absent_header_names_looked_up_once, test_ctu_macro_absent_header_names_many_directories)
C7 _get_active_directives(), used by misra_2_5() instead of all the directives: the directives of the active branches, in the active files (see below) #13957 R1, R2, R7, R10 to R12, R15
C8 CTU step of 2.5, real path of a definition (or of an #undef before every #define) not found from the current directory: only the last definition per summary, as before C1 and C3 (see below) --relative-paths R13

Origin: initial commit = first commit of the PR, review 1 and review 2 = the two review comments of "Added during the review" above, review = the review comment on robustness and performance, follow-up = needed by the previous one.

Rule 2.5

# Topic Before PR Now Tests
R1 Unused #define of a name that another source file also defines or #undefs (#15214), except R13 Missed if the name is used or #undefed there, otherwise only the last one (R2) of the first source file read (see below) Reported, unless an active #undef of the name cancels it (see below) test_ctu_unused_declaration
test_ctu_macro_same_name_same_file_two_lines
test_ctu_macro_undef_counts_as_use
test_ctu_macro_undef_before_define
test_addon_misra_2_5_same_name_two_files
R2 Several #define of a name in one source file and its headers, except R13 Only the last one in the dump checked (the headers come after the source file) Every active one checked (3 new findings in misra-test.c: lines 217, 1925, 1926) test_ctu_macro_same_name_source_and_header
test_ctu_macro_defined_again_after_undef
test_ctu_macro_undef_counts_as_use
test_ctu_macro_undef_before_define
test_ctu_macro_several_definitions
test_addon_misra_2_5_fallback_definition
test_addon_misra_2_5_header_relative_paths_defined_again
test_addon_misra_2_5_source_and_header
R3 Reported path of a 2.5 finding in a header reached through several paths, except R9 and R13 The one used by the first source file read Fewest links or Windows 8.3 names, then absolute, then shortest, then the real case on Windows, then string order test_ctu_declaration_path_spelling_reported
test_ctu_declaration_other_case_reported
test_ctu_declaration_real_case_reported
test_ctu_declaration_real_case_reported_relative
test_ctu_declaration_real_case_reported_lower_case_drive_absolute
test_ctu_declaration_real_case_reported_through_junction
test_ctu_declaration_real_case_reported_through_junction_parent_case
test_ctu_declaration_real_case_reported_junction_name
test_ctu_declaration_link_spelling_reported
test_ctu_declaration_link_spelling_reported_linked_directory
test_ctu_declaration_relative_real_spelling_reported
test_ctu_declaration_shortest_spelling_reported
test_ctu_declaration_same_length_spelling_reported
test_ctu_declaration_8dot3_spelling_reported
test_ctu_declaration_shortest_before_real_case_reported
test_addon_misra_2_5_header_link_spelling
R4 2.5 findings with another file order or -jN without --cppcheck-build-dir (any executor) Findings and paths could change Same findings and paths (the line order can still change) test_ctu_declaration_path_spelling_reported
test_ctu_declaration_other_case_reported
test_ctu_declaration_real_case_reported
test_ctu_declaration_real_case_reported_relative
test_ctu_declaration_real_case_reported_lower_case_drive_absolute
test_ctu_declaration_real_case_reported_through_junction
test_ctu_declaration_real_case_reported_through_junction_parent_case
test_ctu_declaration_real_case_reported_junction_name
test_ctu_declaration_link_spelling_reported
test_ctu_declaration_link_spelling_reported_linked_directory
test_ctu_declaration_relative_real_spelling_reported
test_ctu_declaration_shortest_spelling_reported
test_ctu_declaration_same_length_spelling_reported
test_ctu_declaration_8dot3_spelling_reported
test_ctu_declaration_shortest_before_real_case_reported
test_addon_misra_2_5_header_link_spelling
R5 Suppression of a 2.5 finding in a header reached through several paths, except R9 Matched only if it matches the path used by the first source file read (R3) Matched only if it matches the reported path (R3, R13), in any order test_addon_misra_2_5_header_link_spelling
R6 Suppression of a 2.5 finding with a path of the header that does not match the reported one (R3, R13), see below Matched or not depending on the file order or on -jN (R4) Not matched test_addon_misra_2_5_header_link_spelling
test_addon_misra_2_5_header_dotdot_spelling
R7 --error-exitcode, project whose only finding is a 2.5 that this PR adds (R1, R2, R8, R9, R11, R12) or removes (R10) Exit code 0 (1 for R10) Exit code 1 (0 for R10) none
R8 #define cancelled by an #undef that the dump lists before it, name used or #undefed in another source file, or defined again later in the dump, except R13 (see below) Not reported False positive (rare) none
R9 Header reached through two paths that realpath does not resolve to the same file (see below) One finding if the name is unused through both paths, otherwise none One finding under each path it is unused through, a false positive if it is used through the other path none
R10 Inactive #define, in code that no checked configuration includes, for example under #if 0 (Trac #13957) Checked like an active one when it is the last directive of its name in the dump (R2), false positive when the name is used only in that code Not checked (in misra-test.c, line 219 no longer reported, line 217 reported instead), with exceptions and false negatives in headers (see below) test_ctu_macro_inactive_branch
test_ctu_macro_nested_in_inactive_branch
test_ctu_macro_else_nested_in_inactive_branch
test_ctu_macro_after_nested_endif_in_inactive_branch
test_ctu_macro_elif_branch
test_ctu_macro_elifdef_branch
test_ctu_macro_spaced_directives
test_ctu_macro_condition_evaluated_twice
test_ctu_macro_header_included_in_branch
test_ctu_macro_header_included_in_branch_other_file
test_ctu_macro_header_same_file_name_in_branch
test_ctu_macro_nested_header_same_file_name_in_branch
test_ctu_macro_header_same_file_name_in_branch_include_dir
test_ctu_macro_header_same_file_name_quote_and_angle
test_ctu_macro_header_included_by_macro
test_ctu_macro_header_included_in_other_case
test_ctu_macro_header_included_in_branch_hard_link
test_ctu_macro_header_included_in_branch_hard_link_in_subdirectory
test_ctu_macro_header_included_through_link
test_ctu_macro_header_included_from_parent_directory
test_ctu_macro_header_same_file_name_other_case
test_ctu_macro_header_same_file_name_in_other_directory_case
test_ctu_macro_header_condition_not_evaluated
test_ctu_macro_source_file_name_in_inactive_include
test_ctu_macro_undef_after_inactive_define
test_addon_misra_2_5_inactive_branch
test_addon_misra_2_5_header_in_inactive_branch
test_addon_misra_2_5_header_rp_shorter_than_include
R11 #undef in code that no checked configuration includes Counts as a use, like an active one Not a use (true findings), except in a header that C7 still takes for active (R10) test_ctu_macro_undef_in_inactive_branch
test_addon_misra_2_5_undef_in_inactive_branch
R12 #define unused in every checked configuration where it is active, name used in another checked one, except R13 Missed Reported, unless an active #undef of the name cancels it (R1) test_ctu_macro_used_in_other_configuration
test_addon_misra_2_5_used_in_other_configuration
R13 2.5 for a name with a definition (or an #undef before every #define) whose path in the dump is not found from the current directory (see below) As in R1 to R3 One finding per name, as before, but the same in any file order (see below) test_ctu_macro_path_not_found
test_ctu_macro_path_not_found_first_path_then_smallest_line
test_ctu_macro_path_not_found_undef_in_other_summary
test_ctu_macro_same_name_same_file_two_lines
test_ctu_macro_undef_counts_as_use
test_ctu_macro_undef_before_define
test_ctu_macro_defined_again_after_undef
test_addon_misra_2_5_header_relative_paths
test_addon_misra_2_5_header_relative_paths_defined_again
test_addon_misra_2_5_header_link_spelling_relative_paths
test_addon_misra_2_5_header_relative_paths_first_path_smallest_line
R14 --error-exitcode, project whose only finding is a 2.5 with a suppression as in R5 or R6 Exit code 0 or 1 depending on the file order or on -jN (R4) Exit code 0 (R5) or 1 (R6) in any order test_addon_misra_2_5_header_link_spelling
R15 Suppression of a 2.5 finding that R10 removes, with --enable=information and --addon=misra (see below) Matched, nothing printed unmatchedSuppression (<file>:0, nofile:0 or *:0), exit code 1 with --error-exitcode none

Side effects on other rules (shared add_declarations())

# Topic Before PR Now Tests
S1 2.3, 2.4, 5.6, 5.7 on a header reached through several paths, except R9 and R13 (see below) False positives, or the same finding under each path At most one finding per rule and declaration, under the path chosen as for 2.5 test_ctu_declaration_path_spelling
test_ctu_declaration_path_spelling_reported
test_ctu_declaration_link_spelling
test_ctu_declaration_link_spelling_reported
test_ctu_declaration_conflict_path_spelling
test_addon_misra_5_6_header_link_spelling
S2 Suppression of 2.3, 2.4, 5.6, 5.7 in a header reached through several paths, except R9 and R13 The duplicates under the paths it does not match still reported Matched if it matches the reported path (R3) none
S3 Output order of 5.6 and 5.7 (not a defect, same findings) Printed while the .ctu-info files are read, mixed with each other and with 8.5, 8.6, 5.9 All 5.6, then all 5.7, after 8.5, 8.6, 5.9 and before 2.3, 2.4, 2.5 none
S4 Inline suppression of 2.3, 2.4, 5.6 or 5.7 in a header also reached through a path that does not match the reported one, except R9 and R13 (see below, plain typedef line included) No unmatchedSuppression for the path that is not reported, unless the name is used through it (2.3, 2.4) unmatchedSuppression for the path that is not reported (R3), and for every path after a false positive of S1 (see below) test_addon_misra_unmatched_suppression_header_link_spelling
S5 Suppression without a file name of 2.3, 2.4, 5.6 or 5.7, with --enable=information and --addon=misra, project whose only finding of this id is a false positive of S1 Matched unmatchedSuppression (nofile:0, or *:0 for <id>:*) test_addon_misra_unmatched_suppression_link_spelling
S6 --error-exitcode, project whose only output is an unmatchedSuppression of S4 or S5 Exit code 0 Exit code 1 test_addon_misra_unmatched_suppression_header_link_spelling
S7 --error-exitcode, project whose only finding is a false positive of S1 Exit code 1 Exit code 0 test_addon_misra_5_6_header_link_spelling
S8 Typedef or tag name declared at many places Quadratic time (regression from #9053) Linear (16000 typedefs of one name in one header: second run about 12 times (Linux) to 25 times (Windows) faster than Before PR) none

Tests only

# Topic Before PR Now Tests
T1 Existing 2.5 behaviors (unused macro, #undef, #ifndef fallback definition, symlink, path spelling): 6 pytest and 2 CLI tests Not tested, except the #undef before the #define (CI run of misra-ctu-*-test.c) Tested, passing before and after this PR test_ctu_macro_unused
test_ctu_macro_several_definitions
test_ctu_macro_undef_counts_as_use
test_ctu_macro_undef_before_define
test_ctu_macro_defined_again_after_undef
test_ctu_declaration_symlink
test_addon_misra_2_5_fallback_definition
test_addon_misra_2_5_header_path_spelling
T2 Existing 5.6 and 5.7 behavior when a .ctu-info file is missing, conflicts within the files read before it still reported: 1 pytest test Not tested Tested, passing before and after this PR test_ctu_declaration_conflict_missing_ctu_info
T3 Existing 2.5 behaviors that C7 and C8 keep: 9 pytest and 2 CLI tests Not tested Tested, passing before and after this PR test_ctu_macro_header_same_file_name_quote_and_angle
test_ctu_macro_condition_evaluated_twice
test_ctu_macro_source_file_name_in_inactive_include
test_ctu_macro_header_included_by_macro
test_ctu_macro_header_included_in_other_case
test_ctu_macro_header_included_in_branch_hard_link
test_ctu_macro_header_included_in_branch_hard_link_in_subdirectory
test_ctu_macro_header_included_through_link
test_ctu_macro_header_included_from_parent_directory
test_addon_misra_2_5_header_relative_paths
test_addon_misra_2_5_header_relative_paths_defined_again

Unchanged

# Topic Behavior (before and now) Why unchanged
U1 8.5, 8.6 on a header reached through several paths Paths compared as strings, so the result depends on the path Own code, not the one shared with 2.5
U2 5.9 on a static inline function of a header reached through several paths Paths compared as strings, false positive Own code, not the one shared with 2.5
U3 2.5 on an active #define whose only use is in code that no checked configuration includes (-UX, --max-configs=1, #if 0) Reported A use is a token of the dump, which holds only the checked configurations
U4 8.2, 8.4, 19.2, 21.2 on a header reached through several paths One finding under each path by which a source file includes the header, as before Checked per source file, not in the CTU step
U5 5.8 on a header reached through several paths One finding, the path depends on the file order, as before Own code, not the one shared with 2.5
Details and examples
  • Each section first gives the details of its row when the table says "see below", then an example
  • Linux, one empty directory per example
  • OPTS="-q --addon=misra --enable=style --template={file}:{line}:{id}"
  • S4 to S6 and R15 need the addon named misra (--addon=misra, or <addon>misra</addon> in a project file). With only --addon=misra.py or a JSON file, cppcheck does not report unmatched misra-* suppressions, Before PR and Now
  • l is a symlink to inc (ln -s inc l), shorter than inc
  • Before PR = upstream/main, Now = this PR, same cppcheck binary
  • dump: what cppcheck --dump writes for a source file, one part per configuration, with the directives of the source file first, then those of its headers
  • summary: the MisraMacro line that misra.py reports for one source file and one configuration, with the macros found there, cppcheck keeps it in a .ctu-info file
  • CTU step: the last run of misra.py, after every source file is analyzed, it reads all the .ctu-info files

C3

Before PR, the declarations of a name were grouped all together for 2.5, by path string and line for the other rules.

The declarations of a name are looked up in a dict keyed by real path and line. Before PR a list per name was scanned for each new declaration (2.3, 2.4, 5.6, 5.7). This lookup is what makes S8 linear, not the real path.

C5

Also when a .ctu-info file is missing (for example a source file that cppcheck stopped on): the CTU step stops at it, as before. The 5.6 and 5.7 conflicts within the .ctu-info files read before it are reported, like 5.9, 8.5 and 8.6. 2.3, 2.4, 2.5, 5.8 and 8.7 are not reported.

C7

  • Active branches: those of each configuration, from the condition results of the dump. A condition that has both results (a header included twice, true then false) leaves both of its branches active.
  • Active files: the source file, a file whose file name no #include has, and a file that an active #include of an active file reaches.
  • An #include reaches a file by its file name (case ignored on Windows). Among several files of the dump with directives and that name, it reaches:
    • the one next to the including file for #include "...",
    • else those whose path and the path written in the #include end alike (one ends with the other, the .. components of the #include path left out, the path of the dump can be the shorter one with -rp), compared by whole components (inc/myx/config.h does not end like x/config.h),
    • else all of them.
  • It also reaches a file under another name in the same directory when no file has that name (hard link, symbolic link to the file, path found from the current directory).
  • It reaches every file when it names its file through a macro (#include MACRO).
  • A header given by --include follows the same rules.

C8

Each summary (one per configuration of a source file, its headers included) contributes its last definition of the name. An #undef that the dump lists before every #define of the name counts as a used definition. One finding if none of these last definitions is used, at the first path in the order of R13, then the smallest line.

R1

Before PR, the unused #define is missed if the name is used in the other source file, or if its last directive there is an #undef (or an #undef comes before any #define), in the order of the dump as in R2.

Now it is reported, unless, in a configuration where this #define is active, its dump also lists an active #undef of the name after it, or before every active #define of the name (counts as a use, as before).

M 1 is unused, M 2 is used.

// a.c
#define M 1
void fa(void);
// b.c
#define M 2
extern int gb[M];

cppcheck $OPTS a.c b.c

  • Before PR: no output
  • Now: a.c:1:misra-c2012-2.5

R2

Neither definition is used. The dump lists the one of h.h last.

// h.h
#define M 1
// a.c
#include "h.h"
#define M 1
void fa(void);

cppcheck $OPTS a.c

  • Before PR: h.h:1:misra-c2012-2.5
  • Now: a.c:2:misra-c2012-2.5, h.h:1:misra-c2012-2.5

R3

a.c, read first, reaches h.h through l.

// inc/h.h
#define M 1
// src/a.c
#include "../l/h.h"
void fa(void);
// src/b.c
#include "../inc/h.h"
void fb(void);

cppcheck $OPTS src/a.c src/b.c

  • Before PR: l/h.h:1:misra-c2012-2.5
  • Now: inc/h.h:1:misra-c2012-2.5

Every path through a link: files of R3, top is a symlink to the example directory (ln -s . top). top/inc/h.h goes through one link, top/l/h.h through two. On Windows, starting cppcheck from a junction gives the same result.

cppcheck $OPTS top/src/a.c top/src/b.c

  • Before PR: top/l/h.h:1:misra-c2012-2.5, but top/inc/h.h:1 with top/src/b.c top/src/a.c
  • Now: top/inc/h.h:1:misra-c2012-2.5 in both orders

R4

Files of R3, files in the other order.

cppcheck $OPTS src/b.c src/a.c

  • Before PR: inc/h.h:1:misra-c2012-2.5, but l/h.h:1 in the order of R3
  • Now: inc/h.h:1:misra-c2012-2.5 in both orders

R5

Files of R3, suppression with the path of b.c.

cppcheck $OPTS --suppress=misra-c2012-2.5:inc/h.h src/a.c src/b.c

  • Before PR: l/h.h:1:misra-c2012-2.5, but no output with src/b.c src/a.c
  • Now: no output in both orders

R6

A path by which a source file reaches the header but that does not match the reported path, for example:

  • l/h.h when inc/h.h is reported
  • ../inc/h.h when an absolute path is reported
  • inc/h.h when real/l/h.h is reported
  • inc/h.h when W:\h.h (a Windows subst drive of inc) is reported
  • INCLUD~1/h.h when include_dir_long/h.h is reported (INCLUD~1 is the Windows 8.3 name of include_dir_long)

Link path: files of R3, suppression with the path of a.c.

cppcheck $OPTS --suppress=misra-c2012-2.5:l/h.h src/b.c src/a.c

  • Before PR: inc/h.h:1:misra-c2012-2.5, but no output with src/a.c src/b.c
  • Now: inc/h.h:1:misra-c2012-2.5 in both orders

.. path against an absolute one: run from src. a.c reaches h.h through an absolute -I. <dir> is the absolute path of the example directory.

// inc/h.h
#define HM 3
// src/a.c
#include "h.h"
int a1(void);
// src/b.c
#include "../inc/h.h"
int b1(void);

cd src && cppcheck $OPTS -I"$(realpath ../inc)" --suppress=misra-c2012-2.5:../inc/h.h a.c b.c

  • Before PR: <dir>/inc/h.h:1:misra-c2012-2.5, but no output with b.c a.c
  • Now: <dir>/inc/h.h:1:misra-c2012-2.5 in both orders

Path of the real directory, every path through a link: real/l is a symlink to real/inc (ln -s inc real/l), workspace a symlink to real (ln -s real workspace). workspace/inc/h.h and real/l/h.h both go through one link, the shorter one is reported.

// real/inc/h.h
#define M 1
// real/src/a.c
#include "../inc/h.h"
void fa(void);
// real/src/b.c
#include "../l/h.h"
void fb(void);

cppcheck $OPTS --suppress=misra-c2012-2.5:inc/h.h workspace/src/a.c real/src/b.c

  • Before PR: no output, but real/l/h.h:1:misra-c2012-2.5 with real/src/b.c workspace/src/a.c
  • Now: real/l/h.h:1:misra-c2012-2.5 in both orders

R7

Files of R1.

cppcheck $OPTS --error-exitcode=1 a.c b.c; echo $?

  • Before PR: 0
  • Now: a.c:1:misra-c2012-2.5, then 1

For R2 the Before PR finding must be suppressed, otherwise both give exit code 1. Files of R2.

cppcheck $OPTS --error-exitcode=1 --suppress=misra-c2012-2.5:h.h a.c; echo $?

  • Before PR: 0
  • Now: a.c:2:misra-c2012-2.5, then 1

M is used only in code that #if 0 excludes (R10).

// a.c
#if 0
#define M 1
extern int ga[M];
#endif
void fa(void);

cppcheck $OPTS --error-exitcode=1 a.c; echo $?

  • Before PR: a.c:2:misra-c2012-2.5, then 1
  • Now: 0

R8

The #define is unused in its source file and headers, and the dump lists the #undef after another #define of the name. For example a header #define cancelled in the source file that includes it, when that file also defines the name before.

The dump lists the directives of a.c before those of h.h, so misra.py sees the second #undef before M 1 and counts M 1 as unused. Without #define M 0 / #undef M, misra.py counts the second #undef as a use and nothing is reported.

// a.c
#define M 0
#undef M
#include "h.h"   /* #define M 1 */
#undef M         /* cancels M 1 */
void fa(void);
// b.c
#define M 2
extern int gb[M];
// h.h
#define M 1

cppcheck $OPTS --suppress=misra-c2012-20.5 a.c b.c

  • Before PR: no output, but h.h:1:misra-c2012-2.5 with a.c alone
  • Now: h.h:1:misra-c2012-2.5

With one source file, a #define that is not the last of its name in the dump is reported too, when an #undef of the source file that the dump lists before it cancels it. a.c includes h.h and k.h, each followed by an #undef M.

// a.c
#define M 0
#undef M
#include "h.h"   /* #define M 1 */
#undef M         /* cancels M 1 */
#include "k.h"   /* #define M 5 */
#undef M         /* cancels M 5 */
void fa(void);
// h.h
#define M 1
// k.h
#define M 5

cppcheck $OPTS --suppress=misra-c2012-20.5 a.c

  • Before PR: k.h:1:misra-c2012-2.5
  • Now: h.h:1:misra-c2012-2.5, k.h:1:misra-c2012-2.5

R9

The path of every #define of the name is found from the current directory. Cases: hard link, bind mount, Windows \\?\ or UNC path, relative paths that name other files from the current directory (with -rp or misra.py run directly).

inc/h2.h is a hard link to inc/h.h (ln inc/h.h inc/h2.h). M is used through h2.h, which is the same file as h.h, so the finding under inc/h.h is a false positive.

// inc/h.h
#define M 1
// src/a.c
#include "../inc/h.h"
void fa(void);
// src/b.c
#include "../inc/h2.h"
extern int gb[M];

cppcheck $OPTS src/a.c src/b.c

  • Before PR: no output
  • Now: inc/h.h:1:misra-c2012-2.5

R10

Inactive code: under #if 0, in a branch excluded by -D or -U, or in a header included only there.

Still checked, in a header that C7 still takes for active:

  • one with the file name of the header an active #include finds, when that header has no directive,
  • or when the #include does not find it next to the including file (<...>, -I, or no file at all, for example a system header or an include directory not given) and their paths end alike (y/config.h for #include "config.h" found as x/config.h by -Ix, config.h for #include <sub/config.h> found as inc/sub/config.h by -Iinc),
  • or when no header of that file name has a path that ends like the #include and there are several (the header it finds was first included through another path, for example #include "l/config.h" with l a link to y, after #include "y/config.h" and #include "x/config.h" under #if 0: both x/config.h and y/config.h are then checked),
  • every header when an active #include names its file through a macro.

An #undef that follows only inactive #defines of the name (in a checked configuration) then comes before every #define (R1): it counts as a use of the active #defines that the dump lists after it, which Before PR it did not.

Also not checked (false negatives): an active header that no active #include reaches while an #include has its file name, or whose file name is also the one of an #include that reaches another header or is inactive. It happens when the header is reached through --include, through a hard link or a symbolic link to the file in another directory under another name, or when its path is not found from the current directory (R13).

-DX excludes M 2. M 1 is unused (R2).

// a.c
#ifdef X
#define M 1
#else
#define M 2
#endif
int f(void);

cppcheck $OPTS -DX a.c

  • Before PR: a.c:4:misra-c2012-2.5
  • Now: a.c:2:misra-c2012-2.5

h.h is included only in inactive code.

// h.h
#define M 1
// a.c
#if 0
#include "h.h"
#endif
void fa(void);

cppcheck $OPTS a.c

  • Before PR: h.h:1:misra-c2012-2.5
  • Now: no output

Not checked although active (false negative): --include=h.h makes M active, the dump has only the #include under #if 0. Files of the previous example.

cppcheck $OPTS --include=h.h a.c

  • Before PR: h.h:1:misra-c2012-2.5
  • Now: no output (without --include, no output is expected)

The #undef follows an inactive #define only.

// a.c
#if 0
#define M 1
#endif
#undef M
#define M 2
void fa(void);

cppcheck $OPTS --suppress=misra-c2012-20.5 a.c

  • Before PR: a.c:5:misra-c2012-2.5
  • Now: no output (with #undef M and #define M 2 alone, Before PR already gave no output)

R11

// a.c
#define M 1
#if 0
#undef M
#endif
void fa(void);

cppcheck $OPTS --suppress=misra-c2012-20.5 a.c

  • Before PR: no output
  • Now: a.c:1:misra-c2012-2.5

R12

For example #ifndef X, name used under #ifdef X.

M 2 is unused when X is not defined, M is used only when it is.

// a.c
#ifdef X
#define M 1
extern int ga[M];
#endif
#ifndef X
#define M 2
#endif
void fa(void);

cppcheck $OPTS a.c

  • Before PR: no output
  • Now: a.c:6:misra-c2012-2.5

R13

For example with -rp=p run from the parent of p, with misra.py run directly from another directory, or when a path has a non-ASCII character (hé.h is hxx.h in the dump).

Now, R1, R2, R8 and R12 do not apply to this name, R10 and R11 do. The finding is, of the last definitions of the name in each configuration of each source file, the one whose path comes first in the order of R3 with no link counted for a path not found (l/h.h rather than inc/h.h), then the smallest line.

Files of R3 in a directory p (p/l is the symlink), run from the parent directory of p. The paths of the dump are relative to p.

cppcheck $OPTS -rp=p p/src/a.c p/src/b.c

  • Before PR: l/h.h:1:misra-c2012-2.5, but inc/h.h:1 with p/src/b.c p/src/a.c
  • Now: l/h.h:1:misra-c2012-2.5 in both orders

An #undef that the dump lists before every #define of the name counts as a definition here: with -rp=p, p/a.c = #undef M and b.c defining M twice give no 2.5, whereas without -rp they give b.c:1 and b.c:2.

// p/a.c
#undef M
void fa(void);
// b.c
#define M 1
#define M 2
void fb(void);

cppcheck $OPTS --suppress=misra-c2012-20.5 -rp=p p/a.c b.c

  • Before PR: no output
  • Now: no output (without -rp=p, Now gives b.c:1:misra-c2012-2.5, b.c:2:misra-c2012-2.5)

R14

Files of R3, commands of R5 and R6 with --error-exitcode=1.

cppcheck $OPTS --error-exitcode=1 --suppress=misra-c2012-2.5:inc/h.h src/a.c src/b.c; echo $?

  • Before PR: l/h.h:1:misra-c2012-2.5, then 1, but 0 with src/b.c src/a.c
  • Now: 0 in both orders

cppcheck $OPTS --error-exitcode=1 --suppress=misra-c2012-2.5:l/h.h src/b.c src/a.c; echo $?

  • Before PR: inc/h.h:1:misra-c2012-2.5, then 1, but 0 with src/a.c src/b.c
  • Now: inc/h.h:1:misra-c2012-2.5, then 1 in both orders

R15

Only for a suppression without a line number: for example by id alone, <id>:*, or with the name of a source file of the command line, a pattern too (for a header name or an inline suppression nothing is printed). Nothing is printed either when the suppression still matches another 2.5 finding.

Files of the #if 0 example of R7.

cppcheck $OPTS --enable=information --suppress=checkersReport --suppress=misra-c2012-2.5:a.c --error-exitcode=1 a.c; echo $?

  • Before PR: 0
  • Now: a.c:0:unmatchedSuppression, then 1

S1

Several paths: link, absolute and relative, other case, 8.3 name or subst drive on Windows.

T is used in a.c.

// inc/h.h
typedef int T;
// src/a.c
#include "../inc/h.h"
extern T ta;
// src/b.c
#include "../l/h.h"
void fb(void);

cppcheck $OPTS src/a.c src/b.c

  • Before PR: inc/h.h:1:misra-c2012-5.6, l/h.h:1:misra-c2012-5.6, l/h.h:1:misra-c2012-2.3
  • Now: no output

S2

Files of S1, with void fa(void); instead of extern T ta;, so T is unused. Without the suppression, Now reports only inc/h.h:1:misra-c2012-2.3.

cppcheck $OPTS --suppress=*:inc/h.h src/a.c src/b.c

  • Before PR: l/h.h:1:misra-c2012-5.6, l/h.h:1:misra-c2012-2.3
  • Now: no output

S3

// a.c
typedef int T;
struct S { int x; };
extern T ta;
extern struct S sa;
extern int g;
// b.c
typedef long T;
struct S { long y; };
extern T tb;
extern struct S sb;
extern int g;
// c.c
typedef short T;
struct S { short z; };
extern T tc;
extern struct S sc;

cppcheck $OPTS a.c b.c c.c

Before PR Now
a.c:1:misra-c2012-5.6 b.c:5:misra-c2012-8.5
b.c:1:misra-c2012-5.6 a.c:5:misra-c2012-8.5
a.c:2:misra-c2012-5.7 a.c:1:misra-c2012-5.6
b.c:2:misra-c2012-5.7 b.c:1:misra-c2012-5.6
b.c:5:misra-c2012-8.5 c.c:1:misra-c2012-5.6
a.c:5:misra-c2012-8.5 a.c:2:misra-c2012-5.7
c.c:1:misra-c2012-5.6 b.c:2:misra-c2012-5.7
c.c:2:misra-c2012-5.7 c.c:2:misra-c2012-5.7

S4

The suppression is on a line that keeps tokens: a struct or enum definition, or typedef struct {...} T;. Paths that do not match the reported one: those of R6 (link, .. against an absolute path, Windows 8.3 name or subst drive).

On a plain typedef int T; line cppcheck only checks an inline suppression when a finding, even a suppressed one, is raised on that line. So unmatchedSuppression appears under the reported path only, or not at all, and not under the other path, as with a single path. Before PR the false 5.6 or 2.3 under the other path made the line checked, so a suppression that is no longer needed could be reported then and be silent now (a plain typedef line that was checked for another name), or the opposite (the main example below).

Now, unmatchedSuppression also for every path when the suppressed finding was a false positive of S1. The finding, if any, is suppressed through the reported path.

The 5.7 of h.h is now reported only under inc/h.h, so the inline suppression read through l/h.h matches nothing.

// inc/h.h
// cppcheck-suppress misra-c2012-5.7
struct S { int x; };
// inc/other.h
struct S { long y; };
// src/a.c
#include "../inc/h.h"
extern struct S a1;
// src/b.c
#include "../l/h.h"
extern struct S b1;
// src/c.c
#include "../inc/other.h"
extern struct S c1;

cppcheck $OPTS --enable=information --inline-suppr --suppress=checkersReport src/a.c src/b.c src/c.c

  • Before PR: inc/other.h:1:misra-c2012-5.7
  • Now: inc/other.h:1:misra-c2012-5.7, l/h.h:2:unmatchedSuppression

Plain typedef line, T unused.

// inc/h.h
// cppcheck-suppress misra-c2012-5.6
typedef int T;
// src/a.c
#include "../inc/h.h"
void fa(void);
// src/b.c
#include "../l/h.h"
void fb(void);

cppcheck $OPTS --enable=information --inline-suppr --suppress=checkersReport src/a.c src/b.c

  • Before PR: inc/h.h:2:misra-c2012-2.3, l/h.h:2:misra-c2012-2.3
  • Now: inc/h.h:2:misra-c2012-2.3, inc/h.h:2:unmatchedSuppression

S5

Files of S1. The two suppressions match the false 2.3 and 5.6 of Before PR, Now has no finding left for them.

cppcheck $OPTS --enable=information --suppress=checkersReport --suppress=misra-c2012-2.3 --suppress=misra-c2012-5.6 src/a.c src/b.c

  • Before PR: no output
  • Now: nofile:0:unmatchedSuppression

S6

Files of S4.

cppcheck $OPTS --enable=information --inline-suppr --suppress=checkersReport --suppress=misra-c2012-5.7:inc/other.h --error-exitcode=1 src/a.c src/b.c src/c.c; echo $?

  • Before PR: 0
  • Now: l/h.h:2:unmatchedSuppression, then 1

S7

Files of S1.

cppcheck $OPTS --error-exitcode=1 src/a.c src/b.c; echo $?

  • Before PR: the 3 findings of S1, then 1
  • Now: 0

S8

h.h holds 16000 lines typedef int T;. The second run finds a.c in the build dir and only runs the CTU step. Both sides report the same 2.3 and 5.6 findings, suppressed here.

yes 'typedef int T;' | head -n 16000 > h.h
printf '#include "h.h"\nextern T t;\n' > a.c
mkdir bd
cppcheck $OPTS --cppcheck-build-dir=bd --suppress=misra-c2012-2.3 --suppress=misra-c2012-5.6 a.c   # run twice
  • Before PR: no output
  • Now: no output, second run about 12 times faster than the second run of Before PR (Linux, about 25 times on Windows)

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

The fix works for the case in the description, and with -j2 the output is now the same on every run. However, the PR adds a false positive for misra 2.5. Merging is now by file+line instead of by macro name. When two translation units reach the same header through different path spellings, that header's macro gets two entries. One example is an absolute -I path, which is common with compile_commands.json, in one file and a relative #include "../inc/h.h" in another. If only one of those TUs uses the macro, it is reported as unused. The base version reports nothing. Rules 2.3 and 2.4 already have this problem: a typedef in the same setup gets a 2.3 and also 5.6 on the base version. With this PR, 2.5 has it too. Normalising the path (for example os.path.realpath) in is_different_location would fix all three rules.

Comments

# Comment By Where Status
1 Macro merge by file+line gives 2.5 false positive when a header path is spelled differently AI review addons/misra.py:4949 🆕 New

@g-chauvel, please fix, resolve or reply to the open and new comments: #1.

Testing

  • Built cppcheck from pr/ with make -j8 cppcheck. The PR only changes Python, so testrunner was not needed.
  • PYTHONPATH=addons python3 -m pytest addons/test/misra_test.py: all 23 pass, including the new MisraMacro cases. pytest -k misra test/cli: 10 pass.
  • PR example (m1.c/m2.c): m1.c:1:0 ... [misra-c2012-2.5], as the description says. If m3.c defines M and does not use it, -j2 m1.c m3.c reports both m1.c:1 and m3.c:1, the same on 3 runs.
  • A header included through different relative spellings (h.h, ../h.h, ../sub/../h.h) and used in only one TU: no 2.5 warning, which is correct, because simplecpp normalises those paths.

False positive (regression):

mkdir -p t2/inc t2/src && cd t2
printf '#define HM 3\n' > inc/h.h
printf '#include "h.h"\nint a1(void);\n' > src/a.c
printf '#include "../inc/h.h"\nint b1 = HM;\n' > src/b.c
cppcheck -q --template=simple --addon=misra --enable=style --suppress=misra-c2012-8.4 --suppress=misra-c2012-8.7 --suppress=misra-c2012-20.1 -I$PWD/inc src/a.c src/b.c

PR:

/work/scratch/t2/inc/h.h:1:0: style: misra violation (use --rule-texts=<file> to get proper output) [misra-c2012-2.5]

Base: no output. HM is used in src/b.c.

Passing one source file as an absolute path and the other as a relative one ($PWD/a.c sub/b.c, both including h.h) gives the same false positive, again only with the PR. The same setup with a typedef header already gives 2.3 and 5.6 on the base version, so the path comparison is a known weakness that this PR extends to 2.5.


Automated review of bd55888 by Claude. The pull request was built and tested locally.

Comment thread addons/misra.py Outdated
existing_macro['used'] = existing_macro['used'] or new_macro['used']
else:
all_macro_info[key] = new_macro
add_declaration(all_macro_info, new_macro)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

Before this PR, macros were merged by name, so how the header path was spelled did not matter. Now they are merged by file+line (is_different_location), and the file string differs when TUs reach the same header through different spellings, for example -I$PWD/inc with #include "h.h" in one file and #include "../inc/h.h" in another. The result is two entries, and the TU that does not use the macro gets a 2.5 warning:

/work/scratch/t2/inc/h.h:1:0: style: misra violation ... [misra-c2012-2.5]

The base version reports nothing here. Rules 2.3 and 2.4 already behave like this, and a typedef in the same setup also gets 5.6. Normalising the path in is_different_location (e.g. os.path.realpath(loc['file'])) would avoid the regression and fix the other rules too.

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

The PR adds a new false positive for misra 2.5. For each macro name, misra_2_5() keeps only the last #define directive of a TU, and it decides used by name. Inactive directives count too. Before this PR the CTU merge also went by name, so this did not matter. Now the merge goes by location. Suppose a TU uses a macro from config.h while a later #ifndef X / #define X fallback in another header also defines it. That TU then records only the fallback's location as used, and a second TU that includes config.h without using the macro gets a 2.5 warning on config.h. The base version reports nothing. The path-spelling problem from the earlier review is fixed.

Comments

# Comment By Where Status
1 Macro merge by file+line gives 2.5 false positive when a header path is spelled differently AI review addons/misra.py:4949 ✅ Fixed
2 2.5 false positive: per-TU summary keeps only the last #define per name, so the macro that is really used can look unused AI review addons/misra.py:4949 🆕 New

@g-chauvel, please fix, resolve or reply to the open and new comments: #2.

Testing

  • Built cppcheck in pr/ with make -j8 cppcheck. The PR only changes Python, so testrunner was not needed. To compare with the base version I ran the same binary with base/addons/misra.py.
  • PYTHONPATH=addons python3 -m pytest addons/test/misra_test.py: all 26 pass.
  • I re-ran the repro from the earlier review (absolute -I path in one TU, #include "../inc/h.h" in the other): no 2.5 warning any more.

False positive (regression):

printf '#define BUFSIZE 2\n' > config.h
printf '#ifndef BUFSIZE\n#define BUFSIZE 5\n#endif\n' > defaults.h
printf '#include "config.h"\n#include "defaults.h"\nint x = BUFSIZE;\n' > a.c
printf '#include "config.h"\nint b1(void);\n' > b.c
cppcheck -q --template=simple --addon=misra --enable=style --suppress=misra-c2012-8.4 --suppress=misra-c2012-8.7 a.c b.c

PR, with both -j1 and -j2:

config.h:1:0: style: misra violation (use --rule-texts=<file> to get proper output) [misra-c2012-2.5]

Base addon: no output. a.c uses BUFSIZE from config.h:1: the dump's macro-usage points there. But a.c's summary only has defaults.h:2, which is inactive.


Automated review of 263fd04 by Claude. The pull request was built and tested locally.

Comment thread addons/misra.py Outdated
existing_macro['used'] = existing_macro['used'] or new_macro['used']
else:
all_macro_info[key] = new_macro
add_declaration(all_macro_info, new_macro)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

Merging macros by location only works if each TU's summary has every definition with its own used flag. misra_2_5() stores one entry per name in unused_macro[macro_name], and the last directive wins. Inactive #ifndef fallbacks count as well. used is also decided by name. So a TU that uses config.h's macro can record only an inactive fallback in another header:

// config.h
#define BUFSIZE 2
// defaults.h
#ifndef BUFSIZE
#define BUFSIZE 5
#endif
// a.c
#include "config.h"
#include "defaults.h"
int x = BUFSIZE;
// b.c
#include "config.h"
int b1(void);

cppcheck --addon=misra --enable=style --suppress=misra-c2012-8.4 --suppress=misra-c2012-8.7 a.c b.c with this PR:

config.h:1:0: style: misra violation ... [misra-c2012-2.5]

The base version reports nothing. The macro-usage entries in the dump (file/line of the definition that was used) could set used per definition, and misra_2_5() should keep every #define directive instead of one per name.

@g-chauvel g-chauvel changed the title Fix #15214 (misra.py: false negative 2.5 when several files define the same macro name) Fix #15214, #13957 (misra.py: 2.5 for a macro defined in several files or in an inactive branch) Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants