[cfe-commits] r168327 - in /cfe/trunk: include/clang/Sema/Sema.h lib/Parse/ParseStmt.cpp lib/Sema/SemaDecl.cpp test/CodeCompletion/constexpr.cpp

Richard Smith richard-llvm at metafoo.co.uk
Mon Nov 19 13:13:18 PST 2012


Author: rsmith
Date: Mon Nov 19 15:13:18 2012
New Revision: 168327

URL: http://llvm.org/viewvc/llvm-project?rev=168327&view=rev
Log:
PR14381: Never skip constexpr function bodies when code-completing. We may need
them in order to parse the rest of the file.

Added:
    cfe/trunk/test/CodeCompletion/constexpr.cpp
Modified:
    cfe/trunk/include/clang/Sema/Sema.h
    cfe/trunk/lib/Parse/ParseStmt.cpp
    cfe/trunk/lib/Sema/SemaDecl.cpp

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=168327&r1=168326&r2=168327&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Mon Nov 19 15:13:18 2012
@@ -1379,6 +1379,15 @@
     return D && isa<ObjCMethodDecl>(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);

Modified: cfe/trunk/lib/Parse/ParseStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseStmt.cpp?rev=168327&r1=168326&r2=168327&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseStmt.cpp (original)
+++ cfe/trunk/lib/Parse/ParseStmt.cpp Mon Nov 19 15:13:18 2012
@@ -2003,7 +2003,8 @@
   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 @@
   else
     Actions.ActOnDefaultCtorInitializers(Decl);
 
-  if (SkipFunctionBodies && trySkippingFunctionBody()) {
+  if (SkipFunctionBodies && Actions.canSkipFunctionBody(Decl) &&
+      trySkippingFunctionBody()) {
     BodyScope.Exit();
     return Actions.ActOnFinishFunctionBody(Decl, 0);
   }

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=168327&r1=168326&r2=168327&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Mon Nov 19 15:13:18 2012
@@ -7984,6 +7984,22 @@
     const_cast<VarDecl*>(NRVOCandidate)->setNRVOVariable(true);
 }
 
+bool Sema::canSkipFunctionBody(Decl *D) {
+  if (isa<ObjCMethodDecl>(D))
+    return true;
+
+  FunctionDecl *FD = 0;
+  if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
+    FD = FTD->getTemplatedDecl();
+  else
+    FD = cast<FunctionDecl>(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);
 }

Added: cfe/trunk/test/CodeCompletion/constexpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeCompletion/constexpr.cpp?rev=168327&view=auto
==============================================================================
--- cfe/trunk/test/CodeCompletion/constexpr.cpp (added)
+++ cfe/trunk/test/CodeCompletion/constexpr.cpp Mon Nov 19 15:13:18 2012
@@ -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<int> struct S;
+template<> struct S<1> {
+  typedef int type;
+};
+constexpr int f() {
+  return 1;
+}
+
+S<f()>::
+// CHECK: COMPLETION: type : type





More information about the cfe-commits mailing list