This is an archive of the discontinued LLVM Phabricator instance.

[clang] p1099 1/5: [NFC] Break out BaseUsingDecl from UsingDecl
ClosedPublic

Authored by urnathan on May 3 2021, 12:09 PM.

Details

Summary

this is a pre-patch for adding using-enum support (https://reviews.llvm.org/D101370) On the ML https://lists.llvm.org/pipermail/cfe-dev/2021-April/068074.html, David Rector suggested breaking out the shadow decl handling of UsingDecl to a new intermediate base class. that's what this patch does, altering the decl hierarchy to:

def ShadowIntroducing : DeclNode<Named, "shadow introducer", 1>;
  def Using : DeclNode<ShadowIntroducing>;
def UsingPack : DeclNode<Named>;
def UsingShadow : DeclNode<Named>;
  def ConstructorUsingShadow : DeclNode<UsingShadow>;

I'll add UsingEnumDecl as a sibling of UsingDecl.

I'm not totally enamoured of the 'ShadowIntroducingDecl' name, but couldn't think of a better one right now.

Have I added this new class correctly? (for avoidance of doubt, I don't intend to commit an approved version of this until the using-enum patch is ready to go).

Diff Detail

Event Timeline

urnathan created this revision.May 3 2021, 12:09 PM
urnathan requested review of this revision.May 3 2021, 12:09 PM
davrec added inline comments.May 7 2021, 3:13 PM
clang/include/clang/AST/DeclCXX.h
3166–3185

While we're here it would be nice to clarify this documentation, in particular to suggest some ways by which multiple shadows can be introduced by a single using declaration. I suggest:

/// Represents a shadow declaration implicitly introduced into a scope by a
/// (resolved) using-declaration or using-enum-declaration to achieve 
/// the desired lookup semantics.
///
/// For example:
/// \code
/// namespace A {
///   void foo();
///   void foo(int);
///   struct foo {};
///   enum bar { bar1, bar2 };
/// }
/// namespace B {
///   using A::foo; // adds a UsingDecl and three UsingShadowDecls to B.
///   using enum A::bar; // adds UsingEnumDecl and two UsingShadowDecls to B.
/// }
/// \endcode
3263–3266

I agree the names ShadowIntroducingDecl and getShadowIntroducingDecl() feel a bit odd on second thought -- these are always some kind of using declaration, so it would be nice to get the word Using in there. Also the documentation could be improved while we're here.

I would suggest renaming ShadowIntroducingDecl to something like BaseUsingDecl, and either a) leaving this method named getUsingDecl() (which would minimize churn, but could be a bit confusing) or b) rename it to getIntroducer() (which would be more suggestive).

And I would suggest this documentation:

/// Gets the written or instantiated using declaration which introduced this shadow.
BaseUsingDecl *getUsingDecl() const; // or getIntroducer()
urnathan updated this revision to Diff 344394.May 11 2021, 7:28 AM
urnathan retitled this revision from Break out ShadowIntroducingDecl from UsingDecl to Break out BaseUsingDecl from UsingDecl.

thanks for the feedback. This version uses the different naming and updates documentation.

urnathan retitled this revision from Break out BaseUsingDecl from UsingDecl to [clang] p1099 1/5: Break out BaseUsingDecl from UsingDecl.May 11 2021, 7:48 AM

The approach looks good. There are some test failures - you need to update some bits in clangd and probably anywhere else in the repo that you see uses of getUsingDecl().

You should also update the title with [NFC] or similar.

urnathan retitled this revision from [clang] p1099 1/5: Break out BaseUsingDecl from UsingDecl to [clang] p1099 1/5: [NFC] Break out BaseUsingDecl from UsingDecl.May 14 2021, 3:49 AM
urnathan updated this revision to Diff 345416.May 14 2021, 5:46 AM

Address the build failure --thanks for explaining, it wasn;t clear what else I should try and build. Grepping showed no other getUsingDecl calls. Let's see ...

Herald added a project: Restricted Project. · View Herald TranscriptMay 14 2021, 5:46 AM
urnathan updated this revision to Diff 345426.May 14 2021, 6:30 AM

I stupidly tested that on the stack, not just the first diff. This removes the reference to bits from a later patch

urnathan updated this revision to Diff 345520.May 14 2021, 11:57 AM

unbreaking bot testing of D102241

urnathan updated this revision to Diff 345957.May 17 2021, 12:02 PM

