summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Gräsman <kim.grasman@gmail.com>2023-02-22 20:15:22 +0100
committerKim Gräsman <kim.grasman@gmail.com>2023-03-05 14:43:19 +0100
commit4baf5f522b17679c45f07b645fe84513109174f4 (patch)
treeaad5a502ab9334363737e9bb95a1f4d651523c66
parent0d110e52a3f83446ade55b18c1424523c56449b8 (diff)
Add testcase for template-spills
We've seen a lot of problems with template instantiations, that IWYU mistakenly attributes implementation details in the template to the instantiation location. This testcase replicates a few common standard library constructs that keep triggering this: * Constructor uses unrelated type (Inner<T> does sizeof(detail::Detail)) * Constructor body uses try/catch (Inner<T> catches Inner<T>) * Constructor body uses range for (Inner<T> iterates over itself) * Operator overloads returning nested composite types (Outer returns Inner<T>::value_type) There are other type reporting constructs as well, but they are harder to exercise succinctly. Assume all type reporting is covered by the same mechanics, so we can use this as a trigger for regressions.
-rw-r--r--tests/cxx/tpl_spill_nested-d1.h19
-rw-r--r--tests/cxx/tpl_spill_nested-d2.h32
-rw-r--r--tests/cxx/tpl_spill_nested-d3.h12
-rw-r--r--tests/cxx/tpl_spill_nested.cc26
4 files changed, 89 insertions, 0 deletions
diff --git a/tests/cxx/tpl_spill_nested-d1.h b/tests/cxx/tpl_spill_nested-d1.h
new file mode 100644
index 0000000..56da901
--- /dev/null
+++ b/tests/cxx/tpl_spill_nested-d1.h
@@ -0,0 +1,19 @@
+//===--- tpl_spill_nested-d1.h - test input file for iwyu ------*- C++ -*--===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "tests/cxx/tpl_spill_nested-d2.h"
+
+template <class T>
+struct Outer {
+ Inner<T> inner;
+
+ typename Inner<T>::value_type& operator[](int index) {
+ return *(inner.begin() + index);
+ }
+};
diff --git a/tests/cxx/tpl_spill_nested-d2.h b/tests/cxx/tpl_spill_nested-d2.h
new file mode 100644
index 0000000..697e09f
--- /dev/null
+++ b/tests/cxx/tpl_spill_nested-d2.h
@@ -0,0 +1,32 @@
+//===--- tpl_spill_nested-d2.h - test input file for iwyu ------*- C++ -*--===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "tests/cxx/tpl_spill_nested-d3.h"
+
+template <class T>
+struct Inner {
+ typedef T value_type;
+ typedef value_type* iterator;
+ iterator begin();
+ iterator end();
+
+ Inner()
+ // Use an internal type.
+ : size(sizeof(detail::Detail)) {
+ // Throw in a few statements that exercise type reporting.
+ try {
+ } catch (const Inner<T>&) {
+ }
+
+ for (const value_type& x : *this) {
+ }
+ }
+
+ unsigned long long size;
+};
diff --git a/tests/cxx/tpl_spill_nested-d3.h b/tests/cxx/tpl_spill_nested-d3.h
new file mode 100644
index 0000000..5554d7f
--- /dev/null
+++ b/tests/cxx/tpl_spill_nested-d3.h
@@ -0,0 +1,12 @@
+//===--- tpl_spill_nested-d3.h - test input file for iwyu ------*- C++ -*--===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+namespace detail {
+struct Detail {};
+} // namespace detail
diff --git a/tests/cxx/tpl_spill_nested.cc b/tests/cxx/tpl_spill_nested.cc
new file mode 100644
index 0000000..9ffd1ce
--- /dev/null
+++ b/tests/cxx/tpl_spill_nested.cc
@@ -0,0 +1,26 @@
+//===--- tpl_spill_nested.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/tpl_spill_nested-d1.h"
+
+// IWYU_ARGS: -I .
+
+void Instantiate() {
+ // No diagnostic expected.
+ Outer<int> o;
+
+ // No diagnostic expected.
+ o[100] = 1;
+}
+
+/**** IWYU_SUMMARY
+
+(tests/cxx/tpl_spill_nested.cc has correct #includes/fwd-decls)
+
+***** IWYU_SUMMARY */