Skip to content

Commit 677491f

Browse files
johnfairhnkcsgexi
authored andcommitted
[SourceKit] Add syntaxtype for #error/#warning (#14742)
1 parent 94d1f02 commit 677491f

File tree

10 files changed

+150
-31
lines changed

10 files changed

+150
-31
lines changed

include/swift/IDE/SyntaxModel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ enum class SyntaxNodeKind : uint8_t {
4848
BuildConfigKeyword,
4949
/// An identifier in a #if condition.
5050
BuildConfigId,
51+
/// #-keywords like #warning, #sourceLocation
52+
PoundDirectiveKeyword,
5153
/// Any occurrence of '@<attribute-name>' anywhere.
5254
AttributeId,
5355
/// A "resolved/active" attribute. Mis-applied attributes will be AttributeId.

include/swift/Syntax/TokenKinds.def

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
/// POUND_OBJECT_LITERAL(kw, desc, proto)
2525
/// POUND_OLD_OBJECT_LITERAL(kw, new_kw, old_arg, new_arg)
2626
/// POUND_CONFIG(kw)
27+
/// POUND_DIRECTIVE_KEYWORD(kw)
28+
/// POUND_COND_DIRECTIVE_KEYWORD(kw)
2729
/// PUNCTUATOR(name, str)
2830
/// LITERAL(name)
2931
/// MISC(name)
@@ -107,6 +109,19 @@
107109
#define POUND_CONFIG(kw) POUND_KEYWORD(kw)
108110
#endif
109111

112+
/// POUND_DIRECTIVE_KEYWORD(kw)
113+
/// Every keyword prefixed with a '#' that is a compiler control directive.
114+
#ifndef POUND_DIRECTIVE_KEYWORD
115+
#define POUND_DIRECTIVE_KEYWORD(kw) POUND_KEYWORD(kw)
116+
#endif
117+
118+
/// POUND_COND_DIRECTIVE_KEYWORD(kw)
119+
/// Every keyword prefixed with a '#' that is part of conditional compilation
120+
/// control.
121+
#ifndef POUND_COND_DIRECTIVE_KEYWORD
122+
#define POUND_COND_DIRECTIVE_KEYWORD(kw) POUND_DIRECTIVE_KEYWORD(kw)
123+
#endif
124+
110125
/// PUNCTUATOR(name, str)
111126
/// Expands for every Swift punctuator.
112127
/// \param name The symbolic name of the punctuator, such as
@@ -250,22 +265,29 @@ PUNCTUATOR(multiline_string_quote, "\"\"\"")
250265
PUNCTUATOR(l_square_lit, "[#")
251266
PUNCTUATOR(r_square_lit, "#]")
252267

253-
// Keywords prefixed with a '#'. "if" becomes "tok::pound_if".
254-
POUND_KEYWORD(if)
255-
POUND_KEYWORD(else)
256-
POUND_KEYWORD(elseif)
257-
POUND_KEYWORD(endif)
268+
// Keywords prefixed with a '#'. "keyPath" becomes "tok::pound_keyPath".
258269
POUND_KEYWORD(keyPath)
259270
POUND_KEYWORD(line)
260-
POUND_KEYWORD(sourceLocation)
261271
POUND_KEYWORD(selector)
262-
POUND_KEYWORD(warning)
263-
POUND_KEYWORD(error)
272+
POUND_KEYWORD(file)
273+
POUND_KEYWORD(column)
274+
POUND_KEYWORD(function)
275+
POUND_KEYWORD(dsohandle)
276+
277+
// Directive '#' keywords.
278+
POUND_DIRECTIVE_KEYWORD(sourceLocation)
279+
POUND_DIRECTIVE_KEYWORD(warning)
280+
POUND_DIRECTIVE_KEYWORD(error)
281+
282+
// Conditional compilation '#' keywords.
283+
POUND_COND_DIRECTIVE_KEYWORD(if)
284+
POUND_COND_DIRECTIVE_KEYWORD(else)
285+
POUND_COND_DIRECTIVE_KEYWORD(elseif)
286+
POUND_COND_DIRECTIVE_KEYWORD(endif)
264287

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

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

278-
POUND_KEYWORD(file)
279-
POUND_KEYWORD(column)
280-
POUND_KEYWORD(function)
281-
POUND_KEYWORD(dsohandle)
282-
283300
// Single-token literals
284301
LITERAL(integer_literal)
285302
LITERAL(floating_literal)
@@ -314,6 +331,8 @@ MISC(string_interpolation_anchor)
314331
#undef POUND_OBJECT_LITERAL
315332
#undef POUND_OLD_OBJECT_LITERAL
316333
#undef POUND_CONFIG
334+
#undef POUND_DIRECTIVE_KEYWORD
335+
#undef POUND_COND_DIRECTIVE_KEYWORD
317336
#undef PUNCTUATOR
318337
#undef LITERAL
319338
#undef MISC

lib/IDE/SyntaxModel.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,20 @@ SyntaxModelContext::SyntaxModelContext(SourceFile &SrcFile)
107107
LiteralStartLoc = Loc;
108108
continue;
109109

110+
#define POUND_COND_DIRECTIVE_KEYWORD(Name) case tok::pound_##Name:
111+
#include "swift/Syntax/TokenKinds.def"
112+
Kind = SyntaxNodeKind::BuildConfigKeyword;
113+
break;
114+
115+
#define POUND_DIRECTIVE_KEYWORD(Name) case tok::pound_##Name:
116+
#define POUND_COND_DIRECTIVE_KEYWORD(Name)
117+
#include "swift/Syntax/TokenKinds.def"
118+
Kind = SyntaxNodeKind::PoundDirectiveKeyword;
119+
break;
120+
110121
#define POUND_OBJECT_LITERAL(Name, Desc, Proto)
111122
#define POUND_OLD_OBJECT_LITERAL(Name, NewName, OldArg, NewArg)
123+
#define POUND_DIRECTIVE_KEYWORD(Name)
112124
#define POUND_KEYWORD(Name) case tok::pound_##Name:
113125
#include "swift/Syntax/TokenKinds.def"
114126
Kind = SyntaxNodeKind::Keyword;
@@ -816,17 +828,6 @@ bool ModelASTWalker::walkToDeclPre(Decl *D) {
816828

817829
} else if (auto *ConfigD = dyn_cast<IfConfigDecl>(D)) {
818830
for (auto &Clause : ConfigD->getClauses()) {
819-
unsigned TokLen;
820-
if (&Clause == &*ConfigD->getClauses().begin())
821-
TokLen = 3; // '#if'
822-
else if (Clause.Cond == nullptr)
823-
TokLen = 5; // '#else'
824-
else
825-
TokLen = 7; // '#elseif'
826-
if (!passNonTokenNode({SyntaxNodeKind::BuildConfigKeyword,
827-
CharSourceRange(Clause.Loc, TokLen) }))
828-
return false;
829-
830831
if (Clause.Cond && !annotateIfConfigConditionIdentifiers(Clause.Cond))
831832
return false;
832833

@@ -841,11 +842,6 @@ bool ModelASTWalker::walkToDeclPre(Decl *D) {
841842
VisitedNodesInsideIfConfig.insert(Element);
842843
}
843844
}
844-
845-
if (!ConfigD->hadMissingEnd())
846-
if (!passNonTokenNode({ SyntaxNodeKind::BuildConfigKeyword,
847-
CharSourceRange(ConfigD->getEndLoc(), 6/*'#endif'*/) }))
848-
return false;
849845

850846
} else if (auto *EnumCaseD = dyn_cast<EnumCaseDecl>(D)) {
851847
SyntaxStructureNode SN;

test/IDE/coloring_configs.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,10 @@ class NestedPoundIf {
316316
func foo3() {}
317317
// CHECK: <kw>func</kw> foo3() {}
318318
}
319+
320+
// CHECK: <#kw>#error</#kw>(<str>"Error"</str>)
321+
#error("Error")
322+
// CHECK: <#kw>#warning</#kw>(<str>"Warning"</str>)
323+
#warning("Warning")
324+
// CHECK: <#kw>#sourceLocation</#kw>(file: <str>"x"</str>, line: <int>1</int>)
325+
#sourceLocation(file: "x", line: 1)

test/SourceKit/SyntaxMapData/syntaxmap-pound-keyword.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ let c = #column
88

99
if #available(iOS 9.0, *) {}
1010

11+
#if false
12+
#error("Error")
13+
#elseif true
14+
#warning("Warning")
15+
#else
16+
#sourceLocation(file: "here.swift", line:100)
17+
#sourceLocation()
18+
#endif

test/SourceKit/SyntaxMapData/syntaxmap-pound-keyword.swift.response

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
key.offset: 0,
3-
key.length: 206,
3+
key.length: 342,
44
key.diagnostic_stage: source.diagnostic.stage.swift.parse,
55
key.syntaxmap: [
66
{
@@ -92,6 +92,86 @@
9292
key.kind: source.lang.swift.syntaxtype.number,
9393
key.offset: 194,
9494
key.length: 3
95+
},
96+
{
97+
key.kind: source.lang.swift.syntaxtype.buildconfig.keyword,
98+
key.offset: 206,
99+
key.length: 3
100+
},
101+
{
102+
key.kind: source.lang.swift.syntaxtype.keyword,
103+
key.offset: 210,
104+
key.length: 5
105+
},
106+
{
107+
key.kind: source.lang.swift.syntaxtype.pounddirective.keyword,
108+
key.offset: 216,
109+
key.length: 6
110+
},
111+
{
112+
key.kind: source.lang.swift.syntaxtype.string,
113+
key.offset: 223,
114+
key.length: 7
115+
},
116+
{
117+
key.kind: source.lang.swift.syntaxtype.buildconfig.keyword,
118+
key.offset: 232,
119+
key.length: 7
120+
},
121+
{
122+
key.kind: source.lang.swift.syntaxtype.keyword,
123+
key.offset: 240,
124+
key.length: 4
125+
},
126+
{
127+
key.kind: source.lang.swift.syntaxtype.pounddirective.keyword,
128+
key.offset: 245,
129+
key.length: 8
130+
},
131+
{
132+
key.kind: source.lang.swift.syntaxtype.string,
133+
key.offset: 254,
134+
key.length: 9
135+
},
136+
{
137+
key.kind: source.lang.swift.syntaxtype.buildconfig.keyword,
138+
key.offset: 265,
139+
key.length: 5
140+
},
141+
{
142+
key.kind: source.lang.swift.syntaxtype.pounddirective.keyword,
143+
key.offset: 271,
144+
key.length: 15
145+
},
146+
{
147+
key.kind: source.lang.swift.syntaxtype.identifier,
148+
key.offset: 287,
149+
key.length: 4
150+
},
151+
{
152+
key.kind: source.lang.swift.syntaxtype.string,
153+
key.offset: 293,
154+
key.length: 12
155+
},
156+
{
157+
key.kind: source.lang.swift.syntaxtype.identifier,
158+
key.offset: 307,
159+
key.length: 4
160+
},
161+
{
162+
key.kind: source.lang.swift.syntaxtype.number,
163+
key.offset: 312,
164+
key.length: 3
165+
},
166+
{
167+
key.kind: source.lang.swift.syntaxtype.pounddirective.keyword,
168+
key.offset: 317,
169+
key.length: 15
170+
},
171+
{
172+
key.kind: source.lang.swift.syntaxtype.buildconfig.keyword,
173+
key.offset: 335,
174+
key.length: 6
95175
}
96176
]
97177
}

tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@ class DocSyntaxWalker : public SyntaxModelWalker {
704704
case SyntaxNodeKind::TypeId:
705705
case SyntaxNodeKind::BuildConfigKeyword:
706706
case SyntaxNodeKind::BuildConfigId:
707+
case SyntaxNodeKind::PoundDirectiveKeyword:
707708
case SyntaxNodeKind::AttributeId:
708709
case SyntaxNodeKind::AttributeBuiltin:
709710
case SyntaxNodeKind::ObjectLiteral:

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,8 @@ UIdent SwiftLangSupport::getUIDForSyntaxNodeKind(SyntaxNodeKind SC) {
339339
return KindBuildConfigKeyword;
340340
case SyntaxNodeKind::BuildConfigId:
341341
return KindBuildConfigId;
342+
case SyntaxNodeKind::PoundDirectiveKeyword:
343+
return KindPoundDirectiveKeyword;
342344
case SyntaxNodeKind::AttributeId:
343345
return KindAttributeId;
344346
case SyntaxNodeKind::AttributeBuiltin:

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,7 @@ class PrintSyntaxColorWalker : public ide::SyntaxModelWalker {
897897
case SyntaxNodeKind::TypeId: Id = "type"; break;
898898
case SyntaxNodeKind::BuildConfigKeyword: Id = "#kw"; break;
899899
case SyntaxNodeKind::BuildConfigId: Id = "#id"; break;
900+
case SyntaxNodeKind::PoundDirectiveKeyword: Id = "#kw"; break;
900901
case SyntaxNodeKind::AttributeId: Id = "attr-id"; break;
901902
case SyntaxNodeKind::AttributeBuiltin: Id = "attr-builtin"; break;
902903
case SyntaxNodeKind::EditorPlaceholder: Id = "placeholder"; break;
@@ -927,6 +928,7 @@ class PrintSyntaxColorWalker : public ide::SyntaxModelWalker {
927928
case SyntaxNodeKind::TypeId: Col = llvm::raw_ostream::CYAN; break;
928929
case SyntaxNodeKind::BuildConfigKeyword: Col = llvm::raw_ostream::YELLOW; break;
929930
case SyntaxNodeKind::BuildConfigId: Col = llvm::raw_ostream::YELLOW; break;
931+
case SyntaxNodeKind::PoundDirectiveKeyword: Col = llvm::raw_ostream::YELLOW; break;
930932
case SyntaxNodeKind::AttributeId: Col = llvm::raw_ostream::CYAN; break;
931933
case SyntaxNodeKind::AttributeBuiltin: Col = llvm::raw_ostream::MAGENTA; break;
932934
case SyntaxNodeKind::EditorPlaceholder: Col = llvm::raw_ostream::YELLOW; break;

utils/gyb_sourcekit_support/UIDs.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ def __init__(self, internal_name, external_name):
337337
KIND('BuildConfigKeyword',
338338
'source.lang.swift.syntaxtype.buildconfig.keyword'),
339339
KIND('BuildConfigId', 'source.lang.swift.syntaxtype.buildconfig.id'),
340+
KIND('PoundDirectiveKeyword',
341+
'source.lang.swift.syntaxtype.pounddirective.keyword'),
340342
KIND('AttributeId', 'source.lang.swift.syntaxtype.attribute.id'),
341343
KIND('AttributeBuiltin', 'source.lang.swift.syntaxtype.attribute.builtin'),
342344
KIND('Number', 'source.lang.swift.syntaxtype.number'),

0 commit comments

Comments
 (0)