[cfe-commits] r127737 - in /cfe/trunk: include/clang/Sema/IdentifierResolver.h lib/Sema/IdentifierResolver.cpp lib/Sema/SemaDecl.cpp test/SemaCXX/goto.cpp

Douglas Gregor dgregor at apple.com
Wed Mar 16 09:39:03 PDT 2011


Author: dgregor
Date: Wed Mar 16 11:39:03 2011
New Revision: 127737

URL: http://llvm.org/viewvc/llvm-project?rev=127737&view=rev
Log:
When we're inserting a synthesized label declaration for a
forward-looking "goto" statement, make sure to insert it *after* the
last declaration in the identifier resolver's declaration chain that
is either outside of the function/block/method's scope or that is
declared in that function/block/method's specific scope. Previously,
we could end up inserting the label ahead of declarations in inner
scopes, confusing C++ name lookup.

Fixes PR9491/<rdar://problem/9140426> and <rdar://problem/9135994>.

Note that the crash-on-invalid PR9495 is *not* fixed. That's a
separate issue.

Modified:
    cfe/trunk/include/clang/Sema/IdentifierResolver.h
    cfe/trunk/lib/Sema/IdentifierResolver.cpp
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/SemaCXX/goto.cpp

Modified: cfe/trunk/include/clang/Sema/IdentifierResolver.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/IdentifierResolver.h?rev=127737&r1=127736&r2=127737&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/IdentifierResolver.h (original)
+++ cfe/trunk/include/clang/Sema/IdentifierResolver.h Wed Mar 16 11:39:03 2011
@@ -171,9 +171,9 @@
   /// (and, therefore, replaced).
   bool ReplaceDecl(NamedDecl *Old, NamedDecl *New);
 
-  /// \brief Insert the given declaration prior to the given iterator
-  /// position
-  void InsertDecl(iterator Pos, NamedDecl *D);
+  /// \brief Insert the given declaration after the given iterator
+  /// position.
+  void InsertDeclAfter(iterator Pos, NamedDecl *D);
 
   /// \brief Link the declaration into the chain of declarations for
   /// the given identifier.

Modified: cfe/trunk/lib/Sema/IdentifierResolver.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/IdentifierResolver.cpp?rev=127737&r1=127736&r2=127737&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/IdentifierResolver.cpp (original)
+++ cfe/trunk/lib/Sema/IdentifierResolver.cpp Wed Mar 16 11:39:03 2011
@@ -168,36 +168,26 @@
   IDI->AddDecl(D);
 }
 
-void IdentifierResolver::InsertDecl(iterator Pos, NamedDecl *D) {
-  if (Pos == iterator()) {
-    // Simple case: insert at the beginning of the list (which is the
-    // end of the stored vector).
-    AddDecl(D);
-    return;
-  }
-  
+void IdentifierResolver::InsertDeclAfter(iterator Pos, NamedDecl *D) {
   DeclarationName Name = D->getDeclName();
   void *Ptr = Name.getFETokenInfo<void>();
   
-  if (isDeclPtr(Ptr)) {
-    // There's only one element, and we want to insert before it in the list.
-    // Just create the storage for these identifiers and insert them in the
-    // opposite order we normally would.
-    assert(isDeclPtr(Ptr) && "Not a single declaration!");
-    Name.setFETokenInfo(NULL);
-    IdDeclInfo *IDI = &(*IdDeclInfos)[Name];
-    NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
-    IDI->AddDecl(D);
-    IDI->AddDecl(PrevD);
+  if (Pos == iterator() || isDeclPtr(Ptr)) {
+    // Simple case: insert at the end of the list (which is the
+    // end of the stored vector).
+    AddDecl(D);
     return;
   }
 
+  if (IdentifierInfo *II = Name.getAsIdentifierInfo())
+    II->setIsFromAST(false);
+  
   // General case: insert the declaration at the appropriate point in the 
   // list, which already has at least two elements.
   IdDeclInfo *IDI = toIdDeclInfo(Ptr);
-  if (Pos.isIterator())
-    IDI->InsertDecl(Pos.getIterator(), D);
-  else
+  if (Pos.isIterator()) {
+    IDI->InsertDecl(Pos.getIterator() + 1, D);
+  } else
     IDI->InsertDecl(IDI->decls_begin(), D);
 }
 

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=127737&r1=127736&r2=127737&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Wed Mar 16 11:39:03 2011
@@ -500,11 +500,15 @@
     // isn't strictly lexical, which breaks name lookup. Be careful to insert
     // the label at the appropriate place in the identifier chain.
     for (I = IdResolver.begin(D->getDeclName()); I != IEnd; ++I) {
-      if ((*I)->getLexicalDeclContext()->Encloses(CurContext))
+      DeclContext *IDC = (*I)->getLexicalDeclContext();
+      if (IDC == CurContext) {
+        if (!S->isDeclScope(*I))
+          continue;
+      } else if (IDC->Encloses(CurContext))
         break;
     }
     
-    IdResolver.InsertDecl(I, D);
+    IdResolver.InsertDeclAfter(I, D);
   } else {
     IdResolver.AddDecl(D);
   }

Modified: cfe/trunk/test/SemaCXX/goto.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/goto.cpp?rev=127737&r1=127736&r2=127737&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/goto.cpp (original)
+++ cfe/trunk/test/SemaCXX/goto.cpp Wed Mar 16 11:39:03 2011
@@ -1,5 +1,4 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s
-
 // PR9463
 double *end;
 void f() {
@@ -16,3 +15,32 @@
 void g() {
   end = 1; // expected-error{{assigning to 'double *' from incompatible type 'int'}}
 }
+
+void h(int end) {
+  {
+    goto end; // expected-error{{use of undeclared label 'end'}}
+  }
+}
+
+void h2(int end) {
+  {
+    __label__ end;
+    goto end;
+
+  end:
+    ::end = 0;
+  }
+ end:
+  end = 1;
+}
+
+class X {
+public:
+  X();
+};
+
+void rdar9135994()
+{
+X:  
+    goto X;
+}





More information about the cfe-commits mailing list