]> granicus.if.org Git - clang/commitdiff
Push nested-name-specifier source location information into using directives.
authorDouglas Gregor <dgregor@apple.com>
Fri, 25 Feb 2011 16:33:46 +0000 (16:33 +0000)
committerDouglas Gregor <dgregor@apple.com>
Fri, 25 Feb 2011 16:33:46 +0000 (16:33 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@126489 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/DeclCXX.h
include/clang/AST/RecursiveASTVisitor.h
lib/AST/Decl.cpp
lib/AST/DeclCXX.cpp
lib/AST/NestedNameSpecifier.cpp
lib/Sema/SemaDeclCXX.cpp
lib/Sema/SemaTemplateInstantiateDecl.cpp
lib/Serialization/ASTReaderDecl.cpp
lib/Serialization/ASTWriterDecl.cpp
test/Index/annotate-nested-name-specifier.cpp
tools/libclang/CIndex.cpp

index 7e8bc44396d58d4ae80ba94c35329f109f1db92a..c231e4703bc88557d2ae57014961b6977da39339 100644 (file)
@@ -1741,13 +1741,8 @@ class UsingDirectiveDecl : public NamedDecl {
   /// SourceLocation - Location of 'namespace' token.
   SourceLocation NamespaceLoc;
 
-  /// \brief The source range that covers the nested-name-specifier
-  /// preceding the namespace name.
-  SourceRange QualifierRange;
-
-  /// \brief The nested-name-specifier that precedes the namespace
-  /// name, if any.
-  NestedNameSpecifier *Qualifier;
+  /// \brief The nested-name-specifier that precedes the namespace.
+  NestedNameSpecifierLoc QualifierLoc;
 
   /// NominatedNamespace - Namespace nominated by using-directive.
   NamedDecl *NominatedNamespace;
@@ -1765,25 +1760,24 @@ class UsingDirectiveDecl : public NamedDecl {
 
   UsingDirectiveDecl(DeclContext *DC, SourceLocation UsingLoc,
                      SourceLocation NamespcLoc,
-                     SourceRange QualifierRange,
-                     NestedNameSpecifier *Qualifier,
+                     NestedNameSpecifierLoc QualifierLoc,
                      SourceLocation IdentLoc,
                      NamedDecl *Nominated,
                      DeclContext *CommonAncestor)
     : NamedDecl(UsingDirective, DC, IdentLoc, getName()), UsingLoc(UsingLoc),
-      NamespaceLoc(NamespcLoc), QualifierRange(QualifierRange),
-      Qualifier(Qualifier), NominatedNamespace(Nominated),
-      CommonAncestor(CommonAncestor) {
-  }
+      NamespaceLoc(NamespcLoc), QualifierLoc(QualifierLoc),
+      NominatedNamespace(Nominated), CommonAncestor(CommonAncestor) { }
 
 public:
-  /// \brief Retrieve the source range of the nested-name-specifier
-  /// that qualifies the namespace name.
-  SourceRange getQualifierRange() const { return QualifierRange; }
-
+  /// \brief Retrieve the nested-name-specifier that qualifies the
+  /// name of the namespace, with source-location information.
+  NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
+  
   /// \brief Retrieve the nested-name-specifier that qualifies the
   /// name of the namespace.
-  NestedNameSpecifier *getQualifier() const { return Qualifier; }
+  NestedNameSpecifier *getQualifier() const { 
+    return QualifierLoc.getNestedNameSpecifier(); 
+  }
 
   NamedDecl *getNominatedNamespaceAsWritten() { return NominatedNamespace; }
   const NamedDecl *getNominatedNamespaceAsWritten() const {
@@ -1815,8 +1809,7 @@ public:
   static UsingDirectiveDecl *Create(ASTContext &C, DeclContext *DC,
                                     SourceLocation UsingLoc,
                                     SourceLocation NamespaceLoc,
-                                    SourceRange QualifierRange,
-                                    NestedNameSpecifier *Qualifier,
+                                    NestedNameSpecifierLoc QualifierLoc,
                                     SourceLocation IdentLoc,
                                     NamedDecl *Nominated,
                                     DeclContext *CommonAncestor);
index 63592416608cee7544262e6b35ff79fed9ddf2da..ff59450b6a27abb83c0c3925cfd06e07ee0af238 100644 (file)
@@ -1178,7 +1178,7 @@ DEF_TRAVERSE_DECL(UsingDecl, {
   })
 
 DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
-    TRY_TO(TraverseNestedNameSpecifier(D->getQualifier()));
+    TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
   })
 
 DEF_TRAVERSE_DECL(UsingShadowDecl, { })
index d7e389e616413988fb1ff41d6c226300d91852c4..73fe117b1e4ea76b27417d4ddccfdc785dde53fd 100644 (file)
@@ -841,8 +841,10 @@ bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
   // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
   // We want to keep it, unless it nominates same namespace.
   if (getKind() == Decl::UsingDirective) {
-    return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
-           cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
+    return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
+             ->getOriginalNamespace() ==
+           cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
+             ->getOriginalNamespace();
   }
 
   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
index a1d6cc369e6a787866445dca06008dee2ab3c2a2..aafe480e80c0b55bad9643767e6f8e01aaab2412 100644 (file)
@@ -1267,15 +1267,14 @@ LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
 UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
                                                SourceLocation L,
                                                SourceLocation NamespaceLoc,
-                                               SourceRange QualifierRange,
-                                               NestedNameSpecifier *Qualifier,
+                                           NestedNameSpecifierLoc QualifierLoc,
                                                SourceLocation IdentLoc,
                                                NamedDecl *Used,
                                                DeclContext *CommonAncestor) {
   if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
     Used = NS->getOriginalNamespace();
-  return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
-                                    Qualifier, IdentLoc, Used, CommonAncestor);
+  return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
+                                    IdentLoc, Used, CommonAncestor);
 }
 
 NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
