Skip to content

Commit

Permalink
feat(objc_indexer): support marked source for @Property (#3320)
Browse files Browse the repository at this point in the history
Add basic support for generating marked source for properties in
Objective-C. This should offer similar support to what we do for class
members in C++. It doesn't do anything special for the automatically
generated getter and setter methods. It also doesn't do anything special
with the @Property string (though it is present in the marked source).
  • Loading branch information
salguarnieri committed Dec 13, 2018
1 parent 00fe685 commit b79d49b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
2 changes: 2 additions & 0 deletions kythe/cxx/indexer/cxx/IndexerASTHooks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5178,6 +5178,8 @@ bool IndexerASTVisitor::VisitObjCPropertyDecl(
auto Marks = MarkedSources.Generate(Decl);
GraphObserver::NodeId DeclNode(BuildNodeIdForDecl(Decl));
SourceRange NameRange = RangeForNameOfDeclaration(Decl);
Marks.set_name_range(NameRange);
Marks.set_marked_source_end(Decl->getSourceRange().getEnd());
MaybeRecordDefinitionRange(
RangeInCurrentContext(Decl->isImplicit(), DeclNode, NameRange), DeclNode,
absl::nullopt);
Expand Down
16 changes: 15 additions & 1 deletion kythe/cxx/indexer/cxx/marked_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,17 @@ class DeclAnnotator : public clang::DeclVisitor<DeclAnnotator> {
}
}
}
void VisitObjCPropertyDecl(clang::ObjCPropertyDecl* decl) {
if (const auto* type_source_info = decl->getTypeSourceInfo()) {
if (!ShouldSkipDecl(decl, type_source_info->getType(),
type_source_info->getTypeLoc().getSourceRange())) {
auto type_loc = ExpandRangeBySingleToken(
cache_->source_manager(), cache_->lang_options(),
type_source_info->getTypeLoc().getSourceRange());
InsertTypeAnnotation(type_loc, clang::SourceRange{});
}
}
}
void VisitFunctionDecl(clang::FunctionDecl* decl) {
clang::SourceRange arg_list;
if (const auto* type_info = decl->getTypeSourceInfo()) {
Expand Down Expand Up @@ -524,7 +535,8 @@ bool MarkedSourceGenerator::WillGenerateMarkedSource() const {
llvm::isa<clang::TemplateTypeParmDecl>(decl_) ||
llvm::isa<clang::NonTypeTemplateParmDecl>(decl_) ||
llvm::isa<clang::TemplateTemplateParmDecl>(decl_) ||
llvm::isa<clang::ObjCTypeParamDecl>(decl_);
llvm::isa<clang::ObjCTypeParamDecl>(decl_) ||
llvm::isa<clang::ObjCPropertyDecl>(decl_);
}

std::string GetDeclName(const clang::LangOptions& lang_options,
Expand Down Expand Up @@ -850,6 +862,8 @@ absl::optional<MarkedSource> MarkedSourceGenerator::GenerateMarkedSource(
} else {
return GenerateMarkedSourceUsingSource(decl_id);
}
} else if (llvm::isa<clang::ObjCPropertyDecl>(decl_)) {
return GenerateMarkedSourceUsingSource(decl_id);
} else if (llvm::isa<clang::ObjCMethodDecl>(decl_)) {
return GenerateMarkedSourceUsingSource(decl_id);
} else if (llvm::isa<clang::TemplateTypeParmDecl>(decl_) ||
Expand Down
11 changes: 11 additions & 0 deletions kythe/cxx/indexer/cxx/testdata/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2577,8 +2577,19 @@ objc_indexer_test(
objc_indexer_test(
name = "objc_marked_source_method",
srcs = ["objc/marked_method.m"],
check_for_singletons = True,
convert_marked_source = True,
tags = [
"marked_source",
"objc",
],
)

objc_indexer_test(
name = "objc_marked_source_property",
srcs = ["objc/marked_property.m"],
check_for_singletons = True,
convert_marked_source = True,
tags = [
"marked_source",
"objc",
Expand Down
34 changes: 34 additions & 0 deletions kythe/cxx/indexer/cxx/testdata/objc/marked_property.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Test marked source for properties.

@class Data;

@interface Box {
int _testwidth;
}


//- @width defines/binding WidthPropDecl
//- WidthPropDecl code WPCodeRoot
//- WPCodeRoot.pre_text "@property "
//- WPCodeRoot.kind "BOX"
//- WPCodeRoot child.0 WPRootType
//- WPRootType.kind "TYPE"
//- WPRootType.pre_text "int"
//- WPCodeRoot child.1 WPContext
//- WPContext.kind "CONTEXT"
//- WPContext.post_child_text "::"
//- WPContext child.0 WPContextIdent
//- WPContextIdent.kind "IDENTIFIER"
//- WPContextIdent.pre_text "Box"
//- WPCodeRoot child.2 WPIdent
//- WPIdent.kind "IDENTIFIER"
//- WPIdent.pre_text "width"
@property int width;

@end

@implementation Box

@synthesize width = _testwidth;

@end

0 comments on commit b79d49b

Please sign in to comment.