Skip to content

Commit

Permalink
Revert "[AST] Add UsingType: a sugar type for types found via UsingDecl"
Browse files Browse the repository at this point in the history
This reverts commit e1600db.

Breaks sanitizer tests, at least on windows:
https://lab.llvm.org/buildbot/#/builders/127/builds/21592/steps/4/logs/stdio
  • Loading branch information
sam-mccall committed Dec 20, 2021
1 parent 565c175 commit cc56c66
Show file tree
Hide file tree
Showing 42 changed files with 68 additions and 382 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,6 @@ void UpgradeGoogletestCaseCheck::registerMatchers(MatchFinder *Finder) {
usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(TestCaseTypeAlias)))
.bind("using"),
this);
Finder->addMatcher(
typeLoc(loc(usingType(hasUnderlyingType(
typedefType(hasDeclaration(TestCaseTypeAlias))))),
unless(hasAncestor(decl(isImplicit()))), LocationFilter)
.bind("typeloc"),
this);
}

static llvm::StringRef getNewMethodName(llvm::StringRef CurrentName) {
Expand Down
16 changes: 3 additions & 13 deletions clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ AST_MATCHER_P(DeducedTemplateSpecializationType, refsToTemplatedDecl,
return DeclMatcher.matches(*TD, Finder, Builder);
return false;
}

} // namespace

// A function that helps to tell whether a TargetDecl in a UsingDecl will be
Expand All @@ -61,10 +60,13 @@ static bool shouldCheckDecl(const Decl *TargetDecl) {
void UnusedUsingDeclsCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(usingDecl(isExpansionInMainFile()).bind("using"), this);
auto DeclMatcher = hasDeclaration(namedDecl().bind("used"));
Finder->addMatcher(loc(enumType(DeclMatcher)), this);
Finder->addMatcher(loc(recordType(DeclMatcher)), this);
Finder->addMatcher(loc(templateSpecializationType(DeclMatcher)), this);
Finder->addMatcher(loc(deducedTemplateSpecializationType(
refsToTemplatedDecl(namedDecl().bind("used")))),
this);
Finder->addMatcher(declRefExpr().bind("used"), this);
Finder->addMatcher(callExpr(callee(unresolvedLookupExpr().bind("used"))),
this);
Finder->addMatcher(
Expand All @@ -74,12 +76,6 @@ void UnusedUsingDeclsCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(loc(templateSpecializationType(forEachTemplateArgument(
templateArgument().bind("used")))),
this);
// Cases where we can identify the UsingShadowDecl directly, rather than
// just its target.
// FIXME: cover more cases in this way, as the AST supports it.
auto ThroughShadowMatcher = throughUsingDecl(namedDecl().bind("usedShadow"));
Finder->addMatcher(declRefExpr(ThroughShadowMatcher), this);
Finder->addMatcher(loc(usingType(ThroughShadowMatcher)), this);
}

void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
Expand Down Expand Up @@ -141,12 +137,6 @@ void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
return;
}

if (const auto *UsedShadow =
Result.Nodes.getNodeAs<UsingShadowDecl>("usedShadow")) {
removeFromFoundDecls(UsedShadow->getTargetDecl());
return;
}

if (const auto *Used = Result.Nodes.getNodeAs<TemplateArgument>("used")) {
if (Used->getKind() == TemplateArgument::Template) {
if (const auto *TD = Used->getAsTemplate().getAsTemplateDecl())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ struct UnqualNameVisitor : public RecursiveASTVisitor<UnqualNameVisitor> {
TL.getAs<TypedefTypeLoc>().getTypePtr()->getDecl()->getName()))
return false;
break;
case TypeLoc::Using:
if (visitUnqualName(TL.getAs<UsingTypeLoc>()
.getTypePtr()
->getFoundDecl()
->getName()))
return false;
break;
default:
break;
}
Expand Down
11 changes: 0 additions & 11 deletions clang-tools-extra/clangd/FindTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,6 @@ struct TargetFinder {
Outer.add(ET->desugar(), Flags);
}

void VisitUsingType(const UsingType *ET) {
Outer.add(ET->getFoundDecl(), Flags);
}

void VisitInjectedClassNameType(const InjectedClassNameType *ICNT) {
Outer.add(ICNT->getDecl(), Flags);
}
Expand Down Expand Up @@ -859,13 +855,6 @@ refInTypeLoc(TypeLoc L, const HeuristicResolver *Resolver) {
}
}

