Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion clang-tools-extra/clangd/Selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,14 @@ class SelectionVisitor : public RecursiveASTVisitor<SelectionVisitor> {
}
return traverseNode(X, [&] { return Base::TraverseDecl(X); });
}
// A declaration of a hyperobject should not be allowed to shadow
// the callback references. Process the callbacks first.
// Note that this does not handle typedefs, using, and any other
// methods of making type names available.
bool TraverseVarDecl(VarDecl *Var) {
TraverseType(Var->getType());
return Base::TraverseVarDecl(Var);
}
bool TraverseTypeLoc(TypeLoc X) {
return traverseNode(&X, [&] { return Base::TraverseTypeLoc(X); });
}
Expand Down Expand Up @@ -699,7 +707,19 @@ class SelectionVisitor : public RecursiveASTVisitor<SelectionVisitor> {
}
// Uninteresting parts of the AST that don't have locations within them.
bool TraverseNestedNameSpecifier(NestedNameSpecifier *) { return true; }
bool TraverseType(QualType) { return true; }

// With the introduction of hyperobjects, QualType is interesting.
bool TraverseType(QualType Ty) {
if (Ty.isNull())
return true;
if (const HyperobjectType *H = Ty->getAs<HyperobjectType>()) {
if (Stmt *I = H->getIdentity())
TraverseStmt(I);
if (Stmt *R = H->getReduce())
TraverseStmt(R);
}
return true;
}

// The DeclStmt for the loop variable claims to cover the whole range
// inside the parens, this causes the range-init expression to not be hit.
Expand Down Expand Up @@ -977,6 +997,10 @@ class SelectionVisitor : public RecursiveASTVisitor<SelectionVisitor> {
claimRange(SourceRange(FTL.getLParenLoc(), FTL.getEndLoc()), Result);
return;
}
if (auto HTL = TL->getAs<HyperobjectTypeLoc>()) {
claimRange(HTL.getHyperLoc(), Result);
return;
}
}
claimRange(getSourceRange(N), Result);
}
Expand Down
24 changes: 24 additions & 0 deletions clang-tools-extra/clangd/test/hyper-using.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# RUN: clangd -lit-test < %s | FileCheck %s
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off","initializationOptions":{"fallbackFlags":["-fopencilk"]}}}
---
{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"test:///hyper-using.cpp","languageId":"cpp","version":2,"text":"void reduce(void *, void *) { }\nvoid id(void *) { }\nusing int_reducer = int _Hyperobject(id, reduce);\nvoid f() {\n int_reducer x = 0;\n auto id_copy = reduce;\n}"}}}
---
{"jsonrpc":"2.0","id":2,"method":"textDocument/definition","params":{"textDocument":{"uri":"test:///hyper-using.cpp"},"position":{"line":2,"character":44}}}
---
{"jsonrpc":"2.0","id":6,"method":"shutdown"}
---
{"jsonrpc":"2.0","method":"exit"}
# CHECK: "id": 2,
# CHECK-NEXT: "jsonrpc": "2.0",
# CHECK-NEXT: "result": [
# CHECK-NEXT: {
# CHECK-NEXT: "range": {
# CHECK-NEXT: "end": {
# CHECK-NEXT: "character": 11,
# CHECK-NEXT: "line": 0
# CHECK-NEXT: },
# CHECK-NEXT: "start": {
# CHECK-NEXT: "character": 5,
# CHECK-NEXT: "line": 0
# CHECK-NEXT: }
# CHECK-NEXT: },
24 changes: 24 additions & 0 deletions clang-tools-extra/clangd/test/hyper.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# RUN: clangd -lit-test < %s | FileCheck %s
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off","initializationOptions":{"fallbackFlags":["-fopencilk"]}}}
---
{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"test:///hyper.cpp","languageId":"cpp","version":1,"text":"void reduce(void *, void *) { }\nvoid id(void *) { }\nvoid f() {\n int _Hyperobject(id, reduce) x = 0; auto id_copy = reduce;\n}"}}}
---
{"jsonrpc":"2.0","id":1,"method":"textDocument/definition","params":{"textDocument":{"uri":"test:///hyper.cpp"},"position":{"line":3,"character":24}}}
---
{"jsonrpc":"2.0","id":2,"method":"shutdown"}
---
{"jsonrpc":"2.0","method":"exit"}
# CHECK: "id": 1,
# CHECK-NEXT: "jsonrpc": "2.0",
# CHECK-NEXT: "result": [
# CHECK-NEXT: {
# CHECK-NEXT: "range": {
# CHECK-NEXT: "end": {
# CHECK-NEXT: "character": 11,
# CHECK-NEXT: "line": 0
# CHECK-NEXT: },
# CHECK-NEXT: "start": {
# CHECK-NEXT: "character": 5,
# CHECK-NEXT: "line": 0
# CHECK-NEXT: }
# CHECK-NEXT: },
6 changes: 2 additions & 4 deletions clang/include/clang/AST/RecursiveASTVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -967,10 +967,8 @@ DEF_TRAVERSE_TYPE(ComplexType, { TRY_TO(TraverseType(T->getElementType())); })

