<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/57167>57167</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            bugprone-sizeof-expression behavior much more aggressive after 15f3cd6bfc6
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          tauchris
      </td>
    </tr>
</table>

<pre>
    After commit 15f3cd6bfc6, bugprone-sizeof-expression is flagging many new cases (which were not covered by existing LIT test).  Whether or not the new warnings are considered correct, or even desirable, is perhaps subjective; but I do not think the new behavior is reasonable.

I prepared this reproducer to illustrate behavior of relevant cases before the upstream ElaboratedType sugaring change (labeled "Before 15f3cd6bfc6"), and after that upstream change (labeled "After 15f3cd6bfc6").  Note that this code can be compiled as either "C" or "C++", and that was done purposefully to illustrate the behavior differences in the checker that existed before the ElaboratedType sugaring commit.

```
     1       #ifdef __cplusplus
     2       #define STRKWD
     3       #else
     4       #define STRKWD struct
     5       #endif
     6       
     7       int Test5() {
     8         typedef int Array10[10];
     9       
    10         struct MyStruct {
    11           Array10 arr;
    12           Array10* ptr;
    13         };
    14       
    15         typedef struct TypedefStruct {
    16           Array10 arr;
    17           Array10* ptr;
    18         } TypedefStruct;
    19       
    20         typedef const STRKWD MyStruct TMyStruct;
    21         typedef const STRKWD MyStruct *PMyStruct;
    22         typedef TMyStruct *PMyStruct2;
    23         typedef const TypedefStruct *PTTStruct;
    24       
    25         STRKWD MyStruct S;
    26         TypedefStruct TS;
    27         PMyStruct PS;
    28         PMyStruct2 PS2;
    29         Array10 A10;
    30         PTTStruct PTTS;
    31       
    32         int sum = 0;
    33                                                     // Before 15f3cd6bfc6   After 15f3cd6bfc6
    34         sum += sizeof(&S);                          // (C++:WARN C:WARN)    (C++/C: WARN)
    35         sum += sizeof(__typeof(&S));                // (C++:WARN C:    )    (C++/C: WARN)
    36         sum += sizeof(&TS);                         // (C++:WARN C:WARN)    (C++/C: WARN)
    37         sum += sizeof(__typeof(&TS));               // (C++:     C:    )    (C++/C: WARN)
    38         sum += sizeof(STRKWD MyStruct*);            // (C++:WARN C:    )    (C++/C: WARN)
    39         sum += sizeof(__typeof(STRKWD MyStruct*));  // (C++:WARN C:    )    (C++/C: WARN)
    40         sum += sizeof(TypedefStruct*);              // (C++:     C:    )    (C++/C: WARN)
    41         sum += sizeof(__typeof(TypedefStruct*));    // (C++:     C:    )    (C++/C: WARN)
    42         sum += sizeof(PTTS);                        // (C++:WARN C:WARN)    (C++/C: WARN)
    43         sum += sizeof(PMyStruct);                   // (C++:WARN C:    )    (C++/C: WARN)
    44         sum += sizeof(PMyStruct2);                  // (C++:     C:    )    (C++/C: WARN)
    45         sum += sizeof(PS);                          // (C++:WARN C:WARN)    (C++/C: WARN)
    46         sum += sizeof(PS2);                         // (C++:WARN C:WARN)    (C++/C: WARN)
    47         sum += sizeof(&A10);                        // (C++:WARN C:WARN)    (C++/C: WARN)
    48       
    49         return sum;
    50       } 
```

The code above can be saved as sizeof-expression.c, and then analyzed as C or C++ code, using the following commands, respectively:
```
bash> clang-tidy --checks=-*,bugprone-sizeof-expression sizeof-expression.c -- -x c
bash> clang-tidy --checks=-*,bugprone-sizeof-expression sizeof-expression.c -- -x c++
```
Notable observations are that the upstream ElaboratedType change made the behavior of the checker consistent between C and C++, where before it was not – however, it also introduced the following differences:

1. Prior to the change, struct declarations that are nested in typedef declarations produced warnings (in C++ only) when using the struct tag in the sizeof() expression, but did not produce warnings when using the typedef name.  After the change, warnings are issued in both C++ and C, and are issued whether the struct tag name or the typedef name is used.  (See lines 38 and 40.)
2. Prior to the change, use of the __typeof() builtin would avoid warnings in cases where warnings might otherwise be generated (see line 37, in both C++ and C).  After the change, use of __typeof() has no effect.

In my opinion, taking "sizeof(X *)" is a common and natural thing to do, and I see it frequently in our code base.  It's obviously useful and necessary in cases where there are different types of pointers (different address spaces, or near vs far, e.g.), where different pointers may have different sizes.  The fact that "X" happens to be a struct, or a typedef to a struct, should not matter.   While the new behavior is at least consistent between C and C++, and consistent regardless of declaration style (whether you are using a struct tag name or a typedef name in the sizeof() expression), it simply warns in too many cases now.  I would suggest that the checker has been too aggressive for some time, and that the ElaboratedType change simply exposed that overzealous matching, which was previously hidden due to the ambiguity in desugaring that used to exist before that change.

In any case, the fact that "__typeof()" is no longer a usable way to suppress warnings from this checker is a bug (I assume in the checker itself, though I'm not sure).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy9WduO2zgS_Rr1C9GGLd_aD37oSwI0dnYQTBvIvAWUVJI4kUQNSdlxvn6rSN0tO5lBb4yOY4vF4qlTF5LlQEbn_WNsQLFQ5rkwbLGOl2G0CeJw4_nPLKiSUskC7rX4DjK-h2-lAq2FLJjQLM54kogiYTkvzqyAEwu5Bs08_-GUijBlJ1DACmlQ-xE_Riw4M_gmtKFJv70emAFtPH83Y-xzCiZFHFLZCfjZKjxxVaCwZhw1hbLQIrKKQqkUhIYw4gw4QsEi0ELxIAN6iOhKUCkvNdNV8BeKiiN4yye0yLBXFsl6FVF8bdcKIOVHgepwsgKuZUHaZt78xZs_uvdXhvaXnBDgXBJDeqIqROBGMpFllTaKG-h0yRiFMjjywtTsBBBLNIZWrUoUB56zDxkPJE2MDucSEHLCFXEUprxIgAhFAVQT4Uf_ySkYuMpHFsluXkSMW4ealJtugUlFzvMXetAbv0sDToM1M5QRss8LxE6BUgpSwDUDYX2G057xH3nCfXyyf34DyCo6oXyEocTKSpVSQ1xl2XnEGnHSMheJOEZfFyFyJgo7FqYQfm1ss4FEMdXxeZVGG9wDT3qbef1nvzJ6LZh7ef5SxBHE7MuXsERw9K8n5ndiKCTQpLfDH__5_NITWXYikGnojayuTGZIQYUB3UmuezoKJKM3tGmGukfb-pHAODtgVq3R1ehL5m2felIPrHkZJIhMJPlHpfh5MffWT_T2gmnSm7K7WGsxb7U40Oy_5zf3YbDaYs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