From f86b5dc7009e7686fc31ff0d1e8a7bff138607cd Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Sun, 9 Dec 2012 05:55:43 +0000 Subject: [PATCH] PR14550: If a system header contains a bogus constexpr function definition, don't mark the function as invalid, since we suppress the error. llvm-svn: 169689 --- .../clang/Basic/DiagnosticSemaKinds.td | 2 +- clang/lib/Sema/SemaDeclCXX.cpp | 5 +++-- .../CXX/dcl.dcl/dcl.spec/dcl.constexpr/p5.cpp | 22 ++++++++++++++++++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 399449dc97cb..7a529d6c98ab 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -1538,7 +1538,7 @@ def err_constexpr_vla : Error< "%select{function|constructor}1">; def err_constexpr_var_declaration : Error< "variables cannot be declared in a constexpr %select{function|constructor}0">; -def err_constexpr_function_never_constant_expr : ExtWarn< +def ext_constexpr_function_never_constant_expr : ExtWarn< "constexpr %select{function|constructor}0 never produces a " "constant expression">, InGroup>, DefaultError; def err_constexpr_body_no_return : Error< diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index 5cca43b54808..6f1b489a2d28 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -994,11 +994,12 @@ bool Sema::CheckConstexprFunctionBody(const FunctionDecl *Dcl, Stmt *Body) { // base class sub-objects shall be a constexpr constructor. llvm::SmallVector Diags; if (!Expr::isPotentialConstantExpr(Dcl, Diags)) { - Diag(Dcl->getLocation(), diag::err_constexpr_function_never_constant_expr) + Diag(Dcl->getLocation(), diag::ext_constexpr_function_never_constant_expr) << isa(Dcl); for (size_t I = 0, N = Diags.size(); I != N; ++I) Diag(Diags[I].first, Diags[I].second); - return false; + // Don't return false here: we allow this for compatibility in + // system headers. } return true; diff --git a/clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p5.cpp b/clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p5.cpp index 3c1152c631b8..bca73ee85f6d 100644 --- a/clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p5.cpp +++ b/clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p5.cpp @@ -1,5 +1,5 @@ // RUN: %clang_cc1 -fsyntax-only -triple x86_64-unknown-unknown -verify -std=c++11 -fcxx-exceptions %s -// RUN: %clang_cc1 -fsyntax-only -triple x86_64-unknown-unknown -std=c++11 -fcxx-exceptions -Wno-invalid-constexpr %s +// RUN: %clang_cc1 -fsyntax-only -triple x86_64-unknown-unknown -std=c++11 -fcxx-exceptions -Wno-invalid-constexpr %s -DNO_INVALID_CONSTEXPR namespace StdExample { @@ -110,3 +110,23 @@ int y1 = Y().get(); // ok int y2 = Y().get(); // ok } + +#ifndef NO_INVALID_CONSTEXPR +namespace PR14550 { + // As an "extension", we allow functions which can't produce constant + // expressions to be declared constexpr in system headers (libstdc++ + // marks some functions as constexpr which use builtins which we don't + // support constant folding). Ensure that we don't mark those functions + // as invalid after suppressing the diagnostic. +# 122 "p5.cpp" 1 3 + int n; + struct A { + static constexpr int f() { return n; } + }; + template struct B { + B() { g(T::f()); } // expected-error {{undeclared identifier 'g'}} + }; +# 130 "p5.cpp" 2 + template class B; // expected-note {{here}} +} +#endif