r323998 - PR36157: When injecting an implicit function declaration in C89, find the right

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 1 12:01:49 PST 2018


Author: rsmith
Date: Thu Feb  1 12:01:49 2018
New Revision: 323998

URL: http://llvm.org/viewvc/llvm-project?rev=323998&view=rev
Log:
PR36157: When injecting an implicit function declaration in C89, find the right
DeclContext rather than injecting it wherever we happen to be.

This avoids creating functions whose DeclContext is a struct or similar.

Added:
    cfe/trunk/test/Sema/cxx-as-c.c
Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/Sema/bitfield.c

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=323998&r1=323997&r2=323998&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu Feb  1 12:01:49 2018
@@ -12910,10 +12910,20 @@ void Sema::ActOnFinishDelayedAttribute(S
 /// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
 NamedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc,
                                           IdentifierInfo &II, Scope *S) {
+  // Find the scope in which the identifier is injected and the corresponding
+  // DeclContext.
+  // FIXME: C89 does not say what happens if there is no enclosing block scope.
+  // In that case, we inject the declaration into the translation unit scope
+  // instead.
   Scope *BlockScope = S;
   while (!BlockScope->isCompoundStmtScope() && BlockScope->getParent())
     BlockScope = BlockScope->getParent();
 
+  Scope *ContextScope = BlockScope;
+  while (!ContextScope->getEntity())
+    ContextScope = ContextScope->getParent();
+  ContextRAII SavedContext(*this, ContextScope->getEntity());
+
   // Before we produce a declaration for an implicitly defined
   // function, see whether there was a locally-scoped declaration of
   // this name as a function or variable. If so, use that

Modified: cfe/trunk/test/Sema/bitfield.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/bitfield.c?rev=323998&r1=323997&r2=323998&view=diff
==============================================================================
--- cfe/trunk/test/Sema/bitfield.c (original)
+++ cfe/trunk/test/Sema/bitfield.c Thu Feb  1 12:01:49 2018
@@ -82,3 +82,7 @@ typedef __typeof__(+(t5.n--)) Unsigned;
 struct Test6 {
   : 0.0; // expected-error{{type name requires a specifier or qualifier}}
 };
+
+struct PR36157 {
+  int n : 1 ? 1 : implicitly_declare_function(); // expected-warning {{invalid in C99}}
+};

Added: cfe/trunk/test/Sema/cxx-as-c.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/cxx-as-c.c?rev=323998&view=auto
==============================================================================
--- cfe/trunk/test/Sema/cxx-as-c.c (added)
+++ cfe/trunk/test/Sema/cxx-as-c.c Thu Feb  1 12:01:49 2018
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 %s -verify
+
+// PR36157
+struct Foo {
+  Foo(int n) : n_(n) {} // expected-error 1+{{}} expected-warning 1+{{}}
+private:
+  int n;
+};
+int main() { Foo f; } // expected-error 1+{{}}




More information about the cfe-commits mailing list