Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1a5bd5d

Browse files
committed
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
1 parent 728948f commit 1a5bd5d

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

include/clang/Sema/Sema.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,6 +1379,15 @@ class Sema {
13791379
return D && isa<ObjCMethodDecl>(D);
13801380
}
13811381

1382+
/// \brief Determine whether we can skip parsing the body of a function
1383+
/// definition, assuming we don't care about analyzing its body or emitting
1384+
/// code for that function.
1385+
///
1386+
/// This will be \c false only if we may need the body of the function in
1387+
/// order to parse the rest of the program (for instance, if it is
1388+
/// \c constexpr in C++11 or has an 'auto' return type in C++14).
1389+
bool canSkipFunctionBody(Decl *D);
1390+
13821391
void computeNRVO(Stmt *Body, sema::FunctionScopeInfo *Scope);
13831392
Decl *ActOnFinishFunctionBody(Decl *Decl, Stmt *Body);
13841393
Decl *ActOnFinishFunctionBody(Decl *Decl, Stmt *Body, bool IsInstantiation);

lib/Parse/ParseStmt.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,7 +2003,8 @@ Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
20032003
assert(Tok.is(tok::l_brace));
20042004
SourceLocation LBraceLoc = Tok.getLocation();
20052005

2006-
if (SkipFunctionBodies && trySkippingFunctionBody()) {
2006+
if (SkipFunctionBodies && Actions.canSkipFunctionBody(Decl) &&
2007+
trySkippingFunctionBody()) {
20072008
BodyScope.Exit();
20082009
return Actions.ActOnFinishFunctionBody(Decl, 0);
20092010
}
@@ -2045,7 +2046,8 @@ Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
20452046
else
20462047
Actions.ActOnDefaultCtorInitializers(Decl);
20472048

2048-
if (SkipFunctionBodies && trySkippingFunctionBody()) {
2049+
if (SkipFunctionBodies && Actions.canSkipFunctionBody(Decl) &&
2050+
trySkippingFunctionBody()) {
20492051
BodyScope.Exit();
20502052
return Actions.ActOnFinishFunctionBody(Decl, 0);
20512053
}

lib/Sema/SemaDecl.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7984,6 +7984,22 @@ void Sema::computeNRVO(Stmt *Body, FunctionScopeInfo *Scope) {
79847984
const_cast<VarDecl*>(NRVOCandidate)->setNRVOVariable(true);
79857985
}
79867986

7987+
bool Sema::canSkipFunctionBody(Decl *D) {
7988+
if (isa<ObjCMethodDecl>(D))
7989+
return true;
7990+
7991+
FunctionDecl *FD = 0;
7992+
if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
7993+
FD = FTD->getTemplatedDecl();
7994+
else
7995+
FD = cast<FunctionDecl>(D);
7996+
7997+
// We cannot skip the body of a function (or function template) which is
7998+
// constexpr, since we may need to evaluate its body in order to parse the
7999+
// rest of the file.
8000+
return !FD->isConstexpr();
8001+
}
8002+
79878003
Decl *Sema::ActOnFinishFunctionBody(Decl *D, Stmt *BodyArg) {
79888004
return ActOnFinishFunctionBody(D, BodyArg, false);
79898005
}

test/CodeCompletion/constexpr.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %clang_cc1 -fsyntax-only -std=c++11 -code-completion-at=%s:12:9 %s -o - | FileCheck %s
2+
3+
// PR14381: need constexpr function bodies always, even if code-completing.
4+
template<int> struct S;
5+
template<> struct S<1> {
6+
typedef int type;
7+
};
8+
constexpr int f() {
9+
return 1;
10+
}
11+
12+
S<f()>::
13+
// CHECK: COMPLETION: type : type

0 commit comments

Comments
 (0)