void VisitUsingTypeLoc(UsingTypeLoc L) {
Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
L.getLocalSourceRange().getBegin(),
/*IsDecl=*/false,
{L.getFoundDecl()}});
}

void VisitTagTypeLoc(TagTypeLoc L) {
Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
L.getNameLoc(),
Expand Down
5 changes: 0 additions & 5 deletions clang-tools-extra/clangd/IncludeCleaner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ class ReferencedLocationCrawler
return true;
}

bool VisitUsingType(UsingType *UT) {
add(UT->getFoundDecl());
return true;
}

bool VisitTypedefType(TypedefType *TT) {
add(TT->getDecl());
return true;
Expand Down
12 changes: 8 additions & 4 deletions clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ TEST(IncludeCleaner, ReferencedLocations) {
"struct Foo; struct ^Foo{}; typedef Foo ^Bar;",
"Bar b;",
},
{
"namespace ns { class X; }; using ns::^X;",
"X *y;",
},
// MemberExpr
{
"struct ^X{int ^a;}; X ^foo();",
Expand Down Expand Up @@ -202,6 +198,14 @@ TEST(IncludeCleaner, ReferencedLocations) {
{
"enum class ^Color : char {};",
"Color *c;",
},
{
// When a type is resolved via a using declaration, the
// UsingShadowDecl is not referenced in the AST.
// Compare to TypedefType, or DeclRefExpr::getFoundDecl().
// ^
"namespace ns { class ^X; }; using ns::X;",
"X *y;",
}};
for (const TestCase &T : Cases) {
TestTU TU;
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clangd/unittests/XRefsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,7 @@ TEST(LocateSymbol, Alias) {

R"cpp(
namespace ns { class [[Foo]] {}; }
using ns::[[Foo]];
using ns::Foo;
F^oo f;
)cpp",

Expand Down
10 changes: 1 addition & 9 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,7 @@ Floating Point Support in Clang
Internal API Changes
--------------------

- A new sugar ``Type`` AST node represents types accessed via a C++ using
declaration. Given code ``using std::error_code; error_code x;``, ``x`` has
a ``UsingType`` which desugars to the previous ``RecordType``.
- ...

Build System Changes
--------------------
Expand All @@ -271,12 +269,6 @@ AST Matchers
- The ``hasAnyCapture`` matcher now only accepts an inner matcher of type
``Matcher<LambdaCapture>``. The matcher originally accepted an inner matcher
of type ``Matcher<CXXThisExpr>`` or ``Matcher<VarDecl>``.
- The ``usingType`` matcher is now available and needed to refer to types that
are referred to via using C++ using declarations.
The associated ``UsingShadowDecl`` can be matched using ``throughUsingDecl``
and the underlying ``Type`` with ``hasUnderlyingType``.
``hasDeclaration`` continues to see through the alias and apply to the
underlying type.

clang-format
------------
Expand Down
4 changes: 0 additions & 4 deletions clang/include/clang/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ class ASTContext : public RefCountedBase<ASTContext> {
mutable llvm::ContextualFoldingSet<TemplateSpecializationType, ASTContext&>
TemplateSpecializationTypes;
mutable llvm::FoldingSet<ParenType> ParenTypes;
mutable llvm::FoldingSet<UsingType> UsingTypes;
mutable llvm::FoldingSet<ElaboratedType> ElaboratedTypes;
mutable llvm::FoldingSet<DependentNameType> DependentNameTypes;
mutable llvm::ContextualFoldingSet<DependentTemplateSpecializationType,
Expand Down Expand Up @@ -1556,9 +1555,6 @@ class ASTContext : public RefCountedBase<ASTContext> {
return getTypeDeclTypeSlow(Decl);
}

QualType getUsingType(const UsingShadowDecl *Found,
QualType Underlying) const;

/// Return the unique reference to the type for the specified
/// typedef-name decl.
QualType getTypedefType(const TypedefNameDecl *Decl,
Expand Down
2 changes: 0 additions & 2 deletions clang/include/clang/AST/PropertiesBase.td
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ def DeclRef : RefPropertyType<"Decl"> { let ConstWhenWriting = 1; }
SubclassPropertyType<"TemplateTypeParmDecl", DeclRef>;
def TemplateTemplateParmDeclRef :
SubclassPropertyType<"TemplateTemplateParmDecl", DeclRef>;
def UsingShadowDeclRef :
SubclassPropertyType<"UsingShadowDecl", DeclRef>;
def ValueDeclRef :
SubclassPropertyType<"ValueDecl", DeclRef>;
def ElaboratedTypeKeyword : EnumPropertyType;
Expand Down
10 changes: 1 addition & 9 deletions clang/include/clang/AST/RecursiveASTVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,6 @@ DEF_TRAVERSE_TYPE(FunctionProtoType, {
TRY_TO(TraverseStmt(NE));
})

DEF_TRAVERSE_TYPE(UsingType, {})
DEF_TRAVERSE_TYPE(UnresolvedUsingType, {})
DEF_TRAVERSE_TYPE(TypedefType, {})

Expand Down Expand Up @@ -1253,7 +1252,6 @@ DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
TRY_TO(TraverseStmt(NE));
})

DEF_TRAVERSE_TYPELOC(UsingType, {})
DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, {})
DEF_TRAVERSE_TYPELOC(TypedefType, {})

Expand Down Expand Up @@ -2097,13 +2095,7 @@ bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
}

if (VisitBody) {
TRY_TO(TraverseStmt(D->getBody()));
// Body may contain using declarations whose shadows are parented to the
// FunctionDecl itself.
for (auto *Child : D->decls()) {
if (isa<UsingShadowDecl>(Child))
TRY_TO(TraverseDecl(Child));
}
TRY_TO(TraverseStmt(D->getBody())); // Function body.
}
return true;
}
Expand Down
1 change: 0 additions & 1 deletion clang/include/clang/AST/TextNodeDumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ class TextNodeDumper
void VisitFunctionType(const FunctionType *T);
void VisitFunctionProtoType(const FunctionProtoType *T);
void VisitUnresolvedUsingType(const UnresolvedUsingType *T);
void VisitUsingType(const UsingType *T);
void VisitTypedefType(const TypedefType *T);
void VisitUnaryTransformType(const UnaryTransformType *T);
void VisitTagType(const TagType *T);
Expand Down
22 changes: 0 additions & 22 deletions clang/include/clang/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ class TemplateArgumentLoc;
class TemplateTypeParmDecl;
class TypedefNameDecl;
class UnresolvedUsingTypenameDecl;
class UsingShadowDecl;

