[cfe-commits] r56020 - in /cfe/trunk/lib/Sema: IdentifierResolver.cpp IdentifierResolver.h

Argiris Kirtzidis akyrtzi at gmail.com
Tue Sep 9 14:57:58 PDT 2008


Author: akirtzidis
Date: Tue Sep  9 16:57:58 2008
New Revision: 56020

URL: http://llvm.org/viewvc/llvm-project?rev=56020&view=rev
Log:
Make IdentifierResolver::isDeclInScope regard declarations of a parent 'control' scope as part of the current scope.
The 'control' scope is the 'condition' scope of if/switch/while statements and the scope that contains the for-init-statement and 'condition' of a for statement.

e.g:
if (int x = 0 /*'control' scope*/) {
  // x will be regarded as part of this substatement scope.
} else {
  // and as part of this substatement scope too.
}

Modified:
    cfe/trunk/lib/Sema/IdentifierResolver.cpp
    cfe/trunk/lib/Sema/IdentifierResolver.h

Modified: cfe/trunk/lib/Sema/IdentifierResolver.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/IdentifierResolver.cpp?rev=56020&r1=56019&r2=56020&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/IdentifierResolver.cpp (original)
+++ cfe/trunk/lib/Sema/IdentifierResolver.cpp Tue Sep  9 16:57:58 2008
@@ -13,6 +13,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "IdentifierResolver.h"
+#include "clang/Basic/LangOptions.h"
 #include <list>
 #include <vector>
 
@@ -144,9 +145,26 @@
 /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
 /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
 /// true if 'D' belongs to the given declaration context.
-bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S) {
-  if (Ctx->isFunctionOrMethod())
-    return S->isDeclScope(D);
+bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx,
+                                       Scope *S) const {
+  if (Ctx->isFunctionOrMethod()) {
+    if (S->isDeclScope(D))
+      return true;
+    if (LangOpt.CPlusPlus) {
+      // C++ 3.3.2p4:
+      // Names declared in the for-init-statement, and in the condition of if,
+      // while, for, and switch statements are local to the if, while, for, or
+      // switch statement (including the controlled statement), and shall not be
+      // redeclared in a subsequent condition of that statement nor in the
+      // outermost block (or, for the if statement, any of the outermost blocks)
+      // of the controlled statement.
+      //
+      assert(S->getParent() && "No TUScope?");
+      if (S->getParent()->getFlags() & Scope::ControlScope)
+        return S->getParent()->isDeclScope(D);
+    }
+    return false;
+  }
 
   return LookupContext(D) == LookupContext(Ctx);
 }

Modified: cfe/trunk/lib/Sema/IdentifierResolver.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/IdentifierResolver.h?rev=56020&r1=56019&r2=56020&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/IdentifierResolver.h (original)
+++ cfe/trunk/lib/Sema/IdentifierResolver.h Tue Sep  9 16:57:58 2008
@@ -207,7 +207,7 @@
   /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
   /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
   /// true if 'D' belongs to the given declaration context.
-  static bool isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S = 0);
+  bool isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S = 0) const;
 
   /// AddDecl - Link the decl to its shadowed decl chain.
   void AddDecl(NamedDecl *D);





More information about the cfe-commits mailing list