index 0689ae1c8836a166af441b2fd76bbc14ee78ab74..6f1ec058d74e1f3e2734f9b436f9a9d234e7ab23 100644 (file)
@@ -321,8 +321,11 @@ namespace {
 }
 
 SourceRange NestedNameSpecifierLoc::getSourceRange() const {
+  if (!Qualifier)
+    return SourceRange();
+  
   NestedNameSpecifierLoc First = *this;
-  while (NestedNameSpecifierLoc Prefix= First.getPrefix())
+  while (NestedNameSpecifierLoc Prefix = First.getPrefix())
     First = Prefix;
   
   return SourceRange(First.getLocalSourceRange().getBegin(), 
@@ -330,6 +333,9 @@ SourceRange NestedNameSpecifierLoc::getSourceRange() const {
 }
 
 SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const {
+  if (!Qualifier)
+    return SourceRange();
+  
   unsigned Offset = getDataLength(Qualifier->getPrefix());
   switch (Qualifier->getKind()) {
   case NestedNameSpecifier::Global:
index 89b2263329ddbf812d150b0190ab5e16b1f429f7..fb65dd822b0aabcfda14705e6ba49b45797ff98b 100644 (file)
@@ -3623,8 +3623,7 @@ Decl *Sema::ActOnStartNamespaceDef(Scope *NamespcScope,
         = UsingDirectiveDecl::Create(Context, CurContext,
                                      /* 'using' */ LBrace,
                                      /* 'namespace' */ SourceLocation(),
-                                     /* qualifier */ SourceRange(),
-                                     /* NNS */ NULL,
+                                     /* qualifier */ NestedNameSpecifierLoc(),
                                      /* identifier */ SourceLocation(),
                                      Namespc,
                                      /* Ancestor */ CurContext);
@@ -3768,8 +3767,7 @@ Decl *Sema::ActOnUsingDirective(Scope *S,
       CommonAncestor = CommonAncestor->getParent();
 
     UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc,
-                                      SS.getRange(),
-                                      (NestedNameSpecifier *)SS.getScopeRep(),
+                                      SS.getWithLocInContext(Context),
                                       IdentLoc, Named, CommonAncestor);
     PushUsingDirective(S, UDir);
   } else {
index c96430a5d5db64a3ef12d17dece98fd84367134d..1d75da71f59715777673c854ae0f1e9d4c7966e2 100644 (file)
@@ -1638,12 +1638,13 @@ TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
 }
 
 Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
-  // Using directives are never dependent, so they require no explicit
+  // Using directives are never dependent (and never contain any types or
+  // expressions), so they require no explicit instantiation work.
   
   UsingDirectiveDecl *Inst
     = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
                                  D->getNamespaceKeyLocation(), 
-                                 D->getQualifierRange(), D->getQualifier(), 
+                                 D->getQualifierLoc(),
                                  D->getIdentLocation(), 
                                  D->getNominatedNamespace(), 
                                  D->getCommonAncestor());
index caa51324739cea44a791393aa86b96bf1b677b3f..f7c8e17a340ac17724c36624035e3000beb926eb 100644 (file)
@@ -776,8 +776,7 @@ void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
   VisitNamedDecl(D);
   D->UsingLoc = ReadSourceLocation(Record, Idx);
   D->NamespaceLoc = ReadSourceLocation(Record, Idx);
-  D->QualifierRange = ReadSourceRange(Record, Idx);
-  D->Qualifier = Reader.ReadNestedNameSpecifier(Record, Idx);
+  D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
   D->NominatedNamespace = cast<NamedDecl>(Reader.GetDecl(Record[Idx++]));
   D->CommonAncestor = cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++]));
 }
