[clang] 654c0da - Suppress -Wwarn-unused-variables when we don't know the constructor
Erich Keane via cfe-commits
cfe-commits at lists.llvm.org
Thu Dec 12 11:36:30 PST 2019
Author: Erich Keane
Date: 2019-12-12T11:34:17-08:00
New Revision: 654c0daef75a41eede06b413a5ecdd8ad2640240
URL: https://github.com/llvm/llvm-project/commit/654c0daef75a41eede06b413a5ecdd8ad2640240
DIFF: https://github.com/llvm/llvm-project/commit/654c0daef75a41eede06b413a5ecdd8ad2640240.diff
LOG: Suppress -Wwarn-unused-variables when we don't know the constructor
This warning is supposed to be suppressed when the
constructor/destructor are non-trivial, since it might be a RAII type.
However, if the type has a trivial destructor and the constructor hasn't
been resolved (since it is called with dependent arguments), we were
still warning.
This patch suppresses the warning if the type could possibly have a
be a non-trivial constructor call. Note that this does not take the
arity of the constructors into consideration, so it might suppress
the warning in cases where it isn't possible to call a non-trivial
constructor.
Added:
Modified:
clang/lib/Sema/SemaDecl.cpp
clang/test/SemaCXX/warn-unused-variables.cpp
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 1cf87e45a299..c25712452013 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -1805,6 +1805,13 @@ static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) {
(VD->getInit()->isValueDependent() || !VD->evaluateValue()))
return false;
}
+
+ // Suppress the warning if we don't know how this is constructed, and
+ // it could possibly be non-trivial constructor.
+ if (Init->isTypeDependent())
+ for (const CXXConstructorDecl *Ctor : RD->ctors())
+ if (!Ctor->isTrivial())
+ return false;
}
}
}
diff --git a/clang/test/SemaCXX/warn-unused-variables.cpp b/clang/test/SemaCXX/warn-unused-variables.cpp
index 97634ac43c57..2634fb1ec0f7 100644
--- a/clang/test/SemaCXX/warn-unused-variables.cpp
+++ b/clang/test/SemaCXX/warn-unused-variables.cpp
@@ -238,4 +238,18 @@ void c() {
a d(b::e ? "" : "");
}
}
+
+// Ensure we don't warn on dependent constructor calls.
+namespace dependent_ctor {
+struct S {
+ S() = default;
+ S(const S &) = default;
+ S(int);
+};
+
+template <typename T>
+void foo(T &t) {
+ S s{t};
+}
+}
#endif
More information about the cfe-commits
mailing list