Bug 80076 - -Wmisleading-indentation doesn't trigger when macro is misindented
Summary: -Wmisleading-indentation doesn't trigger when macro is misindented
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 7.0.1
: P3 normal
Target Milestone: 11.0
Assignee: Patrick Palka
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2017-03-17 00:46 UTC by Andres Freund
Modified: 2022-03-29 17:49 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 7.3.0, 8.2.0, 9.0
Last reconfirmed: 2018-08-15 00:00:00


Attachments
reproducer (170 bytes, text/plain)
2017-03-17 00:46 UTC, Andres Freund
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Andres Freund 2017-03-17 00:46:34 UTC
Created attachment 40989 [details]
reproducer

Hi,

-Wmisleading-indentation doesn't trigger when the misindented part of the code consists of a macro.  The attached example is silent when a macro is used, and warns when a function is used.

#ifdef TRIGGER
static void elog(char *msg) { }
#else
#define elog(Y) printf(Y)
#endif

void foo (void);

void test (int flag)
{
  if (flag)
    foo ();
    elog("could not identify CTID variable");
}


andres@alap4:~$ gcc -DTRIGGER -Wmisleading-indentation -c /tmp/repro.c 
/tmp/repro.c: In function 'test':
/tmp/repro.c:13:3: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   if (flag)
   ^~
/tmp/repro.c:15:5: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
     elog("could not identify CTID variable");
     ^~~~

andres@alap4:~$ gcc -Wmisleading-indentation -c /tmp/repro.c 
andres@alap4:~$ gcc --version
gcc (Debian 20170226-1) 7.0.1 20170226 (experimental) [trunk revision 245744]
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


Not sure if this is a known "issue", or not.  If it's hard to make that work reliably for macros...

Andres
Comment 1 Martin Sebor 2018-08-15 23:44:56 UTC
Confirmed with GCC 7, 8, and trunk (9).
Comment 2 CVS Commits 2020-09-17 18:28:44 UTC
The master branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:

https://gcc.gnu.org/g:4839de55e2c98619f4919254abb87e2f393aaead

commit r11-3266-g4839de55e2c98619f4919254abb87e2f393aaead
Author: Patrick Palka <ppalka@redhat.com>
Date:   Thu Sep 17 14:27:22 2020 -0400

    c-family: Macro support in -Wmisleading-indentation [PR80076]
    
    Currently the -Wmisleading-indentation warning doesn't do any analysis
    when the guarded statement or the statement after it is produced by a
    macro.  This means we warn for:
    
      if (flag)
        foo ();
        bar ();
    
    but not for:
    
      #define BAR bar
      if (flag)
        foo ();
        BAR ();
    
    This patch extends the -Wmisleading-indentation implementation to
    support analyzing such statements and their tokens.  This is done in the
    "natural" way by resolving the location of each of the three tokens to
    the token's macro expansion point.  (Additionally, if the tokens all
    resolve to the same macro expansion point then we instead use their
    locations within the macro definition.)  When these resolved locations
    are all different, then we can proceed with applying the warning
    heuristics to them as if no macros were involved.
    
    gcc/c-family/ChangeLog:
    
            PR c/80076
            * c-indentation.c (should_warn_for_misleading_indentation): Move
            declarations of local variables closer to their first use.
            Handle virtual token locations by resolving them to their
            respective macro expansion points.  If all three tokens are
            produced from the same macro expansion, then instead use their
            loci within the macro definition.
    
    gcc/objc/ChangeLog:
    
            PR c/80076
            * objc-gnu-runtime-abi-01.c
            (gnu_runtime_abi_01_get_class_super_ref): Reduce indentation of
            misleadingly indented return statements.
            * objc-next-runtime-abi-01.c
            (next_runtime_abi_01_get_class_super_ref): Likewise.
    
    gcc/ChangeLog:
    
            PR c/80076
            * gensupport.c (alter_attrs_for_subst_insn) <case SET_ATTR>:
            Reduce indentation of misleadingly indented code fragment.
            * lra-constraints.c (multi_block_pseudo_p): Likewise.
            * sel-sched-ir.c (merge_fences): Likewise.
    
    libcpp/ChangeLog:
    
            PR c/80076
            * include/line-map.h (first_map_in_common): Declare.
            * line-map.c (first_map_in_common): Remove static.
    
    gcc/testsuite/ChangeLog:
    
            PR c/80076
            * c-c++-common/Wmisleading-indentation-5.c: New test.
Comment 3 Patrick Palka 2020-10-21 20:08:02 UTC
Fixed for GCC 11.  Thanks for the report.
Comment 4 Andres Freund 2020-10-21 21:13:12 UTC
(In reply to Patrick Palka from comment #3)
> Fixed for GCC 11.  Thanks for the report.

Thanks!