DEF_TRAVERSE_TYPE(HyperobjectType, {
TRY_TO(TraverseType(T->getElementType()));
if (Stmt *I = T->getIdentity())
TRY_TO(TraverseStmt(I));
if (Stmt *R = T->getReduce())
TRY_TO(TraverseStmt(R));
TRY_TO(TraverseStmt(T->getIdentity()));
TRY_TO(TraverseStmt(T->getReduce()));
})

DEF_TRAVERSE_TYPE(PointerType, { TRY_TO(TraverseType(T->getPointeeType())); })
Expand Down
22 changes: 21 additions & 1 deletion clang/include/clang/AST/TypeLoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -1931,17 +1931,37 @@ class ComplexTypeLoc : public InheritingConcreteTypeLoc<TypeSpecTypeLoc,
ComplexType> {
};

/* TODO: More locations so call to getSourceRange in Selection.cpp
canSafelySkipRange covers the callbacks too. */

struct HyperobjectTypeLocInfo : public PointerLikeLocInfo {
SourceLocation RParenLoc;
};

class HyperobjectTypeLoc :
public PointerLikeTypeLoc<HyperobjectTypeLoc, HyperobjectType> {
public PointerLikeTypeLoc<HyperobjectTypeLoc, HyperobjectType,
HyperobjectTypeLocInfo> {
public:
SourceRange getLocalSourceRange() const {
return SourceRange(getSigilLoc(), getRParenLoc());
}

SourceLocation getHyperLoc() const {
return getSigilLoc();
}

SourceLocation getRParenLoc() const {
return getLocalData()->RParenLoc;
}

void setHyperLoc(SourceLocation Loc) {
setSigilLoc(Loc);
}

void setRParenLoc(SourceLocation Loc) {
getLocalData()->RParenLoc = Loc;
}

void initializeLocal(ASTContext &Context, SourceLocation Loc) {
setSigilLoc(Loc);
}
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Sema/SemaType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6787,6 +6787,7 @@ namespace {
}
void VisitHyperobjectTypeLoc(HyperobjectTypeLoc TL) {
TL.setHyperLoc(Chunk.Loc);
TL.setRParenLoc(Chunk.Hyper.RParenLoc);
}

void VisitTypeLoc(TypeLoc TL) {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/TreeTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -5226,7 +5226,7 @@ QualType TreeTransform<Derived>::TransformComplexType(TypeLocBuilder &TLB,
template<typename Derived>
QualType TreeTransform<Derived>::TransformHyperobjectType
(TypeLocBuilder &TLB, HyperobjectTypeLoc TL) {
ExprResult NewR, NewI, NewD;
ExprResult NewR, NewI;

{
const HyperobjectType *H = TL.getTypePtr();
Expand Down