using CanQualType = CanQual<Type>;

Expand Down Expand Up @@ -4369,27 +4368,6 @@ class UnresolvedUsingType : public Type {
}
};

class UsingType : public Type, public llvm::FoldingSetNode {
UsingShadowDecl *Found;
friend class ASTContext; // ASTContext creates these.

UsingType(const UsingShadowDecl *Found, QualType Underlying, QualType Canon);

public:
UsingShadowDecl *getFoundDecl() const { return Found; }
QualType getUnderlyingType() const;

bool isSugared() const { return true; }
QualType desugar() const { return getUnderlyingType(); }

void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, Found); }
static void Profile(llvm::FoldingSetNodeID &ID,
const UsingShadowDecl *Found) {
ID.AddPointer(Found);
}
static bool classof(const Type *T) { return T->getTypeClass() == Using; }
};

class TypedefType : public Type {
TypedefNameDecl *Decl;

Expand Down
10 changes: 0 additions & 10 deletions clang/include/clang/AST/TypeLoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -665,16 +665,6 @@ class BuiltinTypeLoc : public ConcreteTypeLoc<UnqualTypeLoc,
}
};

/// Wrapper for source info for types used via transparent aliases.
class UsingTypeLoc : public InheritingConcreteTypeLoc<TypeSpecTypeLoc,
UsingTypeLoc, UsingType> {
public:
QualType getUnderlyingType() const {
return getTypePtr()->getUnderlyingType();
}
UsingShadowDecl *getFoundDecl() const { return getTypePtr()->getFoundDecl(); }
};

/// Wrapper for source info for typedefs.
class TypedefTypeLoc : public InheritingConcreteTypeLoc<TypeSpecTypeLoc,
TypedefTypeLoc,
Expand Down
13 changes: 0 additions & 13 deletions clang/include/clang/AST/TypeProperties.td
Original file line number Diff line number Diff line change
Expand Up @@ -362,19 +362,6 @@ let Class = UnresolvedUsingType in {
}]>;
}