rebased onto 3cdd05e519dd, (trying to fix patch 4's precommit failure)

shafik added inline comments.May 18 2021, 9:49 AM
clang/lib/AST/ASTImporter.cpp
4627–4629

It would nice to expand upon the ImportUsingShadowDecl in ASTImporterTest.cpp as we expand this change to make sure the ASTImporter is importing all the cases correctly. Right now we just have a very basic test there.

urnathan added inline comments.May 18 2021, 12:50 PM
clang/lib/AST/ASTImporter.cpp
4627–4629

Please clarify what you are requiring of this patch series. What you describe sounds like an orthogonal desire.

shafik added inline comments.May 18 2021, 1:31 PM
clang/lib/AST/ASTImporter.cpp
4627–4629

Sure, happy to clarify. As the functionality is complete, tests should be added to verify that the ASTImporter correctly deals with the new functionality but it would be nice as we add that new test to also expand the tests further but that would be optional but probably not too much more work.

urnathan added inline comments.May 19 2021, 5:55 AM
clang/lib/AST/ASTImporter.cpp
4627–4629

I've added unit tests for UsingEnumDecl into patch 4, and extended the UsingDecl tests a bit further

urnathan marked 4 inline comments as done.May 20 2021, 4:14 AM
bruno accepted this revision.May 20 2021, 2:34 PM

LGTM

This revision is now accepted and ready to land.May 20 2021, 2:34 PM
davrec accepted this revision.May 22 2021, 8:26 AM

Sorry for the delay.
Richard should probably weigh in before absolutely committing to

  1. BaseUsingDecl/UsingEnumDecl/UsingDecl (as implemented here) vs. single UsingDecl (Nathan's original approach) and
  2. Renaming getUsingDecl() to getIntroducer() (if it is to return a BaseUsingDecl).

(Main rationale for separating UsingEnumDecl vs. UsingDecl: parallels the naming distinction in P1099 between a "using-declaration" and a "using-enum-declaration". Cons: churn, adds to quite a collection of Using*-named AST nodes already. Nonetheless I favor distinguishing them.)

clang/include/clang/AST/DeclCXX.h
3482

BaseUsingDecl here should remain UsingDecl.
Additionally, I would add a UsingDecl *getIntroducer() { return cast<UsingDecl>(UsingShadowDecl::getIntroducer()); } method here.

clang/lib/AST/DeclCXX.cpp
3028

With the additional ConstructorUsingShadowDecl::getIntroducer() method suggested elsewhere, the cast won't be necessary here.

urnathan updated this revision to Diff 347401.May 24 2021, 8:23 AM

Addressing Davrec's comments

urnathan marked 2 inline comments as done.May 24 2021, 8:25 AM
urnathan added inline comments.
clang/include/clang/AST/DeclCXX.h
3482

Done, this required a further reordering of class definitions, so that UsingDecl is complete at this point.

davrec accepted this revision.May 24 2021, 4:57 PM

Looks good, thanks for doing this!

This revision now requires review to proceed.May 25 2021, 10:24 AM
urnathan removed 1 blocking reviewer(s): rsmith.Jun 7 2021, 5:06 AM
This revision is now accepted and ready to land.Jun 7 2021, 5:06 AM
This revision was automatically updated to reflect the committed changes.
urnathan marked an inline comment as done.
Herald added a project: Restricted Project. · View Herald TranscriptJun 7 2021, 6:29 AM
yaxunl added a subscriber: yaxunl.Jun 7 2021, 7:19 AM

It seems this patch caused build failure:

FAILED: tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclCXX.cpp.o
/usr/bin/c++ -DCLANG_ROUND_TRIP_CC1_ARGS=ON -DGTEST_HAS_RTTI=0 -D_DEBUG -D_GNU_SOURCE -DSTDC_CONSTANT_MACROS -DSTDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -Itools/clang/lib/Sema -I/buildbot/llvm-project/clang/lib/Sema -I/buildbot/llvm-project/clang/include -Itools/clang/include -Iinclude -I/buildbot/llvm-project/llvm/include -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wno-maybe-uninitialized -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wno-comment -Wmisleading-indentation -fdiagnostics-color -ffunction-sections -fdata-sections -fno-common -Woverloaded-virtual -fno-strict-aliasing -O3 -DNDEBUG -fno-exceptions -fno-rtti -UNDEBUG -std=c++14 -MD -MT tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclCXX.cpp.o -MF tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclCXX.cpp.o.d -o tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclCXX.cpp.o -c /buildbot/llvm-project/clang/lib/Sema/SemaDeclCXX.cpp
/buildbot/llvm-project/clang/lib/Sema/SemaDeclCXX.cpp: In member function ‘bool clang::Sema::CheckUsingShadowDecl(clang::BaseUsingDecl*, clang::NamedDecl*, const clang::LookupResult&, clang::UsingShadowDecl*&)’:
/buildbot/llvm-project/clang/lib/Sema/SemaDeclCXX.cpp:11754:10: error: ‘Using’ was not declared in this scope

Diag(Using->getLocation(), diag::err_using_decl_conflict);
     ^~~~~

/buildbot/llvm-project/clang/lib/Sema/SemaDeclCXX.cpp:11754:10: note: suggested alternative: ‘sinl’

Diag(Using->getLocation(), diag::err_using_decl_conflict);
     ^~~~~
     sinl

https://lab.llvm.org/buildbot/#/builders/165/builds/1401

It seems this patch caused build failure:

Yeah, I've pushed a fix -- I'd attached the fix to the wrong diff in the stack :( Should be good now.