Skip to content

[SourceKit] Add syntaxtype for #error/#warning syntax coloring #14742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2018
Merged
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
2 changes: 2 additions & 0 deletions include/swift/IDE/SyntaxModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ enum class SyntaxNodeKind : uint8_t {
BuildConfigKeyword,
/// An identifier in a #if condition.
BuildConfigId,
/// #-keywords like #warning, #sourceLocation
PoundDirectiveKeyword,
/// Any occurrence of '@<attribute-name>' anywhere.
AttributeId,
/// A "resolved/active" attribute. Mis-applied attributes will be AttributeId.
Expand Down
47 changes: 33 additions & 14 deletions include/swift/Syntax/TokenKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
/// POUND_OBJECT_LITERAL(kw, desc, proto)
/// POUND_OLD_OBJECT_LITERAL(kw, new_kw, old_arg, new_arg)
/// POUND_CONFIG(kw)
/// POUND_DIRECTIVE_KEYWORD(kw)
/// POUND_COND_DIRECTIVE_KEYWORD(kw)
/// PUNCTUATOR(name, str)
/// LITERAL(name)
/// MISC(name)
Expand Down Expand Up @@ -107,6 +109,19 @@
#define POUND_CONFIG(kw) POUND_KEYWORD(kw)
#endif

/// POUND_DIRECTIVE_KEYWORD(kw)
/// Every keyword prefixed with a '#' that is a compiler control directive.
#ifndef POUND_DIRECTIVE_KEYWORD
#define POUND_DIRECTIVE_KEYWORD(kw) POUND_KEYWORD(kw)
#endif

/// POUND_COND_DIRECTIVE_KEYWORD(kw)
/// Every keyword prefixed with a '#' that is part of conditional compilation
/// control.
#ifndef POUND_COND_DIRECTIVE_KEYWORD
#define POUND_COND_DIRECTIVE_KEYWORD(kw) POUND_DIRECTIVE_KEYWORD(kw)
#endif

/// PUNCTUATOR(name, str)
/// Expands for every Swift punctuator.
/// \param name The symbolic name of the punctuator, such as
Expand Down Expand Up @@ -250,22 +265,29 @@ PUNCTUATOR(multiline_string_quote, "\"\"\"")
PUNCTUATOR(l_square_lit, "[#")
PUNCTUATOR(r_square_lit, "#]")

// Keywords prefixed with a '#'. "if" becomes "tok::pound_if".
POUND_KEYWORD(if)
POUND_KEYWORD(else)
POUND_KEYWORD(elseif)
POUND_KEYWORD(endif)
// Keywords prefixed with a '#'. "keyPath" becomes "tok::pound_keyPath".
POUND_KEYWORD(keyPath)
POUND_KEYWORD(line)
POUND_KEYWORD(sourceLocation)
POUND_KEYWORD(selector)
POUND_KEYWORD(warning)
POUND_KEYWORD(error)
POUND_KEYWORD(file)
POUND_KEYWORD(column)
POUND_KEYWORD(function)
POUND_KEYWORD(dsohandle)

// Directive '#' keywords.
POUND_DIRECTIVE_KEYWORD(sourceLocation)
POUND_DIRECTIVE_KEYWORD(warning)
POUND_DIRECTIVE_KEYWORD(error)

// Conditional compilation '#' keywords.
POUND_COND_DIRECTIVE_KEYWORD(if)
POUND_COND_DIRECTIVE_KEYWORD(else)
POUND_COND_DIRECTIVE_KEYWORD(elseif)
POUND_COND_DIRECTIVE_KEYWORD(endif)

// Keywords prefixed with a '#' that are build configurations.
POUND_CONFIG(available)


// Object literals and their corresponding protocols.
POUND_OBJECT_LITERAL(fileLiteral, "file reference", ExpressibleByFileReferenceLiteral)
POUND_OBJECT_LITERAL(imageLiteral, "image", ExpressibleByImageLiteral)
Expand All @@ -275,11 +297,6 @@ POUND_OLD_OBJECT_LITERAL(FileReference, fileLiteral, fileReferenceLiteral, resou
POUND_OLD_OBJECT_LITERAL(Image, imageLiteral, imageLiteral, resourceName)
POUND_OLD_OBJECT_LITERAL(Color, colorLiteral, colorLiteralRed, red)

POUND_KEYWORD(file)
POUND_KEYWORD(column)
POUND_KEYWORD(function)
POUND_KEYWORD(dsohandle)

// Single-token literals
LITERAL(integer_literal)
LITERAL(floating_literal)
Expand Down Expand Up @@ -314,6 +331,8 @@ MISC(string_interpolation_anchor)
#undef POUND_OBJECT_LITERAL
#undef POUND_OLD_OBJECT_LITERAL
#undef POUND_CONFIG
#undef POUND_DIRECTIVE_KEYWORD
#undef POUND_COND_DIRECTIVE_KEYWORD
#undef PUNCTUATOR
#undef LITERAL
#undef MISC
28 changes: 12 additions & 16 deletions lib/IDE/SyntaxModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,20 @@ SyntaxModelContext::SyntaxModelContext(SourceFile &SrcFile)
LiteralStartLoc = Loc;
continue;

#define POUND_COND_DIRECTIVE_KEYWORD(Name) case tok::pound_##Name:
#include "swift/Syntax/TokenKinds.def"
Kind = SyntaxNodeKind::BuildConfigKeyword;
break;

#define POUND_DIRECTIVE_KEYWORD(Name) case tok::pound_##Name:
#define POUND_COND_DIRECTIVE_KEYWORD(Name)
#include "swift/Syntax/TokenKinds.def"
Kind = SyntaxNodeKind::PoundDirectiveKeyword;
break;

#define POUND_OBJECT_LITERAL(Name, Desc, Proto)
#define POUND_OLD_OBJECT_LITERAL(Name, NewName, OldArg, NewArg)
#define POUND_DIRECTIVE_KEYWORD(Name)
#define POUND_KEYWORD(Name) case tok::pound_##Name:
#include "swift/Syntax/TokenKinds.def"
Kind = SyntaxNodeKind::Keyword;
Expand Down Expand Up @@ -816,17 +828,6 @@ bool ModelASTWalker::walkToDeclPre(Decl *D) {

} else if (auto *ConfigD = dyn_cast<IfConfigDecl>(D)) {
for (auto &Clause : ConfigD->getClauses()) {
unsigned TokLen;
if (&Clause == &*ConfigD->getClauses().begin())
TokLen = 3; // '#if'
else if (Clause.Cond == nullptr)
TokLen = 5; // '#else'
else
TokLen = 7; // '#elseif'
if (!passNonTokenNode({SyntaxNodeKind::BuildConfigKeyword,
CharSourceRange(Clause.Loc, TokLen) }))
return false;

if (Clause.Cond && !annotateIfConfigConditionIdentifiers(Clause.Cond))
return false;

Expand All @@ -841,11 +842,6 @@ bool ModelASTWalker::walkToDeclPre(Decl *D) {
VisitedNodesInsideIfConfig.insert(Element);
}
}

if (!ConfigD->hadMissingEnd())
if (!passNonTokenNode({ SyntaxNodeKind::BuildConfigKeyword,
CharSourceRange(ConfigD->getEndLoc(), 6/*'#endif'*/) }))
return false;

} else if (auto *EnumCaseD = dyn_cast<EnumCaseDecl>(D)) {
SyntaxStructureNode SN;
Expand Down
7 changes: 7 additions & 0 deletions test/IDE/coloring_configs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,10 @@ class NestedPoundIf {
func foo3() {}
// CHECK: <kw>func</kw> foo3() {}
}

// CHECK: <#kw>#error</#kw>(<str>"Error"</str>)
#error("Error")
// CHECK: <#kw>#warning</#kw>(<str>"Warning"</str>)
#warning("Warning")
// CHECK: <#kw>#sourceLocation</#kw>(file: <str>"x"</str>, line: <int>1</int>)
#sourceLocation(file: "x", line: 1)
8 changes: 8 additions & 0 deletions test/SourceKit/SyntaxMapData/syntaxmap-pound-keyword.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ let c = #column

if #available(iOS 9.0, *) {}

#if false
#error("Error")
#elseif true
#warning("Warning")
#else
#sourceLocation(file: "here.swift", line:100)
#sourceLocation()
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
key.offset: 0,
key.length: 206,
key.length: 342,
key.diagnostic_stage: source.diagnostic.stage.swift.parse,
key.syntaxmap: [
{
Expand Down Expand Up @@ -92,6 +92,86 @@
key.kind: source.lang.swift.syntaxtype.number,
key.offset: 194,
key.length: 3
},
{
key.kind: source.lang.swift.syntaxtype.buildconfig.keyword,
key.offset: 206,
key.length: 3
},
{
key.kind: source.lang.swift.syntaxtype.keyword,
key.offset: 210,
key.length: 5
},
{
key.kind: source.lang.swift.syntaxtype.pounddirective.keyword,
key.offset: 216,
key.length: 6
},
{
key.kind: source.lang.swift.syntaxtype.string,
key.offset: 223,
key.length: 7
},
{
key.kind: source.lang.swift.syntaxtype.buildconfig.keyword,
key.offset: 232,
key.length: 7
},
{
key.kind: source.lang.swift.syntaxtype.keyword,
key.offset: 240,
key.length: 4
},
{
key.kind: source.lang.swift.syntaxtype.pounddirective.keyword,
key.offset: 245,
key.length: 8
},
{
key.kind: source.lang.swift.syntaxtype.string,
key.offset: 254,
key.length: 9
},
{
key.kind: source.lang.swift.syntaxtype.buildconfig.keyword,
key.offset: 265,
key.length: 5
},
{
key.kind: source.lang.swift.syntaxtype.pounddirective.keyword,
key.offset: 271,
key.length: 15
},
{
key.kind: source.lang.swift.syntaxtype.identifier,
key.offset: 287,
key.length: 4
},
{
key.kind: source.lang.swift.syntaxtype.string,
key.offset: 293,
key.length: 12
},
{
key.kind: source.lang.swift.syntaxtype.identifier,
key.offset: 307,
key.length: 4
},
{
key.kind: source.lang.swift.syntaxtype.number,
key.offset: 312,
key.length: 3
},
{
key.kind: source.lang.swift.syntaxtype.pounddirective.keyword,
key.offset: 317,
key.length: 15
},
{
key.kind: source.lang.swift.syntaxtype.buildconfig.keyword,
key.offset: 335,
key.length: 6
}
]
}
1 change: 1 addition & 0 deletions tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@ class DocSyntaxWalker : public SyntaxModelWalker {
case SyntaxNodeKind::TypeId:
case SyntaxNodeKind::BuildConfigKeyword:
case SyntaxNodeKind::BuildConfigId:
case SyntaxNodeKind::PoundDirectiveKeyword:
case SyntaxNodeKind::AttributeId:
case SyntaxNodeKind::AttributeBuiltin:
case SyntaxNodeKind::ObjectLiteral:
Expand Down
2 changes: 2 additions & 0 deletions tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ UIdent SwiftLangSupport::getUIDForSyntaxNodeKind(SyntaxNodeKind SC) {
return KindBuildConfigKeyword;
case SyntaxNodeKind::BuildConfigId:
return KindBuildConfigId;
case SyntaxNodeKind::PoundDirectiveKeyword:
return KindPoundDirectiveKeyword;
case SyntaxNodeKind::AttributeId:
return KindAttributeId;
case SyntaxNodeKind::AttributeBuiltin:
Expand Down
2 changes: 2 additions & 0 deletions tools/swift-ide-test/swift-ide-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,7 @@ class PrintSyntaxColorWalker : public ide::SyntaxModelWalker {
case SyntaxNodeKind::TypeId: Id = "type"; break;
case SyntaxNodeKind::BuildConfigKeyword: Id = "#kw"; break;
case SyntaxNodeKind::BuildConfigId: Id = "#id"; break;
case SyntaxNodeKind::PoundDirectiveKeyword: Id = "#kw"; break;
case SyntaxNodeKind::AttributeId: Id = "attr-id"; break;
case SyntaxNodeKind::AttributeBuiltin: Id = "attr-builtin"; break;
case SyntaxNodeKind::EditorPlaceholder: Id = "placeholder"; break;
Expand Down Expand Up @@ -927,6 +928,7 @@ class PrintSyntaxColorWalker : public ide::SyntaxModelWalker {
case SyntaxNodeKind::TypeId: Col = llvm::raw_ostream::CYAN; break;
case SyntaxNodeKind::BuildConfigKeyword: Col = llvm::raw_ostream::YELLOW; break;
case SyntaxNodeKind::BuildConfigId: Col = llvm::raw_ostream::YELLOW; break;
case SyntaxNodeKind::PoundDirectiveKeyword: Col = llvm::raw_ostream::YELLOW; break;
case SyntaxNodeKind::AttributeId: Col = llvm::raw_ostream::CYAN; break;
case SyntaxNodeKind::AttributeBuiltin: Col = llvm::raw_ostream::MAGENTA; break;
case SyntaxNodeKind::EditorPlaceholder: Col = llvm::raw_ostream::YELLOW; break;
Expand Down
2 changes: 2 additions & 0 deletions utils/gyb_sourcekit_support/UIDs.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ def __init__(self, internal_name, external_name):
KIND('BuildConfigKeyword',
'source.lang.swift.syntaxtype.buildconfig.keyword'),
KIND('BuildConfigId', 'source.lang.swift.syntaxtype.buildconfig.id'),
KIND('PoundDirectiveKeyword',
'source.lang.swift.syntaxtype.pounddirective.keyword'),
KIND('AttributeId', 'source.lang.swift.syntaxtype.attribute.id'),
KIND('AttributeBuiltin', 'source.lang.swift.syntaxtype.attribute.builtin'),
KIND('Number', 'source.lang.swift.syntaxtype.number'),
Expand Down