LukasScheucher / include-what-you-use

Automatically exported from code.google.com/p/include-what-you-use
Other
0 stars 0 forks source link

false positive: suggested to remove #include when inline function accesses a type's subscript #33

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Given the following files:

a.cpp >>>>>
#include "b.h"
class A {
    B *getB(int i) { return &(_b[i]); }
    B *_b;
};
<<<<<

b.h >>>>>
class B {
    int foo;
};
<<<<<

"$ include-what-you-use a.cpp" suggests:
    a.cpp should add these lines:
    class B;

    a.cpp should remove these lines:
    - #include "b.h"  // lines 1-1

Doing so leads to a compilation error:
   a.cpp:4:30: error: subscript of pointer to incomplete type 'B'
           B *getB(int i) { return &(_b[i]); }

Original issue reported on code.google.com by saltyho...@gmail.com on 1 May 2011 at 1:51

GoogleCodeExporter commented 8 years ago
Wow, this seems like a really simple case, for iwyu to be getting it wrong.  
But I can definitely reproduce it.

It looks like the problem is that iwyu just doesn't intercept 
ArraySubscriptExpr -- we need to treat this similarly to MemberExpr or the 
like.  Would you like to try to add code to handle it?  I wrote up a simple 
test file (which I'm not committing because I don't have XFAIL support in my 
testing suite yet), which you can use to test your change (I put it in 
tests/array.cc):
---

//===--- array.cc - test input file for iwyu ------------------------------===/\
/
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===/\
/

// Tests that we handle correctly identify a[i] as a full use of a.

#include "devtools/maintenance/include_what_you_use/tests/direct.h"

class A {
  // IWYU: IndirectClass needs a declaration
  IndirectClass *getIndirectClass(int i) {
    // IWYU: IndirectClass is...*indirect.h
    return &(_b[i]);
  }
  IndirectClass *_b;
};

/**** IWYU_SUMMARY

devtools/maintenance/include_what_you_use/tests/array.cc should add these lines:
#include "devtools/maintenance/include_what_you_use/tests/indirect.h"

devtools/maintenance/include_what_you_use/tests/array.cc should remove these 
lines:
- #include "devtools/maintenance/include_what_you_use/tests/direct.h"  // lines 
XX-XX

The full include-list for 
devtools/maintenance/include_what_you_use/tests/array.cc:
#include "devtools/maintenance/include_what_you_use/tests/indirect.h"  // for 
IndirectClass

***** IWYU_SUMMARY */

Original comment by csilv...@gmail.com on 2 May 2011 at 2:58

GoogleCodeExporter commented 8 years ago
Never mind about a patch, I ended up fixing this myself in r201

Original comment by csilv...@gmail.com on 4 May 2011 at 6:40