[PATCH] D46190: For a used declaration, mark any associated usings as referenced.

Richard Smith - zygoloid via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 26 13:30:36 PDT 2018


rsmith added inline comments.


================
Comment at: include/clang/Sema/Sema.h:4040-4041
   // explicit nested-name-specifier).
-  void MarkAnyDeclReferenced(SourceLocation Loc, Decl *D, bool MightBeOdrUse);
+  void MarkAnyDeclReferenced(SourceLocation Loc, Decl *D, bool MightBeOdrUse,
+                             CXXScopeSpec *SS = nullptr, Expr *E = nullptr);
   void MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func,
----------------
Pass in the FoundDecl instead of a scope specifier and an expression.


================
Comment at: lib/Sema/SemaDeclCXX.cpp:15520-15525
+  while ((NA = dyn_cast<NamespaceAliasDecl>(ND)) && !NA->isReferenced()) {
+    NA->setReferenced();
+    ND = NA->getAliasedNamespace();
+    if (auto *NNS = NA->getQualifier())
+      MarkNamespaceAliasReferenced(NNS->getAsNamespaceAlias());
+  }
----------------
This is not the right approach. You should not iteratively mark namespaces as referenced here. Instead, when we parse the first namespace alias declaration in the chain, *it* should mark the subsequent one referenced immediately. So:

```
namespace A { int n; }
namespace B = A; // this declaration marks A referenced
namespace C = B; // this declaration marks B referenced
int k = C::n; // parsing the nested-name-specifier of this expression marks C referenced
```


================
Comment at: lib/Sema/SemaDeclCXX.cpp:15531-15586
+void Sema::MarkUsingReferenced(NamedDecl *ND, SourceLocation Loc,
+                               CXXScopeSpec *SS, Expr *E) {
+  // The declaration was not defined in a namespace.
+  auto *DC = ND->getDeclContext();
+  if (!DC->isNamespace())
+    return;
+
----------------
You should not do any of this. You should keep track of how each name was actually originally found, and mark the FoundDecl as referenced. Do not attempt to redo the lookup like you do here. You will get it wrong, and in any case, redoing the lookup is wasteful.


https://reviews.llvm.org/D46190





More information about the cfe-commits mailing list