diff options
author | John Bytheway <jbytheway@gmail.com> | 2018-11-27 19:53:15 +0000 |
---|---|---|
committer | Kim Gräsman <kim.grasman@gmail.com> | 2019-01-05 21:49:10 +0100 |
commit | 9945e543d894e90fab5ebd0b685cabd5aa8dd21f (patch) | |
tree | 8714409e4db4de43c24f3c037c7523f99524052d | |
parent | fff6dadc08822804bf380ed2cf0169d12558f7b4 (diff) |
Support nested wrapper headers
The existing code coped poorly with the case where header A includes B
includes C includes D where D is a private header and B and C are both
public headers for a symbol in D. In this case B would be forced to
include D directly when it ought only to be including C.
Fix this by an additional condition on the special case intended to
handle these sorts of situations.
For further detail, see discussion at
https://github.com/include-what-you-use/include-what-you-use/pull/615
Also test.
-rw-r--r-- | iwyu_output.cc | 6 | ||||
-rwxr-xr-x | run_iwyu_tests.py | 1 | ||||
-rw-r--r-- | tests/cxx/export_nesting-d1.h | 15 | ||||
-rw-r--r-- | tests/cxx/export_nesting-i1.h | 15 | ||||
-rw-r--r-- | tests/cxx/export_nesting.cc | 18 | ||||
-rw-r--r-- | tests/cxx/export_nesting.h | 26 |
6 files changed, 79 insertions, 2 deletions
diff --git a/iwyu_output.cc b/iwyu_output.cc index 797c4d3..faee317 100644 --- a/iwyu_output.cc +++ b/iwyu_output.cc @@ -754,8 +754,10 @@ set<string> CalculateMinimalIncludes( // this is a file that the use-file is re-exporting symbols for, // and we should keep the #include as-is. const string use_file = ConvertToQuotedInclude(GetFilePath(use.use_loc())); - if (use.PublicHeadersContain(use_file)) { - use.set_suggested_header(ConvertToQuotedInclude(use.decl_filepath())); + const string decl_file = ConvertToQuotedInclude(use.decl_filepath()); + if (use.PublicHeadersContain(use_file) && + ContainsKey(direct_includes, decl_file)) { + use.set_suggested_header(decl_file); desired_headers.insert(use.suggested_header()); LogIncludeMapping("private header", use); } else if (use.public_headers().size() == 1) { diff --git a/run_iwyu_tests.py b/run_iwyu_tests.py index 746c1d4..33798ae 100755 --- a/run_iwyu_tests.py +++ b/run_iwyu_tests.py @@ -137,6 +137,7 @@ class OneIwyuTest(unittest.TestCase): 'double_include.cc': ['.'], 'elaborated_struct.c': ['.'], 'elaborated_type.cc': ['.'], + 'export_nesting.cc': ['.'], 'external_including_internal.cc': ['.'], 'forward_declare_in_macro.cc': ['.'], 'fullinfo_for_templates.cc': ['.'], diff --git a/tests/cxx/export_nesting-d1.h b/tests/cxx/export_nesting-d1.h new file mode 100644 index 0000000..8926eee --- /dev/null +++ b/tests/cxx/export_nesting-d1.h @@ -0,0 +1,15 @@ +//===--- export_nesting-d1.h - 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. +// +//===----------------------------------------------------------------------===// + +#ifndef INCLUDE_WHAT_YOU_USE_TESTS_CXX_EXPORT_NESTING_D1_H_ +#define INCLUDE_WHAT_YOU_USE_TESTS_CXX_EXPORT_NESTING_D1_H_ + +#include "tests/cxx/export_nesting-i1.h" // IWYU pragma: export + +#endif // INCLUDE_WHAT_YOU_USE_TESTS_CXX_EXPORT_NESTING_D1_H_ diff --git a/tests/cxx/export_nesting-i1.h b/tests/cxx/export_nesting-i1.h new file mode 100644 index 0000000..3bb7979 --- /dev/null +++ b/tests/cxx/export_nesting-i1.h @@ -0,0 +1,15 @@ +//===--- export_nesting-i1.h - 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. +// +//===----------------------------------------------------------------------===// + +#ifndef INCLUDE_WHAT_YOU_USE_TESTS_CXX_EXPORT_NESTING_I1_H_ +#define INCLUDE_WHAT_YOU_USE_TESTS_CXX_EXPORT_NESTING_I1_H_ + +enum Nested_Enum {}; + +#endif // INCLUDE_WHAT_YOU_USE_TESTS_CXX_EXPORT_NESTING_I1_H_ diff --git a/tests/cxx/export_nesting.cc b/tests/cxx/export_nesting.cc new file mode 100644 index 0000000..4fa488b --- /dev/null +++ b/tests/cxx/export_nesting.cc @@ -0,0 +1,18 @@ +//===--- export_nesting.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. +// +//===----------------------------------------------------------------------===// + +#include "tests/cxx/export_nesting.h" + +Nested_Enum x; + +/**** IWYU_SUMMARY + +(tests/cxx/export_nesting.cc has correct #includes/fwd-decls) + +***** IWYU_SUMMARY */ diff --git a/tests/cxx/export_nesting.h b/tests/cxx/export_nesting.h new file mode 100644 index 0000000..a8f37b3 --- /dev/null +++ b/tests/cxx/export_nesting.h @@ -0,0 +1,26 @@ +//===--- export_nesting.h - 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. +// +//===----------------------------------------------------------------------===// + +#ifndef INCLUDE_WHAT_YOU_USE_TESTS_CXX_EXPORT_NESTING_H_ +#define INCLUDE_WHAT_YOU_USE_TESTS_CXX_EXPORT_NESTING_H_ + +// We export a header which already re-exports things to verify that nested +// exports are acceptable. + +#include "tests/cxx/export_nesting-d1.h" // IWYU pragma: export + +Nested_Enum f; + +#endif // INCLUDE_WHAT_YOU_USE_TESTS_CXX_EXPORT_NESTING_H_ + +/**** IWYU_SUMMARY + +(tests/cxx/export_nesting.h has correct #includes/fwd-decls) + +***** IWYU_SUMMARY */ |