let Class = UsingType in {
def : Property<"foundDeclaration", UsingShadowDeclRef> {
let Read = [{ node->getFoundDecl() }];
}
def : Property<"underlyingType", QualType> {
let Read = [{ node->getUnderlyingType() }];
}

def : Creator<[{
return ctx.getUsingType(foundDeclaration, underlyingType);
}]>;
}

let Class = TypedefType in {
def : Property<"declaration", DeclRef> {
let Read = [{ node->getDecl() }];
Expand Down
52 changes: 15 additions & 37 deletions clang/include/clang/ASTMatchers/ASTMatchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -4128,34 +4128,25 @@ AST_MATCHER_P(DeclRefExpr, to, internal::Matcher<Decl>,
InnerMatcher.matches(*DeclNode, Finder, Builder));
}

/// Matches if a node refers to a declaration through a specific
/// using shadow declaration.
/// Matches a \c DeclRefExpr that refers to a declaration through a
/// specific using shadow declaration.
///
/// Examples:
/// Given
/// \code
/// namespace a { int f(); }
/// namespace a { void f() {} }
/// using a::f;
/// int x = f();
/// void g() {
/// f(); // Matches this ..
/// a::f(); // .. but not this.
/// }
/// \endcode
/// declRefExpr(throughUsingDecl(anything()))
/// matches \c f
///
/// \code
/// namespace a { class X{}; }
/// using a::X;
/// X x;
/// \code
/// typeLoc(loc(usingType(throughUsingDecl(anything()))))
/// matches \c X
///
/// Usable as: Matcher<DeclRefExpr>, Matcher<UsingType>
AST_POLYMORPHIC_MATCHER_P(throughUsingDecl,
AST_POLYMORPHIC_SUPPORTED_TYPES(DeclRefExpr,
UsingType),
internal::Matcher<UsingShadowDecl>, Inner) {
/// matches \c f()
AST_MATCHER_P(DeclRefExpr, throughUsingDecl,
internal::Matcher<UsingShadowDecl>, InnerMatcher) {
const NamedDecl *FoundDecl = Node.getFoundDecl();
if (const UsingShadowDecl *UsingDecl = dyn_cast<UsingShadowDecl>(FoundDecl))
return Inner.matches(*UsingDecl, Finder, Builder);
return InnerMatcher.matches(*UsingDecl, Finder, Builder);
return false;
}

Expand Down Expand Up @@ -6852,7 +6843,7 @@ extern const AstTypeMatcher<DecltypeType> decltypeType;
AST_TYPE_TRAVERSE_MATCHER(hasDeducedType, getDeducedType,
AST_POLYMORPHIC_SUPPORTED_TYPES(AutoType));

/// Matches \c DecltypeType or \c UsingType nodes to find the underlying type.
/// Matches \c DecltypeType nodes to find out the underlying type.
///
/// Given
/// \code
Expand All @@ -6862,10 +6853,9 @@ AST_TYPE_TRAVERSE_MATCHER(hasDeducedType, getDeducedType,
/// decltypeType(hasUnderlyingType(isInteger()))
/// matches the type of "a"
///
/// Usable as: Matcher<DecltypeType>, Matcher<UsingType>
/// Usable as: Matcher<DecltypeType>
AST_TYPE_TRAVERSE_MATCHER(hasUnderlyingType, getUnderlyingType,
AST_POLYMORPHIC_SUPPORTED_TYPES(DecltypeType,
UsingType));
AST_POLYMORPHIC_SUPPORTED_TYPES(DecltypeType));

/// Matches \c FunctionType nodes.
///
Expand Down Expand Up @@ -7193,18 +7183,6 @@ AST_MATCHER_P(ElaboratedType, namesType, internal::Matcher<QualType>,
return InnerMatcher.matches(Node.getNamedType(), Finder, Builder);
}

/// Matches types specified through a using declaration.
///
/// Given
/// \code
/// namespace a { struct S {}; }
/// using a::S;
/// S s;
/// \endcode
///
/// \c usingType() matches the type of the variable declaration of \c s.
extern const AstTypeMatcher<UsingType> usingType;

/// Matches types that represent the result of substituting a type for a
/// template type parameter.
///
Expand Down
Loading

0 comments on commit cc56c66

Please sign in to comment.