From: Richard Smith Date: Mon, 19 Nov 2012 21:13:18 +0000 (+0000) Subject: PR14381: Never skip constexpr function bodies when code-completing. We may need X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1a5bd5d680726f3d133da27791b228b2c8fe96c6;p=clang PR14381: Never skip constexpr function bodies when code-completing. We may need them in order to parse the rest of the file. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@168327 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index a67e630cfd..ee365c3299 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -1379,6 +1379,15 @@ public: return D && isa(D); } + /// \brief Determine whether we can skip parsing the body of a function + /// definition, assuming we don't care about analyzing its body or emitting + /// code for that function. + /// + /// This will be \c false only if we may need the body of the function in + /// order to parse the rest of the program (for instance, if it is + /// \c constexpr in C++11 or has an 'auto' return type in C++14). + bool canSkipFunctionBody(Decl *D); + void computeNRVO(Stmt *Body, sema::FunctionScopeInfo *Scope); Decl *ActOnFinishFunctionBody(Decl *Decl, Stmt *Body); Decl *ActOnFinishFunctionBody(Decl *Decl, Stmt *Body, bool IsInstantiation); diff --git a/lib/Parse/ParseStmt.cpp b/lib/Parse/ParseStmt.cpp index a4e0c9844a..12f9fac484 100644 --- a/lib/Parse/ParseStmt.cpp +++ b/lib/Parse/ParseStmt.cpp @@ -2003,7 +2003,8 @@ Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) { assert(Tok.is(tok::l_brace)); SourceLocation LBraceLoc = Tok.getLocation(); - if (SkipFunctionBodies && trySkippingFunctionBody()) { + if (SkipFunctionBodies && Actions.canSkipFunctionBody(Decl) && + trySkippingFunctionBody()) { BodyScope.Exit(); return Actions.ActOnFinishFunctionBody(Decl, 0); } @@ -2045,7 +2046,8 @@ Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) { else Actions.ActOnDefaultCtorInitializers(Decl); - if (SkipFunctionBodies && trySkippingFunctionBody()) { + if (SkipFunctionBodies && Actions.canSkipFunctionBody(Decl) && + trySkippingFunctionBody()) { BodyScope.Exit(); return Actions.ActOnFinishFunctionBody(Decl, 0); } diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index cc19ce9959..629fccc306 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -7984,6 +7984,22 @@ void Sema::computeNRVO(Stmt *Body, FunctionScopeInfo *Scope) { const_cast(NRVOCandidate)->setNRVOVariable(true); } +bool Sema::canSkipFunctionBody(Decl *D) { + if (isa(D)) + return true; + + FunctionDecl *FD = 0; + if (FunctionTemplateDecl *FTD = dyn_cast(D)) + FD = FTD->getTemplatedDecl(); + else + FD = cast(D); + + // We cannot skip the body of a function (or function template) which is + // constexpr, since we may need to evaluate its body in order to parse the + // rest of the file. + return !FD->isConstexpr(); +} + Decl *Sema::ActOnFinishFunctionBody(Decl *D, Stmt *BodyArg) { return ActOnFinishFunctionBody(D, BodyArg, false); } diff --git a/test/CodeCompletion/constexpr.cpp b/test/CodeCompletion/constexpr.cpp new file mode 100644 index 0000000000..12396c0198 --- /dev/null +++ b/test/CodeCompletion/constexpr.cpp @@ -0,0 +1,13 @@ +// RUN: %clang_cc1 -fsyntax-only -std=c++11 -code-completion-at=%s:12:9 %s -o - | FileCheck %s + +// PR14381: need constexpr function bodies always, even if code-completing. +template struct S; +template<> struct S<1> { + typedef int type; +}; +constexpr int f() { + return 1; +} + +S:: +// CHECK: COMPLETION: type : type