summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJ.Ru <juanruben.segovia@gmail.com>2017-10-01 18:02:40 +0200
committerKim Gräsman <kim.grasman@gmail.com>2017-10-04 20:05:19 +0200
commitcd157da809cc50f1ce0b1be0373413ab3b8b64c7 (patch)
tree59202195a0847aaadcd1ac675df1d09ba7559058
parentfbcc7e31f240920201bae37ff598e6a67a18384e (diff)
Skip non-ctors during instantiation of implicit methods
InstantiateImplicitMethods() wrongly assumed a ctor decl must be present just by checking that it is not a template decl. This is no longer true in C++11 where a using decl is also a valid ctor type. Fixes #402
-rw-r--r--iwyu.cc7
-rwxr-xr-xrun_iwyu_tests.py1
-rw-r--r--tests/cxx/inheriting_ctor-d1.h15
-rw-r--r--tests/cxx/inheriting_ctor-i1.h20
-rw-r--r--tests/cxx/inheriting_ctor.cc26
5 files changed, 67 insertions, 2 deletions
diff --git a/iwyu.cc b/iwyu.cc
index a861712..9d0294a 100644
--- a/iwyu.cc
+++ b/iwyu.cc
@@ -163,6 +163,7 @@ using clang::CallExpr;
using clang::ClassTemplateDecl;
using clang::ClassTemplateSpecializationDecl;
using clang::CompilerInstance;
+using clang::ConstructorUsingShadowDecl;
using clang::Decl;
using clang::DeclContext;
using clang::DeclRefExpr;
@@ -618,8 +619,10 @@ class BaseAstVisitor : public RecursiveASTVisitor<Derived> {
clang::Sema& sema = compiler_->getSema();
DeclContext::lookup_result ctors = sema.LookupConstructors(decl);
for (NamedDecl* ctor_lookup : ctors) {
- // Ignore templated constructors.
- if (isa<FunctionTemplateDecl>(ctor_lookup))
+ // Ignore templated or inheriting constructors.
+ if (isa<FunctionTemplateDecl>(ctor_lookup) ||
+ isa<UsingDecl>(ctor_lookup) ||
+ isa<ConstructorUsingShadowDecl>(ctor_lookup))
continue;
CXXConstructorDecl* ctor = cast<CXXConstructorDecl>(ctor_lookup);
if (!ctor->hasBody() && !ctor->isDeleted() && ctor->isImplicit()) {
diff --git a/run_iwyu_tests.py b/run_iwyu_tests.py
index b06bf38..7e31b19 100755
--- a/run_iwyu_tests.py
+++ b/run_iwyu_tests.py
@@ -108,6 +108,7 @@ class OneIwyuTest(unittest.TestCase):
'prefix_header_includes_keep.cc': prefix_headers,
'prefix_header_includes_remove.cc': prefix_headers,
'typedef_in_template.cc': ['-std=c++11'],
+ 'inheriting_ctor.cc': ['-std=c++11'],
}
include_map = {
'alias_template.cc': ['.'],
diff --git a/tests/cxx/inheriting_ctor-d1.h b/tests/cxx/inheriting_ctor-d1.h
new file mode 100644
index 0000000..ba1b5a8
--- /dev/null
+++ b/tests/cxx/inheriting_ctor-d1.h
@@ -0,0 +1,15 @@
+//===--- inheriting_ctor-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_INHERITING_CTOR_D1_H_
+#define INCLUDE_WHAT_YOU_USE_TESTS_CXX_INHERITING_CTOR_D1_H_
+
+#include "inheriting_ctor-i1.h"
+
+#endif // INCLUDE_WHAT_YOU_USE_TESTS_CXX_INHERITING_CTOR_D1_H_
diff --git a/tests/cxx/inheriting_ctor-i1.h b/tests/cxx/inheriting_ctor-i1.h
new file mode 100644
index 0000000..03ecddb
--- /dev/null
+++ b/tests/cxx/inheriting_ctor-i1.h
@@ -0,0 +1,20 @@
+//===--- inheriting_ctor-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_INHERITING_CTOR_I1_H_
+#define INCLUDE_WHAT_YOU_USE_TESTS_CXX_INHERITING_CTOR_I1_H_
+
+struct Base {
+ Base(int);
+};
+struct Derived : Base {
+ using Base::Base;
+};
+
+#endif // INCLUDE_WHAT_YOU_USE_TESTS_CXX_INHERITING_CTOR_I1_H_
diff --git a/tests/cxx/inheriting_ctor.cc b/tests/cxx/inheriting_ctor.cc
new file mode 100644
index 0000000..b91753b
--- /dev/null
+++ b/tests/cxx/inheriting_ctor.cc
@@ -0,0 +1,26 @@
+//===--- inheriting_ctor.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 "inheriting_ctor-d1.h"
+
+// IWYU: Derived is defined in .*-i1.h
+void func() { Derived d(1); }
+
+/**** IWYU_SUMMARY
+
+tests/cxx/inheriting_ctor.cc should add these lines:
+#include "inheriting_ctor-i1.h"
+
+tests/cxx/inheriting_ctor.cc should remove these lines:
+- #include "inheriting_ctor-d1.h" // lines XX-XX
+
+The full include-list for tests/cxx/inheriting_ctor.cc:
+#include "inheriting_ctor-i1.h" // for Derived
+
+***** IWYU_SUMMARY */