@@ -1442,7 +1441,7 @@ Decl *ASTReader::ReadDeclRecord(unsigned Index, DeclID ID) {
     break;
   case DECL_USING_DIRECTIVE:
     D = UsingDirectiveDecl::Create(*Context, 0, SourceLocation(),
-                                   SourceLocation(), SourceRange(), 0,
+                                   SourceLocation(), NestedNameSpecifierLoc(),
                                    SourceLocation(), 0, 0);
     break;
   case DECL_UNRESOLVED_USING_VALUE:
index 5d0514fca2cf5edd032356509585ec8d93f785e7..43eff0997f123e210cfa372e61e9cdc58bda8012 100644 (file)
@@ -727,8 +727,7 @@ void ASTDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
   VisitNamedDecl(D);
   Writer.AddSourceLocation(D->getUsingLoc(), Record);
   Writer.AddSourceLocation(D->getNamespaceKeyLocation(), Record);
-  Writer.AddSourceRange(D->getQualifierRange(), Record);
-  Writer.AddNestedNameSpecifier(D->getQualifier(), Record);
+  Writer.AddNestedNameSpecifierLoc(D->getQualifierLoc(), Record);
   Writer.AddDeclRef(D->getNominatedNamespace(), Record);
   Writer.AddDeclRef(dyn_cast<Decl>(D->getCommonAncestor()), Record);
   Code = serialization::DECL_USING_DIRECTIVE;
index 62eb419cd9af760d5191ad465f093de14d54e288..cc8e8eedf347d5cf02927112efecda2764e85eba 100644 (file)
@@ -41,7 +41,16 @@ struct X2 : outer::inner::vector<T> {
   using outer::inner::vector<type>::push_back;
 };
 
-// RUN: c-index-test -test-annotate-tokens=%s:13:1:42:1 %s | FileCheck %s
+namespace outer {
+  namespace inner {
+    namespace secret {
+    }
+  }
+}
+
+using namespace outer_alias::inner::secret;
+
+// RUN: c-index-test -test-annotate-tokens=%s:13:1:52:1 %s | FileCheck %s
 
 // CHECK: Keyword: "using" [14:1 - 14:6] UsingDeclaration=vector[4:12]
 // CHECK: Identifier: "outer_alias" [14:7 - 14:18] NamespaceRef=outer_alias:10:11
@@ -89,3 +98,12 @@ struct X2 : outer::inner::vector<T> {
 // CHECK: Punctuation: ">" [41:34 - 41:35] UsingDeclaration=push_back:41:37
 // CHECK: Punctuation: "::" [41:35 - 41:37] UsingDeclaration=push_back:41:37
 // CHECK: Identifier: "push_back" [41:37 - 41:46] UsingDeclaration=push_back:41:37
+
+// Using directive
+// CHECK: Keyword: "using" [51:1 - 51:6] UsingDirective=:51:37
+// CHECK: Keyword: "namespace" [51:7 - 51:16] UsingDirective=:51:37
+// CHECK: Identifier: "outer_alias" [51:17 - 51:28] NamespaceRef=outer_alias:10:11
+// CHECK: Punctuation: "::" [51:28 - 51:30] UsingDirective=:51:37
+// CHECK: Identifier: "inner" [51:30 - 51:35] NamespaceRef=inner:45:13
+// CHECK: Punctuation: "::" [51:35 - 51:37] UsingDirective=:51:37
+// CHECK: Identifier: "secret" [51:37 - 51:43] NamespaceRef=secret:46:15
index 425832596a95a37ece6fe6f449be433dabd3c8c0..ccaeaa69aff495e2c42190d426f9448e32d8df9a 100644 (file)
@@ -1103,8 +1103,8 @@ bool CursorVisitor::VisitUsingDecl(UsingDecl *D) {
 
 bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
   // Visit nested-name-specifier.
-  if (NestedNameSpecifier *Qualifier = D->getQualifier())
-    if (VisitNestedNameSpecifier(Qualifier, D->getQualifierRange()))
+  if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
+    if (VisitNestedNameSpecifierLoc(QualifierLoc))
       return true;
 
   return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(),